From 698d4dcf5afb656750b644b7122d91319a4043cb Mon Sep 17 00:00:00 2001 From: Yuru Shao Date: Sun, 9 Jun 2024 22:03:10 -0700 Subject: [PATCH] Fix manager address empty --- anchor/src/client/base.ts | 6 ++-- anchor/src/client/jupiter.ts | 12 +++++++- anchor/tests/glam_api_tx.spec.ts | 49 ++++++++++++++++++++++++++++++-- api/src/routers/tx.ts | 2 +- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/anchor/src/client/base.ts b/anchor/src/client/base.ts index eb04aee3..b3aed3d7 100644 --- a/anchor/src/client/base.ts +++ b/anchor/src/client/base.ts @@ -133,11 +133,11 @@ export class BaseClient { } getManager(): PublicKey { - return this.provider?.publicKey || new PublicKey(0); + 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 { diff --git a/anchor/src/client/jupiter.ts b/anchor/src/client/jupiter.ts index 74110d9f..2768202d 100644 --- a/anchor/src/client/jupiter.ts +++ b/anchor/src/client/jupiter.ts @@ -174,6 +174,15 @@ 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 @@ -181,7 +190,7 @@ export class JupiterClient { .accounts({ fund, manager, - inputAta: this.base.getManagerAta(new PublicKey(inputMint)), + inputAta, treasury: this.base.getTreasuryPDA(fund), outputAta: destinationTokenAccount, inputMint, @@ -194,6 +203,7 @@ export class JupiterClient { const addressLookupTableAccounts = await this.getAdressLookupTableAccounts( addressLookupTableAddresses ); + let payerPublicKey; try { payerPublicKey = await this.base.getWalletSigner().publicKey; diff --git a/anchor/tests/glam_api_tx.spec.ts b/anchor/tests/glam_api_tx.spec.ts index cb22edc4..e52110fd 100644 --- a/anchor/tests/glam_api_tx.spec.ts +++ b/anchor/tests/glam_api_tx.spec.ts @@ -2,15 +2,22 @@ import * as anchor from "@coral-xyz/anchor"; import { Transaction, SystemProgram, - sendAndConfirmTransaction + sendAndConfirmTransaction, + PublicKey, + VersionedTransaction } from "@solana/web3.js"; import { GlamClient } from "../src/client"; -const API = "https://api.glam.systems"; +// 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", @@ -66,4 +73,42 @@ describe("glam_api_tx", () => { 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); }); diff --git a/api/src/routers/tx.ts b/api/src/routers/tx.ts index 88ef5a7c..77ece039 100644 --- a/api/src/routers/tx.ts +++ b/api/src/routers/tx.ts @@ -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); }