|
@@ -1,16 +1,18 @@
|
|
-import * as anchor from "@project-serum/anchor";
|
|
|
|
-import { Spl } from "@project-serum/anchor";
|
|
|
|
-import { assert } from "chai";
|
|
|
|
|
|
+import { AnchorProvider, setProvider } from "@project-serum/anchor";
|
|
|
|
+import { splTokenProgram } from "@project-serum/spl-token";
|
|
|
|
+import { Keypair, SYSVAR_RENT_PUBKEY, PublicKey } from "@solana/web3.js";
|
|
import BN from "bn.js";
|
|
import BN from "bn.js";
|
|
-import { Keypair, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
|
|
|
|
|
|
+import { assert } from "chai";
|
|
|
|
|
|
-describe("custom-coder", () => {
|
|
|
|
|
|
+describe("spl-token", () => {
|
|
// Configure the client to use the local cluster.
|
|
// Configure the client to use the local cluster.
|
|
- const provider = anchor.AnchorProvider.env();
|
|
|
|
- anchor.setProvider(provider);
|
|
|
|
|
|
+ const provider = AnchorProvider.env();
|
|
|
|
+ setProvider(provider);
|
|
|
|
|
|
// Client.
|
|
// Client.
|
|
- const program = Spl.token();
|
|
|
|
|
|
+ const program = splTokenProgram({
|
|
|
|
+ provider,
|
|
|
|
+ });
|
|
|
|
|
|
// Constants.
|
|
// Constants.
|
|
const mintKeypair = Keypair.generate();
|
|
const mintKeypair = Keypair.generate();
|
|
@@ -19,18 +21,21 @@ describe("custom-coder", () => {
|
|
const rent = SYSVAR_RENT_PUBKEY;
|
|
const rent = SYSVAR_RENT_PUBKEY;
|
|
|
|
|
|
it("Creates a mint", async () => {
|
|
it("Creates a mint", async () => {
|
|
- await program.rpc.initializeMint(6, provider.wallet.publicKey, null, {
|
|
|
|
- accounts: {
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .initializeMint(6, provider.wallet.publicKey, null)
|
|
|
|
+ .accounts({
|
|
mint: mintKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
rent,
|
|
rent,
|
|
- },
|
|
|
|
- signers: [mintKeypair],
|
|
|
|
- preInstructions: [
|
|
|
|
|
|
+ })
|
|
|
|
+ .signers([mintKeypair])
|
|
|
|
+ .preInstructions([
|
|
await program.account.mint.createInstruction(mintKeypair),
|
|
await program.account.mint.createInstruction(mintKeypair),
|
|
- ],
|
|
|
|
- });
|
|
|
|
|
|
+ ])
|
|
|
|
+ .rpc();
|
|
const mintAccount = await program.account.mint.fetch(mintKeypair.publicKey);
|
|
const mintAccount = await program.account.mint.fetch(mintKeypair.publicKey);
|
|
- assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
|
|
|
|
|
|
+ assert.isTrue(
|
|
|
|
+ (mintAccount.mintAuthority as PublicKey).equals(provider.wallet.publicKey)
|
|
|
|
+ );
|
|
assert.isNull(mintAccount.freezeAuthority);
|
|
assert.isNull(mintAccount.freezeAuthority);
|
|
assert.strictEqual(mintAccount.decimals, 6);
|
|
assert.strictEqual(mintAccount.decimals, 6);
|
|
assert.isTrue(mintAccount.isInitialized);
|
|
assert.isTrue(mintAccount.isInitialized);
|
|
@@ -38,41 +43,43 @@ describe("custom-coder", () => {
|
|
});
|
|
});
|
|
|
|
|
|
it("Creates a token account for alice", async () => {
|
|
it("Creates a token account for alice", async () => {
|
|
- await program.rpc.initializeAccount({
|
|
|
|
- accounts: {
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .initializeAccount()
|
|
|
|
+ .accounts({
|
|
account: aliceTokenKeypair.publicKey,
|
|
account: aliceTokenKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
- authority: provider.wallet.publicKey,
|
|
|
|
|
|
+ owner: provider.wallet.publicKey,
|
|
rent,
|
|
rent,
|
|
- },
|
|
|
|
- signers: [aliceTokenKeypair],
|
|
|
|
- preInstructions: [
|
|
|
|
- await program.account.token.createInstruction(aliceTokenKeypair),
|
|
|
|
- ],
|
|
|
|
- });
|
|
|
|
- const token = await program.account.token.fetch(
|
|
|
|
|
|
+ })
|
|
|
|
+ .signers([aliceTokenKeypair])
|
|
|
|
+ .preInstructions([
|
|
|
|
+ await program.account.account.createInstruction(aliceTokenKeypair),
|
|
|
|
+ ])
|
|
|
|
+ .rpc();
|
|
|
|
+ const token = await program.account.account.fetch(
|
|
aliceTokenKeypair.publicKey
|
|
aliceTokenKeypair.publicKey
|
|
);
|
|
);
|
|
- assert.isTrue(token.authority.equals(provider.wallet.publicKey));
|
|
|
|
|
|
+ assert.isTrue(token.owner.equals(provider.wallet.publicKey));
|
|
assert.isTrue(token.mint.equals(mintKeypair.publicKey));
|
|
assert.isTrue(token.mint.equals(mintKeypair.publicKey));
|
|
assert.strictEqual(token.amount.toNumber(), 0);
|
|
assert.strictEqual(token.amount.toNumber(), 0);
|
|
assert.isNull(token.delegate);
|
|
assert.isNull(token.delegate);
|
|
- assert.strictEqual(token.state, 1);
|
|
|
|
|
|
+ assert.strictEqual(Object.keys(token.state)[0], "initialized");
|
|
assert.isNull(token.isNative);
|
|
assert.isNull(token.isNative);
|
|
assert.strictEqual(token.delegatedAmount.toNumber(), 0);
|
|
assert.strictEqual(token.delegatedAmount.toNumber(), 0);
|
|
assert.isNull(token.closeAuthority);
|
|
assert.isNull(token.closeAuthority);
|
|
});
|
|
});
|
|
|
|
|
|
it("Mints a token to alice", async () => {
|
|
it("Mints a token to alice", async () => {
|
|
- await program.rpc.mintTo(new BN(2), {
|
|
|
|
- accounts: {
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .mintTo(new BN(2))
|
|
|
|
+ .accounts({
|
|
mint: mintKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
- to: aliceTokenKeypair.publicKey,
|
|
|
|
- authority: provider.wallet.publicKey,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
|
|
+ account: aliceTokenKeypair.publicKey,
|
|
|
|
+ owner: provider.wallet.publicKey,
|
|
|
|
+ })
|
|
|
|
+ .rpc();
|
|
|
|
|
|
- const token = await program.account.token.fetch(
|
|
|
|
|
|
+ const token = await program.account.account.fetch(
|
|
aliceTokenKeypair.publicKey
|
|
aliceTokenKeypair.publicKey
|
|
);
|
|
);
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|
|
@@ -81,32 +88,34 @@ describe("custom-coder", () => {
|
|
});
|
|
});
|
|
|
|
|
|
it("Creates a token for bob", async () => {
|
|
it("Creates a token for bob", async () => {
|
|
- await program.rpc.initializeAccount({
|
|
|
|
- accounts: {
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .initializeAccount()
|
|
|
|
+ .accounts({
|
|
account: bobTokenKeypair.publicKey,
|
|
account: bobTokenKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
- authority: provider.wallet.publicKey,
|
|
|
|
|
|
+ owner: provider.wallet.publicKey,
|
|
rent,
|
|
rent,
|
|
- },
|
|
|
|
- signers: [bobTokenKeypair],
|
|
|
|
- preInstructions: [
|
|
|
|
- await program.account.token.createInstruction(bobTokenKeypair),
|
|
|
|
- ],
|
|
|
|
- });
|
|
|
|
|
|
+ })
|
|
|
|
+ .signers([bobTokenKeypair])
|
|
|
|
+ .preInstructions([
|
|
|
|
+ await program.account.account.createInstruction(bobTokenKeypair),
|
|
|
|
+ ])
|
|
|
|
+ .rpc();
|
|
});
|
|
});
|
|
|
|
|
|
it("Transfer a token from alice to bob", async () => {
|
|
it("Transfer a token from alice to bob", async () => {
|
|
- await program.rpc.transfer(new BN(1), {
|
|
|
|
- accounts: {
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .transfer(new BN(1))
|
|
|
|
+ .accounts({
|
|
source: aliceTokenKeypair.publicKey,
|
|
source: aliceTokenKeypair.publicKey,
|
|
destination: bobTokenKeypair.publicKey,
|
|
destination: bobTokenKeypair.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
- },
|
|
|
|
- });
|
|
|
|
- const aliceToken = await program.account.token.fetch(
|
|
|
|
|
|
+ })
|
|
|
|
+ .rpc();
|
|
|
|
+ const aliceToken = await program.account.account.fetch(
|
|
aliceTokenKeypair.publicKey
|
|
aliceTokenKeypair.publicKey
|
|
);
|
|
);
|
|
- const bobToken = await program.account.token.fetch(
|
|
|
|
|
|
+ const bobToken = await program.account.account.fetch(
|
|
bobTokenKeypair.publicKey
|
|
bobTokenKeypair.publicKey
|
|
);
|
|
);
|
|
assert.strictEqual(aliceToken.amount.toNumber(), 1);
|
|
assert.strictEqual(aliceToken.amount.toNumber(), 1);
|
|
@@ -114,14 +123,15 @@ describe("custom-coder", () => {
|
|
});
|
|
});
|
|
|
|
|
|
it("Alice burns a token", async () => {
|
|
it("Alice burns a token", async () => {
|
|
- await program.rpc.burn(new BN(1), {
|
|
|
|
- accounts: {
|
|
|
|
- source: aliceTokenKeypair.publicKey,
|
|
|
|
|
|
+ await program.methods
|
|
|
|
+ .burn(new BN(1))
|
|
|
|
+ .accounts({
|
|
|
|
+ account: aliceTokenKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
mint: mintKeypair.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
- },
|
|
|
|
- });
|
|
|
|
- const aliceToken = await program.account.token.fetch(
|
|
|
|
|
|
+ })
|
|
|
|
+ .rpc();
|
|
|
|
+ const aliceToken = await program.account.account.fetch(
|
|
aliceTokenKeypair.publicKey
|
|
aliceTokenKeypair.publicKey
|
|
);
|
|
);
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|