Skip to content

Commit

Permalink
Fix manager address empty
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao committed Jun 10, 2024
1 parent bcb535a commit 698d4dc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
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);
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
12 changes: 11 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 All @@ -194,6 +203,7 @@ export class JupiterClient {
const addressLookupTableAccounts = await this.getAdressLookupTableAccounts(
addressLookupTableAddresses
);

let payerPublicKey;
try {
payerPublicKey = await this.base.getWalletSigner().publicKey;
Expand Down
49 changes: 47 additions & 2 deletions anchor/tests/glam_api_tx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
});
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

0 comments on commit 698d4dc

Please sign in to comment.