diff --git a/__tests__/ain.test.ts b/__tests__/ain.test.ts index 5f3b51e..be2894a 100644 --- a/__tests__/ain.test.ts +++ b/__tests__/ain.test.ts @@ -269,7 +269,12 @@ describe('ain-js', function() { it('getNonce', async function() { const nonce = await ain.wallet.getNonce(); - expect(nonce).toBeNull(); + expect(nonce).toBe(0); + }); + + it('getTimestamp', async function() { + const timestamp = await ain.wallet.getTimestamp(); + expect(timestamp).toBe(0); }); it('transfer with isDryrun = true', async function() { diff --git a/src/signer/default-signer.ts b/src/signer/default-signer.ts index 1936c43..014f3bf 100644 --- a/src/signer/default-signer.ts +++ b/src/signer/default-signer.ts @@ -131,3 +131,4 @@ export class DefaultSigner implements Signer { const billing = transactionInput.billing; return Object.assign(tx, { nonce, timestamp, gas_price: gasPrice, billing }); } + }; diff --git a/src/wallet.ts b/src/wallet.ts index 0361e3e..12512e2 100755 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -268,6 +268,26 @@ export default class Wallet { return this.ain.provider.send('ain_getNonce', { address, from: args.from }) } + /** + * Fetches an account's timestamp value, which is the current transaction count of the account. + * @param {object} args The ferch options. + * It may contain a string 'address' value and a string 'from' value. + * The 'address' is the address of the account to get the timestamp of, + * and the 'from' is the source of the data. + * It could be either the pending transaction pool ("pending") or + * the committed blocks ("committed"). The default value is "committed". + * @returns {Promise} The timestamp value. + */ + getTimestamp(args: { address?: string, from?: string }): Promise { + if (!args) { args = {}; } + const address = args.address ? Ain.utils.toChecksumAddress(args.address) + : this.getImpliedAddress(args.address); + if (args.from !== undefined && args.from !== 'pending' && args.from !== 'committed') { + throw Error("'from' should be either 'pending' or 'committed'"); + } + return this.ain.provider.send('ain_getTimestamp', { address, from: args.from }) + } + /** * Sends a transfer transaction to the network. * @param {{to: string, value: number, from?: string, nonce?: number, gas_price?: number}} input The input parameters of the transaction.