Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decimals to TOKEN_INFO, stronger return types on ix funcs, successful tests #4

Open
wants to merge 2 commits into
base: ts-sdk
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
3 changes: 2 additions & 1 deletion ts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
testWallet.json
testWallet.json
.env
15 changes: 10 additions & 5 deletions ts/src/constants/tokenConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,31 @@ export const TOKEN_INFO: TokenInfo[] = [
{
mint: "mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So",
name: "MSOL",
oracle: "E4v1BBgoso9s64TQvmyownAVJbhbEPGyzA3qn4n46qj9"
oracle: "E4v1BBgoso9s64TQvmyownAVJbhbEPGyzA3qn4n46qj9",
decimals: 9
},
{
mint: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
name: "BONK",
oracle: "8ihFLu5FimgTQ1Unh4dVyEHUGodJ5gJQCrQf4KUVB9bN"
oracle: "8ihFLu5FimgTQ1Unh4dVyEHUGodJ5gJQCrQf4KUVB9bN",
decimals: 5
},
{
mint: "So11111111111111111111111111111111111111112",
name: "WSOL",
oracle: "H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG"
oracle: "H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG",
decimals: 9
},
{
mint: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
name: "USDT",
oracle: "3vxLXJqLqF3JG5TCbYycbKWRBbCJQLxQmBGCkyqEEefL"
oracle: "3vxLXJqLqF3JG5TCbYycbKWRBbCJQLxQmBGCkyqEEefL",
decimals: 6
},
{
mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
name: "USDC",
oracle: "Gnt27xtC473ZT2Mw5u8wZ68Z3gULkSTb5DuxJy7eJotD"
oracle: "Gnt27xtC473ZT2Mw5u8wZ68Z3gULkSTb5DuxJy7eJotD",
decimals: 6
}
]
129 changes: 114 additions & 15 deletions ts/src/public/ssl.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { BN, Program } from "@project-serum/anchor";
import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js";
import { Connection, PublicKey, Signer, TransactionInstruction } from "@solana/web3.js";
import { findAssociatedTokenAddress, getPoolRegistry, getSSLProgram, getValidPairKey, getSslPoolSignerKey, getOraclePriceHistory, getOracleFromMint, getFeeDestination, getLiquidityAccountKey, wrapSOLIx, unwrapAllSOLIx } from "./utils";
import { NATIVE_MINT, TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { NATIVE_MINT, TOKEN_PROGRAM_ID, createAssociatedTokenAccountInstruction } from "@solana/spl-token";
import { assert } from "console";

export type SwapIxParams = {
tokenMintIn: PublicKey;
tokenMintOut: PublicKey;
amountIn: BN;
minAmountOut: BN;
minAmountOut?: BN;
};

export type CreateLiquidityAccountIxParams = {
tokenMint: PublicKey;
}

export type CloseLiquidityAccountIxParams = {
tokenMint: PublicKey;
}

export type DepositIxParams = {
tokenMint: PublicKey;
amountIn: BN;
userAta?: PublicKey;
useNativeSOL: boolean;
useNativeSOL?: boolean;
}

export type WithdrawIxParams = {
Expand All @@ -29,6 +33,32 @@ export type WithdrawIxParams = {
outNativeSOL?: boolean;
}

export type ActionResult = {
transactionInfos: {
ixs: TransactionInstruction[],
signers: Signer[]
preIxs: TransactionInstruction[],
}
};

export interface SwapIxResult extends ActionResult {
}

export interface CreateLiquidityAccountIxResult extends ActionResult {
liquidityAccountAddr: PublicKey;
}

export interface CloseLiquidityAccountIxResult extends ActionResult {
}

export interface DepositIxResult extends ActionResult {
liquidityAccountAddr: PublicKey;
}

export interface WithdrawIxResult extends ActionResult {
liquidityAccountAddr: PublicKey;
}

export class SSL {
connection: Connection;
program: Program;
Expand All @@ -46,16 +76,30 @@ export class SSL {
tokenMintOut,
amountIn,
minAmountOut
}: SwapIxParams): Promise<TransactionInstruction[]> {
}: SwapIxParams): Promise<SwapIxResult> {
if (!this.connection) throw new Error("SSL Not initialized");
const pair = getValidPairKey(
tokenMintIn,
tokenMintOut
);
if (!pair) throw new Error("Pair not supported");

let preInstructions = [];

const userAtaIn = findAssociatedTokenAddress(this.wallet, tokenMintIn)
const userAtaOut = findAssociatedTokenAddress(this.wallet, tokenMintOut)

if((await this.connection.getBalance(userAtaOut)) === 0) {
let userAtaOutInitIx = createAssociatedTokenAccountInstruction(
this.wallet,
userAtaOut,
this.wallet,
tokenMintOut
);

preInstructions.push(userAtaOutInitIx);
}

const sslPoolSignerIn = getSslPoolSignerKey(tokenMintIn)
const sslPoolSignerOut = getSslPoolSignerKey(tokenMintOut)

Expand Down Expand Up @@ -94,12 +138,20 @@ export class SSL {
tokenProgram: TOKEN_PROGRAM_ID
}
const minOutVal = minAmountOut ? minAmountOut : new BN(0)
return [(await this.program.methods.swap(amountIn, minOutVal).accounts(accounts).signers([]).instruction())]
let ix = await this.program.methods.swap(amountIn, minOutVal).accounts(accounts).signers([]).instruction();

return {
transactionInfos: {
ixs: [ix],
signers: [],
preIxs: preInstructions.length > 0 ? preInstructions : []
}
}
}

async createLiquidityAccountIx({
tokenMint
}: CreateLiquidityAccountIxParams): Promise<TransactionInstruction[]> {
}: CreateLiquidityAccountIxParams): Promise<CreateLiquidityAccountIxResult> {
const poolRegistry = getPoolRegistry();

const liquidityAc = await getLiquidityAccountKey(this.wallet, tokenMint);
Expand All @@ -111,15 +163,48 @@ export class SSL {
owner: this.wallet
}

return [(await this.program.methods.createLiquidityAccountIx().accounts(accounts).instruction())];
let ixs = await this.program.methods.createLiquidityAccount().accounts(accounts).instruction();

return {
transactionInfos: {
ixs: [ixs],
preIxs: [],
signers: []
},
liquidityAccountAddr: liquidityAc
};
}

async closeLiquidityAccountIx({
tokenMint
}: CloseLiquidityAccountIxParams): Promise<CloseLiquidityAccountIxResult> {
const poolRegistry = getPoolRegistry();

const liquidityAc = await getLiquidityAccountKey(this.wallet, tokenMint);

const accounts = {
liquidityAccount: liquidityAc,
owner: this.wallet,
rentRecipient: this.wallet,
}

let ixs = await this.program.methods.closeLiquidityAccount().accounts(accounts).instruction();

return {
transactionInfos: {
ixs: [ixs],
preIxs: [],
signers: []
},
};
}

async depositIx({
tokenMint,
amountIn,
userAta,
useNativeSOL
}: DepositIxParams): Promise<TransactionInstruction[]> {
}: DepositIxParams): Promise<DepositIxResult> {

let ixs: TransactionInstruction[] = [];

Expand All @@ -138,7 +223,7 @@ export class SSL {

if((await this.connection.getBalance(liquidityAc)) === 0) {
const createLiquidityAccountIx = (await this.createLiquidityAccountIx({tokenMint}));
ixs.push(...createLiquidityAccountIx);
ixs.push(...createLiquidityAccountIx.transactionInfos.ixs);
}

const poolVaultAc = findAssociatedTokenAddress(sslPoolSigner, tokenMint);
Expand All @@ -156,18 +241,25 @@ export class SSL {
tokenProgram: TOKEN_PROGRAM_ID
};

const depositIx = (await this.program.methods.depositIx(amountIn).accounts(accounts).signers([]).instruction());
const depositIx = (await this.program.methods.deposit(amountIn).accounts(accounts).signers([]).instruction());
ixs.push(depositIx);

return ixs;
return {
transactionInfos: {
ixs: ixs,
preIxs: [],
signers:[]
},
liquidityAccountAddr: liquidityAc
};
}

async withdrawIx({
tokenMint,
amountIn,
userAta,
outNativeSOL
}: WithdrawIxParams): Promise<TransactionInstruction[]> {
}: WithdrawIxParams): Promise<WithdrawIxResult> {
let ixs: TransactionInstruction[] = [];

const poolRegistry = getPoolRegistry();
Expand Down Expand Up @@ -195,7 +287,7 @@ export class SSL {
tokenProgram: TOKEN_PROGRAM_ID
};

let withdrawIx = await this.program.methods.withdrawIx(amountIn).accounts(accounts).signers([]).instruction();
let withdrawIx = await this.program.methods.withdraw(amountIn).accounts(accounts).signers([]).instruction();

ixs.push(withdrawIx);

Expand All @@ -205,6 +297,13 @@ export class SSL {
);
}

return ixs;
return {
transactionInfos: {
ixs: ixs,
preIxs: [],
signers:[]
},
liquidityAccountAddr: liquidityAc
};
}
}
24 changes: 23 additions & 1 deletion ts/src/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ export const getMintFromName = (name: string) => {
return null
}

export const getDecimalsFromMint = (mint: string) => {
for (let i=0 ; i < TOKEN_INFO.length; i++){
const item = TOKEN_INFO[i]
if (item.mint === mint)
return item.decimals
}
return 0
}

export const getDecimalsFromName = (name: string) => {
for (let i=0 ; i < TOKEN_INFO.length; i++){
const item = TOKEN_INFO[i]
if (item.name === name)
return item.decimals
}
return 0
}

export const getLiquidityAccountKey = async (
walletKey: PublicKey,
mint: PublicKey
Expand Down Expand Up @@ -238,4 +256,8 @@ export const unwrapAllSOLIx = (
);

return closeWrappedSOLAtaIx;
}
}

export const delay = (time) => {
return new Promise((resolve) => setTimeout(resolve, time));
};
1 change: 1 addition & 0 deletions ts/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type TokenInfo = {
mint: string,
name: string,
oracle: string
decimals: number
}
30 changes: 30 additions & 0 deletions ts/test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading