Skip to content

Commit

Permalink
EthereumERC20Provider: Throw error if contract does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
monokh committed Nov 1, 2019
1 parent be24185 commit 8a072e3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/ethereum-erc20-provider/lib/EthereumErc20Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default class EthereumErc20Provider extends Provider {
this._contractAddress = remove0x(contractAddress)
}

async assertContractExists () {
const code = await this.getMethod('getCode')(this._contractAddress, 'latest')
if (code === '') throw new Error(`Contract does not exist at given address: ${this._contractAddress}`)
}

generateErc20Transfer (to, value) {
value = BigNumber(value).toString(16)

Expand All @@ -33,7 +38,9 @@ export default class EthereumErc20Provider extends Provider {
].join('').toLowerCase()
}

sendTransaction (to, value, data, from) {
async sendTransaction (to, value, data, from) {
await this.assertContractExists()

if (!data) {
// erc20 transfer
data = this.generateErc20Transfer(to, value)
Expand All @@ -49,6 +56,8 @@ export default class EthereumErc20Provider extends Provider {
}

async getBalance (addresses) {
await this.assertContractExists()

if (!isArray(addresses)) {
addresses = [ addresses ]
}
Expand Down

0 comments on commit 8a072e3

Please sign in to comment.