From 9d2ec4283a228d8db219cb9061c3c54b590c2d6c Mon Sep 17 00:00:00 2001 From: Sayantan Karmakar Date: Wed, 22 Jun 2022 17:46:48 +0530 Subject: [PATCH] added getters, docs, tests, and isEqual for coin --- ts/README.md | 9 +++++ ts/src/coin.ts | 86 ++++++++++++++++++++++++++++++++++++++------ ts/src/dex.ts | 40 ++++++++++++++++----- ts/tests/dev.spec.ts | 5 +-- 4 files changed, 120 insertions(+), 20 deletions(-) diff --git a/ts/README.md b/ts/README.md index 41f8e19..3ee12d1 100644 --- a/ts/README.md +++ b/ts/README.md @@ -88,3 +88,12 @@ dex.runMarketMaker(market, owner, { quoteGeckoSymbol: "usd", }); ``` + +### Run a crank + +```javascript +dex.runCrank(market, owner, { + durationInSecs: 20, + verbose: true, +}); +``` diff --git a/ts/src/coin.ts b/ts/src/coin.ts index 962c056..788b94a 100644 --- a/ts/src/coin.ts +++ b/ts/src/coin.ts @@ -12,15 +12,15 @@ import { } from "@solana/web3.js"; export class Coin { - symbol: string; + private _symbol: string; - decimals: number; + private _decimals: number; - mint: PublicKey; + private _mint: PublicKey; - mintAuthority: Keypair; + private _mintAuthority: Keypair; - freezeAuthority: Keypair | null; + private _freezeAuthority: Keypair | null; constructor( symbol: string, @@ -29,13 +29,43 @@ export class Coin { mintAuthority: Keypair, freezeAuthority: Keypair | null, ) { - this.symbol = symbol; - this.decimals = decimals; - this.mint = mint; - this.mintAuthority = mintAuthority; - this.freezeAuthority = freezeAuthority; + this._symbol = symbol; + this._decimals = decimals; + this._mint = mint; + this._mintAuthority = mintAuthority; + this._freezeAuthority = freezeAuthority; } + public get symbol() { + return this._symbol; + } + + public get decimals() { + return this._decimals; + } + + public get mint() { + return this._mint; + } + + public get mintAuthority() { + return this._mintAuthority; + } + + public get freezeAuthority() { + return this._freezeAuthority; + } + + /** + * Load an exisiting mint as a Coin. + * + * @param connection The `Connection` object to connect to Solana. + * @param symbol The symbol to assign to the coin. + * @param mint The `PublicKey` of the Mint for the coin. + * @param mintAuthority The minting authority `Keypair` for the coin. + * @param freezeAuthority The optional freezing authority `Keypair` for the coin. + * @returns + */ static async load( connection: Connection, symbol: string, @@ -72,6 +102,42 @@ export class Coin { return new Coin(symbol, decimals, mint, mintAuthority, freezeAuthority); } + /** + * Equality check between two `Coin`s. + * + * @param to The `Coin` object to compare to. + * @returns + */ + public isEqual(to: Coin) { + const { mintAuthority, freezeAuthority } = to; + + if ( + mintAuthority.publicKey.toBase58() !== + this.mintAuthority.publicKey.toBase58() + ) { + return false; + } + + if (!!freezeAuthority !== !!this.freezeAuthority) { + return false; + } + + if ( + freezeAuthority && + this.freezeAuthority && + freezeAuthority.publicKey.toBase58() !== + this.freezeAuthority.publicKey.toBase58() + ) { + return false; + } + + return ( + to.symbol === this.symbol && + to.decimals === this.decimals && + to.mint.toBase58() === this.mint.toBase58() + ); + } + /** * Get the token balance for the specified owner. * diff --git a/ts/src/dex.ts b/ts/src/dex.ts index b428fd7..85a9f4f 100644 --- a/ts/src/dex.ts +++ b/ts/src/dex.ts @@ -50,19 +50,35 @@ export type CrankOpts = { * Dex is a wrapper class for a deployed Serum Dex program. */ export class Dex { - public address: PublicKey; + private _address: PublicKey; - coins: Coin[]; + private _coins: Coin[]; - markets: DexMarket[]; + private _markets: DexMarket[]; - connection: Connection; + private _connection: Connection; constructor(address: PublicKey, connection: Connection) { - this.address = address; - this.connection = connection; - this.coins = []; - this.markets = []; + this._address = address; + this._connection = connection; + this._coins = []; + this._markets = []; + } + + public get coins() { + return this._coins; + } + + public get markets() { + return this._markets; + } + + public get connection() { + return this._connection; + } + + public get address() { + return this._address; } /** @@ -301,6 +317,14 @@ export class Dex { return child; } + /** + * Runs a crank on a separate node process for the given `DexMarket` for specified duration. + * + * @param market The `DexMarket` to run a crank for + * @param owner The owner `FileKeypair` consuming events. + * @param opts The crank options used + * @returns + */ public runCrank( market: DexMarket, owner: FileKeypair, diff --git a/ts/tests/dev.spec.ts b/ts/tests/dev.spec.ts index 9302df0..48248f9 100644 --- a/ts/tests/dev.spec.ts +++ b/ts/tests/dev.spec.ts @@ -103,13 +103,14 @@ describe("Serum Dev Tools", () => { const loadedCoin = await Coin.load( connection, - "test-2", + "test", tempCoin.mint, owner.keypair, null, ); - assert.equal(tempCoin.decimals, loadedCoin.decimals); + assert.ok(tempCoin.isEqual(loadedCoin)); + assert.deepEqual(tempCoin, loadedCoin); }); it("invalid freeze authority while load coins", async () => {