Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Allow using existing wsol token account #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,32 @@ export class Base {
const ata = await Spl.getAssociatedTokenAccount({ mint, owner });

if (Token.WSOL.mint.equals(mint)) {
const newTokenAccount = await Spl.insertCreateWrappedNativeAccountInstructions({
connection,
owner,
payer,
instructions: frontInstructions,
signers,
amount,
});
// if no endInstructions provide, no need to close
if (endInstructions) {
endInstructions.push(Spl.makeCloseAccountInstruction({ tokenAccount: newTokenAccount, owner, payer }));
if (!tokenAccount) {
const newTokenAccount = await Spl.insertCreateWrappedNativeAccountInstructions({
connection,
owner,
payer,
instructions: frontInstructions,
signers,
amount,
});
// if no endInstructions provide, no need to close
if (endInstructions) {
endInstructions.push(Spl.makeCloseAccountInstruction({ tokenAccount: newTokenAccount, owner, payer }));
}

return newTokenAccount;
} else {
const newIxs = await Spl.makeEnsureWrappedNativeAccountBalanceInstructions({
connection,
owner,
accountKey: tokenAccount,
payer,
amount,
});
frontInstructions.push(...newIxs);
return tokenAccount;
}

return newTokenAccount;
} else if (!tokenAccount || (side === "out" && !ata.equals(tokenAccount) && !bypassAssociatedCheck)) {
frontInstructions.push(
Spl.makeCreateAssociatedTokenAccountInstruction({
Expand Down
55 changes: 53 additions & 2 deletions src/spl/spl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Token as _Token, u64 as _u64 } from "@solana/spl-token";
// @ts-ignore
import { Token as _Token, u64 as _u64, getAccount, createSyncNativeInstruction } from "@solana/spl-token";
import {
Commitment, Connection, Keypair, PublicKey, Signer, SystemProgram, TransactionInstruction,
Commitment,
Connection,
Keypair,
PublicKey,
Signer,
SystemProgram,
TransactionInstruction,
} from "@solana/web3.js";
import BN from "bn.js";

Expand Down Expand Up @@ -38,6 +45,50 @@ export class Spl {
);
}

// https://github.com/solana-labs/solana-program-library/blob/master/token/js/client/token.js
static async makeEnsureWrappedNativeAccountBalanceInstructions({
connection,
owner,
accountKey,
payer,
amount,
// baseRentExemption,
commitment,
}: {
connection: Connection;
owner: PublicKey;
accountKey: PublicKey;
payer: PublicKey;
amount: BigNumberish;
// baseRentExemption?: number;
commitment?: Commitment;
}): Promise<TransactionInstruction[]> {
const instructions: TransactionInstruction[] = [];

const balanceNeededForRent = await connection.getMinimumBalanceForRentExemption(
SPL_ACCOUNT_LAYOUT.span,
commitment,
);

const lamportsAlreadyPresent = (await getAccount(connection, accountKey)).lamports;
const lamportsToTransfer = parseBigNumberish(amount)
.add(new BN(balanceNeededForRent))
.sub(new BN(lamportsAlreadyPresent));

instructions.push(
SystemProgram.transfer({
fromPubkey: payer,
toPubkey: accountKey,
lamports: lamportsToTransfer.toNumber(),
programId: TOKEN_PROGRAM_ID,
}),
);

instructions.push(createSyncNativeInstruction(accountKey));

return instructions;
}

// https://github.com/solana-labs/solana-program-library/blob/master/token/js/client/token.js
static async makeCreateWrappedNativeAccountInstructions({
connection,
Expand Down