Skip to content

Commit 5df8b3c

Browse files
authored
Merge pull request #488 from alephium/add-chained-dapp-tx-example
Add Test For Chained Execute Script Transactions
2 parents ce276de + 856409b commit 5df8b3c

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

test/transaction.test.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { SubscribeOptions, sleep } from '../packages/web3'
2222
import { web3 } from '../packages/web3'
2323
import { TxStatus } from '../packages/web3'
2424
import { HDWallet, HDWalletAccount, PrivateKeyWallet, generateMnemonic } from '@alephium/web3-wallet'
25-
import { Add, Sub, AddMain, Transact, Deposit, DepositToken } from '../artifacts/ts'
25+
import { Add, Sub, AddMain, Transact, Deposit, DepositToken, Withdraw } from '../artifacts/ts'
2626
import { getSigner, mintToken, testPrivateKeyWallet } from '../packages/web3-test'
2727
import { TransactionBuilder } from '../packages/web3'
2828
import { ALPH_TOKEN_ID } from '../packages/web3'
@@ -105,7 +105,9 @@ describe('transactions', function () {
105105
expect((await addInstance.fetchState()).fields.result).toBe(3n)
106106
})
107107

108-
async function prepareChainedTxTest(): Promise<[HDWallet, HDWalletAccount, HDWalletAccount, HDWalletAccount]> {
108+
async function prepareChainedTxTest(
109+
account1Balance: bigint = 100n * ONE_ALPH
110+
): Promise<[HDWallet, HDWalletAccount, HDWalletAccount, HDWalletAccount]> {
109111
const mnemonic = generateMnemonic()
110112
const wallet = new HDWallet({ mnemonic })
111113
const account1 = wallet.deriveAndAddNewAccount(1)
@@ -114,7 +116,7 @@ describe('transactions', function () {
114116

115117
await testPrivateKeyWallet.signAndSubmitTransferTx({
116118
signerAddress: testPrivateKeyWallet.address,
117-
destinations: [{ address: account1.address, attoAlphAmount: 100n * ONE_ALPH }]
119+
destinations: [{ address: account1.address, attoAlphAmount: account1Balance }]
118120
})
119121

120122
return [wallet, account1, account2, account3]
@@ -281,6 +283,47 @@ describe('transactions', function () {
281283
expect(contractState.fields.totalALPH).toEqual(ONE_ALPH)
282284
})
283285

286+
it('should build chained dApp transactions', async () => {
287+
const nodeProvider = web3.getCurrentNodeProvider()
288+
const [wallet, account1] = await prepareChainedTxTest(ONE_ALPH / 2n)
289+
290+
const deployer = await getSigner(100n * ONE_ALPH, 1)
291+
const { tokenId } = await mintToken(deployer.address, 10n)
292+
const deploy = await Transact.deploy(deployer, {
293+
initialAttoAlphAmount: 10n * ONE_ALPH,
294+
initialFields: { tokenId, totalALPH: 10n * ONE_ALPH, totalTokens: 0n }
295+
})
296+
const transactInstance = deploy.contractInstance
297+
expect(transactInstance.groupIndex).toBe(1)
298+
299+
const account1BalanceBefore = await nodeProvider.addresses.getAddressesAddressBalance(account1.address)
300+
expect(BigInt(account1BalanceBefore.balance)).toBe(ONE_ALPH / 2n)
301+
302+
await wallet.setSelectedAccount(account1.address)
303+
const depositTxParams = await Deposit.script.txParamsForExecution(wallet, {
304+
initialFields: { c: transactInstance.contractId },
305+
attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 3n
306+
})
307+
expect(depositTxParams.signerAddress).toBe(account1.address)
308+
await expect(wallet.signAndSubmitExecuteScriptTx(depositTxParams)).rejects.toThrow(
309+
`Failed to request postContractsUnsignedTxExecuteScript, error: [API Error] - Execution error when emulating tx script or contract: Not enough approved balance for address ${account1.address}, tokenId: ALPH, expected: 1000000000000000000, got: 498000000000000000 - Status code: 400`
310+
)
311+
312+
const withdrawTxParams = await Withdraw.script.txParamsForExecution(wallet, {
313+
initialFields: { c: transactInstance.contractId }
314+
})
315+
316+
const [withdrawTxResult, depositTxResult] = await wallet.signAndSubmitChainedTx([
317+
{ ...withdrawTxParams, type: 'ExecuteScript' },
318+
{ ...depositTxParams, type: 'ExecuteScript' }
319+
])
320+
321+
const withdrawTxGasCost = BigInt(withdrawTxResult.gasAmount) * BigInt(withdrawTxResult.gasPrice)
322+
const depositTxGasCost = BigInt(depositTxResult.gasAmount) * BigInt(depositTxResult.gasPrice)
323+
const account1BalanceAfter = await nodeProvider.addresses.getAddressesAddressBalance(account1.address)
324+
expect(BigInt(account1BalanceAfter.balance)).toBe(ONE_ALPH / 2n - withdrawTxGasCost - depositTxGasCost)
325+
})
326+
284327
it('should fail when public keys do not match the build chained transactions parameters', async () => {
285328
const nodeProvider = web3.getCurrentNodeProvider()
286329
const signer1 = await getSigner(100n * ONE_ALPH, 1)

0 commit comments

Comments
 (0)