Javascript SDK for amm trading on xdock
npm install @koiswap/amm-sdk@koiswap/amm-sdk is designed to interact with the xdock.meme decentralized application. It provides methods for creating, buying, and selling tokens on the xlayer blockchain. The SDK manages the necessary transactions and interactions with the xdock.meme program.
npm i @koiswap/amm-sdk
crypto module when building a front-end project with frameworks such as React, Vue, or Next.js, consider installing a polyfill (for example, the node-polyfills plugin) or adding a crypto-browserify alias to your build configuration.
import { nodePolyfills } from 'vite-plugin-node-polyfills'
plugins: [
nodePolyfills({
include: ['crypto'],
}),
],
OR
resolve: {
alias: {
crypto: 'crypto-browserify',
}
},
`
$3
`typescript
// Buy: Fixed eth amount in, estimated token amount out
async getTokenAmountOut(
createTokenMetadata: CreateTokenMetadata,
buyAmountEth: bigint,
creatorFeeRate: number
): Promise
// Buy: Fixed token amount out, estimated eth amount in
async getEthAmountIn(
token: string,
tokenAmountOut: bigint,
slippage = 500n
): Promise
// Sell: Fixed token amount in, estimated eth amount out
async getEthAmountOut(
token: string,
tokenAmountIn: bigint,
slippage = 500n
): Promise
// Sell: Fixed eth amount out, estimated token amount in
async getTokenAmountIn(
token: string,
ethAmountOut: bigint,
slippage = 500n
): Promise
// Get token's detail
async getTokenInfo(token: string): Promise
// Get the token address that has been completed and is waiting to be migrated
async getToBeMigrateToken(totalMigrated: number): Promise
// Get token's curve state progress
async getBondingCurveProgress(token: string): Promise
`
Usage Example
First, create a .env file and set your RPC URL as shown in the .env.example file.
Then, run the command below to generate a new account, and fund this account with at least 0.0001 OKB.
`typescript
import dotenv from "dotenv";
import fs from "fs";
import { ethers } from "ethers";
import { JsonRpcProvider } from "ethers";
import { Amm } from "../src/amm";
import { Wallet } from "ethers";
const SLIPPAGE_BASIS_POINTS = 1000n;
dotenv.config();
if (!process.env.RPC_URL) {
console.error("Please set RPC_URL in .env file");
console.error(
"Example: RPC_URL=https://blue-delicate-card.xlayer-mainnet.quiknode.pro/"
);
console.error("Get one at: https://www.quicknode.com");
throw new Error("RPC_URL in need")
}
const createAndBuy = async () => {
if (!process.env.RPC_URL) {
console.error("Please set RPC_URL in .env file");
console.error(
"Example: RPC_URL=https://blue-delicate-card.xlayer-mainnet.quiknode.pro/"
);
console.error("Get one at: https://www.quicknode.com");
return;
}
const provider = new JsonRpcProvider(process.env.RPC_URL || "");
let wallet = ethers.Wallet.createRandom(provider); //note this is not used
const sdk = new Amm(provider, new Wallet(wallet.privateKey));
let tokenMetadata = {
name: "TST",
symbol: "TST",
description: "TST: A test token",
file: new File([await fs.openAsBlob("example/test.jpg")], "test.jpg", { type: "image/jpeg" }),
migrateDex: "",
createdOn: "https://example.xdock.meme",
github: "https://github.com/githubusername",
githubVerify: false,
twitter: "https://example.x.com",
telegram: "https://example.t.me",
discord: "https://example.discord.com",
website: "https://www.example.com",
};
let createResults = await sdk.createTokenAndBuy(
tokenMetadata,
BigInt(0.0001 10 * 18),
5000,
);
console.log(createResults);
};
const buyExactIn = async (sdk: Amm, token: string) => {
const buyResults = await sdk.buyExactIn(
token,
BigInt(0.0001 10 * 18),
SLIPPAGE_BASIS_POINTS,
);
console.log(buyResults);
};
const buyExactOut = async (sdk: Amm, token: string) => {
const buyResults = await sdk.buyExactOut(
token,
BigInt(100 10 * 18),
SLIPPAGE_BASIS_POINTS,
);
console.log(buyResults);
};
const sellExactIn = async (sdk: Amm, token: string) => {
const sellResults = await sdk.sellExactIn(
token,
BigInt(100 10 * 18),
SLIPPAGE_BASIS_POINTS,
);
console.log(sellResults);
};
const sellExactOut = async (sdk: Amm, token: string) => {
const sellResults = await sdk.sellExactOut(
token,
BigInt(0.001 10 * 18),
SLIPPAGE_BASIS_POINTS,
);
console.log(sellResults);
};
`
$3
#### Basic Example
To run the example for creating, buying, and selling tokens, use the following command:
`bash
npx ts-node example/index.ts
``