finished adding all v3 functions + SRM fee tier support. Untested

This commit is contained in:
dd 2021-02-17 12:35:45 -05:00
parent 5ec31a740f
commit 3d88bd40c7
4 changed files with 454 additions and 381 deletions

View File

@ -7,7 +7,7 @@
"name": "Blockworks Foundation",
"email": "hello@blockworks.foundation",
"url": "https://blockworks.foundation"
},
},
"main": "lib/index.js",
"source": "src/index.js",
"types": "lib/index.d.ts",
@ -56,10 +56,10 @@
},
"dependencies": {
"@project-serum/serum": "^0.13.20",
"@project-serum/sol-wallet-adapter": "^0.1.4",
"@solana/web3.js": "^0.90.0",
"bn.js": "^5.1.2",
"buffer-layout": "^1.2.0",
"@project-serum/sol-wallet-adapter": "^0.1.4"
"buffer-layout": "^1.2.0"
},
"browserslist": [
">0.2%",

View File

@ -647,6 +647,39 @@ export class MangoClient {
return await this.sendTransaction(connection, transaction, owner, additionalSigners)
}
async withdrawSrm(
connection: Connection,
programId: PublicKey,
mangoGroup: MangoGroup,
marginAccount: MarginAccount,
owner: Account,
srmAccount: PublicKey,
quantity: number
): Promise<TransactionSignature> {
const nativeQuantity = uiToNative(quantity, SRM_DECIMALS)
const keys = [
{ isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey },
{ isSigner: false, isWritable: true, pubkey: marginAccount.publicKey },
{ isSigner: true, isWritable: false, pubkey: owner.publicKey },
{ isSigner: false, isWritable: true, pubkey: srmAccount },
{ isSigner: false, isWritable: true, pubkey: mangoGroup.srmVault },
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey },
{ isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID },
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }
]
const data = encodeMangoInstruction({WithdrawSrm: {quantity: nativeQuantity}})
const instruction = new TransactionInstruction( { keys, data, programId })
const transaction = new Transaction()
transaction.add(instruction)
const additionalSigners = []
return await this.sendTransaction(connection, transaction, owner, additionalSigners)
}
async placeOrder(
connection: Connection,
programId: PublicKey,
@ -668,8 +701,14 @@ export class MangoClient {
orderType = orderType == undefined ? 'limit' : orderType
// orderType = orderType ?? 'limit'
const limitPrice = spotMarket.priceNumberToLots(price)
const maxQuantity = spotMarket.baseSizeNumberToLots(size)
if (maxQuantity.lte(new BN(0))) {
const maxBaseQuantity = spotMarket.baseSizeNumberToLots(size)
// TODO verify if multiplying by highest fee tier is appropriate
const maxQuoteQuantity = new BN(spotMarket['_decoded'].quoteLotSize.toNumber()).mul(
spotMarket.baseSizeNumberToLots(size * 1.0022).mul(spotMarket.priceNumberToLots(price)),
)
if (maxBaseQuantity.lte(new BN(0))) {
throw new Error('size too small')
}
if (limitPrice.lte(new BN(0))) {
@ -712,23 +751,26 @@ export class MangoClient {
{ isSigner: false, isWritable: false, pubkey: spotMarket.programId },
{ isSigner: false, isWritable: true, pubkey: spotMarket.publicKey },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].requestQueue },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].eventQueue },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].bids },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].asks },
{ isSigner: false, isWritable: true, pubkey: mangoGroup.vaults[vaultIndex] },
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].baseVault },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].quoteVault },
{ isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID },
{ isSigner: false, isWritable: false, pubkey: SYSVAR_RENT_PUBKEY },
{ isSigner: false, isWritable: true, pubkey: mangoGroup.srmVault },
...openOrdersKeys.map( (pubkey) => ( { isSigner: false, isWritable: true, pubkey })),
...mangoGroup.oracles.map( (pubkey) => ( { isSigner: false, isWritable: false, pubkey })),
...mangoGroup.tokens.map( (pubkey) => ( { isSigner: false, isWritable: false, pubkey })),
]
const data = encodeMangoInstruction(
{
PlaceOrder:
clientId
? { side, limitPrice, maxQuantity, orderType, clientId, selfTradeBehavior }
: { side, limitPrice, maxQuantity, orderType, selfTradeBehavior }
? { side, limitPrice, maxBaseQuantity, maxQuoteQuantity, selfTradeBehavior, orderType, clientId}
: { side, limitPrice, maxBaseQuantity, maxQuoteQuantity, selfTradeBehavior, orderType}
}
)
@ -804,17 +846,17 @@ export class MangoClient {
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY },
{ isSigner: false, isWritable: false, pubkey: mangoGroup.dexProgramId },
{ isSigner: false, isWritable: true, pubkey: spotMarket.publicKey },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].bids },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].asks },
{ isSigner: false, isWritable: true, pubkey: order.openOrdersAddress },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].requestQueue },
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].eventQueue },
]
const data = encodeMangoInstruction({
CancelOrder: {
side: order.side,
orderId: order.orderId,
openOrders: order.openOrdersAddress,
openOrdersSlot: order.openOrdersSlot
}
})
@ -828,7 +870,6 @@ export class MangoClient {
return await this.sendTransaction(connection, transaction, owner, additionalSigners)
}
async getMangoGroup(
connection: Connection,
mangoGroupPk: PublicKey
@ -837,6 +878,7 @@ export class MangoClient {
const decoded = MangoGroupLayout.decode(acc == null ? undefined : acc.data);
return new MangoGroup(mangoGroupPk, decoded);
}
async getMarginAccount(
connection: Connection,
marginAccountPk: PublicKey
@ -845,7 +887,6 @@ export class MangoClient {
return new MarginAccount(marginAccountPk, MarginAccountLayout.decode(acc == null ? undefined : acc.data))
}
async getCompleteMarginAccount(
connection: Connection,
marginAccountPk: PublicKey,

View File

@ -1,4 +1,4 @@
import { bits, BitStructure, Blob, Layout, seq, struct, u32, u8, UInt, union } from 'buffer-layout';
import { bits, BitStructure, Blob, Layout, seq, struct, u32, u8, u16, UInt, union } from 'buffer-layout';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
@ -187,7 +187,7 @@ export function orderTypeLayout(property) {
}
export function selfTradeBehaviorLayout(property) {
return new EnumLayout({ decrementTake: 0, cancelProvide: 1 }, 4, property);
return new EnumLayout({ decrementTake: 0, cancelProvide: 1, abortTransaction: 2 }, 4, property);
}
export const MangoInstructionLayout = union(u32('instruction'))
@ -207,10 +207,12 @@ MangoInstructionLayout.addVariant(9,
[
sideLayout('side'),
u64('limitPrice'),
u64('maxQuantity'),
u64('maxBaseQuantity'),
u64('maxQuoteQuantity'),
selfTradeBehaviorLayout('selfTradeBehavior'),
orderTypeLayout('orderType'),
u64('clientId'),
selfTradeBehaviorLayout('selfTradeBehavior')
u16('limit'),
]
),
'PlaceOrder'
@ -221,9 +223,7 @@ MangoInstructionLayout.addVariant(11,
struct(
[
sideLayout('side'),
u128('orderId'),
publicKeyLayout('openOrders'),
u8('openOrdersSlot')
u128('orderId')
]
),
'CancelOrder'

754
yarn.lock

File diff suppressed because it is too large Load Diff