Skip to content

Commit

Permalink
added test to test takeOffer
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanCeaser committed Jan 23, 2025
1 parent bfa63f3 commit d02dcda
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
48 changes: 35 additions & 13 deletions escrow/tests/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Escrow } from "../target/types/escrow";
import { getAssociatedTokenAddressSync, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
import { LAMPORTS_PER_SOL, PublicKey, SendTransactionError } from "@solana/web3.js";
import { confirmTransaction, createAccountsMintsAndTokenAccounts, makeKeypairs } from "@solana-developers/helpers";
import { assert } from "chai";

Expand Down Expand Up @@ -31,15 +31,15 @@ describe("escrow", () => {
[ceaser, bob, mintA, mintB] = makeKeypairs(4);

const mintAOfferedAmount = new anchor.BN(1_000_000);

before("Creates Ceaser and Bob Accounts, 2 token mints and associated token accounts for both tokens for both users",
async()=>{
const {users, mints, tokenAccounts} = await createAccountsMintsAndTokenAccounts(
async () => {
const { users, mints, tokenAccounts } = await createAccountsMintsAndTokenAccounts(
[
// Ceaser's token balances, 1,000,000,000 of mintA & 0 of mintB
[1_000_000_000, 0],
// bobs's token balances, 0 of mintA & 1,000,000,000 of mintB
[0,1_000_000_000],
[0, 1_000_000_000],
],
1 * LAMPORTS_PER_SOL,
connection,
Expand Down Expand Up @@ -67,10 +67,10 @@ describe("escrow", () => {
accounts.mintB = mintB.publicKey;
accounts.makerAccountMintB = ceaserMintB // PDA for maker's token mint B
accounts.takerAccountMintB = bobMintB // PDA for taker's token mint B
});
});

it("Puts Ceaser's Tokens into Vault when he makes an Offer!", async () => {

const offerSeed = new anchor.BN(randomBytes(8));

// Derive the account addresses we'll use for the offer and vault
Expand All @@ -90,14 +90,14 @@ describe("escrow", () => {
accounts.offer = offer;
accounts.vault = vault;

console.log("accounts: ", accounts);
// console.log("accounts: ", accounts);

const tx = await program.methods
.makeOffer(offerSeed, mintAOfferedAmount)
.accounts({...accounts})
.signers([ceaser])
.rpc()
.makeOffer(offerSeed, mintAOfferedAmount)
.accounts({ ...accounts })
.signers([ceaser])
.rpc()

await confirmTransaction(connection, tx);

// Check our vault contains the tokens offered
Expand All @@ -113,4 +113,26 @@ describe("escrow", () => {
assert(offerAccount.mintB.equals(accounts.mintB));
assert(offerAccount.receiveAmount.eq(mintAOfferedAmount));
});

it("sends token from vault to Bob's account and gives Ceaser Bob's tokens when Bob takes an Offer", async () => {

const tx = await program.methods
.takeOffer()
.accounts({ ...accounts })
.signers([bob,])
.rpc();
const confirmed = await confirmTransaction(connection, tx);
console.log("Transaction signature:", confirmed);

// Check the offered tokens are now in Bob's account
const bobTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.takerAccountMintA);
const bobTokenAccountBalanceAfter = new anchor.BN(bobTokenAccountBalanceAfterResponse.value.amount);
assert(bobTokenAccountBalanceAfter.eq(mintAOfferedAmount));

// Check the wanted tokens are now in Ceaser's account
const ceaserTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.makerAccountMintB);
const ceaserTokenAccountBalanceAfter = new anchor.BN(ceaserTokenAccountBalanceAfterResponse.value.amount);
assert(ceaserTokenAccountBalanceAfter.eq(mintAOfferedAmount));

});
});
30 changes: 28 additions & 2 deletions vault/tests/vault.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Vault } from "../target/types/vault";
import { PublicKey, SystemProgram } from "@solana/web3.js";
import { expect } from "chai";

describe("vault", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());

const program = anchor.workspace.Vault as Program<Vault>;
const provider = anchor.getProvider() as anchor.AnchorProvider;
const wallet = provider.wallet;

let vaultState: PublicKey;
let vault: PublicKey;
let vaultBump: number;
let stateBump: number;

it("Is initialized!", async () => {
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
[vaultState, stateBump] = await PublicKey.findProgramAddressSync(
[Buffer.from("state"), wallet.publicKey.toBuffer()],
program.programId
);
[vault, vaultBump] = await PublicKey.findProgramAddressSync(
[vaultState.toBuffer()],
program.programId
);
const tx = await program.methods
.initialize()
.accounts({
signer: wallet.publicKey,
}).rpc();
console.log("Initialized vault. Transaction signature:", tx);

const vaultStateAccount = await program.account.vaultState.fetch(vaultState);

expect(vaultStateAccount.vaultBump).to.equal(vaultBump);
expect(vaultStateAccount.stateBump).to.equal(stateBump);
});
});

0 comments on commit d02dcda

Please sign in to comment.