@cks-systems/manifest-sdk
Version:
TypeScript SDK for Manifest
55 lines (54 loc) • 2.46 kB
JavaScript
import { Connection, Keypair, sendAndConfirmTransaction, Transaction, } from '@solana/web3.js';
import { ManifestClient } from '../src/client';
import { OrderType } from '../src/manifest/types';
import { createMarket } from './createMarket';
import { deposit } from './deposit';
import { Market } from '../src/market';
import { assert } from 'chai';
import { NO_EXPIRATION_LAST_VALID_SLOT } from '../src/constants';
async function testPlaceOrder() {
const connection = new Connection('http://127.0.0.1:8899', 'confirmed');
const payerKeypair = Keypair.generate();
const marketAddress = await createMarket(connection, payerKeypair);
const market = await Market.loadFromAddress({
connection,
address: marketAddress,
});
await deposit(connection, payerKeypair, marketAddress, market.baseMint(), 10);
await placeOrder(connection, payerKeypair, marketAddress, 5, 5, false, OrderType.Limit, 0);
await placeOrder(connection, payerKeypair, marketAddress, 3, 3, false, OrderType.Limit, 1);
await market.reload(connection);
market.prettyPrint();
// Asks are sorted worst to best.
assert(market.asks().length == 2, 'place asks did not work');
assert(Number(market.asks()[0].numBaseTokens) == 5, 'ask top of book wrong size');
assert(market.asks()[0].tokenPrice == 5, `ask top of book wrong price ${market.asks()[0].tokenPrice}`);
assert(market.bids().length == 0, 'place bids did not work');
}
export async function placeOrder(connection, payerKeypair, marketAddress, numBaseTokens, tokenPrice, isBid, orderType, clientOrderId, lastValidSlot = NO_EXPIRATION_LAST_VALID_SLOT, spreadBps = 0) {
const client = await ManifestClient.getClientForMarket(connection, marketAddress, payerKeypair);
const placeOrderIx = client.placeOrderIx(orderType == OrderType.Reverse
? {
numBaseTokens,
tokenPrice,
isBid,
spreadBps,
orderType,
clientOrderId,
}
: {
numBaseTokens,
tokenPrice,
isBid,
lastValidSlot,
orderType,
clientOrderId,
});
const signature = await sendAndConfirmTransaction(connection, new Transaction().add(placeOrderIx), [payerKeypair]);
console.log(`Placed order in ${signature}`);
}
describe('Place Order test', () => {
it('Place Order', async () => {
await testPlaceOrder();
});
});