added docs for Dex
This commit is contained in:
parent
51d58dc9c3
commit
353f8787a0
|
@ -14,13 +14,27 @@ import { Coin } from "./coin";
|
||||||
import { ChildProcess, fork } from "child_process";
|
import { ChildProcess, fork } from "child_process";
|
||||||
import { FileKeypair } from "./fileKeypair";
|
import { FileKeypair } from "./fileKeypair";
|
||||||
|
|
||||||
export type MarketArgs = {
|
/**
|
||||||
|
* @param lotSize
|
||||||
|
* @param tickSize
|
||||||
|
* @param feeRate
|
||||||
|
* @param quoteDustThreshold
|
||||||
|
*/
|
||||||
|
export type MarketParams = {
|
||||||
lotSize: number;
|
lotSize: number;
|
||||||
tickSize: number;
|
tickSize: number;
|
||||||
feeRate: number;
|
feeRate: number;
|
||||||
quoteDustThreshold: BN;
|
quoteDustThreshold: BN;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param unref
|
||||||
|
* @param durationInSecs
|
||||||
|
* @param orderCount
|
||||||
|
* @param initialBidSize
|
||||||
|
* @param baseGeckoSymbol
|
||||||
|
* @param quoteGeckoSymbol
|
||||||
|
*/
|
||||||
type MarketMakerOpts = {
|
type MarketMakerOpts = {
|
||||||
unref: boolean;
|
unref: boolean;
|
||||||
durationInSecs: number;
|
durationInSecs: number;
|
||||||
|
@ -49,6 +63,16 @@ export class Dex {
|
||||||
this.markets = [];
|
this.markets = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a `Coin` object to be associated with the `Dex`.
|
||||||
|
*
|
||||||
|
* @param symbol The symbol of the coin to create
|
||||||
|
* @param decimals The decimals of the coin to create
|
||||||
|
* @param payer The payer `Keypair` to use for the transactions
|
||||||
|
* @param mintAuthority The mint authority `Keypair` to use for the mint
|
||||||
|
* @param freezeAuthority The freeze authority `Keypair` to use for the mint
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public createCoin = async (
|
public createCoin = async (
|
||||||
symbol: string,
|
symbol: string,
|
||||||
decimals: number,
|
decimals: number,
|
||||||
|
@ -77,12 +101,25 @@ export class Dex {
|
||||||
return coin;
|
return coin;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch one of the `Coin` objects associated with the `Dex` by symbol.
|
||||||
|
*
|
||||||
|
* @param symbol The symbol of the coin to fetch
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public getCoin(symbol: string): Coin | null {
|
public getCoin(symbol: string): Coin | null {
|
||||||
const coin = this.coins.find((coin) => coin.symbol === symbol);
|
const coin = this.coins.find((coin) => coin.symbol === symbol);
|
||||||
|
|
||||||
return coin ? coin : null;
|
return coin ? coin : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a `DexMarket` object associated with the `Dex` by the base coin and quote coin.
|
||||||
|
*
|
||||||
|
* @param baseCoin The base `Coin` of the market to fetch
|
||||||
|
* @param quoteCoin The quote `Coin` of the market to fetch
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public getMarket(baseCoin: Coin, quoteCoin: Coin): DexMarket | null {
|
public getMarket(baseCoin: Coin, quoteCoin: Coin): DexMarket | null {
|
||||||
const dexMarket = this.markets.find(
|
const dexMarket = this.markets.find(
|
||||||
(market) =>
|
(market) =>
|
||||||
|
@ -92,11 +129,20 @@ export class Dex {
|
||||||
return dexMarket ? dexMarket : null;
|
return dexMarket ? dexMarket : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a `DexMarket` instance associated with the `Dex`.
|
||||||
|
*
|
||||||
|
* @param payer The payer `Keypair` to use for the transactions
|
||||||
|
* @param baseCoin The base `Coin` of the market to create
|
||||||
|
* @param quoteCoin The quote `Coin` of the market to create
|
||||||
|
* @param marketParams The parameters required to create the market
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public async initDexMarket(
|
public async initDexMarket(
|
||||||
payer: Keypair,
|
payer: Keypair,
|
||||||
baseCoin: Coin,
|
baseCoin: Coin,
|
||||||
quoteCoin: Coin,
|
quoteCoin: Coin,
|
||||||
marketArgs: MarketArgs,
|
marketParams: MarketParams,
|
||||||
): Promise<DexMarket> {
|
): Promise<DexMarket> {
|
||||||
if (this.getMarket(baseCoin, quoteCoin) != null) {
|
if (this.getMarket(baseCoin, quoteCoin) != null) {
|
||||||
throw new Error("Market already exists");
|
throw new Error("Market already exists");
|
||||||
|
@ -136,10 +182,10 @@ export class Dex {
|
||||||
|
|
||||||
let baseLotSize;
|
let baseLotSize;
|
||||||
let quoteLotSize;
|
let quoteLotSize;
|
||||||
if (marketArgs.lotSize > 0) {
|
if (marketParams.lotSize > 0) {
|
||||||
baseLotSize = Math.round(10 ** baseCoin.decimals * marketArgs.lotSize);
|
baseLotSize = Math.round(10 ** baseCoin.decimals * marketParams.lotSize);
|
||||||
quoteLotSize = Math.round(
|
quoteLotSize = Math.round(
|
||||||
10 ** quoteCoin.decimals * marketArgs.lotSize * marketArgs.tickSize,
|
10 ** quoteCoin.decimals * marketParams.lotSize * marketParams.tickSize,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Invalid Lot Size");
|
throw new Error("Invalid Lot Size");
|
||||||
|
@ -164,8 +210,8 @@ export class Dex {
|
||||||
quoteMint: quoteCoin.mint,
|
quoteMint: quoteCoin.mint,
|
||||||
baseLotSize: new BN(baseLotSize),
|
baseLotSize: new BN(baseLotSize),
|
||||||
quoteLotSize: new BN(quoteLotSize),
|
quoteLotSize: new BN(quoteLotSize),
|
||||||
feeRateBps: marketArgs.feeRate,
|
feeRateBps: marketParams.feeRate,
|
||||||
quoteDustThreshold: marketArgs.quoteDustThreshold,
|
quoteDustThreshold: marketParams.quoteDustThreshold,
|
||||||
vaultSignerNonce: vaultOwnerNonce,
|
vaultSignerNonce: vaultOwnerNonce,
|
||||||
programId: this.address,
|
programId: this.address,
|
||||||
});
|
});
|
||||||
|
@ -196,7 +242,14 @@ export class Dex {
|
||||||
return dexMarket;
|
return dexMarket;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
/**
|
||||||
|
* Runs a Market Making on a separate node process for `durationInSecs` seconds.
|
||||||
|
*
|
||||||
|
* @param market The `DexMarket` to run market maker on
|
||||||
|
* @param owner The owner `Keypair` to use for the market making.
|
||||||
|
* @param opts The market making options used.
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public runMarketMaker(
|
public runMarketMaker(
|
||||||
market: DexMarket,
|
market: DexMarket,
|
||||||
owner: FileKeypair,
|
owner: FileKeypair,
|
||||||
|
@ -205,10 +258,6 @@ export class Dex {
|
||||||
const child = fork("./src/scripts/marketMaker", null, {
|
const child = fork("./src/scripts/marketMaker", null, {
|
||||||
detached: true,
|
detached: true,
|
||||||
stdio: ["pipe", 0, 0, "ipc"],
|
stdio: ["pipe", 0, 0, "ipc"],
|
||||||
env: {
|
|
||||||
size: "1",
|
|
||||||
price: "10",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://nodejs.org/api/child_process.html#optionsdetached
|
// https://nodejs.org/api/child_process.html#optionsdetached
|
||||||
|
|
Loading…
Reference in New Issue