-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli] transfer:erc20 and balance commands (#7753)
Command to transfer and check balance of erc20 contracts
- Loading branch information
1 parent
6c54a88
commit 372ffee
Showing
9 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
import { BaseCommand } from '../../base' | ||
import { printValueMap } from '../../utils/cli' | ||
import { Args } from '../../utils/command' | ||
import { failWith, printValueMap } from '../../utils/cli' | ||
import { Args, Flags } from '../../utils/command' | ||
|
||
export default class Balance extends BaseCommand { | ||
static description = 'View Celo Dollar and Gold balances for an address' | ||
static description = 'View Celo Stables and CELO balances for an address' | ||
|
||
static flags = { | ||
...BaseCommand.flags, | ||
erc20Address: Flags.address({ | ||
description: 'Address of generic ERC-20 token to also check balance for', | ||
}), | ||
} | ||
|
||
static args = [Args.address('address')] | ||
|
||
static examples = ['balance 0x5409ed021d9299bf6814279a6a1411a7e866a631'] | ||
static examples = [ | ||
'balance 0x5409ed021d9299bf6814279a6a1411a7e866a631', | ||
'balance 0x5409ed021d9299bf6814279a6a1411a7e866a631 --erc20Address 0x765DE816845861e75A25fCA122bb6898B8B1282a', | ||
] | ||
|
||
async run() { | ||
const { args } = this.parse(Balance) | ||
const { args, flags } = this.parse(Balance) | ||
|
||
console.log('All balances expressed in units of 10^-18.') | ||
printValueMap(await this.kit.getTotalBalance(args.address)) | ||
if (flags.erc20Address) { | ||
try { | ||
const erc20 = await this.kit.contracts.getErc20(flags.erc20Address) | ||
printValueMap({ erc20: await erc20.balanceOf(args.address) }) | ||
} catch { | ||
failWith('Invalid erc20 address') | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Ierc20 } from '@celo/contractkit/lib/generated/IERC20' | ||
import { Erc20Wrapper } from '@celo/contractkit/lib/wrappers/Erc20Wrapper' | ||
import { flags } from '@oclif/command' | ||
import BigNumber from 'bignumber.js' | ||
import { BaseCommand } from '../../base' | ||
import { newCheckBuilder } from '../../utils/checks' | ||
import { displaySendTx, failWith } from '../../utils/cli' | ||
import { Flags } from '../../utils/command' | ||
|
||
export default class TransferErc20 extends BaseCommand { | ||
static description = 'Transfer ERC20 to a specified address' | ||
|
||
static flags = { | ||
...BaseCommand.flags, | ||
erc20Address: Flags.address({ | ||
required: true, | ||
description: "Custom erc20 to check it's balance too", | ||
}), | ||
from: Flags.address({ | ||
required: true, | ||
description: 'Address of the sender', | ||
}), | ||
to: Flags.address({ | ||
required: true, | ||
description: 'Address of the receiver', | ||
}), | ||
value: flags.string({ | ||
required: true, | ||
description: 'Amount to transfer (in wei)', | ||
}), | ||
} | ||
|
||
static examples = [ | ||
'erc20 --erc20Address 0x765DE816845861e75A25fCA122bb6898B8B1282a --from 0xa0Af2E71cECc248f4a7fD606F203467B500Dd53B --to 0x5409ed021d9299bf6814279a6a1411a7e866a631 --value 10000000000000000000', | ||
] | ||
|
||
async run() { | ||
const res = this.parse(TransferErc20) | ||
|
||
const from: string = res.flags.from | ||
const to: string = res.flags.to | ||
const value = new BigNumber(res.flags.value) | ||
|
||
this.kit.defaultAccount = from | ||
let celoToken: Erc20Wrapper<Ierc20> | ||
try { | ||
celoToken = await this.kit.contracts.getErc20(res.flags.erc20Address) | ||
// this call allow us to check if it is a valid erc20 | ||
await celoToken.balanceOf(res.flags.from) | ||
} catch { | ||
failWith('Invalid erc20 address') | ||
} | ||
await newCheckBuilder(this).hasEnoughErc20(from, value, res.flags.erc20Address).runChecks() | ||
|
||
await displaySendTx('transfer', celoToken.transfer(to, value.toFixed())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters