diff --git a/anchor/src/client/base.ts b/anchor/src/client/base.ts index f75aa4c..ff0551b 100644 --- a/anchor/src/client/base.ts +++ b/anchor/src/client/base.ts @@ -75,7 +75,9 @@ export class BaseClient { this.program = new Program(GlamIDL, this.provider) as GlamProgram; } else { const defaultProvider = anchor.AnchorProvider.env(); - const url = defaultProvider.connection.rpcEndpoint; + const url = + process.env.ANCHOR_PROVIDER_URL || + defaultProvider.connection.rpcEndpoint; const connection = new Connection(url, { commitment: "confirmed", confirmTransactionInitialTimeout: 45000, // default timeout is 30s, we extend it to 45s diff --git a/anchor/src/client/investor.ts b/anchor/src/client/investor.ts index 2b22548..0a52b9b 100644 --- a/anchor/src/client/investor.ts +++ b/anchor/src/client/investor.ts @@ -8,8 +8,9 @@ import { } from "@solana/web3.js"; import { getAssociatedTokenAddressSync, - createAssociatedTokenAccountInstruction, + createAssociatedTokenAccountIdempotentInstruction, createSyncNativeInstruction, + TOKEN_2022_PROGRAM_ID, } from "@solana/spl-token"; import { BaseClient, ApiTxOptions } from "./base"; @@ -128,32 +129,43 @@ export class InvestorClient { // SOL -> wSOL // If the user doesn't have enough wSOL but does have SOL, we auto wrap - let preInstructions: TransactionInstruction[] = []; + let preInstructions: TransactionInstruction[] = [ + createAssociatedTokenAccountIdempotentInstruction( + signer, + signerAssetAta, + signer, + asset, + assetMeta?.programId + ), + createAssociatedTokenAccountIdempotentInstruction( + signer, + treasuryAta, + treasury, + asset, + assetMeta?.programId + ), + createAssociatedTokenAccountIdempotentInstruction( + signer, + signerShareAta, + signer, + shareClass, + TOKEN_2022_PROGRAM_ID + ), + ]; + if (WSOL.equals(asset)) { const connection = this.base.provider.connection; let wsolBalance = new BN(0); - let signerAssetAtaExists = true; try { wsolBalance = new BN( (await connection.getTokenAccountBalance(signerAssetAta)).value.amount ); } catch (err) { // ignore - signerAssetAtaExists = false; } - const solBalance = new BN(String(await connection.getBalance(signer))); + // const solBalance = new BN(String(await connection.getBalance(signer))); const delta = amount.sub(wsolBalance); - if (delta > new BN(0) && solBalance > delta) { - if (!signerAssetAtaExists) { - preInstructions = preInstructions.concat([ - createAssociatedTokenAccountInstruction( - signer, - signerAssetAta, - signer, - asset - ), - ]); - } + if (delta.gt(new BN(0)) /*&& solBalance > delta*/) { preInstructions = preInstructions.concat([ SystemProgram.transfer({ fromPubkey: signer, @@ -165,24 +177,6 @@ export class InvestorClient { } } - // Treasury ATA - // If the treasury ATA doesn't exist, create it. - // This is unlikely, but especially with wSOL may happen. - const accountInfo = await this.base.provider.connection.getAccountInfo( - treasuryAta - ); - if (!accountInfo) { - preInstructions = preInstructions.concat([ - createAssociatedTokenAccountInstruction( - signer, - treasuryAta, - treasury, - asset, - assetMeta?.programId - ), - ]); - } - const tx = await this.base.program.methods .subscribe(amount, skipState) .accounts({ @@ -261,12 +255,7 @@ export class InvestorClient { assetMeta?.programId ); - const accountInfo = - await this.base.provider.connection.getAccountInfo(signerAta); - if (accountInfo) { - return null; - } - return createAssociatedTokenAccountInstruction( + return createAssociatedTokenAccountIdempotentInstruction( signer, signerAta, signer,