From e568edd3badd310dda0240045872e9252dbab1c9 Mon Sep 17 00:00:00 2001 From: Or Neeman Date: Thu, 22 Apr 2021 15:43:23 -0600 Subject: [PATCH 1/3] Update releasegold:withdraw test to catch bug due to loss of precision --- .../cli/src/commands/releasegold/withdraw.test.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/releasegold/withdraw.test.ts b/packages/cli/src/commands/releasegold/withdraw.test.ts index 34047f39736..0f12995ee11 100644 --- a/packages/cli/src/commands/releasegold/withdraw.test.ts +++ b/packages/cli/src/commands/releasegold/withdraw.test.ts @@ -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' @@ -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 (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 (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 () => { From 480e2852eb1369d7fc08a1978db54de7c901d729 Mon Sep 17 00:00:00 2001 From: Or Neeman Date: Thu, 22 Apr 2021 15:52:54 -0600 Subject: [PATCH 2/3] Fix bug due to loss in precision in releasegold:withdraw --- packages/cli/src/commands/releasegold/withdraw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/releasegold/withdraw.ts b/packages/cli/src/commands/releasegold/withdraw.ts index 7f7a57513b8..52fe5f7d79f 100644 --- a/packages/cli/src/commands/releasegold/withdraw.ts +++ b/packages/cli/src/commands/releasegold/withdraw.ts @@ -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)) } } From 5b698766cabbaa9e62f348e26b4d6880395671eb Mon Sep 17 00:00:00 2001 From: Or Neeman Date: Fri, 23 Apr 2021 08:10:45 -0600 Subject: [PATCH 3/3] Remove unnecessary awaits Co-authored-by: Eela Nagaraj <7308464+eelanagaraj@users.noreply.github.com> --- packages/cli/src/commands/releasegold/withdraw.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/releasegold/withdraw.test.ts b/packages/cli/src/commands/releasegold/withdraw.test.ts index 0f12995ee11..9d71c067d7d 100644 --- a/packages/cli/src/commands/releasegold/withdraw.test.ts +++ b/packages/cli/src/commands/releasegold/withdraw.test.ts @@ -32,11 +32,11 @@ 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 (await kit.getTotalBalance(beneficiary)).CELO! + 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 (await kit.getTotalBalance(beneficiary)).CELO! + const balanceAfter = (await kit.getTotalBalance(beneficiary)).CELO! const difference = balanceAfter.minus(balanceBefore) expect(difference).toEqBigNumber(new BigNumber(withdrawalAmount)) })