added getters, docs, tests, and isEqual for coin

This commit is contained in:
Sayantan Karmakar 2022-06-22 17:46:48 +05:30
parent 6e1ff7e51b
commit 9d2ec4283a
4 changed files with 120 additions and 20 deletions

View File

@ -88,3 +88,12 @@ dex.runMarketMaker(market, owner, {
quoteGeckoSymbol: "usd", quoteGeckoSymbol: "usd",
}); });
``` ```
### Run a crank
```javascript
dex.runCrank(market, owner, {
durationInSecs: 20,
verbose: true,
});
```

View File

@ -12,15 +12,15 @@ import {
} from "@solana/web3.js"; } from "@solana/web3.js";
export class Coin { 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( constructor(
symbol: string, symbol: string,
@ -29,13 +29,43 @@ export class Coin {
mintAuthority: Keypair, mintAuthority: Keypair,
freezeAuthority: Keypair | null, freezeAuthority: Keypair | null,
) { ) {
this.symbol = symbol; this._symbol = symbol;
this.decimals = decimals; this._decimals = decimals;
this.mint = mint; this._mint = mint;
this.mintAuthority = mintAuthority; this._mintAuthority = mintAuthority;
this.freezeAuthority = freezeAuthority; 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( static async load(
connection: Connection, connection: Connection,
symbol: string, symbol: string,
@ -72,6 +102,42 @@ export class Coin {
return new Coin(symbol, decimals, mint, mintAuthority, freezeAuthority); 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. * Get the token balance for the specified owner.
* *

View File

@ -50,19 +50,35 @@ export type CrankOpts = {
* Dex is a wrapper class for a deployed Serum Dex program. * Dex is a wrapper class for a deployed Serum Dex program.
*/ */
export class Dex { 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) { constructor(address: PublicKey, connection: Connection) {
this.address = address; this._address = address;
this.connection = connection; this._connection = connection;
this.coins = []; this._coins = [];
this.markets = []; 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; 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( public runCrank(
market: DexMarket, market: DexMarket,
owner: FileKeypair, owner: FileKeypair,

View File

@ -103,13 +103,14 @@ describe("Serum Dev Tools", () => {
const loadedCoin = await Coin.load( const loadedCoin = await Coin.load(
connection, connection,
"test-2", "test",
tempCoin.mint, tempCoin.mint,
owner.keypair, owner.keypair,
null, null,
); );
assert.equal(tempCoin.decimals, loadedCoin.decimals); assert.ok(tempCoin.isEqual(loadedCoin));
assert.deepEqual(tempCoin, loadedCoin);
}); });
it("invalid freeze authority while load coins", async () => { it("invalid freeze authority while load coins", async () => {