Change tuple to a struct, seemingly anchor ts doesnt support tuples

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-05-24 22:03:35 +02:00
parent e7d5b7cff9
commit ed4e63a82f
5 changed files with 51 additions and 9 deletions

View File

@ -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<MarginTradeWithdraw>,
cpi_data: Vec<u8>,
) -> 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.

View File

@ -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;

View File

@ -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<MarginTradeWithdraw>,
cpi_data: Vec<u8>,
) -> Result<()> {
instructions::margin_trade(ctx, num_health_accounts, withdraws, cpi_data)

View File

@ -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(),
};

View File

@ -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": {