Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anchor: add test suite for api integ tests #126

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ glam = "Gco1pcjxCMYjKJjSNJ7mKV7qezeUTE7arXJgy7PAPNRc"
url = "https://api.apr.dev"

[provider]
cluster = "localnet"
# cluster = "localnet"
#cluster = "devnet"
#cluster = "mainnet"
cluster = "mainnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_api_tx"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_crud"
test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_investor"
# test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_investor"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_drift"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_staking"
#test = "../node_modules/.bin/nx run --skip-nx-cache anchor:jest --verbose --testPathPattern tests/ --testNamePattern glam_jupiter"
Expand Down
6 changes: 3 additions & 3 deletions anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ export class BaseClient {
}

getManager(): PublicKey {
return this.provider?.publicKey || new PublicKey(0);
Copy link
Contributor Author

@yurushao yurushao Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we cannot find manager public key just let it fail, because this should be a fatal error. Otherwise the error is swallowed and PublicKey(0) will lead to glam program errors.

return this.provider?.publicKey;
}

getManagerAta(mint: PublicKey): PublicKey {
return getAssociatedTokenAddressSync(mint, this.getManager());
getManagerAta(mint: PublicKey, manager?: PublicKey): PublicKey {
return getAssociatedTokenAddressSync(mint, this.getManager() || manager);
}

getWalletSigner(): Keypair {
Expand Down
11 changes: 10 additions & 1 deletion anchor/src/client/jupiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,23 @@ export class JupiterClient {
const swapIx: { data: any; keys: AccountMeta[] } =
this.ixDataToTransactionInstruction(swapInstruction);

let inputAta;
if (this.base.getManager()) {
inputAta = this.base.getManagerAta(new PublicKey(inputMint));
} else {
// When called from API, we cannot get manager from provider
// We need to pass the manager from client side
inputAta = this.base.getManagerAta(new PublicKey(inputMint), manager);
}

const instructions = [
...computeBudgetInstructions.map(this.ixDataToTransactionInstruction),
await this.base.program.methods
.jupiterSwap(new anchor.BN(swapAmount), swapIx.data)
.accounts({
fund,
manager,
inputAta: this.base.getManagerAta(new PublicKey(inputMint)),
inputAta,
treasury: this.base.getTreasuryPDA(fund),
outputAta: destinationTokenAccount,
inputMint,
Expand Down
114 changes: 114 additions & 0 deletions anchor/tests/glam_api_tx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as anchor from "@coral-xyz/anchor";
import {
Transaction,
SystemProgram,
sendAndConfirmTransaction,
PublicKey,
VersionedTransaction
} from "@solana/web3.js";
import { GlamClient } from "../src/client";

// const API = "https://api.glam.systems";
const API = "http://localhost:8080";
const wsol = new PublicKey("So11111111111111111111111111111111111111112");
const msol = new PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So");
const usdc = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");

describe("glam_api_tx", () => {
const glamClient = new GlamClient();

/*
it("Wrap sol", async () => {
const response = await fetch(`${API}/tx/wsol/wrap`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
manager: "gLJHKPrZLGBiBZ33hFgZh6YnsEhTVxuRT17UCqNp6ff",
fund: "4gAcSdfSAxVPcxj2Hi3AvKKViGat3iUysDD5ZzbqhDTk",
amount: 1000000
})
});
const { tx } = await response.json();
console.log("Wrap tx:", tx);
const t = Transaction.from(Buffer.from(tx, "hex"));
t.sign(glamClient.getWalletSigner());
try {
const txId = await glamClient.provider.connection.sendRawTransaction(
t.serialize()
);
console.log("Wrap txId:", txId);
} catch (error) {
console.log("Error", error);
throw error;
}
});

it("unwrap", async () => {
const response = await fetch(`${API}/tx/wsol/unwrap`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
manager: "gLJHKPrZLGBiBZ33hFgZh6YnsEhTVxuRT17UCqNp6ff",
fund: "4gAcSdfSAxVPcxj2Hi3AvKKViGat3iUysDD5ZzbqhDTk"
})
});
const { tx } = await response.json();
console.log("unwrap tx", tx);

const t = Transaction.from(Buffer.from(tx, "hex"));
t.recentBlockhash = (
await glamClient.provider.connection.getLatestBlockhash()
).blockhash;
console.log("unwrap recentBlockhash", t.recentBlockhash);
t.sign(glamClient.getWalletSigner());
try {
const txId = await glamClient.provider.connection.sendRawTransaction(
t.serialize()
);
console.log("Wrap txId:", txId);
} catch (error) {
console.log("Error", error);
throw error;
}
});
*/

it("jupiter swap", async () => {
const response = await fetch(`${API}/tx/jupiter/swap`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
fund: "4gAcSdfSAxVPcxj2Hi3AvKKViGat3iUysDD5ZzbqhDTk",
manager: "gLJHKPrZLGBiBZ33hFgZh6YnsEhTVxuRT17UCqNp6ff",
quote: {
inputMint: wsol.toBase58(),
outputMint: msol.toBase58(),
amount: 10000000,
autoSlippage: true,
autoSlippageCollisionUsdValue: 1000,
swapMode: "ExactIn",
onlyDirectRoutes: false,
asLegacyTransaction: false,
maxAccounts: 20
}
})
});
const { tx, versioned } = await response.json();
// console.log("is versioned:", versioned, "jupiter swap tx:", tx);

const vTx = VersionedTransaction.deserialize(Buffer.from(tx, "hex"));
try {
const txId = await (
glamClient.provider as anchor.AnchorProvider
).sendAndConfirm(vTx, [glamClient.getWalletSigner()]);
console.log("jupiter swap txId", txId);
} catch (error) {
console.error("Error", error);
throw error;
}
}, 30_000);
});
2 changes: 1 addition & 1 deletion api/src/routers/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const jupiterSwapTx = async (client, req, res) => {
const fund = validatePubkey(req.body.fund);
const manager = validatePubkey(req.body.manager);

if (fund === undefined || manager === undefined) {
if (!fund || !manager) {
return res.sendStatus(400);
}

Expand Down
Loading