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

feat: use new subnet endpoint fields #16

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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=
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
@@ -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",
16 changes: 6 additions & 10 deletions src/execute/execute.processor.spec.ts
Original file line number Diff line number Diff line change
@@ -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: {
@@ -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'),
})
@@ -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
}
}),
}
@@ -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
48 changes: 26 additions & 22 deletions src/execute/execute.processor.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -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'
@@ -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'),
@@ -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
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}`
}