Skip to content

Commit

Permalink
Update nonce calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Dec 6, 2024
1 parent 9a005eb commit dc549a0
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 14 deletions.
175 changes: 173 additions & 2 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions example/src/components/stacks/SignTransactions/mutationFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
makeUnsignedContractDeploy,
makeUnsignedSTXTokenTransfer,
noneCV,
StacksTransactionWire,
standardPrincipalCV,
uintCV,
} from '@stacks/transactions';
Expand Down Expand Up @@ -40,6 +41,14 @@ export interface MutationFnArgs {
publicKey: string;
}

function getLastUsedNonce(transactions: StacksTransactionWire[]) {
const lastTransaction = transactions.at(-1);

if (!lastTransaction) return undefined;

return lastTransaction.auth.spendingCondition.nonce + 1n;
}

export async function mutationFunction({
isPoolAllowContractSelected,
isPoolDelegateStacksSelected,
Expand All @@ -48,7 +57,7 @@ export async function mutationFunction({
broadcast,
publicKey,
}: MutationFnArgs) {
const transactions: string[] = [];
const transactions: StacksTransactionWire[] = [];

if (isPoolAllowContractSelected) {
const transaction = await makeUnsignedContractCall({
Expand All @@ -57,8 +66,9 @@ export async function mutationFunction({
functionName: 'allow-contract-caller',
functionArgs: [contractPrincipalCV(poolContractAddress, poolContractName), noneCV()],
publicKey,
...(getLastUsedNonce(transactions) && { nonce: getLastUsedNonce(transactions) }),
});
transactions.push(transaction.serialize());
transactions.push(transaction);
}

if (isPoolDelegateStacksSelected) {
Expand All @@ -67,16 +77,17 @@ export async function mutationFunction({
contractName: poolContractName,
functionName: 'delegate-stx',
functionArgs: [
uintCV(1234567890),
uintCV(101_000_000),
standardPrincipalCV(poolAdminStacksAddress),
noneCV(),
noneCV(),
poxAddressToTuple(poolAdminPoxAddress),
noneCV(),
],
publicKey,
...(getLastUsedNonce(transactions) && { nonce: getLastUsedNonce(transactions) }),
});
transactions.push(transaction.serialize());
transactions.push(transaction);
}

if (isContractDeploySelected) {
Expand All @@ -85,21 +96,23 @@ export async function mutationFunction({
contractName: `hello-world-${now}`,
codeBody: helloWorldContractBody,
publicKey,
...(getLastUsedNonce(transactions) && { nonce: getLastUsedNonce(transactions) }),
});
transactions.push(transaction.serialize());
transactions.push(transaction);
}

if (isTokenTransferSelected) {
const transaction = await makeUnsignedSTXTokenTransfer({
recipient: 'SP1VYV2JBF1QPNDSKHBZRAWRC4KQXP8ZSSRNKPJE4', // acc 4
amount: '100000', // 0.1 STX
publicKey,
...(getLastUsedNonce(transactions) && { nonce: getLastUsedNonce(transactions) }),
});
transactions.push(transaction.serialize());
transactions.push(transaction);
}

const res = await request('stx_signTransactions', {
transactions,
transactions: transactions.map((t) => t.serialize()),
broadcast,
});

Expand Down
Loading

0 comments on commit dc549a0

Please sign in to comment.