Skip to content
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
65 changes: 52 additions & 13 deletions tests/custom-coder/tests/system-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PublicKey,
SystemProgram,
SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
Transaction,
} from "@solana/web3.js";
import * as assert from "assert";
import BN from "bn.js";
Expand Down Expand Up @@ -299,19 +300,28 @@ describe("system-coder", () => {
])
.signers([nonceKeypair])
.rpc();

let nonceAccount = await program.account.nonce.fetch(
nonceKeypair.publicKey
);
let previousNonce = nonceAccount.nonce.toString();

// These have to be separate to make sure advance is in another slot.
await program.methods
const tx = await program.methods
.advanceNonceAccount(provider.wallet.publicKey)
.accounts({
nonce: nonceKeypair.publicKey,
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
})
.rpc();
.transaction();
tx.recentBlockhash = previousNonce;
tx.feePayer = provider.publicKey;

await provider.sendAndConfirm(tx, []);
// assert
const nonceAccount = await program.account.nonce.fetch(
nonceKeypair.publicKey
);
nonceAccount = await program.account.nonce.fetch(nonceKeypair.publicKey);
assert.notEqual(nonceAccount, null);
assert.notEqual(nonceAccount.nonce.toString(), previousNonce);
});

it("Authorizes a nonce account", async () => {
Expand Down Expand Up @@ -383,7 +393,13 @@ describe("system-coder", () => {
])
.signers([nonceKeypair])
.rpc();
await program.methods

let nonceAccount = await program.account.nonce.fetch(
nonceKeypair.publicKey
);
let previousNonce = nonceAccount.nonce.toString();

const tx = await program.methods
.advanceNonceAccount(provider.wallet.publicKey)
.accounts({
nonce: nonceKeypair.publicKey,
Expand All @@ -398,24 +414,47 @@ describe("system-coder", () => {
})
.instruction(),
])
.rpc();
.transaction();
tx.feePayer = provider.publicKey;
tx.recentBlockhash = previousNonce;

await provider.sendAndConfirm(tx, []);

nonceAccount = await program.account.nonce.fetch(nonceKeypair.publicKey);
previousNonce = nonceAccount.nonce.toString();

await program.methods
.authorizeNonceAccount(aliceKeypair.publicKey)
.accounts({
nonce: nonceKeypair.publicKey,
authorized: provider.wallet.publicKey,
})
.rpc();
await program.methods
.withdrawNonceAccount(new BN(amount))

let withdrawTx = await program.methods
.advanceNonceAccount(aliceKeypair.publicKey)
.accounts({
authorized: aliceKeypair.publicKey,
nonce: nonceKeypair.publicKey,
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
to: aliceKeypair.publicKey,
authorized: aliceKeypair.publicKey,
})
.signers([aliceKeypair])
.rpc();
.postInstructions([
await program.methods
.withdrawNonceAccount(new BN(amount))
.accounts({
authorized: aliceKeypair.publicKey,
nonce: nonceKeypair.publicKey,
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
to: aliceKeypair.publicKey,
})
.instruction(),
])
.transaction();

withdrawTx.feePayer = provider.publicKey;
withdrawTx.recentBlockhash = previousNonce;
await provider.sendAndConfirm(withdrawTx, [aliceKeypair]);

// assert
const aliceBalanceAfter = (
await program.provider.connection.getAccountInfo(aliceKeypair.publicKey)
Expand Down
12 changes: 9 additions & 3 deletions ts/packages/anchor/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ export class AnchorProvider implements Provider {
}
} else {
tx.feePayer = tx.feePayer ?? this.wallet.publicKey;
tx.recentBlockhash = (
await this.connection.getLatestBlockhash(opts.preflightCommitment)
).blockhash;

if (
!tx.recentBlockhash ||
tx.recentBlockhash === "11111111111111111111111111111111"
) {
tx.recentBlockhash = (
await this.connection.getLatestBlockhash(opts.preflightCommitment)
).blockhash;
}

if (signers) {
for (const signer of signers) {
Expand Down
Loading