2022-01-09 18:10:25 -08:00
|
|
|
import * as anchor from "@project-serum/anchor";
|
|
|
|
import { Spl } from "@project-serum/anchor";
|
2022-03-24 08:44:33 -07:00
|
|
|
import { assert } from "chai";
|
2022-01-09 18:10:25 -08:00
|
|
|
import BN from "bn.js";
|
|
|
|
import { Keypair, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
|
|
|
|
|
|
|
|
describe("custom-coder", () => {
|
|
|
|
// Configure the client to use the local cluster.
|
2022-04-17 12:54:28 -07:00
|
|
|
const provider = anchor.AnchorProvider.env();
|
|
|
|
anchor.setProvider(provider);
|
2022-01-09 18:10:25 -08:00
|
|
|
|
|
|
|
// Client.
|
|
|
|
const program = Spl.token();
|
|
|
|
|
|
|
|
// Constants.
|
|
|
|
const mintKeypair = Keypair.generate();
|
|
|
|
const aliceTokenKeypair = Keypair.generate();
|
|
|
|
const bobTokenKeypair = Keypair.generate();
|
|
|
|
const rent = SYSVAR_RENT_PUBKEY;
|
|
|
|
|
|
|
|
it("Creates a mint", async () => {
|
2022-04-17 12:54:28 -07:00
|
|
|
await program.rpc.initializeMint(6, provider.wallet.publicKey, null, {
|
|
|
|
accounts: {
|
|
|
|
mint: mintKeypair.publicKey,
|
|
|
|
rent,
|
|
|
|
},
|
|
|
|
signers: [mintKeypair],
|
|
|
|
preInstructions: [
|
|
|
|
await program.account.mint.createInstruction(mintKeypair),
|
|
|
|
],
|
|
|
|
});
|
2022-01-09 18:10:25 -08:00
|
|
|
const mintAccount = await program.account.mint.fetch(mintKeypair.publicKey);
|
2022-04-17 12:54:28 -07:00
|
|
|
assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
|
2022-03-24 08:44:33 -07:00
|
|
|
assert.isNull(mintAccount.freezeAuthority);
|
|
|
|
assert.strictEqual(mintAccount.decimals, 6);
|
|
|
|
assert.isTrue(mintAccount.isInitialized);
|
|
|
|
assert.strictEqual(mintAccount.supply.toNumber(), 0);
|
2022-01-09 18:10:25 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Creates a token account for alice", async () => {
|
|
|
|
await program.rpc.initializeAccount({
|
|
|
|
accounts: {
|
|
|
|
account: aliceTokenKeypair.publicKey,
|
|
|
|
mint: mintKeypair.publicKey,
|
2022-04-17 12:54:28 -07:00
|
|
|
authority: provider.wallet.publicKey,
|
2022-01-09 18:10:25 -08:00
|
|
|
rent,
|
|
|
|
},
|
|
|
|
signers: [aliceTokenKeypair],
|
|
|
|
preInstructions: [
|
|
|
|
await program.account.token.createInstruction(aliceTokenKeypair),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const token = await program.account.token.fetch(
|
|
|
|
aliceTokenKeypair.publicKey
|
|
|
|
);
|
2022-04-17 12:54:28 -07:00
|
|
|
assert.isTrue(token.authority.equals(provider.wallet.publicKey));
|
2022-03-24 08:44:33 -07:00
|
|
|
assert.isTrue(token.mint.equals(mintKeypair.publicKey));
|
|
|
|
assert.strictEqual(token.amount.toNumber(), 0);
|
|
|
|
assert.isNull(token.delegate);
|
|
|
|
assert.strictEqual(token.state, 1);
|
|
|
|
assert.isNull(token.isNative);
|
|
|
|
assert.strictEqual(token.delegatedAmount.toNumber(), 0);
|
|
|
|
assert.isNull(token.closeAuthority);
|
2022-01-09 18:10:25 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Mints a token to alice", async () => {
|
|
|
|
await program.rpc.mintTo(new BN(2), {
|
|
|
|
accounts: {
|
|
|
|
mint: mintKeypair.publicKey,
|
|
|
|
to: aliceTokenKeypair.publicKey,
|
2022-04-17 12:54:28 -07:00
|
|
|
authority: provider.wallet.publicKey,
|
2022-01-09 18:10:25 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const token = await program.account.token.fetch(
|
|
|
|
aliceTokenKeypair.publicKey
|
|
|
|
);
|
|
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|
2022-03-24 08:44:33 -07:00
|
|
|
assert.strictEqual(token.amount.toNumber(), 2);
|
|
|
|
assert.strictEqual(mint.supply.toNumber(), 2);
|
2022-01-09 18:10:25 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Creates a token for bob", async () => {
|
|
|
|
await program.rpc.initializeAccount({
|
|
|
|
accounts: {
|
|
|
|
account: bobTokenKeypair.publicKey,
|
|
|
|
mint: mintKeypair.publicKey,
|
2022-04-17 12:54:28 -07:00
|
|
|
authority: provider.wallet.publicKey,
|
2022-01-09 18:10:25 -08:00
|
|
|
rent,
|
|
|
|
},
|
|
|
|
signers: [bobTokenKeypair],
|
|
|
|
preInstructions: [
|
|
|
|
await program.account.token.createInstruction(bobTokenKeypair),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Transfer a token from alice to bob", async () => {
|
|
|
|
await program.rpc.transfer(new BN(1), {
|
|
|
|
accounts: {
|
|
|
|
source: aliceTokenKeypair.publicKey,
|
|
|
|
destination: bobTokenKeypair.publicKey,
|
2022-04-17 12:54:28 -07:00
|
|
|
authority: provider.wallet.publicKey,
|
2022-01-09 18:10:25 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const aliceToken = await program.account.token.fetch(
|
|
|
|
aliceTokenKeypair.publicKey
|
|
|
|
);
|
|
|
|
const bobToken = await program.account.token.fetch(
|
|
|
|
bobTokenKeypair.publicKey
|
|
|
|
);
|
2022-03-24 08:44:33 -07:00
|
|
|
assert.strictEqual(aliceToken.amount.toNumber(), 1);
|
|
|
|
assert.strictEqual(bobToken.amount.toNumber(), 1);
|
2022-01-09 18:10:25 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Alice burns a token", async () => {
|
|
|
|
await program.rpc.burn(new BN(1), {
|
|
|
|
accounts: {
|
|
|
|
source: aliceTokenKeypair.publicKey,
|
|
|
|
mint: mintKeypair.publicKey,
|
2022-04-17 12:54:28 -07:00
|
|
|
authority: provider.wallet.publicKey,
|
2022-01-09 18:10:25 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const aliceToken = await program.account.token.fetch(
|
|
|
|
aliceTokenKeypair.publicKey
|
|
|
|
);
|
|
|
|
const mint = await program.account.mint.fetch(mintKeypair.publicKey);
|
2022-03-24 08:44:33 -07:00
|
|
|
assert.strictEqual(aliceToken.amount.toNumber(), 0);
|
|
|
|
assert.strictEqual(mint.supply.toNumber(), 1);
|
2022-01-09 18:10:25 -08:00
|
|
|
});
|
|
|
|
});
|