Rename MarginTrade -> FlashLoan

This commit is contained in:
Christian Kamm 2022-06-23 10:19:33 +02:00
parent d8a6a29e70
commit c8ebc1f611
8 changed files with 34 additions and 34 deletions

View File

@ -13,7 +13,7 @@ pub enum MangoError {
#[msg("")] #[msg("")]
UnknownOracleType, UnknownOracleType,
#[msg("")] #[msg("")]
InvalidMarginTradeTargetCpiProgram, InvalidFlashLoanTargetCpiProgram,
#[msg("")] #[msg("")]
HealthMustBePositive, HealthMustBePositive,
#[msg("The account is bankrupt")] #[msg("The account is bankrupt")]

View File

@ -9,7 +9,7 @@ use solana_program::instruction::Instruction;
use std::cell::Ref; use std::cell::Ref;
use std::collections::HashMap; use std::collections::HashMap;
/// The margin trade instruction /// The flash loan instruction
/// ///
/// In addition to these accounts, there must be a sequence of remaining_accounts: /// In addition to these accounts, there must be a sequence of remaining_accounts:
/// 1. health_accounts: accounts needed for health checking /// 1. health_accounts: accounts needed for health checking
@ -22,7 +22,7 @@ use std::collections::HashMap;
/// Every vault that is to be withdrawn from must appear in the `withdraws` instruction argument. /// Every vault that is to be withdrawn from must appear in the `withdraws` instruction argument.
/// The corresponding bank may be used as an authority for vault withdrawals. /// The corresponding bank may be used as an authority for vault withdrawals.
#[derive(Accounts)] #[derive(Accounts)]
pub struct MarginTrade<'info> { pub struct FlashLoan<'info> {
pub group: AccountLoader<'info, Group>, pub group: AccountLoader<'info, Group>,
#[account( #[account(
@ -52,7 +52,7 @@ struct AllowedVault {
} }
#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy)] #[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy)]
pub struct MarginTradeWithdraw { pub struct FlashLoanWithdraw {
/// Account index of the vault to withdraw from in the target_accounts section. /// Account index of the vault to withdraw from in the target_accounts section.
/// Meaning that the first account after target_program_id would have index 0. /// Meaning that the first account after target_program_id would have index 0.
pub index: u8, pub index: u8,
@ -66,12 +66,12 @@ pub struct CpiData {
pub data: Vec<u8>, pub data: Vec<u8>,
} }
/// - `withdraws` is a list of MarginTradeWithdraw requests. /// - `withdraws` is a list of FlashLoanWithdraw requests.
/// - `cpi_datas` is a list of bytes per cpi to call the target_program_id with. /// - `cpi_datas` is a list of bytes per cpi to call the target_program_id with.
/// - `cpi_account_starts` is a list of index into the remaining accounts per cpi to call the target_program_id with. /// - `cpi_account_starts` is a list of index into the remaining accounts per cpi to call the target_program_id with.
pub fn margin_trade<'key, 'accounts, 'remaining, 'info>( pub fn flash_loan<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, MarginTrade<'info>>, ctx: Context<'key, 'accounts, 'remaining, 'info, FlashLoan<'info>>,
withdraws: Vec<MarginTradeWithdraw>, withdraws: Vec<FlashLoanWithdraw>,
cpi_datas: Vec<CpiData>, cpi_datas: Vec<CpiData>,
) -> Result<()> { ) -> Result<()> {
require!(!cpi_datas.is_empty(), MangoError::SomeError); require!(!cpi_datas.is_empty(), MangoError::SomeError);

View File

@ -120,12 +120,12 @@ pub mod mango_v4 {
instructions::token_withdraw(ctx, amount, allow_borrow) instructions::token_withdraw(ctx, amount, allow_borrow)
} }
pub fn margin_trade<'key, 'accounts, 'remaining, 'info>( pub fn flash_loan<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, MarginTrade<'info>>, ctx: Context<'key, 'accounts, 'remaining, 'info, FlashLoan<'info>>,
withdraws: Vec<MarginTradeWithdraw>, withdraws: Vec<FlashLoanWithdraw>,
cpi_datas: Vec<CpiData>, cpi_datas: Vec<CpiData>,
) -> Result<()> { ) -> Result<()> {
instructions::margin_trade(ctx, withdraws, cpi_datas) instructions::flash_loan(ctx, withdraws, cpi_datas)
} }
/// ///

View File

@ -6,7 +6,7 @@ use anchor_spl::token::{Token, TokenAccount};
use fixed::types::I80F48; use fixed::types::I80F48;
use itertools::Itertools; use itertools::Itertools;
use mango_v4::instructions::{ use mango_v4::instructions::{
CpiData, InterestRateParams, MarginTradeWithdraw, Serum3OrderType, Serum3SelfTradeBehavior, CpiData, InterestRateParams, FlashLoanWithdraw, Serum3OrderType, Serum3SelfTradeBehavior,
Serum3Side, Serum3Side,
}; };
use solana_program::instruction::Instruction; use solana_program::instruction::Instruction;
@ -279,7 +279,7 @@ pub async fn account_position_f64(solana: &SolanaCookie, account: Pubkey, bank:
// ClientInstruction impl // ClientInstruction impl
// //
pub struct MarginTradeInstruction<'keypair> { pub struct FlashLoanInstruction<'keypair> {
pub account: Pubkey, pub account: Pubkey,
pub owner: &'keypair Keypair, pub owner: &'keypair Keypair,
pub mango_token_bank: Pubkey, pub mango_token_bank: Pubkey,
@ -291,9 +291,9 @@ pub struct MarginTradeInstruction<'keypair> {
pub margin_trade_program_ix_cpi_data: Vec<u8>, pub margin_trade_program_ix_cpi_data: Vec<u8>,
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl<'keypair> ClientInstruction for MarginTradeInstruction<'keypair> { impl<'keypair> ClientInstruction for FlashLoanInstruction<'keypair> {
type Accounts = mango_v4::accounts::MarginTrade; type Accounts = mango_v4::accounts::FlashLoan;
type Instruction = mango_v4::instruction::MarginTrade; type Instruction = mango_v4::instruction::FlashLoan;
async fn to_instruction( async fn to_instruction(
&self, &self,
account_loader: impl ClientAccountLoader + 'async_trait, account_loader: impl ClientAccountLoader + 'async_trait,
@ -319,7 +319,7 @@ impl<'keypair> ClientInstruction for MarginTradeInstruction<'keypair> {
.await; .await;
let instruction = Self::Instruction { let instruction = Self::Instruction {
withdraws: vec![MarginTradeWithdraw { withdraws: vec![FlashLoanWithdraw {
index: 1, index: 1,
amount: self.withdraw_amount, amount: self.withdraw_amount,
}], }],

View File

@ -143,7 +143,7 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
{ {
send_tx( send_tx(
solana, solana,
MarginTradeInstruction { FlashLoanInstruction {
account, account,
owner, owner,
mango_token_bank: bank, mango_token_bank: bank,
@ -193,7 +193,7 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
{ {
send_tx( send_tx(
solana, solana,
MarginTradeInstruction { FlashLoanInstruction {
account, account,
owner, owner,
mango_token_bank: bank, mango_token_bank: bank,
@ -237,7 +237,7 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
{ {
send_tx( send_tx(
solana, solana,
MarginTradeInstruction { FlashLoanInstruction {
account, account,
owner, owner,
mango_token_bank: bank, mango_token_bank: bank,
@ -283,7 +283,7 @@ async fn test_margin_trade() -> Result<(), BanksClientError> {
{ {
send_tx( send_tx(
solana, solana,
MarginTradeInstruction { FlashLoanInstruction {
account, account,
owner, owner,
mango_token_bank: bank, mango_token_bank: bank,

View File

@ -41,7 +41,7 @@ import {
ORCA_TOKEN_SWAP_ID_DEVNET, ORCA_TOKEN_SWAP_ID_DEVNET,
} from './integrations/orca/index'; } from './integrations/orca/index';
import { IDL, MangoV4 } from './mango_v4'; import { IDL, MangoV4 } from './mango_v4';
import { MarginTradeWithdraw } from './types'; import { FlashLoanWithdraw } from './types';
import { import {
getAssociatedTokenAddress, getAssociatedTokenAddress,
I64_MAX_BN, I64_MAX_BN,
@ -1146,13 +1146,13 @@ export class MangoClient {
); );
const targetRemainingAccounts = instruction.keys; const targetRemainingAccounts = instruction.keys;
const withdraws: MarginTradeWithdraw[] = [ const withdraws: FlashLoanWithdraw[] = [
{ index: 3, amount: toU64(amountIn, 9) }, { index: 3, amount: toU64(amountIn, 9) },
]; ];
const cpiData = instruction.data; const cpiData = instruction.data;
return await this.program.methods return await this.program.methods
.marginTrade(withdraws, [ .flashLoan(withdraws, [
{ accountStart: new BN(parsedHealthAccounts.length), data: cpiData }, { accountStart: new BN(parsedHealthAccounts.length), data: cpiData },
]) ])
.accounts({ .accounts({

View File

@ -616,7 +616,7 @@ export type MangoV4 = {
] ]
}, },
{ {
"name": "marginTrade", "name": "flashLoan",
"accounts": [ "accounts": [
{ {
"name": "group", "name": "group",
@ -644,7 +644,7 @@ export type MangoV4 = {
"name": "withdraws", "name": "withdraws",
"type": { "type": {
"vec": { "vec": {
"defined": "MarginTradeWithdraw" "defined": "FlashLoanWithdraw"
} }
} }
}, },
@ -2577,7 +2577,7 @@ export type MangoV4 = {
], ],
"types": [ "types": [
{ {
"name": "MarginTradeWithdraw", "name": "FlashLoanWithdraw",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -3274,7 +3274,7 @@ export type MangoV4 = {
}, },
{ {
"code": 6004, "code": 6004,
"name": "InvalidMarginTradeTargetCpiProgram", "name": "InvalidFlashLoanTargetCpiProgram",
"msg": "" "msg": ""
}, },
{ {
@ -3908,7 +3908,7 @@ export const IDL: MangoV4 = {
] ]
}, },
{ {
"name": "marginTrade", "name": "flashLoan",
"accounts": [ "accounts": [
{ {
"name": "group", "name": "group",
@ -3936,7 +3936,7 @@ export const IDL: MangoV4 = {
"name": "withdraws", "name": "withdraws",
"type": { "type": {
"vec": { "vec": {
"defined": "MarginTradeWithdraw" "defined": "FlashLoanWithdraw"
} }
} }
}, },
@ -5869,7 +5869,7 @@ export const IDL: MangoV4 = {
], ],
"types": [ "types": [
{ {
"name": "MarginTradeWithdraw", "name": "FlashLoanWithdraw",
"type": { "type": {
"kind": "struct", "kind": "struct",
"fields": [ "fields": [
@ -6566,7 +6566,7 @@ export const IDL: MangoV4 = {
}, },
{ {
"code": 6004, "code": 6004,
"name": "InvalidMarginTradeTargetCpiProgram", "name": "InvalidFlashLoanTargetCpiProgram",
"msg": "" "msg": ""
}, },
{ {

View File

@ -1,6 +1,6 @@
import { BN } from '@project-serum/anchor'; import { BN } from '@project-serum/anchor';
export class MarginTradeWithdraw { export class FlashLoanWithdraw {
static index: number; static index: number;
static amount: BN; static amount: BN;
} }