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

Commit 34d5cce

Browse files
author
Sébastien Dan
authored
feat: use new subnet endpoint fields (#16)
1 parent 1bf66b1 commit 34d5cce

File tree

7 files changed

+42
-48
lines changed

7 files changed

+42
-48
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ AUTH0_AUDIENCE=
22
AUTH0_ISSUER_URL=
33
REDIS_HOST=
44
REDIS_PORT=
5-
TOPOS_SUBNET_ENDPOINT=
5+
TOPOS_SUBNET_ENDPOINT_WS=
66
SUBNET_REGISTRATOR_CONTRACT_ADDRESS=
77
TOPOS_CORE_PROXY_CONTRACT_ADDRESS=
88
TRACING_SERVICE_NAME=

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AUTH0_AUDIENCE=
3838
AUTH0_ISSUER_URL=
3939
REDIS_HOST=
4040
REDIS_PORT=
41-
TOPOS_SUBNET_ENDPOINT=
41+
TOPOS_SUBNET_ENDPOINT_WS=
4242
SUBNET_REGISTRATOR_CONTRACT_ADDRESS=
4343
TOPOS_CORE_PROXY_CONTRACT_ADDRESS=
4444
ERC20_MESSAGING_CONTRACT_ADDRESS=

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@nestjs/passport": "^9.0.0",
3535
"@nestjs/platform-express": "^9.4.0",
3636
"@nestjs/swagger": "^6.1.2",
37-
"@topos-protocol/topos-smart-contracts": "^1.2.3",
37+
"@topos-protocol/topos-smart-contracts": "^2.0.0",
3838
"bcrypt": "^5.1.0",
3939
"bull": "^4.10.1",
4040
"class-transformer": "^0.5.1",

src/execute/execute.processor.spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const VALID_PRIVATE_KEY =
1313
'0xc6cbd7d76bc5baca530c875663711b947efa6a86a900a9e8645ce32e5821484e'
1414
const TOPOS_CORE_PROXY_CONTRACT_ADDRESS =
1515
'0x1D7b9f9b1FF6cf0A3BEB0F84fA6F8628E540E97F'
16-
const TOPOS_SUBNET_ENDPOINT = 'topos-subnet-endpoint'
16+
const TOPOS_SUBNET_ENDPOINT_WS = 'ws://topos-subnet-endpoint/ws'
1717

1818
const validExecuteJob: Partial<Job<ExecuteDto & TracingOptions>> = {
1919
data: {
@@ -27,7 +27,7 @@ const validExecuteJob: Partial<Job<ExecuteDto & TracingOptions>> = {
2727
progress: jest.fn(),
2828
}
2929

30-
const subnetMock = { endpoint: 'endpoint' }
30+
const subnetMock = { endpointWs: 'ws://endpoint/ws' }
3131
const providerMock = Object.assign(new EventEmitter(), {
3232
getCode: jest.fn().mockResolvedValue('0x123'),
3333
})
@@ -57,8 +57,8 @@ describe('ExecuteProcessor', () => {
5757
return VALID_PRIVATE_KEY
5858
case 'TOPOS_CORE_PROXY_CONTRACT_ADDRESS':
5959
return TOPOS_CORE_PROXY_CONTRACT_ADDRESS
60-
case 'TOPOS_SUBNET_ENDPOINT':
61-
return TOPOS_SUBNET_ENDPOINT
60+
case 'TOPOS_SUBNET_ENDPOINT_WS':
61+
return TOPOS_SUBNET_ENDPOINT_WS
6262
}
6363
}),
6464
}
@@ -97,12 +97,8 @@ describe('ExecuteProcessor', () => {
9797
validExecuteJob as unknown as Job<ExecuteDto & TracingOptions>
9898
)
9999

100-
expect(ethersProviderMock).toHaveBeenCalledWith(
101-
`ws://${TOPOS_SUBNET_ENDPOINT}/ws`
102-
)
103-
expect(ethersProviderMock).toHaveBeenCalledWith(
104-
`ws://${subnetMock.endpoint}/ws`
105-
)
100+
expect(ethersProviderMock).toHaveBeenCalledWith(TOPOS_SUBNET_ENDPOINT_WS)
101+
expect(ethersProviderMock).toHaveBeenCalledWith(subnetMock.endpointWs)
106102
expect(ethersWalletMock).toHaveBeenCalledWith(
107103
VALID_PRIVATE_KEY,
108104
providerMock

src/execute/execute.processor.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { Job } from 'bull'
2323
import { ethers, providers } from 'ethers'
2424

2525
import { ApmService } from '../apm/apm.service'
26-
import { sanitizeURLProtocol } from '../utils'
2726
import { ExecuteDto } from './execute.dto'
2827
import {
2928
CONTRACT_ERRORS,
@@ -137,7 +136,7 @@ export class ExecutionProcessorV1 {
137136

138137
private async _getReceivingSubnetEndpointFromId(subnetId: string) {
139138
const toposSubnetEndpoint = this.configService.get<string>(
140-
'TOPOS_SUBNET_ENDPOINT'
139+
'TOPOS_SUBNET_ENDPOINT_WS'
141140
)
142141
const toposCoreContractAddress = this.configService.get<string>(
143142
'TOPOS_CORE_PROXY_CONTRACT_ADDRESS'
@@ -166,31 +165,36 @@ export class ExecutionProcessorV1 {
166165
)) as SubnetRegistrator
167166

168167
const receivingSubnet = await subnetRegistratorContract.subnets(subnetId)
169-
return receivingSubnet.endpoint
168+
return receivingSubnet.endpointWs || receivingSubnet.endpointHttp
170169
}
171170
}
172171

173172
private _createProvider(endpoint: string) {
174-
return new Promise<providers.WebSocketProvider>((resolve, reject) => {
175-
const provider = new ethers.providers.WebSocketProvider(
176-
sanitizeURLProtocol('ws', `${endpoint}/ws`)
177-
)
178-
179-
// Fix: Timeout to leave time to errors to be asynchronously caught
180-
const timeoutId = setTimeout(() => {
181-
resolve(provider)
182-
}, 1000)
183-
184-
provider.on('debug', (data) => {
185-
if (data.error) {
186-
clearTimeout(timeoutId)
187-
reject(new Error(PROVIDER_ERRORS.INVALID_ENDPOINT))
188-
}
189-
})
190-
})
173+
return new Promise<providers.WebSocketProvider | providers.JsonRpcProvider>(
174+
(resolve, reject) => {
175+
const url = new URL(endpoint)
176+
const provider = url.protocol.startsWith('ws')
177+
? new providers.WebSocketProvider(endpoint)
178+
: new providers.JsonRpcProvider(endpoint)
179+
180+
// Fix: Timeout to leave time to errors to be asynchronously caught
181+
const timeoutId = setTimeout(() => {
182+
resolve(provider)
183+
}, 1000)
184+
185+
provider.on('debug', (data) => {
186+
if (data.error) {
187+
clearTimeout(timeoutId)
188+
reject(new Error(PROVIDER_ERRORS.INVALID_ENDPOINT))
189+
}
190+
})
191+
}
192+
)
191193
}
192194

193-
private _createWallet(provider: providers.WebSocketProvider) {
195+
private _createWallet(
196+
provider: providers.WebSocketProvider | providers.JsonRpcProvider
197+
) {
194198
try {
195199
return new ethers.Wallet(
196200
this.configService.get<string>('PRIVATE_KEY'),
@@ -202,7 +206,7 @@ export class ExecutionProcessorV1 {
202206
}
203207

204208
private async _getContract(
205-
provider: providers.WebSocketProvider,
209+
provider: providers.WebSocketProvider | providers.JsonRpcProvider,
206210
contractAddress: string,
207211
contractInterface: ethers.ContractInterface,
208212
wallet?: ethers.Wallet

src/utils/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
export function sanitizeURLProtocol(protocol: 'ws' | 'http', endpoint: string) {
2-
return endpoint.indexOf('localhost') > -1 ||
3-
endpoint.indexOf('127.0.0.1') > -1
4-
? `${protocol}://${endpoint}`
5-
: `${protocol}s://${endpoint}`
6-
}

0 commit comments

Comments
 (0)