2021-01-22 04:55:03 -08:00
|
|
|
const assert = require("assert");
|
|
|
|
const anchor = require("@project-serum/anchor");
|
2021-01-04 18:29:16 -08:00
|
|
|
|
2021-01-22 04:55:03 -08:00
|
|
|
describe("basic-1", () => {
|
2021-01-04 18:29:16 -08:00
|
|
|
// Use a local provider.
|
2021-01-22 04:55:03 -08:00
|
|
|
const provider = anchor.Provider.local();
|
2021-01-04 18:29:16 -08:00
|
|
|
|
|
|
|
// Configure the client to use the local cluster.
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
|
2021-01-22 04:55:03 -08:00
|
|
|
it("Creates and initializes an account in two different transactions", async () => {
|
2021-01-04 18:29:16 -08:00
|
|
|
// The program owning the account to create.
|
|
|
|
const program = anchor.workspace.Basic1;
|
|
|
|
|
|
|
|
// The Account to create.
|
2021-05-15 14:43:25 -07:00
|
|
|
const myAccount = anchor.web3.Keypair.generate();
|
2021-01-04 18:29:16 -08:00
|
|
|
|
|
|
|
// Create account transaction.
|
|
|
|
const tx = new anchor.web3.Transaction();
|
|
|
|
tx.add(
|
|
|
|
anchor.web3.SystemProgram.createAccount({
|
|
|
|
fromPubkey: provider.wallet.publicKey,
|
|
|
|
newAccountPubkey: myAccount.publicKey,
|
2021-01-22 04:55:03 -08:00
|
|
|
space: 8 + 8,
|
|
|
|
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
|
|
|
8 + 8
|
|
|
|
),
|
2021-01-04 18:29:16 -08:00
|
|
|
programId: program.programId,
|
2021-01-22 04:55:03 -08:00
|
|
|
})
|
2021-01-04 18:29:16 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Execute the transaction against the cluster.
|
|
|
|
await provider.send(tx, [myAccount]);
|
|
|
|
|
|
|
|
// Execute the RPC.
|
|
|
|
// #region code-separated
|
|
|
|
await program.rpc.initialize(new anchor.BN(1234), {
|
|
|
|
accounts: {
|
|
|
|
myAccount: myAccount.publicKey,
|
2021-01-11 12:00:18 -08:00
|
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
2021-01-04 18:29:16 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
// #endregion code-separated
|
|
|
|
|
|
|
|
// Fetch the newly created account from the cluster.
|
2021-05-25 20:04:05 -07:00
|
|
|
const account = await program.account.myAccount.fetch(myAccount.publicKey);
|
2021-01-04 18:29:16 -08:00
|
|
|
|
|
|
|
// Check it's state was initialized.
|
|
|
|
assert.ok(account.data.eq(new anchor.BN(1234)));
|
|
|
|
});
|
|
|
|
|
2021-01-09 22:03:14 -08:00
|
|
|
// Reference to an account to use between multiple tests.
|
|
|
|
let _myAccount = undefined;
|
2021-01-04 18:29:16 -08:00
|
|
|
|
2021-01-22 04:55:03 -08:00
|
|
|
it("Creates and initializes an account in a single atomic transaction", async () => {
|
2021-01-04 18:29:16 -08:00
|
|
|
// The program to execute.
|
|
|
|
const program = anchor.workspace.Basic1;
|
|
|
|
|
|
|
|
// #region code
|
|
|
|
// The Account to create.
|
2021-05-15 14:43:25 -07:00
|
|
|
const myAccount = anchor.web3.Keypair.generate();
|
2021-01-04 18:29:16 -08:00
|
|
|
|
|
|
|
// Atomically create the new account and initialize it with the program.
|
|
|
|
await program.rpc.initialize(new anchor.BN(1234), {
|
|
|
|
accounts: {
|
|
|
|
myAccount: myAccount.publicKey,
|
2021-01-11 12:00:18 -08:00
|
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
2021-01-04 18:29:16 -08:00
|
|
|
},
|
|
|
|
signers: [myAccount],
|
|
|
|
instructions: [
|
|
|
|
anchor.web3.SystemProgram.createAccount({
|
|
|
|
fromPubkey: provider.wallet.publicKey,
|
|
|
|
newAccountPubkey: myAccount.publicKey,
|
2021-01-22 04:55:03 -08:00
|
|
|
space: 8 + 8, // Add 8 for the account discriminator.
|
|
|
|
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
|
|
|
8 + 8
|
|
|
|
),
|
2021-01-04 18:29:16 -08:00
|
|
|
programId: program.programId,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fetch the newly created account from the cluster.
|
2021-05-25 20:04:05 -07:00
|
|
|
const account = await program.account.myAccount.fetch(myAccount.publicKey);
|
2021-01-04 18:29:16 -08:00
|
|
|
|
|
|
|
// Check it's state was initialized.
|
|
|
|
assert.ok(account.data.eq(new anchor.BN(1234)));
|
|
|
|
// #endregion code
|
2021-01-22 04:55:03 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Creates and initializes an account in a single atomic transaction (simplified)", async () => {
|
|
|
|
// The program to execute.
|
|
|
|
const program = anchor.workspace.Basic1;
|
|
|
|
|
|
|
|
// The Account to create.
|
2021-05-15 14:43:25 -07:00
|
|
|
const myAccount = anchor.web3.Keypair.generate();
|
2021-01-22 04:55:03 -08:00
|
|
|
|
|
|
|
// Atomically create the new account and initialize it with the program.
|
|
|
|
// #region code-simplified
|
|
|
|
await program.rpc.initialize(new anchor.BN(1234), {
|
|
|
|
accounts: {
|
|
|
|
myAccount: myAccount.publicKey,
|
|
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
|
},
|
|
|
|
signers: [myAccount],
|
2021-01-23 17:38:23 -08:00
|
|
|
instructions: [await program.account.myAccount.createInstruction(myAccount)],
|
2021-01-22 04:55:03 -08:00
|
|
|
});
|
|
|
|
// #endregion code-simplified
|
|
|
|
|
|
|
|
// Fetch the newly created account from the cluster.
|
2021-05-25 20:04:05 -07:00
|
|
|
const account = await program.account.myAccount.fetch(myAccount.publicKey);
|
2021-01-22 04:55:03 -08:00
|
|
|
|
|
|
|
// Check it's state was initialized.
|
|
|
|
assert.ok(account.data.eq(new anchor.BN(1234)));
|
2021-01-09 22:03:14 -08:00
|
|
|
|
|
|
|
// Store the account for the next test.
|
|
|
|
_myAccount = myAccount;
|
|
|
|
});
|
|
|
|
|
2021-01-22 04:55:03 -08:00
|
|
|
it("Updates a previously created account", async () => {
|
2021-01-09 22:03:14 -08:00
|
|
|
const myAccount = _myAccount;
|
|
|
|
|
|
|
|
// #region update-test
|
|
|
|
|
|
|
|
// The program to execute.
|
|
|
|
const program = anchor.workspace.Basic1;
|
|
|
|
|
|
|
|
// Invoke the update rpc.
|
|
|
|
await program.rpc.update(new anchor.BN(4321), {
|
|
|
|
accounts: {
|
|
|
|
myAccount: myAccount.publicKey,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fetch the newly updated account.
|
2021-05-25 20:04:05 -07:00
|
|
|
const account = await program.account.myAccount.fetch(myAccount.publicKey);
|
2021-01-09 22:03:14 -08:00
|
|
|
|
|
|
|
// Check it's state was mutated.
|
|
|
|
assert.ok(account.data.eq(new anchor.BN(4321)));
|
|
|
|
|
|
|
|
// #endregion update-test
|
2021-01-04 18:29:16 -08:00
|
|
|
});
|
|
|
|
});
|