Skip to content

Commit

Permalink
investor: use create ata idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece committed Oct 2, 2024
1 parent 2638ecd commit e7bb4fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
4 changes: 3 additions & 1 deletion anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 29 additions & 40 deletions anchor/src/client/investor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e7bb4fc

Please sign in to comment.