Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: use new subnet endpoint fields (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan authored Nov 6, 2023
1 parent 1bf66b1 commit 34d5cce
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AUTH0_AUDIENCE=
AUTH0_ISSUER_URL=
REDIS_HOST=
REDIS_PORT=
TOPOS_SUBNET_ENDPOINT=
TOPOS_SUBNET_ENDPOINT_WS=
SUBNET_REGISTRATOR_CONTRACT_ADDRESS=
TOPOS_CORE_PROXY_CONTRACT_ADDRESS=
TRACING_SERVICE_NAME=
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AUTH0_AUDIENCE=
AUTH0_ISSUER_URL=
REDIS_HOST=
REDIS_PORT=
TOPOS_SUBNET_ENDPOINT=
TOPOS_SUBNET_ENDPOINT_WS=
SUBNET_REGISTRATOR_CONTRACT_ADDRESS=
TOPOS_CORE_PROXY_CONTRACT_ADDRESS=
ERC20_MESSAGING_CONTRACT_ADDRESS=
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.4.0",
"@nestjs/swagger": "^6.1.2",
"@topos-protocol/topos-smart-contracts": "^1.2.3",
"@topos-protocol/topos-smart-contracts": "^2.0.0",
"bcrypt": "^5.1.0",
"bull": "^4.10.1",
"class-transformer": "^0.5.1",
Expand Down
16 changes: 6 additions & 10 deletions src/execute/execute.processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const VALID_PRIVATE_KEY =
'0xc6cbd7d76bc5baca530c875663711b947efa6a86a900a9e8645ce32e5821484e'
const TOPOS_CORE_PROXY_CONTRACT_ADDRESS =
'0x1D7b9f9b1FF6cf0A3BEB0F84fA6F8628E540E97F'
const TOPOS_SUBNET_ENDPOINT = 'topos-subnet-endpoint'
const TOPOS_SUBNET_ENDPOINT_WS = 'ws://topos-subnet-endpoint/ws'

const validExecuteJob: Partial<Job<ExecuteDto & TracingOptions>> = {
data: {
Expand All @@ -27,7 +27,7 @@ const validExecuteJob: Partial<Job<ExecuteDto & TracingOptions>> = {
progress: jest.fn(),
}

const subnetMock = { endpoint: 'endpoint' }
const subnetMock = { endpointWs: 'ws://endpoint/ws' }
const providerMock = Object.assign(new EventEmitter(), {
getCode: jest.fn().mockResolvedValue('0x123'),
})
Expand Down Expand Up @@ -57,8 +57,8 @@ describe('ExecuteProcessor', () => {
return VALID_PRIVATE_KEY
case 'TOPOS_CORE_PROXY_CONTRACT_ADDRESS':
return TOPOS_CORE_PROXY_CONTRACT_ADDRESS
case 'TOPOS_SUBNET_ENDPOINT':
return TOPOS_SUBNET_ENDPOINT
case 'TOPOS_SUBNET_ENDPOINT_WS':
return TOPOS_SUBNET_ENDPOINT_WS
}
}),
}
Expand Down Expand Up @@ -97,12 +97,8 @@ describe('ExecuteProcessor', () => {
validExecuteJob as unknown as Job<ExecuteDto & TracingOptions>
)

expect(ethersProviderMock).toHaveBeenCalledWith(
`ws://${TOPOS_SUBNET_ENDPOINT}/ws`
)
expect(ethersProviderMock).toHaveBeenCalledWith(
`ws://${subnetMock.endpoint}/ws`
)
expect(ethersProviderMock).toHaveBeenCalledWith(TOPOS_SUBNET_ENDPOINT_WS)
expect(ethersProviderMock).toHaveBeenCalledWith(subnetMock.endpointWs)
expect(ethersWalletMock).toHaveBeenCalledWith(
VALID_PRIVATE_KEY,
providerMock
Expand Down
48 changes: 26 additions & 22 deletions src/execute/execute.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { Job } from 'bull'
import { ethers, providers } from 'ethers'

import { ApmService } from '../apm/apm.service'
import { sanitizeURLProtocol } from '../utils'
import { ExecuteDto } from './execute.dto'
import {
CONTRACT_ERRORS,
Expand Down Expand Up @@ -137,7 +136,7 @@ export class ExecutionProcessorV1 {

private async _getReceivingSubnetEndpointFromId(subnetId: string) {
const toposSubnetEndpoint = this.configService.get<string>(
'TOPOS_SUBNET_ENDPOINT'
'TOPOS_SUBNET_ENDPOINT_WS'
)
const toposCoreContractAddress = this.configService.get<string>(
'TOPOS_CORE_PROXY_CONTRACT_ADDRESS'
Expand Down Expand Up @@ -166,31 +165,36 @@ export class ExecutionProcessorV1 {
)) as SubnetRegistrator

const receivingSubnet = await subnetRegistratorContract.subnets(subnetId)
return receivingSubnet.endpoint
return receivingSubnet.endpointWs || receivingSubnet.endpointHttp
}
}

private _createProvider(endpoint: string) {
return new Promise<providers.WebSocketProvider>((resolve, reject) => {
const provider = new ethers.providers.WebSocketProvider(
sanitizeURLProtocol('ws', `${endpoint}/ws`)
)

// Fix: Timeout to leave time to errors to be asynchronously caught
const timeoutId = setTimeout(() => {
resolve(provider)
}, 1000)

provider.on('debug', (data) => {
if (data.error) {
clearTimeout(timeoutId)
reject(new Error(PROVIDER_ERRORS.INVALID_ENDPOINT))
}
})
})
return new Promise<providers.WebSocketProvider | providers.JsonRpcProvider>(
(resolve, reject) => {
const url = new URL(endpoint)
const provider = url.protocol.startsWith('ws')
? new providers.WebSocketProvider(endpoint)
: new providers.JsonRpcProvider(endpoint)

// Fix: Timeout to leave time to errors to be asynchronously caught
const timeoutId = setTimeout(() => {
resolve(provider)
}, 1000)

provider.on('debug', (data) => {
if (data.error) {
clearTimeout(timeoutId)
reject(new Error(PROVIDER_ERRORS.INVALID_ENDPOINT))
}
})
}
)
}

private _createWallet(provider: providers.WebSocketProvider) {
private _createWallet(
provider: providers.WebSocketProvider | providers.JsonRpcProvider
) {
try {
return new ethers.Wallet(
this.configService.get<string>('PRIVATE_KEY'),
Expand All @@ -202,7 +206,7 @@ export class ExecutionProcessorV1 {
}

private async _getContract(
provider: providers.WebSocketProvider,
provider: providers.WebSocketProvider | providers.JsonRpcProvider,
contractAddress: string,
contractInterface: ethers.ContractInterface,
wallet?: ethers.Wallet
Expand Down
6 changes: 0 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
export function sanitizeURLProtocol(protocol: 'ws' | 'http', endpoint: string) {
return endpoint.indexOf('localhost') > -1 ||
endpoint.indexOf('127.0.0.1') > -1
? `${protocol}://${endpoint}`
: `${protocol}s://${endpoint}`
}

0 comments on commit 34d5cce

Please sign in to comment.