Skip to content
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

Fix loss of precision releasegold:withdraw CLI command #7748

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/cli/src/commands/releasegold/withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ContractKit, newKitFromWeb3 } from '@celo/contractkit'
import { newReleaseGold } from '@celo/contractkit/lib/generated/ReleaseGold'
import { ReleaseGoldWrapper } from '@celo/contractkit/lib/wrappers/ReleaseGold'
import { getContractFromEvent, testWithGanache, timeTravel } from '@celo/dev-utils/lib/ganache-test'
import { BigNumber } from 'bignumber.js'
import Web3 from 'web3'
import CreateAccount from './create-account'
import SetLiquidityProvision from './set-liquidity-provision'
Expand Down Expand Up @@ -31,10 +32,13 @@ testWithGanache('releasegold:withdraw cmd', (web3: Web3) => {
await timeTravel(100000000, web3)
const releaseGoldWrapper = new ReleaseGoldWrapper(kit, newReleaseGold(web3, contractAddress))
const beneficiary = await releaseGoldWrapper.getBeneficiary()
const balanceBefore = await kit.getTotalBalance(beneficiary)
await Withdraw.run(['--contract', contractAddress, '--value', '10000000000000000000000'])
const balanceAfter = await kit.getTotalBalance(beneficiary)
expect(balanceBefore.CELO!.toNumber()).toBeLessThan(balanceAfter.CELO!.toNumber())
const balanceBefore = (await kit.getTotalBalance(beneficiary)).CELO!
// Use a value which would lose precision if converted to a normal javascript number
const withdrawalAmount = '10000000000000000000005'
await Withdraw.run(['--contract', contractAddress, '--value', withdrawalAmount])
const balanceAfter = (await kit.getTotalBalance(beneficiary)).CELO!
const difference = balanceAfter.minus(balanceBefore)
expect(difference).toEqBigNumber(new BigNumber(withdrawalAmount))
})

test("can't withdraw the whole balance if there is a cUSD balance", async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/releasegold/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ export default class Withdraw extends ReleaseGoldBaseCommand {
.runChecks()

this.kit.defaultAccount = await this.releaseGoldWrapper.getBeneficiary()
await displaySendTx('withdrawTx', this.releaseGoldWrapper.withdraw(value.toNumber()))
await displaySendTx('withdrawTx', this.releaseGoldWrapper.withdraw(value))
}
}