Skip to content

Commit 176fc50

Browse files
committed
add more tests
1 parent 45b807e commit 176fc50

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

e2e/zombienet/src/hooks/useContract2.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeAll, describe, expect, it } from 'vitest';
2-
import { ALICE, BOB, deployPsp22Contract, psp22Metadata, wrapper } from '../utils';
2+
import { ALICE, BOB, deployPsp22Contract, devPairs, mintPSP22Balance, psp22Metadata, wrapper } from '../utils';
33
import { numberToHex } from 'dedot/utils';
44
import { Contract } from 'dedot/contracts';
55
import { Psp22ContractApi } from '../contracts/psp22';
@@ -70,4 +70,25 @@ describe('useContract2', () => {
7070
// BOB's balance should be different from ALICE's
7171
expect(result.current.data).not.toEqual(aliceBalance);
7272
});
73+
74+
it('should automatic update balance when watch is enabled', async () => {
75+
const { result } = renderHook(() => usePSP22Balance({ contractAddress, address: ALICE, watch: true }), { wrapper });
76+
77+
// Wait for ALICE's balance to be fetched
78+
await waitFor(() => {
79+
expect(result.current.data).toBeDefined();
80+
});
81+
82+
const aliceBalance = result.current.data;
83+
84+
// Simulate a transfer event
85+
const { alice } = devPairs();
86+
await mintPSP22Balance(contractAddress, alice, BigInt(1e12));
87+
88+
// Wait for the balance to be updated
89+
await waitFor(() => {
90+
expect(result.current.data).toBe(BigInt(1e20) + BigInt(1e12));
91+
expect(result.current.data).not.toEqual(aliceBalance);
92+
});
93+
});
7394
});

e2e/zombienet/src/utils.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Psp22ContractApi } from './contracts/psp22';
66
import * as flipper from './contracts/flipper_v5.json';
77
// @ts-ignore
88
import * as psp22 from './contracts/psp22.json';
9-
import { ContractDeployer, parseRawMetadata } from 'dedot/contracts';
9+
import { Contract, ContractDeployer, parseRawMetadata } from 'dedot/contracts';
1010
import { assert, deferred } from 'dedot/utils';
1111
import { cryptoWaitReady } from '@polkadot/util-crypto';
1212
import { KeyringPair } from '@polkadot/keyring/types';
@@ -145,6 +145,29 @@ export const deployPsp22Contract = async (salt?: string): Promise<string> => {
145145
return defer.promise;
146146
};
147147

148+
export const mintPSP22Balance = async (psp22Address: string, pair: KeyringPair, amount: bigint): Promise<void> => {
149+
console.log('[mintPSP22Balance]', psp22Address, pair.address, amount);
150+
151+
const contract = new Contract<Psp22ContractApi>(client, psp22Metadata, psp22Address, { defaultCaller: pair.address });
152+
153+
const {
154+
raw: { gasRequired },
155+
} = await contract.query.psp22MintableMint(amount);
156+
157+
const defer = deferred<void>();
158+
await contract.tx
159+
.psp22MintableMint(amount, { gasLimit: gasRequired }) // prettier-end-here
160+
.signAndSend(pair, ({ status }) => {
161+
console.log('Transaction status:', status.type);
162+
163+
if (status.type === 'BestChainBlockIncluded') {
164+
defer.resolve();
165+
}
166+
});
167+
168+
return defer.promise;
169+
};
170+
148171
export const devPairs = () => {
149172
const alice = KEYRING.addFromUri('//Alice');
150173
const bob = KEYRING.addFromUri('//Bob');

0 commit comments

Comments
 (0)