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

Fixed ammCreatePool #22

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RPC_URL=http://localhost:8899
PRIVATE_KEY="232, 110, 131, 203, 205, 245, 133, 66, 67, 122, 33, 235, 71, 188, 99, 250, 2, 155, 169, 2, 166, 129, 33, 36, 159, 75, 231, 194, 248, 24, 106, 4, 207, 80, 68, 108, 151, 56, 131, 112, 156, 25, 2, 148, 188, 157, 123, 151, 1, 161, 66, 51, 4, 181, 33, 172, 177, 82, 83, 159, 174, 89, 24, 35"
ENV=localnet
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
config.ts
js/
js/
.env
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ This project is for [RAYDIUM SDK](https://github.com/raydium-io/raydium-sdk) dem

this will install the dependencies for running the demo script

### Prerequisites
Modify `config.ts.template` to fit your configuration, and rename it to `config.ts`

- `<YOUR_WALLET_SECRET_KEY>`: replace to your own one
- `<YOUR_RPC_URL>`: replace to your prefer one

### Usage

- `yarn clean` clean up the old scripts (you don't need this for the very first time)
Expand Down
19 changes: 14 additions & 5 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'dotenv/config';

import {
ENDPOINT as _ENDPOINT,
Currency,
DEVNET_PROGRAM_ID,
LOOKUP_TABLE_CACHE,
MAINNET_PROGRAM_ID,
ProgramId,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Importing ProgramId ?

RAYDIUM_MAINNET,
Token,
TOKEN_PROGRAM_ID,
Expand All @@ -14,22 +18,27 @@ import {
PublicKey,
} from '@solana/web3.js';

export const rpcUrl: string = 'https://xxx.xxx.xxx/'
export const rpcUrl: string = process.env.RPC_URL!

export const rpcToken: string | undefined = undefined

export const wallet = Keypair.fromSecretKey(Buffer.from('<YOUR_WALLET_SECRET_KEY>'))
export const wallet = Keypair.fromSecretKey(new Uint8Array(process.env.PRIVATE_KEY!.split(",").map(Number)))

export const connection = new Connection(rpcUrl);

export const connection = new Connection('<YOUR_RPC_URL>');
export const PROGRAMIDS = process.env.ENV == "MAINNET" ? MAINNET_PROGRAM_ID: DEVNET_PROGRAM_ID;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export const connection = new Connection('');

export const PROGRAMIDS = MAINNET_PROGRAM_ID;
export const FEE_DESTINATION = process.env.ENV == "MAINNET"
? new PublicKey('7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5')
: new PublicKey('3XMrhbv989VxAMi3DErLV9eJht1pHppW5LbKxe9fkEFR');

export const ENDPOINT = _ENDPOINT;

export const RAYDIUM_MAINNET_API = RAYDIUM_MAINNET;

export const makeTxVersion = TxVersion.V0; // LEGACY

export const addLookupTableInfo = LOOKUP_TABLE_CACHE // only mainnet. other = undefined
export const addLookupTableInfo = process.env.ENV == "MAINNET" ? LOOKUP_TABLE_CACHE: undefined; // only mainnet. other = undefined

export const DEFAULT_TOKEN = {
'SOL': new Currency(9, 'USDC', 'USDC'),
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"dependencies": {
"@coral-xyz/anchor": "^0.29.0",
"@project-serum/serum": "^0.13.64",
"@raydium-io/raydium-sdk": "1.3.1-beta.50",
"@solana/spl-token": "^0.4.1",
"@solana/web3.js": "1.90.0",
Expand All @@ -8,6 +10,7 @@
"axios": "^1.6.7",
"bs58": "^5.0.0",
"decimal.js": "^10.4.3",
"dotenv": "^16.4.5",
"node-cron": "^3.0.3",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
Expand All @@ -20,6 +23,7 @@
"start": "node",
"build": "tsc",
"clean": "tsc --build --clean",
"amm-create-pool": "ts-node src/ammCreatePool.ts",
"ammv3-market": "ts-node src/ammV3MarketMaker/index.ts"
}
}
39 changes: 31 additions & 8 deletions src/ammCreatePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BN } from 'bn.js';

import {
Liquidity,
MAINNET_PROGRAM_ID,
Token,
} from '@raydium-io/raydium-sdk';
import {
Expand All @@ -12,16 +11,20 @@ import {

import {
connection,
DEFAULT_TOKEN,
FEE_DESTINATION,
makeTxVersion,
PROGRAMIDS,
wallet,
} from '../config';
import {
buildAndSendTx,
generateMint,
getWalletTokenAccount,
mintToAta,
} from './util';
import Decimal from 'decimal.js';
import { createMarket } from './utilsCreateMarket2';
import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet';

const ZERO = new BN(0)
type BN = typeof ZERO
Expand Down Expand Up @@ -68,18 +71,36 @@ async function ammCreatePool(input: TestTxInputInfo): Promise<{ txids: string[]
associatedOnly: false,
checkCreateATAOwner: true,
makeTxVersion,
feeDestinationId: new PublicKey('7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5'), // only mainnet use this
feeDestinationId: FEE_DESTINATION
})

return { txids: await buildAndSendTx(initPoolInstructionResponse.innerTransactions) }
}

async function howToUse() {
const baseToken = DEFAULT_TOKEN.USDC // USDC
const quoteToken = DEFAULT_TOKEN.RAY // RAY
const targetMarketId = Keypair.generate().publicKey
const addBaseAmount = new BN(10000)
const addQuoteAmount = new BN(10000)
const DECIMALS = 9;
const MINT_AUTHORITY = Keypair.generate();
const baseToken = await generateMint(MINT_AUTHORITY.publicKey, "MY_USDC", DECIMALS);
const quoteToken = await generateMint(MINT_AUTHORITY.publicKey, "MY_RAY", DECIMALS);
const marketKeyPair = Keypair.generate();
const targetMarketId = marketKeyPair.publicKey;

await createMarket({
connection,
wallet: new NodeWallet(wallet),
baseMint: baseToken.mint,
quoteMint: quoteToken.mint,
baseLotSize: 1,
quoteLotSize: 1,
dexProgram: PROGRAMIDS.OPENBOOK_MARKET,
market: marketKeyPair,
});

const addBaseAmount = new BN(10000 * Math.pow(10, DECIMALS))
const addQuoteAmount = new BN(10000 * Math.pow(10, DECIMALS))
await mintToAta(MINT_AUTHORITY, baseToken.mint, wallet.publicKey, addBaseAmount.toNumber());
await mintToAta(MINT_AUTHORITY, quoteToken.mint, wallet.publicKey, addBaseAmount.toNumber());

const startTime = Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7 // start from 7 days later
const walletTokenAccounts = await getWalletTokenAccount(connection, wallet.publicKey)

Expand All @@ -100,3 +121,5 @@ async function howToUse() {
console.log('txids', txids)
})
}

howToUse();
35 changes: 35 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
findProgramAddress,
InnerSimpleV0Transaction,
SPL_ACCOUNT_LAYOUT,
Token,
TOKEN_PROGRAM_ID,
TokenAccount,
} from '@raydium-io/raydium-sdk';
Expand All @@ -22,6 +23,7 @@ import {
makeTxVersion,
wallet,
} from '../config';
import { createMint, getOrCreateAssociatedTokenAccount, mintTo } from '@solana/spl-token';

export async function sendTx(
connection: Connection,
Expand Down Expand Up @@ -76,3 +78,36 @@ export async function sleepTime(ms: number) {
console.log((new Date()).toLocaleString(), 'sleepTime', ms)
return new Promise(resolve => setTimeout(resolve, ms))
}

export const generateMint = async (mintAuthority: PublicKey, label: string, decimals: number) : Promise<Token> => {
const mint = await createMint(connection,
wallet,
mintAuthority,
null,
decimals);
return new Token(TOKEN_PROGRAM_ID, mint, 6, label, label);
}

export const mintToAta = async (mintAuthority: Keypair, mint: PublicKey, to: PublicKey, amount: number) => {
const ata = await getOrCreateAssociatedTokenAccount(
connection,
wallet,
mint,
to,
false,
"processed",
undefined
);

await mintTo(connection,
wallet,
mint,
ata.address,
mintAuthority,
amount,
[],
{ skipPreflight: true }
);

return ata;
}
Loading