Skip to content

Commit

Permalink
chore: test js refactor (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwenSchaeferjohann authored Mar 27, 2024
1 parent d691e58 commit 0d00e28
Show file tree
Hide file tree
Showing 79 changed files with 2,845 additions and 3,948 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions cli/src/commands/balance/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Command, Flags } from "@oclif/core";
import { CustomLoader, getSolanaRpcUrl } from "../../utils/utils";
import { getCompressedTokenAccountsFromMockRpc } from "@lightprotocol/compressed-token";
import { Connection, PublicKey } from "@solana/web3.js";
import { getCompressedTokenAccountsForTest } from "@lightprotocol/compressed-token";
import { PublicKey } from "@solana/web3.js";
import { getTestRpc } from "@lightprotocol/stateless.js";

class BalanceCommand extends Command {
static summary = "Get balance";
Expand All @@ -28,9 +29,9 @@ class BalanceCommand extends Command {
try {
const refMint = new PublicKey(flags["mint"]);
const refOwner = new PublicKey(flags["owner"]);
const connection = new Connection(getSolanaRpcUrl());
const tokenAccounts = await getCompressedTokenAccountsFromMockRpc(
connection,
const rpc = await getTestRpc(getSolanaRpcUrl());
const tokenAccounts = await getCompressedTokenAccountsForTest(
rpc,
refOwner,
refMint,
);
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/create-mint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../utils/utils";
import { createMint } from "@lightprotocol/compressed-token";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { Rpc } from "@lightprotocol/stateless.js/src";

const DEFAULT_DECIMAL_COUNT = 9;

Expand Down Expand Up @@ -47,7 +48,7 @@ class CreateMintCommand extends Command {
const mintAuthority = this.getMintAuthority(flags, payer);
const connection = new Connection(getSolanaRpcUrl());
const { mint, transactionSignature } = await createMint(
connection,
connection as Rpc,
payer,
mintAuthority,
mintDecimals,
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/mint-to/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { getKeypairFromFile } from "@solana-developers/helpers";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { mintTo } from "@lightprotocol/compressed-token";
import { Rpc } from "@lightprotocol/stateless.js";

class MintToCommand extends Command {
static summary = "Mint tokens to an account.";
Expand Down Expand Up @@ -63,7 +64,7 @@ class MintToCommand extends Command {
const connection = new Connection(getSolanaRpcUrl());

const txId = await mintTo(
connection,
connection as Rpc,
payer,
mintPublicKey,
toPublicKey,
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/transfer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { transfer } from "@lightprotocol/compressed-token";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { getKeypairFromFile } from "@solana-developers/helpers";
import { Rpc } from "@lightprotocol/stateless.js";

class TransferCommand extends Command {
static summary = "Transfer tokens from one account to another.";
Expand Down Expand Up @@ -61,7 +62,7 @@ class TransferCommand extends Command {
const connection = new Connection(getSolanaRpcUrl());

const txId = await transfer(
connection,
connection as Rpc,
payer,
mintPublicKey,
amount,
Expand Down
26 changes: 10 additions & 16 deletions cli/test/commands/balance/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { expect, test } from "@oclif/test";
import { initTestEnvIfNeeded } from "../../../src/utils/initTestEnv";
import { defaultSolanaWalletKeypair, getSolanaRpcUrl } from "../../../src";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { Keypair, PublicKey } from "@solana/web3.js";
import { createMint, mintTo } from "@lightprotocol/compressed-token";
import { requestAirdrop } from "../../helpers/helpers";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
import { getTestRpc } from "@lightprotocol/stateless.js";
describe("Get balance", () => {
test.it(async () => {
await initTestEnvIfNeeded();
const payerKeypair = defaultSolanaWalletKeypair();

const mintKeypair = Keypair.generate();
await requestAirdrop(mintKeypair.publicKey);
const mintAuthority = payerKeypair.publicKey;
const mintAuthority = payerKeypair;

const mintAmount = 10;
const mintDestination = Keypair.generate().publicKey;
Expand Down Expand Up @@ -42,30 +43,23 @@ describe("Get balance", () => {
});

async function createTestMint(payer: Keypair) {
const connection = new Connection(getSolanaRpcUrl());
const { mint } = await createMint(
connection,
payer,
payer.publicKey,
9,
undefined,
{
commitment: "finalized",
},
);
const rpc = await getTestRpc(getSolanaRpcUrl());
const { mint } = await createMint(rpc, payer, payer, 9, undefined, {
commitment: "finalized",
});
return mint;
}

async function testMintTo(
payer: Keypair,
mintAddress: PublicKey,
mintDestination: PublicKey,
mintAuthority: PublicKey | Keypair,
mintAuthority: Keypair,
mintAmount: number,
) {
const connection = new Connection(getSolanaRpcUrl());
const rpc = await getTestRpc(getSolanaRpcUrl());
const txId = await mintTo(
connection,
rpc,
payer,
mintAddress,
mintDestination,
Expand Down
19 changes: 12 additions & 7 deletions cli/test/commands/mint-to/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../../../src";
import { Connection, Keypair } from "@solana/web3.js";
import { createMint } from "@lightprotocol/compressed-token";
import { confirmTx } from "@lightprotocol/stateless.js";
import { confirmTx, getTestRpc } from "@lightprotocol/stateless.js";
import { requestAirdrop } from "../../helpers/helpers";

describe("mint-to", () => {
Expand Down Expand Up @@ -37,15 +37,20 @@ describe("mint-to", () => {
});

async function createTestMint() {
const connection = new Connection(getSolanaRpcUrl(), "finalized");
const payer = await getPayer();
const rpc = await getTestRpc(
getSolanaRpcUrl(),
undefined,
undefined,
undefined,
);

const { mint, transactionSignature } = await createMint(
connection,
payer,
payer.publicKey,
rpc,
await getPayer(),
await getPayer(),
9,
);
await confirmTx(connection, transactionSignature);
await confirmTx(rpc, transactionSignature);
return mint;
}
});
26 changes: 11 additions & 15 deletions cli/test/commands/transfer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { createMint, mintTo } from "@lightprotocol/compressed-token";
import { requestAirdrop } from "../../helpers/helpers";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
import { getTestRpc } from "@lightprotocol/stateless.js";
describe("transfer", () => {
test.it(async () => {
await initTestEnvIfNeeded();
const payerKeypair = defaultSolanaWalletKeypair();

const mintKeypair = Keypair.generate();
await requestAirdrop(mintKeypair.publicKey);
const mintAuthority = payerKeypair.publicKey;
const mintAuthority = payerKeypair;

const mintAmount = 10;
const mintDestination = Keypair.generate().publicKey;
Expand Down Expand Up @@ -44,30 +45,25 @@ describe("transfer", () => {
});

async function createTestMint(payer: Keypair) {
const connection = new Connection(getSolanaRpcUrl());
const { mint } = await createMint(
connection,
payer,
payer.publicKey,
9,
undefined,
{
commitment: "finalized",
},
);
const rpc = await getTestRpc(getSolanaRpcUrl());

const { mint } = await createMint(rpc, payer, payer, 9, undefined, {
commitment: "finalized",
});
return mint;
}

async function testMintTo(
payer: Keypair,
mintAddress: PublicKey,
mintDestination: PublicKey,
mintAuthority: PublicKey | Keypair,
mintAuthority: Keypair,
mintAmount: number,
) {
const connection = new Connection(getSolanaRpcUrl());
const rpc = await getTestRpc(getSolanaRpcUrl());

const txId = await mintTo(
connection,
rpc,
payer,
mintAddress,
mintDestination,
Expand Down
5 changes: 3 additions & 2 deletions cli/test/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Connection, PublicKey } from "@solana/web3.js";
import { getSolanaRpcUrl } from "../../src";
import { confirmTx } from "@lightprotocol/stateless.js";
import { confirmTx, getTestRpc } from "@lightprotocol/stateless.js";

export async function requestAirdrop(address: PublicKey, amount = 3e9) {
const rpc = await getTestRpc(getSolanaRpcUrl());
const connection = new Connection(getSolanaRpcUrl(), "finalized");
let sig = await connection.requestAirdrop(address, amount);
await confirmTx(connection, sig);
await confirmTx(rpc, sig);
}
4 changes: 3 additions & 1 deletion js/compressed-token/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80,
"useTabs": false,
"tabWidth": 2,
"bracketSpacing": true
"bracketSpacing": true,
"arrowParens": "avoid"
}
19 changes: 10 additions & 9 deletions js/compressed-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
"dist"
],
"scripts": {
"test": "pnpm run test:program",
"gnark-prover": "../../circuit-lib/circuit-lib.js/scripts/prover.sh",
"test": "pnpm test:e2e:all",
"test-all": "vitest run",
"test-validator:silent": "./../../cli/test_bin/run test-validator -b && pnpm gnark-prover",
"test:unit:all": "EXCLUDE_E2E=true vitest run",
"test-all:verbose": "vitest run --reporter=verbose",
"gnark-prover": "../../circuit-lib/circuit-lib.js/scripts/prover.sh",
"test-validator": "./../../cli/test_bin/run test-validator",
"test:program": "pnpm run test-validator:silent && vitest run tests/e2e/program.test.ts",
"emit-event:create_mint": "vitest run tests/e2e/emit-create_mint.test.ts",
"emit-event:mint_to": "vitest run tests/e2e/emit-mint_to.test.ts",
"emit-event:transfer": "vitest run tests/e2e/emit-transfer.test.ts",
"test:serde": "vitest run tests/e2e/serialization.test.ts",
"test:verbose": "vitest run --reporter=verbose",
"pretest:e2e": "./../../cli/test_bin/run test-validator -b && pnpm gnark-prover",
"test:e2e:create-mint": "pnpm pretest:e2e && vitest run tests/e2e/create-mint.test.ts",
"test:e2e:mint-to": "pnpm pretest:e2e && vitest run tests/e2e/mint-to.test.ts",
"test:e2e:transfer": "pnpm pretest:e2e && vitest run tests/e2e/transfer.test.ts",
"test:e2e:multisig": "pnpm pretest:e2e && vitest run tests/e2e/multisig.test.ts",
"test:e2e:all": "pnpm pretest:e2e && vitest run tests/e2e/*.test.ts",
"pull-idl": "../../scripts/push-compressed-token-idl.sh",
"build": "rimraf dist && pnpm run pull-idl && pnpm build:bundle",
"build:bundle": "rollup -c --bundleConfigAsCjs",
Expand Down
2 changes: 1 addition & 1 deletion js/compressed-token/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import typescript from '@rollup/plugin-typescript';
import pkg from './package.json';
import nodePolyfills from 'rollup-plugin-polyfill-node';

const rolls = (fmt) => ({
const rolls = fmt => ({
input: 'src/index.ts',
output: {
dir: 'dist',
Expand Down
27 changes: 27 additions & 0 deletions js/compressed-token/src/actions/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Signer, PublicKey } from '@solana/web3.js';

/** @internal */
export function getSigners(
signerOrMultisig: Signer | PublicKey,
multiSigners: Signer[],
): [PublicKey, Signer[]] {
// TODO: add multisig support
if (multiSigners.length > 0) throw new Error('Multisig not supported yet.');

if (signerOrMultisig instanceof PublicKey)
throw new Error('Multisig not supported yet.');

return signerOrMultisig instanceof PublicKey
? [signerOrMultisig, multiSigners]
: [signerOrMultisig.publicKey, [signerOrMultisig]];
}

/** @internal remove signer from signers if part of signers */
export function dedupeSigner(signer: Signer, signers: Signer[]): Signer[] {
if (signers.includes(signer)) {
return signers.filter(
s => s.publicKey.toString() !== signer.publicKey.toString(),
);
}
return signers;
}
29 changes: 17 additions & 12 deletions js/compressed-token/src/actions/create-mint.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
import {
ConfirmOptions,
Connection,
Keypair,
PublicKey,
Signer,
TransactionSignature,
} from '@solana/web3.js';
import { CompressedTokenProgram } from '../program';
import { MINT_SIZE } from '@solana/spl-token';
import { sendAndConfirmTx } from '@lightprotocol/stateless.js';
import { buildAndSignTx } from '@lightprotocol/stateless.js';
import {
Rpc,
buildAndSignTx,
sendAndConfirmTx,
} from '@lightprotocol/stateless.js';
import { dedupeSigner } from './common';

/**
* Create and initialize a new compressed token mint
*
* @param connection Connection to use
* @param rpc RPC to use
* @param payer Payer of the transaction and initialization fees
* @param mintAuthority Account or multisig that will control minting
* @param mintAuthority Account or multisig that will control minting. Is signer.
* @param decimals Location of the decimal place
* @param keypair Optional keypair, defaulting to a new random one
* @param confirmOptions Options for confirming the transaction
*
* @return Address of the new mint and the transaction signature
*/
export async function createMint(
connection: Connection,
rpc: Rpc,
payer: Signer,
mintAuthority: PublicKey,
mintAuthority: Signer,
decimals: number,
keypair = Keypair.generate(),
confirmOptions?: ConfirmOptions,
): Promise<{ mint: PublicKey; transactionSignature: TransactionSignature }> {
const rentExemptBalance =
await connection.getMinimumBalanceForRentExemption(MINT_SIZE);
await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);

const ixs = await CompressedTokenProgram.createMint({
feePayer: payer.publicKey,
mint: keypair.publicKey,
decimals,
authority: mintAuthority,
authority: mintAuthority.publicKey,
freezeAuthority: null, // TODO: add feature
rentExemptBalance,
});

const { blockhash } = await connection.getLatestBlockhash();
const { blockhash } = await rpc.getLatestBlockhash();

const additionalSigners = dedupeSigner(payer, [mintAuthority, keypair]);

const tx = buildAndSignTx(ixs, payer, blockhash, [keypair]);
const tx = buildAndSignTx(ixs, payer, blockhash, additionalSigners);

const txId = await sendAndConfirmTx(connection, tx, confirmOptions);
const txId = await sendAndConfirmTx(rpc, tx, confirmOptions);

return { mint: keypair.publicKey, transactionSignature: txId };
}
Loading

0 comments on commit 0d00e28

Please sign in to comment.