UNPKG

@aeternity/aepp-sdk

Version:

SDK for the æternity blockchain

40 lines 1.35 kB
import { isAccountNotFoundError } from '../../../utils/other.js'; import shortUInt from './short-u-int.js'; import { ArgumentError } from '../../../utils/errors.js'; import { Tag } from '../constants.js'; export default function genNonceField(senderKey) { return { ...shortUInt, serialize(value, { tag }) { if (Tag.GaAttachTx === tag && value !== 1) { throw new ArgumentError('nonce', 'equal 1 if GaAttachTx', value); } return shortUInt.serialize(value); }, async prepare(value, params, options) { if (value != null) return value; // TODO: uncomment the below line // if (options._isInternalBuild === true) return 0; const { onNode, strategy } = options; const senderId = options[senderKey]; const requirement = 'provided (or provide `nonce` instead)'; if (onNode == null) throw new ArgumentError('onNode', requirement, onNode); if (senderId == null) throw new ArgumentError('senderId', requirement, senderId); return (await onNode.getAccountNextNonce(senderId.replace(/^ok_/, 'ak_'), { strategy }).catch(error => { if (!isAccountNotFoundError(error)) throw error; return { nextNonce: 1 }; })).nextNonce; }, senderKey }; } //# sourceMappingURL=nonce.js.map