Skip to content

Commit

Permalink
Add getTimestamp() method
Browse files Browse the repository at this point in the history
  • Loading branch information
platfowner committed Feb 23, 2024
1 parent 8fcf83d commit c66214e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/signer/default-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ export class DefaultSigner implements Signer {
const billing = transactionInput.billing;
return Object.assign(tx, { nonce, timestamp, gas_price: gasPrice, billing });
}
};
20 changes: 20 additions & 0 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} The timestamp value.
*/
getTimestamp(args: { address?: string, from?: string }): Promise<number> {
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.
Expand Down

0 comments on commit c66214e

Please sign in to comment.