From 795a378383803065bd14f0df3248c96ac1bb2e9e Mon Sep 17 00:00:00 2001 From: Leszek Stachowski Date: Mon, 19 Aug 2024 12:52:28 +0200 Subject: [PATCH 1/3] Migrate StableTokenWrapper tests to anvil --- .../sdk/contractkit/src/test-utils/utils.ts | 19 ++++++++++++ .../src/wrappers/StableToken.test.ts | 30 +++++++++++-------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/packages/sdk/contractkit/src/test-utils/utils.ts b/packages/sdk/contractkit/src/test-utils/utils.ts index 07f5c3af5..0cda2e223 100644 --- a/packages/sdk/contractkit/src/test-utils/utils.ts +++ b/packages/sdk/contractkit/src/test-utils/utils.ts @@ -1,5 +1,9 @@ +import { StableToken } from '@celo/base' +import { STABLES_ADDRESS, withImpersonatedAccount } from '@celo/dev-utils/lib/anvil-test' import { mineBlocks } from '@celo/dev-utils/lib/ganache-test' +import BigNumber from 'bignumber.js' import Web3 from 'web3' +import { ContractKit } from '../kit' const GANACHE_EPOCH_SIZE = 100 export const currentEpochNumber = async (web3: Web3, epochSize: number = GANACHE_EPOCH_SIZE) => { @@ -35,3 +39,18 @@ export const mineToNextEpoch = async (web3: Web3, epochSize: number = GANACHE_EP const blocksUntilNextEpoch = getFirstBlockNumberForEpoch(epochNumber + 1, epochSize) - blockNumber await mineBlocks(blocksUntilNextEpoch, web3) } + +export const topUpWithToken = async ( + kit: ContractKit, + stableToken: StableToken, + account: string, + amount: BigNumber +) => { + const token = await kit.contracts.getStableToken(stableToken) + + await withImpersonatedAccount(kit.web3, STABLES_ADDRESS, async () => { + await token.transfer(account, amount.toFixed()).sendAndWaitForReceipt({ + from: STABLES_ADDRESS, + }) + }) +} diff --git a/packages/sdk/contractkit/src/wrappers/StableToken.test.ts b/packages/sdk/contractkit/src/wrappers/StableToken.test.ts index c81b3aace..1b5bcef82 100644 --- a/packages/sdk/contractkit/src/wrappers/StableToken.test.ts +++ b/packages/sdk/contractkit/src/wrappers/StableToken.test.ts @@ -1,12 +1,14 @@ import { StrongAddress } from '@celo/base' -import { testWithGanache } from '@celo/dev-utils/lib/ganache-test' +import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test' +import BigNumber from 'bignumber.js' import { StableToken } from '../celo-tokens' import { ContractKit, newKitFromWeb3 } from '../kit' +import { topUpWithToken } from '../test-utils/utils' import { StableTokenWrapper } from './StableTokenWrapper' // TEST NOTES: balances defined in test-utils/migration-override -testWithGanache('StableToken Wrapper', async (web3) => { +testWithAnvilL1('StableToken Wrapper', async (web3) => { const kit = newKitFromWeb3(web3) const stableTokenInfos: { @@ -57,20 +59,24 @@ export function testStableToken( let accounts: string[] = [] let stableToken: StableTokenWrapper - beforeAll(async () => { + beforeEach(async () => { accounts = (await web3.eth.getAccounts()) as StrongAddress[] kit.defaultAccount = accounts[0] as StrongAddress stableToken = await kit.contracts.getStableToken(stableTokenName) + + // Make sure the accounts we're transferring from has some stable token + for (let i = 0; i <= 3; i++) { + await topUpWithToken(kit, stableTokenName, accounts[i], new BigNumber(ONE_STABLE)) + } }) - test('SBAT check balance', () => - expect(stableToken.balanceOf(accounts[0])).resolves.toBeBigNumber()) - test('SBAT check decimals', () => expect(stableToken.decimals()).resolves.toBe(18)) - test('SBAT check name', () => expect(stableToken.name()).resolves.toBe(expectedName)) - test('SBAT check symbol', () => expect(stableToken.symbol()).resolves.toBe(expectedSymbol)) - test('SBAT check totalSupply', () => expect(stableToken.totalSupply()).resolves.toBeBigNumber()) + it('checks balance', () => expect(stableToken.balanceOf(accounts[0])).resolves.toBeBigNumber()) + it('checks decimals', () => expect(stableToken.decimals()).resolves.toBe(18)) + it('checks name', () => expect(stableToken.name()).resolves.toBe(expectedName)) + it('checks symbol', () => expect(stableToken.symbol()).resolves.toBe(expectedSymbol)) + it('checks totalSupply', () => expect(stableToken.totalSupply()).resolves.toBeBigNumber()) - test('SBAT transfer', async () => { + it('transfers', async () => { const before = await stableToken.balanceOf(accounts[1]) const tx = await stableToken.transfer(accounts[1], ONE_STABLE).send() await tx.waitReceipt() @@ -79,7 +85,7 @@ export function testStableToken( expect(after.minus(before)).toEqBigNumber(ONE_STABLE) }) - test('SBAT approve spender', async () => { + it('approves spender', async () => { const before = await stableToken.allowance(accounts[0], accounts[1]) expect(before).toEqBigNumber(0) @@ -88,7 +94,7 @@ export function testStableToken( expect(after).toEqBigNumber(ONE_STABLE) }) - test('SBAT tranfer from', async () => { + it('transfers from', async () => { const before = await stableToken.balanceOf(accounts[3]) // account1 approves account0 await stableToken.approve(accounts[1], ONE_STABLE).sendAndWaitForReceipt({ from: accounts[0] }) From 323c6ccb805122559b45f8640260c1db4381d1d9 Mon Sep 17 00:00:00 2001 From: Leszek Stachowski Date: Thu, 29 Aug 2024 12:45:27 +0200 Subject: [PATCH 2/3] CR fixes + expect failure --- packages/sdk/contractkit/src/test-utils/utils.ts | 4 ++-- packages/sdk/contractkit/src/wrappers/StableToken.test.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/sdk/contractkit/src/test-utils/utils.ts b/packages/sdk/contractkit/src/test-utils/utils.ts index 0cda2e223..fbec17cde 100644 --- a/packages/sdk/contractkit/src/test-utils/utils.ts +++ b/packages/sdk/contractkit/src/test-utils/utils.ts @@ -43,13 +43,13 @@ export const mineToNextEpoch = async (web3: Web3, epochSize: number = GANACHE_EP export const topUpWithToken = async ( kit: ContractKit, stableToken: StableToken, - account: string, + recipientAddress: string, amount: BigNumber ) => { const token = await kit.contracts.getStableToken(stableToken) await withImpersonatedAccount(kit.web3, STABLES_ADDRESS, async () => { - await token.transfer(account, amount.toFixed()).sendAndWaitForReceipt({ + await token.transfer(recipientAddress, amount.toFixed()).sendAndWaitForReceipt({ from: STABLES_ADDRESS, }) }) diff --git a/packages/sdk/contractkit/src/wrappers/StableToken.test.ts b/packages/sdk/contractkit/src/wrappers/StableToken.test.ts index 1b5bcef82..5c2c7a36e 100644 --- a/packages/sdk/contractkit/src/wrappers/StableToken.test.ts +++ b/packages/sdk/contractkit/src/wrappers/StableToken.test.ts @@ -73,7 +73,8 @@ export function testStableToken( it('checks balance', () => expect(stableToken.balanceOf(accounts[0])).resolves.toBeBigNumber()) it('checks decimals', () => expect(stableToken.decimals()).resolves.toBe(18)) it('checks name', () => expect(stableToken.name()).resolves.toBe(expectedName)) - it('checks symbol', () => expect(stableToken.symbol()).resolves.toBe(expectedSymbol)) + // TODO fix once symbols are available in the devchain + it.failing('checks symbol', () => expect(stableToken.symbol()).resolves.toBe(expectedSymbol)) it('checks totalSupply', () => expect(stableToken.totalSupply()).resolves.toBeBigNumber()) it('transfers', async () => { From 32aaa42455fa8a4f5de8c33caad598370f05478d Mon Sep 17 00:00:00 2001 From: Leszek Stachowski Date: Thu, 29 Aug 2024 13:29:40 +0200 Subject: [PATCH 3/3] yarn docs --- .../contractkit/modules/test_utils_utils.md | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/sdk/contractkit/modules/test_utils_utils.md b/docs/sdk/contractkit/modules/test_utils_utils.md index 91819018b..7af6d48c3 100644 --- a/docs/sdk/contractkit/modules/test_utils_utils.md +++ b/docs/sdk/contractkit/modules/test_utils_utils.md @@ -8,6 +8,7 @@ - [currentEpochNumber](test_utils_utils.md#currentepochnumber) - [mineToNextEpoch](test_utils_utils.md#minetonextepoch) +- [topUpWithToken](test_utils_utils.md#topupwithtoken) ## Functions @@ -28,7 +29,7 @@ #### Defined in -[packages/sdk/contractkit/src/test-utils/utils.ts:5](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/test-utils/utils.ts#L5) +[packages/sdk/contractkit/src/test-utils/utils.ts:9](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/test-utils/utils.ts#L9) ___ @@ -49,4 +50,27 @@ ___ #### Defined in -[packages/sdk/contractkit/src/test-utils/utils.ts:32](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/test-utils/utils.ts#L32) +[packages/sdk/contractkit/src/test-utils/utils.ts:36](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/test-utils/utils.ts#L36) + +___ + +### topUpWithToken + +▸ **topUpWithToken**(`kit`, `stableToken`, `recipientAddress`, `amount`): `Promise`\<`void`\> + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `kit` | [`ContractKit`](../classes/kit.ContractKit.md) | +| `stableToken` | [`StableToken`](../enums/celo_tokens.StableToken.md) | +| `recipientAddress` | `string` | +| `amount` | `BigNumber` | + +#### Returns + +`Promise`\<`void`\> + +#### Defined in + +[packages/sdk/contractkit/src/test-utils/utils.ts:43](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/test-utils/utils.ts#L43)