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
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan committed Nov 2, 2023
1 parent 477fde4 commit 9f9cafc
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 57 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

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

53 changes: 27 additions & 26 deletions packages/backend/src/faucet/faucet.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { ethers, providers } from 'ethers'
import { providers, utils, Wallet } from 'ethers'

import { GetSubnetAssetsDto } from './faucet.dto'
import { sanitizeURLProtocol } from '../utils'
import { PROVIDER_ERRORS, WALLET_ERRORS } from './faucet.errors'
import { ApmService } from 'src/apm/apm.service'

Expand Down Expand Up @@ -68,7 +67,7 @@ export class FaucetService {

const tx = await wallet.sendTransaction({
to: address,
value: ethers.utils.parseUnits('1.0'),
value: utils.parseUnits('1.0'),
})

const receipt = await tx.wait()
Expand All @@ -81,32 +80,34 @@ export class FaucetService {
}

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)
this.apmService.captureError(data.error)
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)
this.apmService.captureError(data.error)
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'),
provider
)
return new Wallet(this.configService.get<string>('PRIVATE_KEY'), provider)
} catch (error) {
this.apmService.captureError(error)
throw new Error(WALLET_ERRORS.INVALID_PRIVATE_KEY)
Expand Down
6 changes: 0 additions & 6 deletions packages/backend/src/utils/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
VITE_SUBNET_REGISTRATOR_CONTRACT_ADDRESS=
VITE_TOPOS_CORE_PROXY_CONTRACT_ADDRESS=
VITE_TOPOS_SUBNET_ENDPOINT=
VITE_TOPOS_SUBNET_ENDPOINT_HTTP=
VITE_TOPOS_SUBNET_ENDPOINT_WS=
VITE_TRACING_SERVICE_NAME=
VITE_TRACING_SERVICE_VERSION=
VITE_ELASTIC_APM_ENDPOINT=
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@elastic/apm-rum": "^5.14.0",
"@emotion/react": "11.10.6",
"@emotion/styled": "11.10.6",
"@topos-protocol/topos-smart-contracts": "^1.2.2",
"@topos-protocol/topos-smart-contracts": "^2.0.0-rc1",
"antd": "^5.4.0",
"axios": "^1.4.0",
"ethers": "^5.7.2",
Expand Down
13 changes: 8 additions & 5 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Content from './components/Content'
import { SubnetWithId } from './types'
import useRegisteredSubnets from './hooks/useRegisteredSubnets'
import { BigNumber, ethers } from 'ethers'
import { sanitizeURLProtocol } from './utils'
import { SubnetsContext } from './contexts/subnets'
import { toposCoreContract } from './contracts'
import { SuccessesContext } from './contexts/successes'
Expand Down Expand Up @@ -48,12 +47,15 @@ const App = () => {
function onRegisteredSubnetsChange() {
async function _() {
if (registeredSubnets) {
const toposSubnetEndpoint = import.meta.env.VITE_TOPOS_SUBNET_ENDPOINT
const toposSubnetEndpointHttp = import.meta.env
.VITE_TOPOS_SUBNET_ENDPOINT_HTTP
const toposSubnetEndpointWs = import.meta.env
.VITE_TOPOS_SUBNET_ENDPOINT_WS
let toposSubnet: SubnetWithId | undefined

if (toposSubnetEndpoint) {
if (toposSubnetEndpointHttp && toposSubnetEndpointWs) {
const provider = new ethers.providers.JsonRpcProvider(
sanitizeURLProtocol('http', toposSubnetEndpoint)
toposSubnetEndpointHttp
)
const network = await provider.getNetwork()
const chainId = network.chainId
Expand All @@ -63,7 +65,8 @@ const App = () => {

toposSubnet = {
chainId: BigNumber.from(chainId.toString()),
endpoint: toposSubnetEndpoint,
endpointHttp: toposSubnetEndpointHttp,
endpointWs: toposSubnetEndpointWs,
currencySymbol: 'TOPOS',
id: subnetId,
logoURL: '/logo.svg',
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/FaucetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const FaucetForm = () => {
const subnetEnpoints = subnetIds
.map((id) => {
const subnet = registeredSubnets?.find((s) => s.id === id)!
return subnet?.endpoint
return subnet.endpointWs || subnet.endpointHttp
})
.filter((s) => s)

Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/src/components/SubnetSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const subnetsMock: SubnetWithId[] = [
{
chainId: BigNumber.from(1),
currencySymbol: 'TST',
endpoint: '',
endpointHttp: '',
endpointWs: '',
id: '',
logoURL: '',
name: 'subnetMock',
Expand Down
6 changes: 4 additions & 2 deletions packages/frontend/src/hooks/useRegisteredSubnets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const registeredSubnets: { [x: string]: Subnet } = {
subnet1: {
chainId: BigNumber.from(1),
currencySymbol: 'TST',
endpoint: '',
endpointHttp: '',
endpointWs: '',
logoURL: '',
name: 'subnetMock',
},
subnet2: {
chainId: BigNumber.from(2),
currencySymbol: 'TST2',
endpoint: '',
endpointHttp: '',
endpointWs: '',
logoURL: '',
name: 'subnetMock',
},
Expand Down
6 changes: 1 addition & 5 deletions packages/frontend/src/hooks/useRegisteredSubnets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useMemo } from 'react'

import { subnetRegistratorContract } from '../contracts'
import { Subnet, SubnetWithId } from '../types'
import { sanitizeURLProtocol } from '../utils'
import { ErrorsContext } from '../contexts/errors'

export default function useRegisteredSubnets() {
Expand All @@ -15,10 +14,7 @@ export default function useRegisteredSubnets() {
const provider = useMemo(
() =>
new ethers.providers.WebSocketProvider(
sanitizeURLProtocol(
'ws',
`${import.meta.env.VITE_TOPOS_SUBNET_ENDPOINT}/ws`
)
import.meta.env.VITE_TOPOS_SUBNET_ENDPOINT_WS
),
[]
)
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BigNumber } from 'ethers'
export interface Subnet {
chainId: BigNumber
currencySymbol: string
endpoint: string
endpointHttp: string
endpointWs: string
logoURL: string
name: string
}
Expand Down

0 comments on commit 9f9cafc

Please sign in to comment.