-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cli] transfer:erc20 and balance commands #7753
Changes from all commits
bff47f0
f6d8982
5255ac2
78a9d46
0ae271f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
} | ||
} | ||
} | ||
} |
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())) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ export enum CeloContract { | |
DowntimeSlasher = 'DowntimeSlasher', | ||
Election = 'Election', | ||
EpochRewards = 'EpochRewards', | ||
ERC20 = 'ERC20', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like the wrong place for this to live There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is "ERC20" a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should define what we think it is a CeloContract. In a way, we think of those as the contracts supported/maintained by celo. The ERC20 is a standard accepted by everyone. If we think of the CeloContracts as only the ones that live on the registry, we should also remove the multisig from that list, for example. If we think of the CeloContracts, as the one accepted by the community, all the ercXXX accepted, should be supported too |
||
Escrow = 'Escrow', | ||
Exchange = 'Exchange', | ||
ExchangeEUR = 'ExchangeEUR', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can define this flag once and reuse it across the commands
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know if we require this in more that these two commands. We could leave it this way and if we see another pattern, we could extract it in the future