From 0efe7005381138dfda8c80ae24d2d64a5ee33196 Mon Sep 17 00:00:00 2001 From: pragmaxim Date: Mon, 7 Oct 2024 08:39:35 +0200 Subject: [PATCH] multi-group spec --- packages/web3-test/src/test-wallet.ts | 14 ++++--- test/transaction.test.ts | 59 ++++++++++++++++----------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/packages/web3-test/src/test-wallet.ts b/packages/web3-test/src/test-wallet.ts index f104e8f9e..cd732b301 100644 --- a/packages/web3-test/src/test-wallet.ts +++ b/packages/web3-test/src/test-wallet.ts @@ -96,11 +96,13 @@ export async function getSigner(alphAmount = ONE_ALPH * 100n, group = 0): Promis } const rootWallet = new PrivateKeyWallet({ privateKey: testPrivateKey }) const wallet = PrivateKeyWallet.Random(group) - const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }] - await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations }) + if (alphAmount > 0n) { + const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }] + await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations }) + } return wallet - } catch (_) { - throw new Error('Failed to get signer, please restart the devnet') + } catch (error) { + throw new Error(`Failed to get signer, please restart the devnet due to : ${error}`) } } @@ -118,8 +120,8 @@ export async function getSigners( wallets.push(wallet) } return wallets - } catch (_) { - throw new Error('Failed to get signers, please restart the devnet') + } catch (error) { + throw new Error(`Failed to get signers, please restart the devnet due tot : ${error}`) } } diff --git a/test/transaction.test.ts b/test/transaction.test.ts index a01f6584c..faf140f9c 100644 --- a/test/transaction.test.ts +++ b/test/transaction.test.ts @@ -16,7 +16,7 @@ You should have received a copy of the GNU Lesser General Public License along with the library. If not, see . */ -import { subscribeToTxStatus } from '../packages/web3' +import { subscribeToTxStatus, TransactionBuilder } from '../packages/web3' import { node } from '../packages/web3' import { SubscribeOptions, sleep } from '../packages/web3' import { web3 } from '../packages/web3' @@ -25,7 +25,8 @@ import { PrivateKeyWallet } from '@alephium/web3-wallet' import { ONE_ALPH } from '../packages/web3/src' import { Add, Sub, AddMain } from '../artifacts/ts' import { getSigner } from '@alephium/web3-test' -import { TransactionBuilder } from '@alephium/web3/dist/src/signer/tx-builder' + +jest.setTimeout(10_000) describe('transactions', function () { let signer: PrivateKeyWallet @@ -37,32 +38,40 @@ describe('transactions', function () { it('should build multi-group transfer', async () => { const nodeProvider = web3.getCurrentNodeProvider() - const signer0 = await getSigner(100n * ONE_ALPH, 0) - const signer1 = await getSigner(0n, 1) - const signer2 = await getSigner(0n, 2) + const signer0 = await getSigner(100n * ONE_ALPH, 1) + const signer1 = await getSigner(0n, 2) + const signer2 = await getSigner(0n, 3) const signer3 = await getSigner(0n, 3) const signer4 = await getSigner(0n, 0) + const signer0Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address) + expect(BigInt(signer0Balance.balance)).toBe(100n * ONE_ALPH) + const transferFrom0to1and2 = await TransactionBuilder.from(nodeProvider).buildMultiGroupTransferTx( { signerAddress: signer0.address, destinations: [signer1, signer2].map((signer) => ({ address: signer.address, - attoAlphAmount: 2n * ONE_ALPH + attoAlphAmount: 10n * ONE_ALPH })) }, - signer0.publicKey + await signer0.getPublicKey(signer0.address) ) const transferFrom0to1and2Result = await Promise.all( transferFrom0to1and2.map(async (tx) => { - return await signer.signAndSubmitUnsignedTx({ - signerAddress: signer0.publicKey, + return await signer0.signAndSubmitUnsignedTx({ + signerAddress: signer0.address, unsignedTx: tx.unsignedTx }) }) ) + const signer1Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address) + expect(BigInt(signer1Balance.balance)).toBe(10n * ONE_ALPH) + const signer2Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address) + expect(BigInt(signer2Balance.balance)).toBe(10n * ONE_ALPH) + const transferFrom1to3and4 = await TransactionBuilder.from(nodeProvider).buildMultiGroupTransferTx( { signerAddress: signer1.address, @@ -76,8 +85,8 @@ describe('transactions', function () { const transferFrom1to3and4Result = await Promise.all( transferFrom1to3and4.map(async (tx) => { - return await signer.signAndSubmitUnsignedTx({ - signerAddress: signer1.publicKey, + return await signer1.signAndSubmitUnsignedTx({ + signerAddress: signer1.address, unsignedTx: tx.unsignedTx }) }) @@ -96,27 +105,31 @@ describe('transactions', function () { const transferFrom2to3and4Result = await Promise.all( transferFrom2to3and4.map(async (tx) => { - return await signer.signAndSubmitUnsignedTx({ - signerAddress: signer2.publicKey, + return await signer2.signAndSubmitUnsignedTx({ + signerAddress: signer2.address, unsignedTx: tx.unsignedTx }) }) ) - const signer1Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address) - const signer2Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address) - const signer3Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address) + const signer0FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address) + const signer1FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address) + const signer2FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address) + const signer3FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer3.address) + const signer4FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer4.address) const gasCostTransferFrom0to1and2 = transferFrom0to1and2Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0)) const gasCostTransferFrom1to3and4 = transferFrom1to3and4Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0)) const gasCostTransferFrom2to3and4 = transferFrom2to3and4Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0)) - const expectedSigner1Balance = 100n * ONE_ALPH - 10n * ONE_ALPH - gasCostTransferFrom0to1and2 - const expectedSigner2Balance = 10n * ONE_ALPH - 5n * ONE_ALPH - gasCostTransferFrom1to3and4 - const expectedSigner3Balance = 5n * ONE_ALPH - - expect(BigInt(signer1Balance.balance)).toBe(expectedSigner1Balance) - expect(BigInt(signer2Balance.balance)).toBe(expectedSigner2Balance) - expect(BigInt(signer3Balance.balance)).toBe(expectedSigner3Balance) + const expectedSigner0Balance = 100n * ONE_ALPH - 20n * ONE_ALPH - gasCostTransferFrom0to1and2 + const expectedSigner1Balance = 10n * ONE_ALPH - 2n * ONE_ALPH - gasCostTransferFrom1to3and4 + const expectedSigner2Balance = 10n * ONE_ALPH - 2n * ONE_ALPH - gasCostTransferFrom2to3and4 + + expect(BigInt(signer0FinalBalance.balance)).toBe(expectedSigner0Balance) + expect(BigInt(signer1FinalBalance.balance)).toBe(expectedSigner1Balance) + expect(BigInt(signer2FinalBalance.balance)).toBe(expectedSigner2Balance) + expect(BigInt(signer3FinalBalance.balance)).toBe(4n * ONE_ALPH) + expect(BigInt(signer4FinalBalance.balance)).toBe(4n * ONE_ALPH) }) it('should subscribe transaction status', async () => {