Skip to content

Commit

Permalink
test: added new tests against nonce types
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho112 committed Sep 6, 2022
1 parent d439492 commit 8a8f205
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/near-api-js/test/serialize.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

const fs = require('fs');
const BN = require('bn.js');
const nearApi = require('../src/index');

class Test extends nearApi.utils.enums.Assignable {
Expand Down Expand Up @@ -111,4 +112,55 @@ describe('roundtrip test', () => {
});
}
}
});

describe('serialize and deserialize on different types of nonce', () => {
const actions = [
nearApi.transactions.transfer(1),
];
const blockHash = nearApi.utils.serialize.base_decode('244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM');
const targetNonce = new BN(1);
test('number typed nonce', async() => {
const transaction = nearApi.transactions.createTransaction(
'test.near',
nearApi.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'),
'whatever.near',
1,
actions,
blockHash);
const serialized = transaction.encode();
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6010000000301000000000000000000000000000000');
const deserialized = nearApi.transactions.Transaction.decode(serialized);
expect(deserialized.encode()).toEqual(serialized);
expect(deserialized.nonce.toString()).toEqual(targetNonce.toString());

});
test('string typed nonce', async() => {
const transaction = nearApi.transactions.createTransaction(
'test.near',
nearApi.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'),
'whatever.near',
'1',
actions,
blockHash);
const serialized = transaction.encode();
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6010000000301000000000000000000000000000000');
const deserialized = nearApi.transactions.Transaction.decode(serialized);
expect(deserialized.encode()).toEqual(serialized);
expect(deserialized.nonce.toString()).toEqual(targetNonce.toString());
});
test('BN typed nonce', async() => {
const transaction = nearApi.transactions.createTransaction(
'test.near',
nearApi.utils.PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'),
'whatever.near',
new BN(1),
actions,
blockHash);
const serialized = transaction.encode();
expect(serialized.toString('hex')).toEqual('09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef6010000000301000000000000000000000000000000');
const deserialized = nearApi.transactions.Transaction.decode(serialized);
expect(deserialized.encode()).toEqual(serialized);
expect(deserialized.nonce.toString()).toEqual(targetNonce.toString());
});
});

0 comments on commit 8a8f205

Please sign in to comment.