|
@@ -1,5 +1,5 @@
|
|
|
import * as anchor from "@coral-xyz/anchor";
|
|
|
-import { Program, BN, AnchorError, Wallet, IdlEvents } from "@coral-xyz/anchor";
|
|
|
+import { Program, AnchorError, Wallet } from "@coral-xyz/anchor";
|
|
|
import {
|
|
|
PublicKey,
|
|
|
Keypair,
|
|
@@ -14,11 +14,12 @@ import {
|
|
|
Token,
|
|
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
} from "@solana/spl-token";
|
|
|
+import { assert, expect } from "chai";
|
|
|
+
|
|
|
import { Misc } from "../../target/types/misc";
|
|
|
import { MiscOptional } from "../../target/types/misc_optional";
|
|
|
|
|
|
const utf8 = anchor.utils.bytes.utf8;
|
|
|
-const { assert, expect } = require("chai");
|
|
|
const nativeAssert = require("assert");
|
|
|
const miscIdl = require("../../target/idl/misc.json");
|
|
|
|
|
@@ -31,36 +32,217 @@ const miscTest = (
|
|
|
const wallet = provider.wallet as Wallet;
|
|
|
anchor.setProvider(provider);
|
|
|
|
|
|
- const data = anchor.web3.Keypair.generate();
|
|
|
+ describe("Data Account", () => {
|
|
|
+ const data = anchor.web3.Keypair.generate();
|
|
|
+ const udata = new anchor.BN(1);
|
|
|
+ const idata = new anchor.BN(2);
|
|
|
+
|
|
|
+ it("Can initialize data account", async () => {
|
|
|
+ await program.methods
|
|
|
+ .initialize(udata, idata)
|
|
|
+ .accounts({ data: data.publicKey })
|
|
|
+ .signers([data])
|
|
|
+ .preInstructions([await program.account.data.createInstruction(data)])
|
|
|
+ .rpc();
|
|
|
+ const dataAccount = await program.account.data.fetch(data.publicKey);
|
|
|
+ assert(dataAccount.udata.eq(udata));
|
|
|
+ assert(dataAccount.idata.eq(idata));
|
|
|
+ });
|
|
|
|
|
|
- it("Can use u128 and i128", async () => {
|
|
|
- const tx = await program.rpc.initialize(
|
|
|
- new anchor.BN(1234),
|
|
|
- new anchor.BN(22),
|
|
|
- {
|
|
|
- accounts: {
|
|
|
+ it("Can use base58 strings to fetch an account", async () => {
|
|
|
+ const dataAccount = await program.account.data.fetch(
|
|
|
+ data.publicKey.toString()
|
|
|
+ );
|
|
|
+ assert(dataAccount.udata.eq(udata));
|
|
|
+ assert(dataAccount.idata.eq(idata));
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Can use the owner constraint", async () => {
|
|
|
+ await program.methods
|
|
|
+ .testOwner()
|
|
|
+ .accounts({
|
|
|
data: data.publicKey,
|
|
|
+ misc: program.programId,
|
|
|
+ })
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ await nativeAssert.rejects(
|
|
|
+ async () => {
|
|
|
+ await program.methods
|
|
|
+ .testOwner()
|
|
|
+ .accounts({
|
|
|
+ data: provider.wallet.publicKey,
|
|
|
+ misc: program.programId,
|
|
|
+ })
|
|
|
+ .rpc();
|
|
|
},
|
|
|
- signers: [data],
|
|
|
- instructions: [await program.account.data.createInstruction(data)],
|
|
|
+ (err) => {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Should fail to close an account when sending lamports to itself", async () => {
|
|
|
+ try {
|
|
|
+ await program.methods
|
|
|
+ .testClose()
|
|
|
+ .accounts({
|
|
|
+ data: data.publicKey,
|
|
|
+ solDest: data.publicKey,
|
|
|
+ })
|
|
|
+ .rpc();
|
|
|
+ expect(false).to.be.true;
|
|
|
+ } catch (err) {
|
|
|
+ assert.strictEqual(
|
|
|
+ err.error.errorMessage,
|
|
|
+ "A close constraint was violated"
|
|
|
+ );
|
|
|
+ assert.strictEqual(err.error.errorCode.number, 2011);
|
|
|
}
|
|
|
- );
|
|
|
- const dataAccount = await program.account.data.fetch(data.publicKey);
|
|
|
- assert.isTrue(dataAccount.udata.eq(new anchor.BN(1234)));
|
|
|
- assert.isTrue(dataAccount.idata.eq(new anchor.BN(22)));
|
|
|
- });
|
|
|
+ });
|
|
|
|
|
|
- it("Can use u16", async () => {
|
|
|
- const data = anchor.web3.Keypair.generate();
|
|
|
- const tx = await program.rpc.testU16(99, {
|
|
|
- accounts: {
|
|
|
- myAccount: data.publicKey,
|
|
|
- },
|
|
|
- signers: [data],
|
|
|
- instructions: [await program.account.dataU16.createInstruction(data)],
|
|
|
+ it("Can close an account", async () => {
|
|
|
+ const connection = program.provider.connection;
|
|
|
+ const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+
|
|
|
+ assert.isNotNull(openAccount);
|
|
|
+ const openAccountBalance = openAccount.lamports;
|
|
|
+ // double balance to calculate closed balance correctly
|
|
|
+ const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
+ fromPubkey: provider.wallet.publicKey,
|
|
|
+ toPubkey: data.publicKey,
|
|
|
+ lamports: openAccountBalance,
|
|
|
+ });
|
|
|
+ const transferTransaction = new anchor.web3.Transaction().add(
|
|
|
+ transferIx
|
|
|
+ );
|
|
|
+ await provider.sendAndConfirm(transferTransaction);
|
|
|
+
|
|
|
+ let beforeBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ await program.methods
|
|
|
+ .testClose()
|
|
|
+ .accounts({
|
|
|
+ data: data.publicKey,
|
|
|
+ solDest: provider.wallet.publicKey,
|
|
|
+ })
|
|
|
+ .postInstructions([transferIx])
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ let afterBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ // Retrieved rent exemption sol.
|
|
|
+ expect(afterBalance > beforeBalance).to.be.true;
|
|
|
+
|
|
|
+ const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+
|
|
|
+ assert.isTrue(closedAccount.data.length === 0);
|
|
|
+ assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Can close an account twice", async () => {
|
|
|
+ const data = anchor.web3.Keypair.generate();
|
|
|
+ await program.methods
|
|
|
+ .initialize(new anchor.BN(10), new anchor.BN(10))
|
|
|
+ .accounts({ data: data.publicKey })
|
|
|
+ .preInstructions([await program.account.data.createInstruction(data)])
|
|
|
+ .signers([data])
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ const connection = program.provider.connection;
|
|
|
+ const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+ assert.isNotNull(openAccount);
|
|
|
+
|
|
|
+ const openAccountBalance = openAccount.lamports;
|
|
|
+ // double balance to calculate closed balance correctly
|
|
|
+ const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
+ fromPubkey: provider.wallet.publicKey,
|
|
|
+ toPubkey: data.publicKey,
|
|
|
+ lamports: openAccountBalance,
|
|
|
+ });
|
|
|
+ const transferTransaction = new anchor.web3.Transaction().add(
|
|
|
+ transferIx
|
|
|
+ );
|
|
|
+ await provider.sendAndConfirm(transferTransaction);
|
|
|
+
|
|
|
+ let beforeBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ await program.methods
|
|
|
+ .testCloseTwice()
|
|
|
+ .accounts({
|
|
|
+ data: data.publicKey,
|
|
|
+ solDest: provider.wallet.publicKey,
|
|
|
+ })
|
|
|
+ .postInstructions([transferIx])
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ let afterBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ // Retrieved rent exemption sol.
|
|
|
+ expect(afterBalance > beforeBalance).to.be.true;
|
|
|
+
|
|
|
+ const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+ assert.isTrue(closedAccount.data.length === 0);
|
|
|
+ assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Can close a mut account manually", async () => {
|
|
|
+ const data = anchor.web3.Keypair.generate();
|
|
|
+ await program.methods
|
|
|
+ .initialize(new anchor.BN(10), new anchor.BN(10))
|
|
|
+ .accounts({ data: data.publicKey })
|
|
|
+ .preInstructions([await program.account.data.createInstruction(data)])
|
|
|
+ .signers([data])
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ const connection = program.provider.connection;
|
|
|
+ const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+
|
|
|
+ assert.isNotNull(openAccount);
|
|
|
+ const openAccountBalance = openAccount.lamports;
|
|
|
+ // double balance to calculate closed balance correctly
|
|
|
+ const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
+ fromPubkey: provider.wallet.publicKey,
|
|
|
+ toPubkey: data.publicKey,
|
|
|
+ lamports: openAccountBalance,
|
|
|
+ });
|
|
|
+ const transferTransaction = new anchor.web3.Transaction().add(
|
|
|
+ transferIx
|
|
|
+ );
|
|
|
+ await provider.sendAndConfirm(transferTransaction);
|
|
|
+
|
|
|
+ let beforeBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ await program.methods
|
|
|
+ .testCloseMut()
|
|
|
+ .accounts({
|
|
|
+ data: data.publicKey,
|
|
|
+ solDest: provider.wallet.publicKey,
|
|
|
+ })
|
|
|
+ .postInstructions([transferIx])
|
|
|
+ .rpc();
|
|
|
+
|
|
|
+ let afterBalance = (
|
|
|
+ await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
+ ).lamports;
|
|
|
+
|
|
|
+ // Retrieved rent exemption sol.
|
|
|
+ expect(afterBalance > beforeBalance).to.be.true;
|
|
|
+
|
|
|
+ const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
+ assert.isTrue(closedAccount.data.length === 0);
|
|
|
+ assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
});
|
|
|
- const dataAccount = await program.account.dataU16.fetch(data.publicKey);
|
|
|
- assert.strictEqual(dataAccount.data, 99);
|
|
|
});
|
|
|
|
|
|
it("Can send VersionedTransaction", async () => {
|
|
@@ -216,29 +398,6 @@ const miscTest = (
|
|
|
assert.isTrue(accInfo.executable);
|
|
|
});
|
|
|
|
|
|
- it("Can use the owner constraint", async () => {
|
|
|
- await program.rpc.testOwner({
|
|
|
- accounts: {
|
|
|
- data: data.publicKey,
|
|
|
- misc: program.programId,
|
|
|
- },
|
|
|
- });
|
|
|
-
|
|
|
- await nativeAssert.rejects(
|
|
|
- async () => {
|
|
|
- await program.rpc.testOwner({
|
|
|
- accounts: {
|
|
|
- data: provider.wallet.publicKey,
|
|
|
- misc: program.programId,
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
- (err) => {
|
|
|
- return true;
|
|
|
- }
|
|
|
- );
|
|
|
- });
|
|
|
-
|
|
|
it("Can use the executable attribute", async () => {
|
|
|
await program.rpc.testExecutable({
|
|
|
accounts: {
|
|
@@ -291,292 +450,6 @@ const miscTest = (
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- it("Can use enum in idl", async () => {
|
|
|
- const resp1 = await program.methods
|
|
|
- .testInputEnum({ first: {} })
|
|
|
- .simulate();
|
|
|
- const event1 = resp1.events[0].data as IdlEvents<
|
|
|
- typeof program.idl
|
|
|
- >["E7"];
|
|
|
- assert.deepEqual(event1.data.first, {});
|
|
|
-
|
|
|
- const resp2 = await program.methods
|
|
|
- .testInputEnum({ second: { x: new BN(1), y: new BN(2) } })
|
|
|
- .simulate();
|
|
|
- const event2 = resp2.events[0].data as IdlEvents<
|
|
|
- typeof program.idl
|
|
|
- >["E7"];
|
|
|
- assert.isTrue(new BN(1).eq(event2.data.second.x));
|
|
|
- assert.isTrue(new BN(2).eq(event2.data.second.y));
|
|
|
-
|
|
|
- const resp3 = await program.methods
|
|
|
- .testInputEnum({
|
|
|
- tupleStructTest: [
|
|
|
- { data1: 1, data2: 11, data3: 111, data4: new BN(1111) },
|
|
|
- ],
|
|
|
- })
|
|
|
- .simulate();
|
|
|
- const event3 = resp3.events[0].data as IdlEvents<
|
|
|
- typeof program.idl
|
|
|
- >["E7"];
|
|
|
- assert.strictEqual(event3.data.tupleStructTest[0].data1, 1);
|
|
|
- assert.strictEqual(event3.data.tupleStructTest[0].data2, 11);
|
|
|
- assert.strictEqual(event3.data.tupleStructTest[0].data3, 111);
|
|
|
- assert.isTrue(event3.data.tupleStructTest[0].data4.eq(new BN(1111)));
|
|
|
-
|
|
|
- const resp4 = await program.methods
|
|
|
- .testInputEnum({ tupleTest: [1, 2, 3, 4] })
|
|
|
- .simulate();
|
|
|
- const event4 = resp4.events[0].data as IdlEvents<
|
|
|
- typeof program.idl
|
|
|
- >["E7"];
|
|
|
- assert.strictEqual(event4.data.tupleTest[0], 1);
|
|
|
- assert.strictEqual(event4.data.tupleTest[1], 2);
|
|
|
- assert.strictEqual(event4.data.tupleTest[2], 3);
|
|
|
- assert.strictEqual(event4.data.tupleTest[3], 4);
|
|
|
- });
|
|
|
-
|
|
|
- it("Can use enum in accounts", async () => {
|
|
|
- const testAccountEnum = async (
|
|
|
- ...args: Parameters<typeof program["methods"]["testAccountEnum"]>
|
|
|
- ) => {
|
|
|
- const dataKp = anchor.web3.Keypair.generate();
|
|
|
- const txHash = await program.methods
|
|
|
- .testAccountEnum(...(args as any))
|
|
|
- .accounts({
|
|
|
- data: dataKp.publicKey,
|
|
|
- payer: program.provider.publicKey,
|
|
|
- })
|
|
|
- .signers([dataKp])
|
|
|
- .rpc();
|
|
|
- await program.provider.connection.confirmTransaction(txHash);
|
|
|
- return await program.account.dataEnum.fetch(dataKp.publicKey);
|
|
|
- };
|
|
|
-
|
|
|
- // Unit
|
|
|
- const unit = await testAccountEnum({ first: {} });
|
|
|
- assert.deepEqual(unit.data.first, {});
|
|
|
-
|
|
|
- // Named
|
|
|
- const named = await testAccountEnum({
|
|
|
- second: { x: new BN(1), y: new BN(2) },
|
|
|
- });
|
|
|
- assert.isTrue(new BN(1).eq(named.data.second.x));
|
|
|
- assert.isTrue(new BN(2).eq(named.data.second.y));
|
|
|
-
|
|
|
- // Unnamed
|
|
|
- const unnamed = await testAccountEnum({ tupleTest: [1, 2, 3, 4] });
|
|
|
- assert.strictEqual(unnamed.data.tupleTest[0], 1);
|
|
|
- assert.strictEqual(unnamed.data.tupleTest[1], 2);
|
|
|
- assert.strictEqual(unnamed.data.tupleTest[2], 3);
|
|
|
- assert.strictEqual(unnamed.data.tupleTest[3], 4);
|
|
|
-
|
|
|
- // Unnamed struct
|
|
|
- const unnamedStruct = await testAccountEnum({
|
|
|
- tupleStructTest: [
|
|
|
- { data1: 1, data2: 11, data3: 111, data4: new BN(1111) },
|
|
|
- ],
|
|
|
- });
|
|
|
- assert.strictEqual(unnamedStruct.data.tupleStructTest[0].data1, 1);
|
|
|
- assert.strictEqual(unnamedStruct.data.tupleStructTest[0].data2, 11);
|
|
|
- assert.strictEqual(unnamedStruct.data.tupleStructTest[0].data3, 111);
|
|
|
- assert.isTrue(
|
|
|
- unnamedStruct.data.tupleStructTest[0].data4.eq(new BN(1111))
|
|
|
- );
|
|
|
- });
|
|
|
-
|
|
|
- let dataI8;
|
|
|
-
|
|
|
- it("Can use i8 in the idl", async () => {
|
|
|
- dataI8 = anchor.web3.Keypair.generate();
|
|
|
- await program.rpc.testI8(-3, {
|
|
|
- accounts: {
|
|
|
- data: dataI8.publicKey,
|
|
|
- },
|
|
|
- instructions: [await program.account.dataI8.createInstruction(dataI8)],
|
|
|
- signers: [dataI8],
|
|
|
- });
|
|
|
- const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
|
|
|
- assert.strictEqual(dataAccount.data, -3);
|
|
|
- });
|
|
|
-
|
|
|
- let dataPubkey;
|
|
|
-
|
|
|
- it("Can use i16 in the idl", async () => {
|
|
|
- const data = anchor.web3.Keypair.generate();
|
|
|
- await program.rpc.testI16(-2048, {
|
|
|
- accounts: {
|
|
|
- data: data.publicKey,
|
|
|
- },
|
|
|
- instructions: [await program.account.dataI16.createInstruction(data)],
|
|
|
- signers: [data],
|
|
|
- });
|
|
|
- const dataAccount = await program.account.dataI16.fetch(data.publicKey);
|
|
|
- assert.strictEqual(dataAccount.data, -2048);
|
|
|
-
|
|
|
- dataPubkey = data.publicKey;
|
|
|
- });
|
|
|
-
|
|
|
- it("Can use base58 strings to fetch an account", async () => {
|
|
|
- const dataAccount = await program.account.dataI16.fetch(
|
|
|
- dataPubkey.toString()
|
|
|
- );
|
|
|
- assert.strictEqual(dataAccount.data, -2048);
|
|
|
- });
|
|
|
-
|
|
|
- it("Should fail to close an account when sending lamports to itself", async () => {
|
|
|
- try {
|
|
|
- await program.rpc.testClose({
|
|
|
- accounts: {
|
|
|
- data: data.publicKey,
|
|
|
- solDest: data.publicKey,
|
|
|
- },
|
|
|
- });
|
|
|
- expect(false).to.be.true;
|
|
|
- } catch (err) {
|
|
|
- const errMsg = "A close constraint was violated";
|
|
|
- assert.strictEqual(err.error.errorMessage, errMsg);
|
|
|
- assert.strictEqual(err.error.errorCode.number, 2011);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- it("Can close an account", async () => {
|
|
|
- const connection = program.provider.connection;
|
|
|
- const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
-
|
|
|
- assert.isNotNull(openAccount);
|
|
|
- const openAccountBalance = openAccount.lamports;
|
|
|
- // double balance to calculate closed balance correctly
|
|
|
- const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
- fromPubkey: provider.wallet.publicKey,
|
|
|
- toPubkey: data.publicKey,
|
|
|
- lamports: openAccountBalance,
|
|
|
- });
|
|
|
- const transferTransaction = new anchor.web3.Transaction().add(transferIx);
|
|
|
- await provider.sendAndConfirm(transferTransaction);
|
|
|
-
|
|
|
- let beforeBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- await program.methods
|
|
|
- .testClose()
|
|
|
- .accounts({
|
|
|
- data: data.publicKey,
|
|
|
- solDest: provider.wallet.publicKey,
|
|
|
- })
|
|
|
- .postInstructions([transferIx])
|
|
|
- .rpc();
|
|
|
-
|
|
|
- let afterBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- // Retrieved rent exemption sol.
|
|
|
- expect(afterBalance > beforeBalance).to.be.true;
|
|
|
-
|
|
|
- const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
-
|
|
|
- assert.isTrue(closedAccount.data.length === 0);
|
|
|
- assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
- });
|
|
|
-
|
|
|
- it("Can close an account twice", async () => {
|
|
|
- const data = anchor.web3.Keypair.generate();
|
|
|
- await program.methods
|
|
|
- .initialize(new anchor.BN(10), new anchor.BN(10))
|
|
|
- .accounts({ data: data.publicKey })
|
|
|
- .preInstructions([await program.account.data.createInstruction(data)])
|
|
|
- .signers([data])
|
|
|
- .rpc();
|
|
|
-
|
|
|
- const connection = program.provider.connection;
|
|
|
- const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
- assert.isNotNull(openAccount);
|
|
|
-
|
|
|
- const openAccountBalance = openAccount.lamports;
|
|
|
- // double balance to calculate closed balance correctly
|
|
|
- const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
- fromPubkey: provider.wallet.publicKey,
|
|
|
- toPubkey: data.publicKey,
|
|
|
- lamports: openAccountBalance,
|
|
|
- });
|
|
|
- const transferTransaction = new anchor.web3.Transaction().add(transferIx);
|
|
|
- await provider.sendAndConfirm(transferTransaction);
|
|
|
-
|
|
|
- let beforeBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- await program.methods
|
|
|
- .testCloseTwice()
|
|
|
- .accounts({
|
|
|
- data: data.publicKey,
|
|
|
- solDest: provider.wallet.publicKey,
|
|
|
- })
|
|
|
- .postInstructions([transferIx])
|
|
|
- .rpc();
|
|
|
-
|
|
|
- let afterBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- // Retrieved rent exemption sol.
|
|
|
- expect(afterBalance > beforeBalance).to.be.true;
|
|
|
-
|
|
|
- const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
- assert.isTrue(closedAccount.data.length === 0);
|
|
|
- assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
- });
|
|
|
-
|
|
|
- it("Can close a mut account manually", async () => {
|
|
|
- const data = anchor.web3.Keypair.generate();
|
|
|
- await program.methods
|
|
|
- .initialize(new anchor.BN(10), new anchor.BN(10))
|
|
|
- .accounts({ data: data.publicKey })
|
|
|
- .preInstructions([await program.account.data.createInstruction(data)])
|
|
|
- .signers([data])
|
|
|
- .rpc();
|
|
|
-
|
|
|
- const connection = program.provider.connection;
|
|
|
- const openAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
-
|
|
|
- assert.isNotNull(openAccount);
|
|
|
- const openAccountBalance = openAccount.lamports;
|
|
|
- // double balance to calculate closed balance correctly
|
|
|
- const transferIx = anchor.web3.SystemProgram.transfer({
|
|
|
- fromPubkey: provider.wallet.publicKey,
|
|
|
- toPubkey: data.publicKey,
|
|
|
- lamports: openAccountBalance,
|
|
|
- });
|
|
|
- const transferTransaction = new anchor.web3.Transaction().add(transferIx);
|
|
|
- await provider.sendAndConfirm(transferTransaction);
|
|
|
-
|
|
|
- let beforeBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- await program.methods
|
|
|
- .testCloseMut()
|
|
|
- .accounts({
|
|
|
- data: data.publicKey,
|
|
|
- solDest: provider.wallet.publicKey,
|
|
|
- })
|
|
|
- .postInstructions([transferIx])
|
|
|
- .rpc();
|
|
|
-
|
|
|
- let afterBalance = (
|
|
|
- await connection.getAccountInfo(provider.wallet.publicKey)
|
|
|
- ).lamports;
|
|
|
-
|
|
|
- // Retrieved rent exemption sol.
|
|
|
- expect(afterBalance > beforeBalance).to.be.true;
|
|
|
-
|
|
|
- const closedAccount = await connection.getAccountInfo(data.publicKey);
|
|
|
- assert.isTrue(closedAccount.data.length === 0);
|
|
|
- assert.isTrue(closedAccount.owner.equals(SystemProgram.programId));
|
|
|
- });
|
|
|
-
|
|
|
it("Can use instruction data in accounts constraints", async () => {
|
|
|
// b"my-seed"
|
|
|
const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
|
|
@@ -1501,27 +1374,6 @@ const miscTest = (
|
|
|
assert.deepStrictEqual(dataAccount.data, dataArray);
|
|
|
});
|
|
|
|
|
|
- it("Should include BASE const in IDL", async () => {
|
|
|
- assert.isDefined(
|
|
|
- miscIdl.constants.find(
|
|
|
- (c) =>
|
|
|
- c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
|
|
|
- )
|
|
|
- );
|
|
|
- });
|
|
|
-
|
|
|
- it("Should include DECIMALS const in IDL", async () => {
|
|
|
- assert.isDefined(
|
|
|
- miscIdl.constants.find(
|
|
|
- (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
|
|
|
- )
|
|
|
- );
|
|
|
- });
|
|
|
-
|
|
|
- it("Should not include NO_IDL const in IDL", async () => {
|
|
|
- assert.isUndefined(miscIdl.constants.find((c) => c.name === "NO_IDL"));
|
|
|
- });
|
|
|
-
|
|
|
it("init_if_needed creates mint account if not exists", async () => {
|
|
|
const newMint = anchor.web3.Keypair.generate();
|
|
|
|
|
@@ -2678,6 +2530,7 @@ const miscTest = (
|
|
|
});
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
describe("Token Constraint Test", () => {
|
|
|
it("Token Constraint Test(no init) - Can make token::mint and token::authority", async () => {
|
|
|
const mint = anchor.web3.Keypair.generate();
|
|
@@ -3349,22 +3202,6 @@ const miscTest = (
|
|
|
);
|
|
|
assert.isDefined(thisTx);
|
|
|
});
|
|
|
- it("Can access enum variant fields using camel case without throwing a type error", async () => {
|
|
|
- const anotherProgram = new anchor.Program<Misc>(
|
|
|
- miscIdl,
|
|
|
- program.programId,
|
|
|
- provider
|
|
|
- );
|
|
|
- const enumWrappers =
|
|
|
- await anotherProgram.account.coolEnumWrapperAccount.all();
|
|
|
- for (let enumWrapper of enumWrappers) {
|
|
|
- // this code never gets run so just putting whatever
|
|
|
- console.log(enumWrapper.account.myEnum.variant2?.someSlot);
|
|
|
- console.log(enumWrapper.account.myEnum.variant2?.user1);
|
|
|
- console.log(enumWrapper.account.myEnum.variant3?.someSlot);
|
|
|
- console.log(enumWrapper.account.myEnum.variant3?.user2);
|
|
|
- }
|
|
|
- });
|
|
|
});
|
|
|
};
|
|
|
};
|