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",
});
```
### Run a crank
```javascript
dex.runCrank(market, owner, {
durationInSecs: 20,
verbose: true,
});
```

View File

@ -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.
*

View File

@ -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,

View File

@ -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 () => {