Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to manually specify fee payer nonce #497

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Also replaces `Experimental.accountUpdateFromCallback()`
- `Circuit.log()` to easily log Fields and other provable types inside a method, with the same API as `console.log()`
- `AccountUpdate.attachToTransaction()` for explicitly adding an account update to the current transaction. This replaces some previous behaviour where an account update got attached implicitly.
-

### Changed

- BREAKING CHANGE: `tx.send()` is now asynchronous: old: `send(): TransactionId` new: `send(): Promise<TransactionId>` and `tx.send()` now directly waits for the network response, as opposed to `tx.send().wait()`
- `Circuit.witness` can now be called outside circuits, where it will just directly return the callback result
- The `FeePayerSpec`, which is used to specify properties of the transaction via `Mina.transaction()`, now has another optional parameter to specify the nonce manually. `Mina.transaction({ feePayerKey: feePayer, nonce: 1 }, () => {})`
- BREAKING CHANGE: Static methods of type `.fromString()`, `.fromNumber()` and `.fromBigInt()` on `Field`, `UInt64`, `UInt32` and `Int64` are not longer supported.

### Deprecated
Expand Down
28 changes: 26 additions & 2 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ let currentTransaction = Context.create<CurrentTransaction>();

type FeePayerSpec =
| PrivateKey
| { feePayerKey: PrivateKey; fee?: number | string | UInt64; memo?: string }
| {
feePayerKey: PrivateKey;
fee?: number | string | UInt64;
memo?: string;
nonce?: number;
}
| undefined;

function reportGetAccountError(publicKey: string, tokenId: string) {
Expand All @@ -105,6 +110,7 @@ function createTransaction(
feePayer instanceof PrivateKey ? feePayer : feePayer?.feePayerKey;
let fee = feePayer instanceof PrivateKey ? undefined : feePayer?.fee;
let memo = feePayer instanceof PrivateKey ? '' : feePayer?.memo ?? '';
let nonce = feePayer instanceof PrivateKey ? undefined : feePayer?.nonce;

let transactionId = currentTransaction.enter({
sender: feePayerKey?.toPublicKey(),
Expand Down Expand Up @@ -154,11 +160,29 @@ function createTransaction(
if (feePayerKey !== undefined) {
// if senderKey is provided, fetch account to get nonce and mark to be signed
let senderAddress = feePayerKey.toPublicKey();

let nonce_;
let senderAccount = getAccount(senderAddress, TokenId.default);

if (nonce === undefined) {
nonce_ = senderAccount.nonce;
} else {
nonce_ = UInt32.fromNumber(nonce);
senderAccount.nonce = nonce_;
Fetch.addCachedAccount({
nonce: senderAccount.nonce,
publicKey: senderAccount.publicKey,
tokenId: senderAccount.tokenId.toString(),
balance: senderAccount.balance,
zkapp: {
appState: senderAccount.appState ?? [],
},
});
}
feePayerAccountUpdate = AccountUpdate.defaultFeePayer(
senderAddress,
feePayerKey,
senderAccount.nonce
nonce_
);
if (fee !== undefined) {
feePayerAccountUpdate.body.fee =
Expand Down