From a9fd31c9ffd30d88edeb3fb154d9c18af649ad1c Mon Sep 17 00:00:00 2001 From: Yuru Shao Date: Sun, 9 Jun 2024 23:28:16 -0700 Subject: [PATCH] Set confirm options --- anchor/src/client/base.ts | 8 ++++++-- anchor/src/client/jupiter.ts | 5 +++-- anchor/tests/glam_api_tx.spec.ts | 29 +++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/anchor/src/client/base.ts b/anchor/src/client/base.ts index b3aed3d7..dc289713 100644 --- a/anchor/src/client/base.ts +++ b/anchor/src/client/base.ts @@ -133,11 +133,15 @@ export class BaseClient { } getManager(): PublicKey { - return this.provider?.publicKey; + const managerPublicKey = this.provider?.publicKey; + if (!managerPublicKey) { + throw new Error("Manager public key cannot be retrieved from provider"); + } + return managerPublicKey; } getManagerAta(mint: PublicKey, manager?: PublicKey): PublicKey { - return getAssociatedTokenAddressSync(mint, this.getManager() || manager); + return getAssociatedTokenAddressSync(mint, manager || this.getManager()); } getWalletSigner(): Keypair { diff --git a/anchor/src/client/jupiter.ts b/anchor/src/client/jupiter.ts index 2768202d..2b2232f1 100644 --- a/anchor/src/client/jupiter.ts +++ b/anchor/src/client/jupiter.ts @@ -175,9 +175,10 @@ export class JupiterClient { this.ixDataToTransactionInstruction(swapInstruction); let inputAta; - if (this.base.getManager()) { + try { inputAta = this.base.getManagerAta(new PublicKey(inputMint)); - } else { + } catch (e) { + console.log("Cannot get manager ata:", e); // 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); diff --git a/anchor/tests/glam_api_tx.spec.ts b/anchor/tests/glam_api_tx.spec.ts index 16ad43fd..fb14fc4b 100644 --- a/anchor/tests/glam_api_tx.spec.ts +++ b/anchor/tests/glam_api_tx.spec.ts @@ -3,16 +3,31 @@ import { Transaction, sendAndConfirmTransaction, PublicKey, - VersionedTransaction + VersionedTransaction, + ConfirmOptions } from "@solana/web3.js"; import { GlamClient } from "../src/client"; -// const API = "https://api.glam.systems"; -const API = "http://localhost:8080"; +/** + * This test suite is a demonstration of how to interact with the glam API. + * + * Before running this test suite, make sure you [provider] is correclty set up in Anchor.toml: + * 1) cluster: set to "mainnet-beta" + * 2) wallet: set to the path of the manager wallet + * + * To run the tests (optional to skip build, must skip deploy): + * anchor test --skip-build --skip-deploy + */ + +const API = "https://api.glam.systems"; const wsol = new PublicKey("So11111111111111111111111111111111111111112"); const msol = new PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"); const manager = "gLJHKPrZLGBiBZ33hFgZh6YnsEhTVxuRT17UCqNp6ff"; const fund = "4gAcSdfSAxVPcxj2Hi3AvKKViGat3iUysDD5ZzbqhDTk"; +const confirmOptions: ConfirmOptions = { + commitment: "confirmed", + maxRetries: 3 +}; describe("glam_api_tx", () => { const glamClient = new GlamClient(); @@ -30,7 +45,8 @@ describe("glam_api_tx", () => { const txId = await sendAndConfirmTransaction( glamClient.provider.connection, Transaction.from(Buffer.from(tx, "hex")), - [glamClient.getWalletSigner()] + [glamClient.getWalletSigner()], + confirmOptions ); console.log("Wrap txId:", txId); } catch (error) { @@ -52,7 +68,8 @@ describe("glam_api_tx", () => { const txId = await sendAndConfirmTransaction( glamClient.provider.connection, Transaction.from(Buffer.from(tx, "hex")), - [glamClient.getWalletSigner()] + [glamClient.getWalletSigner()], + confirmOptions ); console.log("Unwrap txId:", txId); } catch (error) { @@ -86,7 +103,7 @@ describe("glam_api_tx", () => { try { const txId = await ( glamClient.provider as anchor.AnchorProvider - ).sendAndConfirm(vTx, [glamClient.getWalletSigner()]); + ).sendAndConfirm(vTx, [glamClient.getWalletSigner()], confirmOptions); console.log("jupiter swap txId", txId); } catch (error) { console.error("Error", error);