Skip to content

Commit

Permalink
test: add deployment of SPL token
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Jun 19, 2024
1 parent 24ec0d5 commit a1e3cc1
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 224 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.0"
"@coral-xyz/anchor": "^0.30.0",
"@solana/spl-token": "^0.4.6"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2"
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
}
63 changes: 60 additions & 3 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Gateway } from "../target/types/gateway";
import * as spl from "@solana/spl-token";

describe("some tests", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const conn = anchor.getProvider().connection;

const program = anchor.workspace.Gateway as Program<Gateway>;

it("Is initialized!", async () => {
it("Test suite 1", async () => {
let transaction = new anchor.web3.Transaction();
const wallet = anchor.workspace.Gateway.provider.wallet.payer;
console.log("wallet address", wallet.publicKey.toString());
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
transaction.add(await program.methods.initialize().instruction());
const tx = await anchor.web3.sendAndConfirmTransaction(anchor.getProvider().connection, transaction,[wallet]);
// console.log("Your transaction signature", tx);

// let txres = await anchor.getProvider().connection.getParsedTransaction(tx, {commitment:"confirmed"});
// now deploying a fake USDC SPL Token
// 1. create a mint account
const mint = anchor.web3.Keypair.generate();
const mintRent = await spl.getMinimumBalanceForRentExemptMint(conn);
const tokenTransaction = new anchor.web3.Transaction();
tokenTransaction.add(
anchor.web3.SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: mint.publicKey,
lamports: mintRent,
space: spl.MINT_SIZE,
programId: spl.TOKEN_PROGRAM_ID
}),
spl.createInitializeMintInstruction(
mint.publicKey,
6,
wallet.publicKey,
null,
)
);
await anchor.web3.sendAndConfirmTransaction(conn, tokenTransaction,[wallet, mint]);
console.log("mint account created!", mint.publicKey.toString());

// 2. create token account to receive mint
const tokenAccount = await spl.getOrCreateAssociatedTokenAccount(
conn,
wallet,
mint.publicKey,
wallet.publicKey,
);
// 3. mint some tokens
const mintToTransaction = new anchor.web3.Transaction().add(
spl.createMintToInstruction(
mint.publicKey,
tokenAccount.address,
wallet.publicKey,
1_000_000,
)
);
await anchor.web3.sendAndConfirmTransaction(anchor.getProvider().connection, mintToTransaction, [wallet]);
console.log("Minted 10 USDC to:", tokenAccount.address.toString());
const account = await spl.getAccount(conn, tokenAccount.address);
console.log("Account balance:", account.amount.toString());
console.log("Account owner: ", account.owner.toString());



});


});
Loading

0 comments on commit a1e3cc1

Please sign in to comment.