From 7f254fe289089f88b46480cc0f05ba3cafc2ed3b Mon Sep 17 00:00:00 2001 From: Tyler Shipe Date: Mon, 22 Feb 2021 17:30:08 -0500 Subject: [PATCH] add fn to create settle borrow instructions --- src/instruction.ts | 52 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/instruction.ts b/src/instruction.ts index 2c6e0ba..46d34c5 100644 --- a/src/instruction.ts +++ b/src/instruction.ts @@ -1,3 +1,4 @@ +import * as BN from 'bn.js'; import { PublicKey, SYSVAR_CLOCK_PUBKEY, TransactionInstruction } from '@solana/web3.js'; import { Order } from '@project-serum/serum/lib/market'; import { encodeMangoInstruction } from './layout'; @@ -15,12 +16,12 @@ export function makeCancelOrderInstruction( openOrdersPk: PublicKey, signerKey: PublicKey, eventQueuePk: PublicKey, - order: Order + order: Order, ): TransactionInstruction { const keys = [ - { isSigner: false, isWritable: true, pubkey: mangoGroupPk}, - { isSigner: true, isWritable: false, pubkey: ownerPk }, - { isSigner: false, isWritable: true, pubkey: marginAccountPk }, + { 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 }, @@ -29,18 +30,17 @@ export function makeCancelOrderInstruction( { isSigner: false, isWritable: true, pubkey: openOrdersPk }, { isSigner: false, isWritable: false, pubkey: signerKey }, { isSigner: false, isWritable: true, pubkey: eventQueuePk }, - ] + ]; const data = encodeMangoInstruction({ CancelOrder: { side: order.side, orderId: order.orderId, - } - }) - return new TransactionInstruction( { keys, data, programId }) + }, + }); + return new TransactionInstruction({ keys, data, programId }); } - export function makeSettleFundsInstruction( programId: PublicKey, mangoGroupPk: PublicKey, @@ -57,9 +57,9 @@ export function makeSettleFundsInstruction( 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: 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 }, @@ -71,8 +71,28 @@ export function makeSettleFundsInstruction( { isSigner: false, isWritable: true, pubkey: mangoQuoteVaultPk }, { isSigner: false, isWritable: false, pubkey: dexSignerKey }, { isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID }, - ] - const data = encodeMangoInstruction( {SettleFunds: {}} ) + ]; + const data = encodeMangoInstruction({ SettleFunds: {} }); - return new TransactionInstruction( { keys, data, programId }) -} \ No newline at end of file + return new TransactionInstruction({ keys, data, programId }); +} + +export function makeSettleBorrowInstruction( + programId: PublicKey, + mangoGroupPk: PublicKey, + marginAccountPk: PublicKey, + walletPk: PublicKey, + tokenIndex: number, + quantity: number, +): TransactionInstruction { + const keys = [ + { isSigner: false, isWritable: true, pubkey: mangoGroupPk }, + { isSigner: false, isWritable: true, pubkey: marginAccountPk }, + { isSigner: true, isWritable: false, pubkey: walletPk }, + { isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, + ]; + const data = encodeMangoInstruction({ + SettleBorrow: { tokenIndex: new BN(tokenIndex), quantity }, + }); + return new TransactionInstruction({ keys, data, programId }); +}