@@ -22,7 +22,7 @@ import { SubscribeOptions, sleep } from '../packages/web3'
22
22
import { web3 } from '../packages/web3'
23
23
import { TxStatus } from '../packages/web3'
24
24
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'
26
26
import { getSigner , mintToken , testPrivateKeyWallet } from '../packages/web3-test'
27
27
import { TransactionBuilder } from '../packages/web3'
28
28
import { ALPH_TOKEN_ID } from '../packages/web3'
@@ -105,7 +105,9 @@ describe('transactions', function () {
105
105
expect ( ( await addInstance . fetchState ( ) ) . fields . result ) . toBe ( 3n )
106
106
} )
107
107
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 ] > {
109
111
const mnemonic = generateMnemonic ( )
110
112
const wallet = new HDWallet ( { mnemonic } )
111
113
const account1 = wallet . deriveAndAddNewAccount ( 1 )
@@ -114,7 +116,7 @@ describe('transactions', function () {
114
116
115
117
await testPrivateKeyWallet . signAndSubmitTransferTx ( {
116
118
signerAddress : testPrivateKeyWallet . address ,
117
- destinations : [ { address : account1 . address , attoAlphAmount : 100n * ONE_ALPH } ]
119
+ destinations : [ { address : account1 . address , attoAlphAmount : account1Balance } ]
118
120
} )
119
121
120
122
return [ wallet , account1 , account2 , account3 ]
@@ -281,6 +283,47 @@ describe('transactions', function () {
281
283
expect ( contractState . fields . totalALPH ) . toEqual ( ONE_ALPH )
282
284
} )
283
285
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
+
284
327
it ( 'should fail when public keys do not match the build chained transactions parameters' , async ( ) => {
285
328
const nodeProvider = web3 . getCurrentNodeProvider ( )
286
329
const signer1 = await getSigner ( 100n * ONE_ALPH , 1 )
0 commit comments