protocol/contracts/zero-ex/src/nonce_utils.ts
Lawrence Forman 5946d32a7d
Berlin wrappers (#212)
* `@0x/contract-wrappers`: Regenerate wrappers using updated abi-gen
Update tools deps

* update deps to hopefully fix doc gen
2021-04-27 12:16:08 -04:00

27 lines
897 B
TypeScript

import { hexUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as ethjs from 'ethereumjs-util';
/**
* Fetch and RLP encode the transaction count (nonce) of an account.
*/
export async function getRLPEncodedAccountNonceAsync(web3Wrapper: Web3Wrapper, address: string): Promise<string> {
const nonce = await web3Wrapper.getAccountNonceAsync(address);
return rlpEncodeNonce(nonce);
}
/**
* RLP encode the transaction count (nonce) of an account.
*/
export function rlpEncodeNonce(nonce: number): string {
if (nonce === 0) {
return '0x80';
} else if (nonce <= 0x7f) {
return ethjs.bufferToHex(ethjs.toBuffer(nonce));
} else {
const rlpNonce = ethjs.toBuffer(nonce);
// tslint:disable-next-line: restrict-plus-operands
return hexUtils.concat(rlpNonce.length + 0x80, ethjs.bufferToHex(rlpNonce));
}
}