From c128a9a6762bb6ef24bb63fca122e46210495a71 Mon Sep 17 00:00:00 2001 From: dd Date: Fri, 19 Feb 2021 09:56:16 -0500 Subject: [PATCH] added makeSettleFundsInstruction as another example --- src/client.ts | 37 ++++++++++++++++--------------------- src/instruction.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/client.ts b/src/client.ts index c095d1a..ab595d8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -31,7 +31,7 @@ import { Market, OpenOrders, Orderbook } from '@project-serum/serum'; import { SRM_DECIMALS, TOKEN_PROGRAM_ID } from '@project-serum/serum/lib/token-instructions'; import { Order } from '@project-serum/serum/lib/market'; import Wallet from '@project-serum/sol-wallet-adapter'; -import { makeCancelOrderInstruction } from './instruction'; +import { makeCancelOrderInstruction, makeSettleFundsInstruction } from './instruction'; export class MangoGroup { @@ -778,27 +778,22 @@ export class MangoClient { spotMarket.programId ) - const keys = [ - { isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey}, - { isSigner: true, isWritable: false, pubkey: owner.publicKey }, - { isSigner: false, isWritable: true, pubkey: marginAccount.publicKey }, - { isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, - { isSigner: false, isWritable: false, pubkey: spotMarket.programId }, - { isSigner: false, isWritable: true, pubkey: spotMarket.publicKey }, - { isSigner: false, isWritable: true, pubkey: marginAccount.openOrders[marketIndex] }, - { 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: true, pubkey: mangoGroup.vaults[marketIndex] }, - { isSigner: false, isWritable: true, pubkey: mangoGroup.vaults[mangoGroup.vaults.length - 1] }, - { isSigner: false, isWritable: false, pubkey: dexSigner }, - { isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID }, - ] - const data = encodeMangoInstruction( {SettleFunds: {}} ) + const instruction = makeSettleFundsInstruction( + programId, + mangoGroup.publicKey, + owner.publicKey, + marginAccount.publicKey, + spotMarket.programId, + spotMarket.publicKey, + marginAccount.openOrders[marketIndex], + mangoGroup.signerKey, + spotMarket['_decoded'].baseVault, + spotMarket['_decoded'].quoteVault, + mangoGroup.vaults[marketIndex], + mangoGroup.vaults[mangoGroup.vaults.length - 1], + dexSigner + ) - const instruction = new TransactionInstruction( { keys, data, programId }) - - // Add all instructions to one atomic transaction const transaction = new Transaction() transaction.add(instruction) diff --git a/src/instruction.ts b/src/instruction.ts index ef74673..2c6e0ba 100644 --- a/src/instruction.ts +++ b/src/instruction.ts @@ -1,6 +1,7 @@ import { PublicKey, SYSVAR_CLOCK_PUBKEY, TransactionInstruction } from '@solana/web3.js'; import { Order } from '@project-serum/serum/lib/market'; import { encodeMangoInstruction } from './layout'; +import { TOKEN_PROGRAM_ID } from '@project-serum/serum/lib/token-instructions'; export function makeCancelOrderInstruction( programId: PublicKey, @@ -37,4 +38,41 @@ export function makeCancelOrderInstruction( } }) return new TransactionInstruction( { keys, data, programId }) +} + + +export function makeSettleFundsInstruction( + programId: PublicKey, + mangoGroupPk: PublicKey, + ownerPk: PublicKey, + marginAccountPk: PublicKey, + dexProgramId: PublicKey, + spotMarketPk: PublicKey, + openOrdersPk: PublicKey, + signerKey: PublicKey, + spotMarketBaseVaultPk: PublicKey, + spotMarketQuoteVaultPk: PublicKey, + mangoBaseVaultPk: PublicKey, + mangoQuoteVaultPk: PublicKey, + dexSignerKey: PublicKey, +): TransactionInstruction { + const keys = [ + { isSigner: false, isWritable: true, pubkey: mangoGroupPk}, + { isSigner: true, isWritable: false, pubkey: ownerPk }, + { isSigner: false, isWritable: true, pubkey: marginAccountPk }, + { isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, + { isSigner: false, isWritable: false, pubkey: dexProgramId }, + { isSigner: false, isWritable: true, pubkey: spotMarketPk }, + { isSigner: false, isWritable: true, pubkey: openOrdersPk }, + { isSigner: false, isWritable: false, pubkey: signerKey }, + { isSigner: false, isWritable: true, pubkey: spotMarketBaseVaultPk }, + { isSigner: false, isWritable: true, pubkey: spotMarketQuoteVaultPk }, + { isSigner: false, isWritable: true, pubkey: mangoBaseVaultPk }, + { isSigner: false, isWritable: true, pubkey: mangoQuoteVaultPk }, + { isSigner: false, isWritable: false, pubkey: dexSignerKey }, + { isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID }, + ] + const data = encodeMangoInstruction( {SettleFunds: {}} ) + + return new TransactionInstruction( { keys, data, programId }) } \ No newline at end of file