@koiswap/amm-sdk
v1.0.0
Published
Javascript SDK for amm trading on xdock
Readme
@koiswap/amm-sdk
Important
Never click any links in this repository or its issues that lead you away from GitHub. Do not run any code that others post without reading and understanding it first. This software is provided "as is" without warranty.
Overview
The @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.
Installation
npm i @koiswap/amm-sdk
Notice (for front-end developers)
If you encounter an error regarding the 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.
For example:
import { nodePolyfills } from 'vite-plugin-node-polyfills'
plugins: [
nodePolyfills({
include: ['crypto'],
}),
],
OR
resolve: {
alias: {
crypto: 'crypto-browserify',
}
},Some preview methods for calculations
// Buy: Fixed eth amount in, estimated token amount out
async getTokenAmountOut(
createTokenMetadata: CreateTokenMetadata,
buyAmountEth: bigint,
creatorFeeRate: number
): Promise<bigint>
// Buy: Fixed token amount out, estimated eth amount in
async getEthAmountIn(
token: string,
tokenAmountOut: bigint,
slippage = 500n
): Promise<bigint>
// Sell: Fixed token amount in, estimated eth amount out
async getEthAmountOut(
token: string,
tokenAmountIn: bigint,
slippage = 500n
): Promise<bigint>
// Sell: Fixed eth amount out, estimated token amount in
async getTokenAmountIn(
token: string,
ethAmountOut: bigint,
slippage = 500n
): Promise<bigint>
// Get token's detail
async getTokenInfo(token: string): Promise<TokenInfo>
// Get the token address that has been completed and is waiting to be migrated
async getToBeMigrateToken(totalMigrated: number): Promise<string>
// Get token's curve state progress
async getBondingCurveProgress(token: string): Promise<number>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.
npx ts-node example/basic/index.ts
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/<your api key>"
);
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/<your api key>"
);
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);
};Running the Examples
Basic Example
To run the example for creating, buying, and selling tokens, use the following command:
npx ts-node example/index.tsDisclaimer
This software is provided "as is" without any warranty, express or implied, including, but not limited to, the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claims, damages, or other liabilities, whether arising from a contract, tort, or any other legal theory in connection with the software or its use.
Use at your own risk. The authors take no responsibility for any harm or damage caused by the use of this software. Users are responsible for ensuring the suitability and safety of this software for their specific use cases.
By using this software, you acknowledge that you have read, understood, and agree to this disclaimer.
Feel free to customize it further to suit the specific context and requirements of your project.
By following this README, you should be able to install the Bet SDK, run the provided examples, and understand how to set up event listeners and perform token operations.
