import * as anchor from "@anchor-lang/core";
import { Program } from "@anchor-lang/core";
import { TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";
import { assert } from "chai";
import { Escrow } from "../target/types/escrow";
describe("escrow", () => {
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.Escrow as Program<Escrow>;
let mintA: Token;
let initializerTokenAccountA: anchor.web3.PublicKey;
const initializerAmount = 500;
it("Initializes escrow", async () => {
const payer = anchor.web3.Keypair.generate();
const mintAuthority = anchor.web3.Keypair.generate();
// Airdrop SOL to payer
await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(
payer.publicKey,
10000000000
)
);
// Create mint
mintA = await Token.createMint(
provider.connection,
payer,
mintAuthority.publicKey,
null,
0,
TOKEN_PROGRAM_ID
);
// Create token account
initializerTokenAccountA = await mintA.createAccount(
provider.wallet.publicKey
);
// Mint tokens
await mintA.mintTo(
initializerTokenAccountA,
mintAuthority.publicKey,
[mintAuthority],
initializerAmount
);
// Initialize escrow
const escrowAccount = anchor.web3.Keypair.generate();
await program.methods
.initialize(
new anchor.BN(initializerAmount),
new anchor.BN(1000)
)
.accounts({
initializer: provider.wallet.publicKey,
escrowAccount: escrowAccount.publicKey,
initializerDepositTokenAccount: initializerTokenAccountA,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
})
.signers([escrowAccount])
.rpc();
// Verify escrow state
const escrowState = await program.account.escrowAccount.fetch(
escrowAccount.publicKey
);
assert.ok(escrowState.initializerAmount.eq(new anchor.BN(initializerAmount)));
});
});