Skip to content

Commit

Permalink
Fixes to client to make a read only version (#169)
Browse files Browse the repository at this point in the history
* Fixes to client to make a read only version

* fmt

* fix test
  • Loading branch information
brittcyr authored Oct 10, 2024
1 parent 9b236a5 commit 0695516
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
94 changes: 86 additions & 8 deletions client/ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export class ManifestClient {

private constructor(
public connection: Connection,
public wrapper: Wrapper,
public wrapper: Wrapper | null,
public market: Market,
private payer: PublicKey,
private payer: PublicKey | null,
private baseMint: Mint,
private quoteMint: Mint,
) {
Expand Down Expand Up @@ -335,8 +335,6 @@ export class ManifestClient {
return setupData;
}

// TODO: Make a version that does not require a trader with a wrapper for use
// in cases where we just want to see the orderbook.
/**
* Create a new client. throws if setup ixs are needed. Call ManifestClient.getSetupIxs to check if ixs are needed.
* This is the way to create a client without directly passing in `Keypair` types (for example when building a UI).
Expand Down Expand Up @@ -396,12 +394,47 @@ export class ManifestClient {
);
}

/**
* Create a new client that is read only. Cannot send transactions or generate instructions.
*
* @param connection Connection
* @param marketPk PublicKey of the market
*
* @returns ManifestClient
*/
public static async getClientReadOnly(
connection: Connection,
marketPk: PublicKey,
): Promise<ManifestClient> {
const marketObject: Market = await Market.loadFromAddress({
connection: connection,
address: marketPk,
});
const baseMintPk: PublicKey = marketObject.baseMint();
const quoteMintPk: PublicKey = marketObject.quoteMint();
const baseMint: Mint = await getMint(connection, baseMintPk);
const quoteMint: Mint = await getMint(connection, quoteMintPk);

return new ManifestClient(
connection,
null,
marketObject,
null,
baseMint,
quoteMint,
);
}

/**
* Reload the market and wrapper objects.
*/
public async reload(): Promise<void> {
await Promise.all([
this.wrapper.reload(this.connection),
() => {
if (this.wrapper) {
return this.wrapper.reload(this.connection);
}
},
this.market.reload(this.connection),
]);
}
Expand Down Expand Up @@ -450,6 +483,9 @@ export class ManifestClient {
mint: PublicKey,
amountTokens: number,
): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
const vault: PublicKey = getVaultAddress(this.market.address, mint);
const traderTokenAccount: PublicKey = getAssociatedTokenAddressSync(
mint,
Expand Down Expand Up @@ -497,6 +533,9 @@ export class ManifestClient {
mint: PublicKey,
amountTokens: number,
): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
const vault: PublicKey = getVaultAddress(this.market.address, mint);
const traderTokenAccount: PublicKey = getAssociatedTokenAddressSync(
mint,
Expand Down Expand Up @@ -536,6 +575,9 @@ export class ManifestClient {
* @returns TransactionInstruction[]
*/
public withdrawAllIx(): TransactionInstruction[] {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
const withdrawInstructions: TransactionInstruction[] = [];

const baseBalance = this.market.getWithdrawableBalanceTokens(
Expand Down Expand Up @@ -580,6 +622,9 @@ export class ManifestClient {
public placeOrderIx(
params: WrapperPlaceOrderParamsExternal,
): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
return createBatchUpdateInstruction(
{
market: this.market.address,
Expand Down Expand Up @@ -703,6 +748,9 @@ export class ManifestClient {
public cancelOrderIx(
params: WrapperCancelOrderParams,
): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
return createBatchUpdateInstruction(
{
market: this.market.address,
Expand Down Expand Up @@ -733,6 +781,9 @@ export class ManifestClient {
cancelParams: WrapperCancelOrderParams[],
cancelAll: boolean,
): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
return createBatchUpdateInstruction(
{
market: this.market.address,
Expand All @@ -758,6 +809,9 @@ export class ManifestClient {
* @returns TransactionInstruction
*/
public cancelAllIx(): TransactionInstruction {
if (!this.wrapper || !this.payer) {
throw new Error('Read only');
}
return createBatchUpdateInstruction(
{
market: this.market.address,
Expand Down Expand Up @@ -842,7 +896,31 @@ export class ManifestClient {
}

/**
* CreateGlobalAddTrader instruction. Adds a new trader to the global account
* CreateGlobal instruction. Creates the global account. Should only be called
* once ever for a mint. Static because it does not require a wrapper.
*
* @param payer PublicKey of the rent payer
* @param mint PublicKey of the globalMint
*
* @returns TransactionInstruction
*/
public static createGlobalIx(
payer: PublicKey,
mint: PublicKey,
): TransactionInstruction {
const global: PublicKey = getGlobalAddress(mint);
const globalVault: PublicKey = getGlobalVaultAddress(mint);
return createGlobalCreateInstruction({
payer,
global,
mint,
globalVault,
});
}

/**
* CreateGlobalAddTrader instruction. Adds a new trader to the global account.
* Static because it does not require a wrapper.
*
* @param payer PublicKey of the trader
* @param globalMint PublicKey of the globalMint
Expand All @@ -861,7 +939,7 @@ export class ManifestClient {
}

/**
* Global deposit instruction
* Global deposit instruction. Static because it does not require a wrapper.
*
* @param connection Connection to pull mint info
* @param payer PublicKey of the trader
Expand Down Expand Up @@ -905,7 +983,7 @@ export class ManifestClient {
}

/**
* Global withdraw instruction
* Global withdraw instruction. Static because it does not require a wrapper.
*
* @param connection Connection to pull mint info
* @param payer PublicKey of the trader
Expand Down
2 changes: 1 addition & 1 deletion client/ts/tests/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function testVolume(): Promise<void> {
// Test loading successfully.
const wrapper: Wrapper = await Wrapper.loadFromAddress({
connection,
address: client.wrapper.address,
address: client.wrapper!.address,
});
const marketInfoParsed: MarketInfoParsed =
wrapper.marketInfoForMarket(marketAddress)!;
Expand Down
2 changes: 1 addition & 1 deletion client/ts/tests/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function testWrapper(): Promise<void> {
// Test loading successfully.
const wrapper: Wrapper = await Wrapper.loadFromAddress({
connection,
address: client.wrapper.address,
address: client.wrapper!.address,
});

// Test loading fails on bad address
Expand Down

0 comments on commit 0695516

Please sign in to comment.