Skip to content

Commit

Permalink
Merge pull request #497 from o1-labs/nonce-feepayerspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivo25 committed Nov 10, 2022
2 parents ec3f1c6 + bf04e8a commit 5bbae3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 5bbae3c

Please sign in to comment.