Skip to content

Commit

Permalink
Merge pull request #284 from ensdomains/try-catch-estimate-gas
Browse files Browse the repository at this point in the history
Try/Catch estimateGas
  • Loading branch information
jefflau authored May 13, 2019
2 parents 0e1bdc4 + bde99e7 commit 7d878ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
23 changes: 9 additions & 14 deletions src/api/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
legacyRegistrar as legacyRegistrarInterfaceId,
permanentRegistrar as permanentRegistrarInterfaceId
} from '../constants/interfaces'
import { estimateAndSend } from './resolverUtils'
let ethRegistrar
let ethRegistrarRead
let permanentRegistrar
Expand Down Expand Up @@ -385,25 +386,19 @@ export const transferRegistrars = async label => {
const account = await getAccount()
const web3 = await getWeb3()
const hash = web3.utils.sha3(label)
const tx = ethRegistrar.transferRegistrars(hash)
const gas = await tx.estimateGas({ from: account })
return () =>
tx.send({
from: account,
gas: gas
})
return await estimateAndSend(
ethRegistrar.transferRegistrars(hash),
account
)
}

export const releaseDeed = async label => {
const { ethRegistrar } = await getLegacyAuctionRegistrar()
const account = await getAccount()
const web3 = await getWeb3()
const hash = web3.utils.sha3(label)
const tx = ethRegistrar.releaseDeed(hash)
const gas = await tx.estimateGas({ from: account })
return () =>
tx.send({
from: account,
gas: gas
})
return await estimateAndSend(
ethRegistrar.releaseDeed(hash),
account
)
}
18 changes: 11 additions & 7 deletions src/api/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import getENS, {
import { decryptHashes } from './preimage'
import { uniq, ensStartBlock, checkLabels, mergeLabels } from '../utils/utils'
import getWeb3, { getAccount } from './web3'
import { estimateAndSend } from './resolverUtils'

export async function getOwner(name) {
const { readENS: ENS } = await getENS()
Expand Down Expand Up @@ -160,23 +161,26 @@ export async function setAddress(name, address) {
}

export async function setContent(name, content) {
const account = await getAccount()
let account = await getAccount()
const namehash = getNamehash(name)
const resolverAddr = await getResolver(name)
const { Resolver } = await getResolverContract(resolverAddr)
const gas = await Resolver.setContent(namehash, content).estimateGas({from:account})
return () =>
Resolver.setContent(namehash, content).send({ from: account, gas })
return await estimateAndSend(
Resolver.setContent(namehash, content),
account
)
}

export async function setContenthash(name, content) {
const account = await getAccount()
const namehash = getNamehash(name)
const resolverAddr = await getResolver(name)
const { Resolver } = await getResolverContract(resolverAddr)
const tx = Resolver.setContenthash(namehash, content)
const gas = await tx.estimateGas({from:account})
return () => tx.send({ from: account, gas: gas })

return await estimateAndSend(
Resolver.setContenthash(namehash, content),
account
)
}

export async function checkSubDomain(subDomain, domain) {
Expand Down
12 changes: 12 additions & 0 deletions src/api/resolverUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ export function sendHelper(tx) {
})
})
}

export async function estimateAndSend(tx, account){
let gas
try{
gas = await tx.estimateGas({from:account})
}catch(e){
console.log('gasEstimate error', {e, gas})
}
return () =>
tx.send({ from: account, gas })
}

0 comments on commit 7d878ea

Please sign in to comment.