Skip to content

Commit

Permalink
feat: allow custom nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 9, 2021
1 parent 3837e72 commit 17666de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
17 changes: 17 additions & 0 deletions __tests__/signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,21 @@ describe('deploy and test Wallet', () => {

expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
});
test('execute with custom nonce', async () => {
const { result } = await signer.callContract({
contract_address: signer.address,
entry_point_selector: stark.getSelectorFromName('get_nonce'),
});
const nonce = parseInt(result[0], 10);
const { code, transaction_hash } = await signer.addTransaction({
type: 'INVOKE_FUNCTION',
contract_address: erc20Address,
entry_point_selector: stark.getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
nonce,
});

expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTx(transaction_hash);
});
});
16 changes: 11 additions & 5 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ export class Signer extends Provider implements SignerInterface {
"Adding signatures to a signer transaction currently isn't supported"
);

const { result } = await this.callContract({
contract_address: this.address,
entry_point_selector: getSelectorFromName('get_nonce'),
});
const nonceBn = toBN(result[0]);
let nonceBn;
if (transaction.nonce) {
nonceBn = toBN(transaction.nonce);
} else {
const { result } = await this.callContract({
contract_address: this.address,
entry_point_selector: getSelectorFromName('get_nonce'),
});
nonceBn = toBN(result[0]);
}

const calldataDecimal = (transaction.calldata || []).map((x) => toBN(x).toString());

const msgHash = addHexPrefix(
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type DeployTransaction = {
contract_definition: CompressedCompiledContract;
contract_address_salt: BigNumberish;
constructor_calldata: string[];
nonce?: BigNumberish;
};

export type InvokeFunctionTransaction = {
Expand All @@ -60,6 +61,7 @@ export type InvokeFunctionTransaction = {
entry_point_type?: EntryPointType;
entry_point_selector: string;
calldata?: string[];
nonce?: BigNumberish;
};

export type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type'>;
Expand Down

0 comments on commit 17666de

Please sign in to comment.