Skip to content

Commit 29cae54

Browse files
committed
Merge branch 'feat/nc-args-serialization' into feat/nc-args-class
* feat/nc-args-serialization: feat: add support for creating a create token transaction with nano header (#860)
2 parents 1cc26fb + 7973b49 commit 29cae54

File tree

6 files changed

+553
-91
lines changed

6 files changed

+553
-91
lines changed

__tests__/integration/hathorwallet_facade.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ describe('start', () => {
644644
await expect(hWallet.createAndSendNanoContractTransaction()).rejects.toThrow(
645645
WalletFromXPubGuard
646646
);
647+
await expect(hWallet.createAndSendNanoContractCreateTokenTransaction()).rejects.toThrow(
648+
WalletFromXPubGuard
649+
);
647650
await expect(hWallet.getPrivateKeyFromAddress()).rejects.toThrow(WalletFromXPubGuard);
648651
await expect(hWallet.createOnChainBlueprintTransaction()).rejects.toThrow(WalletFromXPubGuard);
649652

__tests__/integration/nanocontracts/bet.test.ts

+85-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
waitForTxReceived,
99
waitTxConfirmed,
1010
} from '../helpers/wallet.helper';
11-
import { NATIVE_TOKEN_UID, NANO_CONTRACTS_INITIALIZE_METHOD } from '../../../src/constants';
11+
import {
12+
CREATE_TOKEN_TX_VERSION,
13+
NATIVE_TOKEN_UID,
14+
NANO_CONTRACTS_INITIALIZE_METHOD,
15+
} from '../../../src/constants';
1216
import ncApi from '../../../src/api/nano';
1317
import dateFormatter from '../../../src/utils/date';
1418
import { bufferToHex } from '../../../src/utils/buffer';
@@ -364,8 +368,7 @@ describe('full cycle of bet nano contract', () => {
364368
).toMatchBuffer(inputData);
365369
expect((txSetResultParser.parsedArgs[0].value as NanoContractSignedData).value).toEqual(result);
366370

367-
// Try to withdraw to address 2, success
368-
const txWithdrawal = await wallet.createAndSendNanoContractTransaction('withdraw', address2, {
371+
const withdrawalData = {
369372
ncId: tx1.hash,
370373
actions: [
371374
{
@@ -375,13 +378,76 @@ describe('full cycle of bet nano contract', () => {
375378
address: address2,
376379
},
377380
],
378-
});
381+
};
382+
383+
const withdrawalCreateTokenOptions = {
384+
mintAddress: address0,
385+
name: 'Withdrawal Token',
386+
symbol: 'WTK',
387+
amount: 10000n,
388+
changeAddress: null,
389+
createMint: false,
390+
mintAuthorityAddress: null,
391+
createMelt: false,
392+
meltAuthorityAddress: null,
393+
data: null,
394+
isCreateNFT: false,
395+
contractPaysTokenDeposit: true,
396+
};
397+
398+
// Error with invalid address
399+
await expect(
400+
wallet.createAndSendNanoContractCreateTokenTransaction(
401+
'withdraw',
402+
'abc',
403+
withdrawalData,
404+
withdrawalCreateTokenOptions
405+
)
406+
).rejects.toThrow(NanoContractTransactionError);
407+
408+
// Error with invalid mint authority address
409+
await expect(
410+
wallet.createAndSendNanoContractCreateTokenTransaction('withdraw', address2, withdrawalData, {
411+
...withdrawalCreateTokenOptions,
412+
mintAuthorityAddress: 'abc',
413+
})
414+
).rejects.toThrow(NanoContractTransactionError);
415+
416+
// Error with invalid melt authority address
417+
await expect(
418+
wallet.createAndSendNanoContractCreateTokenTransaction('withdraw', address2, withdrawalData, {
419+
...withdrawalCreateTokenOptions,
420+
meltAuthorityAddress: 'abc',
421+
})
422+
).rejects.toThrow(NanoContractTransactionError);
423+
424+
// Try to withdraw to address 2, success
425+
const txWithdrawal = await wallet.createAndSendNanoContractCreateTokenTransaction(
426+
'withdraw',
427+
address2,
428+
withdrawalData,
429+
withdrawalCreateTokenOptions
430+
);
379431
await checkTxValid(wallet, txWithdrawal);
380432
txIds.push(txWithdrawal.hash);
381433

382434
const txWithdrawalData = await wallet.getFullTxById(txWithdrawal.hash);
383435
expect(isNanoContractCreateTx(txWithdrawalData)).toBe(false);
384436

437+
expect(txWithdrawalData.tx.nc_id).toBe(tx1.hash);
438+
expect(txWithdrawalData.tx.nc_method).toBe('withdraw');
439+
expect(txWithdrawalData.tx.version).toBe(CREATE_TOKEN_TX_VERSION);
440+
expect(txWithdrawalData.tx.token_name).toBe('Withdrawal Token');
441+
expect(txWithdrawalData.tx.token_symbol).toBe('WTK');
442+
expect(txWithdrawalData.tx.outputs.length).toBe(2);
443+
// First the created token output with 10000n amount
444+
expect(txWithdrawalData.tx.outputs[0].value).toBe(10000n);
445+
expect(txWithdrawalData.tx.outputs[0].token_data).toBe(1);
446+
// Then what's left of the withdrawal, after paying 100n
447+
// in deposit fee for the token creation
448+
expect(txWithdrawalData.tx.outputs[1].value).toBe(200n);
449+
expect(txWithdrawalData.tx.outputs[1].token_data).toBe(0);
450+
385451
// We must have two transactions in the address2
386452
const address2Meta2 = await wallet.storage.store.getAddressMeta(address2);
387453
expect(address2Meta2.numTransactions).toBe(2);
@@ -683,6 +749,19 @@ describe('full cycle of bet nano contract', () => {
683749
})
684750
).rejects.toThrow(NanoContractTransactionError);
685751

752+
// If we remove the pin from the wallet object, it should throw error
753+
const oldPin = hWallet.pinCode;
754+
hWallet.pinCode = '';
755+
await expect(
756+
hWallet.createAndSendNanoContractTransaction('withdraw', address2, {})
757+
).rejects.toThrow(PinRequiredError);
758+
759+
await expect(
760+
hWallet.createAndSendNanoContractCreateTokenTransaction('withdraw', address2, {}, {})
761+
).rejects.toThrow(PinRequiredError);
762+
// Add the pin back for the other tests
763+
hWallet.pinCode = oldPin;
764+
686765
// Test ocb errors
687766
const { seed } = WALLET_CONSTANTS.ocb;
688767
const ocbWallet = await generateWalletHelper({ seed });
@@ -694,13 +773,13 @@ describe('full cycle of bet nano contract', () => {
694773
).rejects.toThrow(NanoContractTransactionError);
695774

696775
// If we remove the pin from the wallet object, it should throw error
697-
const oldPin = ocbWallet.pinCode;
776+
const oldOcbPin = ocbWallet.pinCode;
698777
ocbWallet.pinCode = '';
699778
await expect(
700779
ocbWallet.createAndSendOnChainBlueprintTransaction(code, address0)
701780
).rejects.toThrow(PinRequiredError);
702781

703782
// Add the pin back in case there are more tests here
704-
ocbWallet.pinCode = oldPin;
783+
ocbWallet.pinCode = oldOcbPin;
705784
});
706785
});

0 commit comments

Comments
 (0)