From ed4e63a82f83af4cc7040c298c20cc42abec198f Mon Sep 17 00:00:00 2001 From: microwavedcola1 Date: Tue, 24 May 2022 22:03:35 +0200 Subject: [PATCH] Change tuple to a struct, seemingly anchor ts doesnt support tuples Signed-off-by: microwavedcola1 --- .../mango-v4/src/instructions/margin_trade.rs | 13 +++++-- programs/mango-v4/src/instructions/mod.rs | 2 +- programs/mango-v4/src/lib.rs | 2 +- .../tests/program_test/mango_client.rs | 7 ++-- ts/client/src/mango_v4.ts | 36 +++++++++++++++++-- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/programs/mango-v4/src/instructions/margin_trade.rs b/programs/mango-v4/src/instructions/margin_trade.rs index 24aac8820..f57045ae8 100644 --- a/programs/mango-v4/src/instructions/margin_trade.rs +++ b/programs/mango-v4/src/instructions/margin_trade.rs @@ -50,6 +50,12 @@ struct AllowedVault { loan_amount: I80F48, } +#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy)] +pub struct MarginTradeWithdraw { + pub index: u8, + pub amount: u64, +} + /// - `num_health_accounts` is the number of health accounts that remaining_accounts starts with. /// - `withdraws` is a list of tuples containing the index to a vault in target_accounts and the /// amount that the target program shall be allowed to withdraw @@ -57,7 +63,7 @@ struct AllowedVault { pub fn margin_trade<'key, 'accounts, 'remaining, 'info>( ctx: Context<'key, 'accounts, 'remaining, 'info, MarginTrade<'info>>, num_health_accounts: usize, - withdraws: Vec<(u8, u64)>, + withdraws: Vec, cpi_data: Vec, ) -> Result<()> { let group = ctx.accounts.group.load()?; @@ -155,8 +161,9 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>( if let Some(vault_info) = used_vaults.get_mut(&bank.vault) { let withdraw_amount = withdraws .iter() - .find_map(|&(index, amount)| { - (index as usize == vault_info.vault_cpi_ai_index).then(|| amount) + .find_map(|&withdraw| { + (withdraw.index as usize == vault_info.vault_cpi_ai_index) + .then(|| withdraw.amount) }) // Even if we don't withdraw from a vault we still need to track it: // Possibly the invoked program will deposit funds into it. diff --git a/programs/mango-v4/src/instructions/mod.rs b/programs/mango-v4/src/instructions/mod.rs index aa21e474e..9ff423538 100644 --- a/programs/mango-v4/src/instructions/mod.rs +++ b/programs/mango-v4/src/instructions/mod.rs @@ -32,7 +32,7 @@ mod create_group; mod create_stub_oracle; mod deposit; mod liq_token_with_token; -mod margin_trade; +pub mod margin_trade; mod perp_cancel_all_orders; mod perp_cancel_all_orders_by_side; mod perp_cancel_order; diff --git a/programs/mango-v4/src/lib.rs b/programs/mango-v4/src/lib.rs index bbd5cd257..6e0413a9a 100644 --- a/programs/mango-v4/src/lib.rs +++ b/programs/mango-v4/src/lib.rs @@ -100,7 +100,7 @@ pub mod mango_v4 { pub fn margin_trade<'key, 'accounts, 'remaining, 'info>( ctx: Context<'key, 'accounts, 'remaining, 'info, MarginTrade<'info>>, num_health_accounts: usize, - withdraws: Vec<(u8, u64)>, + withdraws: Vec, cpi_data: Vec, ) -> Result<()> { instructions::margin_trade(ctx, num_health_accounts, withdraws, cpi_data) diff --git a/programs/mango-v4/tests/program_test/mango_client.rs b/programs/mango-v4/tests/program_test/mango_client.rs index 5d49ffbc5..8187c2c05 100644 --- a/programs/mango-v4/tests/program_test/mango_client.rs +++ b/programs/mango-v4/tests/program_test/mango_client.rs @@ -6,7 +6,7 @@ use anchor_spl::token::{Token, TokenAccount}; use fixed::types::I80F48; use itertools::Itertools; use mango_v4::instructions::{ - InterestRateParams, Serum3OrderType, Serum3SelfTradeBehavior, Serum3Side, + InterestRateParams, MarginTradeWithdraw, Serum3OrderType, Serum3SelfTradeBehavior, Serum3Side, }; use solana_program::instruction::Instruction; use solana_sdk::instruction; @@ -295,7 +295,10 @@ impl<'keypair> ClientInstruction for MarginTradeInstruction<'keypair> { let instruction = Self::Instruction { num_health_accounts: health_check_metas.len(), - withdraws: vec![(1, self.withdraw_amount)], + withdraws: vec![MarginTradeWithdraw { + index: 1, + amount: self.withdraw_amount, + }], cpi_data: self.margin_trade_program_ix_cpi_data.clone(), }; diff --git a/ts/client/src/mango_v4.ts b/ts/client/src/mango_v4.ts index 1a07a6150..fe1502b76 100644 --- a/ts/client/src/mango_v4.ts +++ b/ts/client/src/mango_v4.ts @@ -530,7 +530,7 @@ export type MangoV4 = { "name": "withdraws", "type": { "vec": { - "defined": "(u8,u64)" + "defined": "MarginTradeWithdraw" } } }, @@ -2201,6 +2201,22 @@ export type MangoV4 = { } ], "types": [ + { + "name": "MarginTradeWithdraw", + "type": { + "kind": "struct", + "fields": [ + { + "name": "index", + "type": "u8" + }, + { + "name": "amount", + "type": "u64" + } + ] + } + }, { "name": "InterestRateParams", "type": { @@ -3387,7 +3403,7 @@ export const IDL: MangoV4 = { "name": "withdraws", "type": { "vec": { - "defined": "(u8,u64)" + "defined": "MarginTradeWithdraw" } } }, @@ -5058,6 +5074,22 @@ export const IDL: MangoV4 = { } ], "types": [ + { + "name": "MarginTradeWithdraw", + "type": { + "kind": "struct", + "fields": [ + { + "name": "index", + "type": "u8" + }, + { + "name": "amount", + "type": "u64" + } + ] + } + }, { "name": "InterestRateParams", "type": {