Skip to content

Commit

Permalink
fix: better handle token registration errors (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan authored Jan 19, 2024
1 parent 6308e74 commit 0884437
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const App = () => {
const subnetId = await contract.networkSubnetId()

toposSubnet = {
chainId: chainId,
chainId,
endpointHttp: toposSubnetEndpointHttp,
endpointWs: toposSubnetEndpointWs,
currencySymbol: 'TOPOS',
Expand Down
22 changes: 12 additions & 10 deletions packages/frontend/src/components/RegisterToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ERROR, SUCCESS } from '../constants/wordings'
import { MultiStepFormContext } from '../contexts/multiStepForm'
import useRegisterToken from '../hooks/useRegisterToken'
import TestId from '../utils/testId'
import { ErrorsContext } from '../contexts/errors'

export interface Values {
cap: number
Expand All @@ -26,10 +27,10 @@ interface RegisterTokenFormProps {
}

const RegisterTokenForm = ({ open, setOpen }: RegisterTokenFormProps) => {
const [loading, setLoading] = useState(false)
const { setErrors } = useContext(ErrorsContext)
const { registeredTokens } = useContext(MultiStepFormContext)
const [form] = Form.useForm()
const { registerToken } = useRegisterToken()
const { loading, registerToken } = useRegisterToken()

const onCancel = useCallback(() => {
setOpen(false)
Expand All @@ -51,14 +52,15 @@ const RegisterTokenForm = ({ open, setOpen }: RegisterTokenFormProps) => {
form
.validateFields()
.then((values) => {
setLoading(true)

registerToken(values).then(() => {
message.success(SUCCESS.REGISTERED_TOKEN)
setLoading(false)
form.resetFields()
setOpen(false)
})
registerToken(values)
.then(() => {
message.success(SUCCESS.REGISTERED_TOKEN)
form.resetFields()
setOpen(false)
})
.catch((error) => {
setErrors((e) => [...e, error])
})
})
.catch((info) => {
console.log('Validate Failed:', info)
Expand Down
4 changes: 3 additions & 1 deletion packages/frontend/src/hooks/useRegisterToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ describe('registerToken', () => {
supply,
})
.then(() => {
expect(deployTokenMock).toHaveBeenCalledWith(params)
expect(deployTokenMock).toHaveBeenCalledWith(params, {
gasLimit: 5_000_000,
})
})
.finally(() => {
expect(result.current.loading).toBe(false)
Expand Down
48 changes: 15 additions & 33 deletions packages/frontend/src/hooks/useRegisterToken.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { ERC20Messaging__factory } from '@topos-protocol/topos-smart-contracts/typechain-types'
import { AbiCoder, BrowserProvider, parseUnits } from 'ethers'
import { useCallback, useContext, useState } from 'react'
import { useCallback, useState } from 'react'

import { Values } from '../components/RegisterToken'
import { ErrorsContext } from '../contexts/errors'
import useEthers from './useEthers'

export default function useRegisterToken() {
const { provider } = useEthers({
viaMetaMask: true,
})
const { setErrors } = useContext(ErrorsContext)
const [loading, setLoading] = useState(false)

const registerToken = useCallback(
Expand All @@ -35,36 +33,20 @@ export default function useRegisterToken() {
]
)

return new Promise((resolve, reject) => {
erc20Messaging
.deployToken(params)
.then((tx) =>
tx
.wait()
.then((receipt) => {
resolve(receipt)
})
.catch((error: Error) => {
console.error(error)
setErrors((e) => [
...e,
{ message: `Error when registering the token` },
])
reject(error)
})
)
.catch((error: Error) => {
console.error(JSON.stringify(error))
setErrors((e) => [
...e,
{ message: `Error when registering the token` },
])
reject(error)
})
.finally(() => {
setLoading(false)
})
})
try {
const tx = await erc20Messaging.deployToken(params, {
gasLimit: 5_000_000,
})
const receipt = await tx.wait()
setLoading(false)
return receipt
} catch (error: any) {
console.error(error)
setLoading(false)
throw Error(
`Error when registering the token (reason: ${error.reason})`
)
}
},
[provider]
)
Expand Down

0 comments on commit 0884437

Please sign in to comment.