Skip to content

Commit

Permalink
Merge branch 'main' into fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptobrah authored Jun 18, 2024
2 parents 408d0ed + ea9a5ea commit 89368c4
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 111 deletions.
5 changes: 5 additions & 0 deletions .changeset/fixedexports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@degenfrends/solana-rugchecker': patch
---

## @degenfrends/solana-rugchecker: Initial version
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 20.x
cache: 'pnpm'
node-version: '20.x'

- run: pnpm install --frozen-lockfile
- run: pnpm run lint && pnpm run build
11 changes: 4 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ jobs:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
node-version: 20.x
- uses: pnpm/action-setup@v2
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
node-version: 20.x
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 20.x
cache: 'pnpm'
node-version: '20.x'

- run: pnpm install --frozen-lockfile
- name: Create Release Pull Request or Publish
Expand Down
2 changes: 2 additions & 0 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ var PumpFunTrader = class {
}
txBuilder.add(instruction);
const walletPrivateKey = await getKeyPairFromPrivateKey(privateKey);

const transaction = await createTransaction(this.connection, txBuilder.instructions, walletPrivateKey.publicKey, priorityFee);
if (isSimulation == false) {
const signature = await sendTransaction(this.connection, transaction, [
Expand Down Expand Up @@ -389,6 +390,7 @@ var PumpFunTrader = class {
});
return instruction;
}

};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';

declare const GLOBAL: PublicKey;
Expand All @@ -17,6 +18,7 @@ declare class PumpFunTrader {
buy(privateKey: string, tokenAddress: string, amount: number, priorityFee?: number, slippage?: number, isSimulation?: boolean): Promise<void>;
sell(privateKey: string, tokenAddress: string, tokenBalance: number, priorityFee?: number, slippage?: number, isSimulation?: boolean): Promise<void>;
getBuyInstruction(privateKey: string, tokenAddress: string, amount: number, slippage: number | undefined, txBuilder: Transaction): Promise<TransactionInstruction | undefined>;

}

export { ASSOC_TOKEN_ACC_PROG, FEE_RECIPIENT, GLOBAL, PUMP_FUN_ACCOUNT, PUMP_FUN_PROGRAM, RENT, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID, PumpFunTrader as default };
3 changes: 2 additions & 1 deletion dist/index.d.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PublicKey } from '@solana/web3.js';
import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';

declare const GLOBAL: PublicKey;
declare const FEE_RECIPIENT: PublicKey;
Expand All @@ -16,6 +16,7 @@ declare class PumpFunTrader {
setLogger(logger: any): this;
buy(privateKey: string, tokenAddress: string, amount: number, priorityFee?: number, slippage?: number, isSimulation?: boolean): Promise<void>;
sell(privateKey: string, tokenAddress: string, tokenBalance: number, priorityFee?: number, slippage?: number, isSimulation?: boolean): Promise<void>;
getBuyInstruction(privateKey: string, tokenAddress: string, amount: number, slippage: number | undefined, txBuilder: Transaction): Promise<TransactionInstruction | undefined>;
}

export { ASSOC_TOKEN_ACC_PROG, FEE_RECIPIENT, GLOBAL, PUMP_FUN_ACCOUNT, PUMP_FUN_PROGRAM, RENT, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID, PumpFunTrader as default };
4 changes: 3 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ var PumpFunTrader = class {
}
async buy(privateKey, tokenAddress, amount, priorityFee = 0, slippage = 0.25, isSimulation = true) {
try {

const txBuilder = new Transaction2();
const instruction = await this.getBuyInstruction(privateKey, tokenAddress, amount, slippage, txBuilder);
if (!instruction) {
Expand Down Expand Up @@ -267,14 +268,14 @@ var PumpFunTrader = class {
} else {
tokenAccount = tokenAccountAddress;
}

const solInLamports = amount * LAMPORTS_PER_SOL;
const tokenOut = Math.floor(solInLamports * coinData["virtual_token_reserves"] / coinData["virtual_sol_reserves"]);
const amountWithSlippage = amount * (1 + slippage);
const maxSolCost = Math.floor(amountWithSlippage * LAMPORTS_PER_SOL);
const ASSOCIATED_USER = tokenAccount;
const USER = owner;
const BONDING_CURVE = new PublicKey(coinData["bonding_curve"]);
const ASSOCIATED_BONDING_CURVE = new PublicKey(coinData["associated_bonding_curve"]);
const keys = [
{
pubkey: GLOBAL,
Expand Down Expand Up @@ -343,6 +344,7 @@ var PumpFunTrader = class {
bufferFromUInt64(maxSolCost)
]);
const instruction = new TransactionInstruction({

keys,
programId: PUMP_FUN_PROGRAM,
data
Expand Down
203 changes: 106 additions & 97 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ __name(bufferFromUInt64, "bufferFromUInt64");
import axios from "axios";
async function getCoinData(mintStr) {
try {
const url = `https://client-api-2-74b1891ee9f9.herokuapp.com/coins/${mintStr}`;
const url = `https://frontend-api.pump.fun/coins/${mintStr}`;
const response = await axios.get(url, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
Expand Down Expand Up @@ -119,105 +119,14 @@ var PumpFunTrader = class {
}
async buy(privateKey, tokenAddress, amount, priorityFee = 0, slippage = 0.25, isSimulation = true) {
try {
const coinData = await getCoinData(tokenAddress);
if (!coinData) {
this.logger.error("Failed to retrieve coin data...");
return;
}
const walletPrivateKey = await getKeyPairFromPrivateKey(privateKey);
const owner = walletPrivateKey.publicKey;
const token = new PublicKey(tokenAddress);
const txBuilder = new Transaction2();
const tokenAccountAddress = await getAssociatedTokenAddress(token, owner, false);
const tokenAccountInfo = await this.connection.getAccountInfo(tokenAccountAddress);
let tokenAccount;
if (!tokenAccountInfo) {
txBuilder.add(createAssociatedTokenAccountInstruction(walletPrivateKey.publicKey, tokenAccountAddress, walletPrivateKey.publicKey, token));
tokenAccount = tokenAccountAddress;
} else {
tokenAccount = tokenAccountAddress;
const instruction = await this.getBuyInstruction(privateKey, tokenAddress, amount, slippage, txBuilder);
if (!instruction) {
this.logger.error("Failed to retrieve buy instruction...");
return;
}
const solInLamports = amount * LAMPORTS_PER_SOL;
const tokenOut = Math.floor(solInLamports * coinData["virtual_token_reserves"] / coinData["virtual_sol_reserves"]);
const amountWithSlippage = amount * (1 + slippage);
const maxSolCost = Math.floor(amountWithSlippage * LAMPORTS_PER_SOL);
const ASSOCIATED_USER = tokenAccount;
const USER = owner;
const BONDING_CURVE = new PublicKey(coinData["bonding_curve"]);
const ASSOCIATED_BONDING_CURVE = new PublicKey(coinData["associated_bonding_curve"]);
const keys = [
{
pubkey: GLOBAL,
isSigner: false,
isWritable: false
},
{
pubkey: FEE_RECIPIENT,
isSigner: false,
isWritable: true
},
{
pubkey: token,
isSigner: false,
isWritable: false
},
{
pubkey: BONDING_CURVE,
isSigner: false,
isWritable: true
},
{
pubkey: ASSOCIATED_BONDING_CURVE,
isSigner: false,
isWritable: true
},
{
pubkey: ASSOCIATED_USER,
isSigner: false,
isWritable: true
},
{
pubkey: USER,
isSigner: false,
isWritable: true
},
{
pubkey: SYSTEM_PROGRAM_ID,
isSigner: false,
isWritable: false
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false
},
{
pubkey: RENT,
isSigner: false,
isWritable: false
},
{
pubkey: PUMP_FUN_ACCOUNT,
isSigner: false,
isWritable: false
},
{
pubkey: PUMP_FUN_PROGRAM,
isSigner: false,
isWritable: false
}
];
const data = Buffer.concat([
bufferFromUInt64("16927863322537952870"),
bufferFromUInt64(tokenOut),
bufferFromUInt64(maxSolCost)
]);
const instruction = new TransactionInstruction({
keys,
programId: PUMP_FUN_PROGRAM,
data
});
txBuilder.add(instruction);
const walletPrivateKey = await getKeyPairFromPrivateKey(privateKey);
const transaction = await createTransaction(this.connection, txBuilder.instructions, walletPrivateKey.publicKey, priorityFee);
if (isSimulation == false) {
const signature = await sendTransaction(this.connection, transaction, [
Expand Down Expand Up @@ -340,6 +249,106 @@ var PumpFunTrader = class {
this.logger.log(error);
}
}
async getBuyInstruction(privateKey, tokenAddress, amount, slippage = 0.25, txBuilder) {
const coinData = await getCoinData(tokenAddress);
if (!coinData) {
this.logger.error("Failed to retrieve coin data...");
return;
}
const walletPrivateKey = await getKeyPairFromPrivateKey(privateKey);
const owner = walletPrivateKey.publicKey;
const token = new PublicKey(tokenAddress);
const tokenAccountAddress = await getAssociatedTokenAddress(token, owner, false);
const tokenAccountInfo = await this.connection.getAccountInfo(tokenAccountAddress);
let tokenAccount;
if (!tokenAccountInfo) {
txBuilder.add(createAssociatedTokenAccountInstruction(walletPrivateKey.publicKey, tokenAccountAddress, walletPrivateKey.publicKey, token));
tokenAccount = tokenAccountAddress;
} else {
tokenAccount = tokenAccountAddress;
}
const solInLamports = amount * LAMPORTS_PER_SOL;
const tokenOut = Math.floor(solInLamports * coinData["virtual_token_reserves"] / coinData["virtual_sol_reserves"]);
const amountWithSlippage = amount * (1 + slippage);
const maxSolCost = Math.floor(amountWithSlippage * LAMPORTS_PER_SOL);
const ASSOCIATED_USER = tokenAccount;
const USER = owner;
const BONDING_CURVE = new PublicKey(coinData["bonding_curve"]);
const ASSOCIATED_BONDING_CURVE = new PublicKey(coinData["associated_bonding_curve"]);
const keys = [
{
pubkey: GLOBAL,
isSigner: false,
isWritable: false
},
{
pubkey: FEE_RECIPIENT,
isSigner: false,
isWritable: true
},
{
pubkey: token,
isSigner: false,
isWritable: false
},
{
pubkey: BONDING_CURVE,
isSigner: false,
isWritable: true
},
{
pubkey: ASSOCIATED_BONDING_CURVE,
isSigner: false,
isWritable: true
},
{
pubkey: ASSOCIATED_USER,
isSigner: false,
isWritable: true
},
{
pubkey: USER,
isSigner: false,
isWritable: true
},
{
pubkey: SYSTEM_PROGRAM_ID,
isSigner: false,
isWritable: false
},
{
pubkey: TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false
},
{
pubkey: RENT,
isSigner: false,
isWritable: false
},
{
pubkey: PUMP_FUN_ACCOUNT,
isSigner: false,
isWritable: false
},
{
pubkey: PUMP_FUN_PROGRAM,
isSigner: false,
isWritable: false
}
];
const data = Buffer.concat([
bufferFromUInt64("16927863322537952870"),
bufferFromUInt64(tokenOut),
bufferFromUInt64(maxSolCost)
]);
const instruction = new TransactionInstruction({
keys,
programId: PUMP_FUN_PROGRAM,
data
});
return instruction;
}
};
export {
ASSOC_TOKEN_ACC_PROG,
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.0.5",
"description": "Functions to buy and sell SPL tokens on pump.fun",
"main": "src/index.ts",
"type": "module",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"test": "jest --config jest.config.ts --detectOpenHandles --coverage --maxWorkers=1",
Expand Down

0 comments on commit 89368c4

Please sign in to comment.