Skip to content

Commit

Permalink
move fee receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSichuan committed Jul 27, 2021
1 parent 20f4107 commit afd84d9
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 94 deletions.
29 changes: 27 additions & 2 deletions token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct PartialReserveConfig {
pub deposit_limit: Option<u64>,
/// Borrow limit
pub borrow_limit: Option<u64>,
/// Liquidity fee receiver
pub fee_receiver: Option<Pubkey>,
}

/// Reserve Fees with optional fields
Expand Down Expand Up @@ -513,6 +515,15 @@ fn main() {
.required(false)
.help("Borrow Limit"),
)
.arg(
Arg::with_name("fee_receiver")
.long("fee-receiver")
.validator(is_pubkey)
.value_name("PUBKEY")
.takes_value(true)
.required(false)
.help("Fee receiver address"),
)
)
.get_matches();

Expand Down Expand Up @@ -592,6 +603,8 @@ fn main() {
let borrow_fee_wad = (borrow_fee * WAD as f64) as u64;
let flash_loan_fee_wad = (flash_loan_fee * WAD as f64) as u64;

let liquidity_fee_receiver_keypair = Keypair::new();

command_add_reserve(
&mut config,
ui_amount,
Expand All @@ -610,6 +623,7 @@ fn main() {
},
deposit_limit,
borrow_limit,
fee_receiver: liquidity_fee_receiver_keypair.pubkey(),
},
source_liquidity_pubkey,
source_liquidity_owner_keypair,
Expand All @@ -618,6 +632,7 @@ fn main() {
pyth_product_pubkey,
pyth_price_pubkey,
switchboard_feed_pubkey,
liquidity_fee_receiver_keypair,
)
}
("update-reserve", Some(arg_matches)) => {
Expand All @@ -637,6 +652,7 @@ fn main() {
let host_fee_percentage = value_of(arg_matches, "host_fee_percentage");
let deposit_limit = value_of(arg_matches, "deposit_limit");
let borrow_limit = value_of(arg_matches, "borrow_limit");
let fee_receiver = pubkey_of(arg_matches, "fee_receiver");

let borrow_fee_wad = borrow_fee.map(|fee| (fee * WAD as f64) as u64);
let flash_loan_fee_wad = flash_loan_fee.map(|fee| (fee * WAD as f64) as u64);
Expand All @@ -658,6 +674,7 @@ fn main() {
},
deposit_limit,
borrow_limit,
fee_receiver,
},
reserve_pubkey,
lending_market_pubkey,
Expand Down Expand Up @@ -737,6 +754,7 @@ fn command_add_reserve(
pyth_product_pubkey: Pubkey,
pyth_price_pubkey: Pubkey,
switchboard_feed_pubkey: Pubkey,
liquidity_fee_receiver_keypair: Keypair,
) -> CommandResult {
let source_liquidity_account = config.rpc_client.get_account(&source_liquidity_pubkey)?;
let source_liquidity = Token::unpack_from_slice(source_liquidity_account.data.borrow())?;
Expand All @@ -750,7 +768,6 @@ fn command_add_reserve(
let collateral_mint_keypair = Keypair::new();
let collateral_supply_keypair = Keypair::new();
let liquidity_supply_keypair = Keypair::new();
let liquidity_fee_receiver_keypair = Keypair::new();
let user_collateral_keypair = Keypair::new();
let user_transfer_authority_keypair = Keypair::new();

Expand Down Expand Up @@ -877,7 +894,6 @@ fn command_add_reserve(
reserve_keypair.pubkey(),
source_liquidity.mint,
liquidity_supply_keypair.pubkey(),
liquidity_fee_receiver_keypair.pubkey(),
collateral_mint_keypair.pubkey(),
collateral_supply_keypair.pubkey(),
pyth_product_pubkey,
Expand Down Expand Up @@ -1066,6 +1082,15 @@ fn command_update_reserve(
)
}

if reserve_config.fee_receiver.is_some() {
println!(
"Updating fee_receiver from {} to {}",
reserve.config.fee_receiver,
reserve_config.fee_receiver.unwrap(),
);
reserve.config.fee_receiver = reserve_config.fee_receiver.unwrap();
}

let mut transaction = Transaction::new_with_payer(
&[update_reserve_config(
config.lending_program_id,
Expand Down
14 changes: 10 additions & 4 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ impl LendingInstruction {
let (flash_loan_fee_wad, rest) = Self::unpack_u64(rest)?;
let (host_fee_percentage, rest) = Self::unpack_u8(rest)?;
let (deposit_limit, rest) = Self::unpack_u64(rest)?;
let (borrow_limit, _) = Self::unpack_u64(rest)?;
let (borrow_limit, rest) = Self::unpack_u64(rest)?;
let (fee_receiver, _) = Self::unpack_pubkey(rest)?;
Self::InitReserve {
liquidity_amount,
config: ReserveConfig {
Expand All @@ -428,6 +429,7 @@ impl LendingInstruction {
},
deposit_limit,
borrow_limit,
fee_receiver,
},
}
}
Expand Down Expand Up @@ -486,7 +488,8 @@ impl LendingInstruction {
let (flash_loan_fee_wad, _rest) = Self::unpack_u64(_rest)?;
let (host_fee_percentage, _rest) = Self::unpack_u8(_rest)?;
let (deposit_limit, _rest) = Self::unpack_u64(_rest)?;
let (borrow_limit, _) = Self::unpack_u64(_rest)?;
let (borrow_limit, _rest) = Self::unpack_u64(_rest)?;
let (fee_receiver, _) = Self::unpack_pubkey(_rest)?;

Self::UpdateReserveConfig {
config: ReserveConfig {
Expand All @@ -504,6 +507,7 @@ impl LendingInstruction {
},
deposit_limit,
borrow_limit,
fee_receiver,
},
}
}
Expand Down Expand Up @@ -601,6 +605,7 @@ impl LendingInstruction {
},
deposit_limit,
borrow_limit,
fee_receiver,
},
} => {
buf.push(2);
Expand All @@ -617,6 +622,7 @@ impl LendingInstruction {
buf.extend_from_slice(&host_fee_percentage.to_le_bytes());
buf.extend_from_slice(&deposit_limit.to_le_bytes());
buf.extend_from_slice(&borrow_limit.to_le_bytes());
buf.extend_from_slice(&fee_receiver.to_bytes());
}
Self::RefreshReserve => {
buf.push(3);
Expand Down Expand Up @@ -681,6 +687,7 @@ impl LendingInstruction {
buf.extend_from_slice(&config.fees.host_fee_percentage.to_le_bytes());
buf.extend_from_slice(&config.deposit_limit.to_le_bytes());
buf.extend_from_slice(&config.borrow_limit.to_le_bytes());
buf.extend_from_slice(&config.fee_receiver.to_bytes());
}
}
buf
Expand Down Expand Up @@ -739,7 +746,6 @@ pub fn init_reserve(
reserve_pubkey: Pubkey,
reserve_liquidity_mint_pubkey: Pubkey,
reserve_liquidity_supply_pubkey: Pubkey,
reserve_liquidity_fee_receiver_pubkey: Pubkey,
reserve_collateral_mint_pubkey: Pubkey,
reserve_collateral_supply_pubkey: Pubkey,
pyth_product_pubkey: Pubkey,
Expand All @@ -759,7 +765,7 @@ pub fn init_reserve(
AccountMeta::new(reserve_pubkey, false),
AccountMeta::new_readonly(reserve_liquidity_mint_pubkey, false),
AccountMeta::new(reserve_liquidity_supply_pubkey, false),
AccountMeta::new(reserve_liquidity_fee_receiver_pubkey, false),
AccountMeta::new(config.fee_receiver, false),
AccountMeta::new(reserve_collateral_mint_pubkey, false),
AccountMeta::new(reserve_collateral_supply_pubkey, false),
AccountMeta::new_readonly(pyth_product_pubkey, false),
Expand Down
5 changes: 2 additions & 3 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ fn process_init_reserve(
mint_pubkey: *reserve_liquidity_mint_info.key,
mint_decimals: reserve_liquidity_mint.decimals,
supply_pubkey: *reserve_liquidity_supply_info.key,
fee_receiver: *reserve_liquidity_fee_receiver_info.key,
pyth_oracle_pubkey: *pyth_price_info.key,
switchboard_oracle_pubkey: *switchboard_feed_info.key,
market_price,
Expand Down Expand Up @@ -1331,7 +1330,7 @@ fn process_borrow_obligation_liquidity(
);
return Err(LendingError::InvalidAccountInput.into());
}
if &borrow_reserve.liquidity.fee_receiver != borrow_reserve_liquidity_fee_receiver_info.key {
if &borrow_reserve.config.fee_receiver != borrow_reserve_liquidity_fee_receiver_info.key {
msg!("Borrow reserve liquidity fee receiver does not match the borrow reserve liquidity fee receiver provided");
return Err(LendingError::InvalidAccountInput.into());
}
Expand Down Expand Up @@ -1813,7 +1812,7 @@ fn process_flash_loan(
msg!("Reserve liquidity supply must be used as the source liquidity provided");
return Err(LendingError::InvalidAccountInput.into());
}
if &reserve.liquidity.fee_receiver != reserve_liquidity_fee_receiver_info.key {
if &reserve.config.fee_receiver != reserve_liquidity_fee_receiver_info.key {
msg!("Reserve liquidity fee receiver does not match the reserve liquidity fee receiver provided");
return Err(LendingError::InvalidAccountInput.into());
}
Expand Down
19 changes: 8 additions & 11 deletions token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ pub struct ReserveLiquidity {
pub mint_decimals: u8,
/// Reserve liquidity supply address
pub supply_pubkey: Pubkey,
/// Reserve liquidity fee receiver address
pub fee_receiver: Pubkey,
/// Reserve liquidity pyth oracle account
pub pyth_oracle_pubkey: Pubkey,
/// Reserve liquidity switchboard oracle account
Expand All @@ -376,7 +374,6 @@ impl ReserveLiquidity {
mint_pubkey: params.mint_pubkey,
mint_decimals: params.mint_decimals,
supply_pubkey: params.supply_pubkey,
fee_receiver: params.fee_receiver,
pyth_oracle_pubkey: params.pyth_oracle_pubkey,
switchboard_oracle_pubkey: params.switchboard_oracle_pubkey,
available_amount: 0,
Expand Down Expand Up @@ -478,8 +475,6 @@ pub struct NewReserveLiquidityParams {
pub mint_decimals: u8,
/// Reserve liquidity supply address
pub supply_pubkey: Pubkey,
/// Reserve liquidity fee receiver address
pub fee_receiver: Pubkey,
/// Reserve liquidity pyth oracle account
pub pyth_oracle_pubkey: Pubkey,
/// Reserve liquidity switchboard oracle account
Expand Down Expand Up @@ -615,6 +610,8 @@ pub struct ReserveConfig {
pub deposit_limit: u64,
/// Borrows disabled
pub borrow_limit: u64,
/// Reserve liquidity fee receiver address
pub fee_receiver: Pubkey,
}

/// Additional fee information on a reserve
Expand Down Expand Up @@ -737,7 +734,6 @@ impl Pack for Reserve {
liquidity_mint_pubkey,
liquidity_mint_decimals,
liquidity_supply_pubkey,
liquidity_fee_receiver,
liquidity_pyth_oracle_pubkey,
liquidity_switchboard_oracle_pubkey,
liquidity_available_amount,
Expand All @@ -759,6 +755,7 @@ impl Pack for Reserve {
config_fees_host_fee_percentage,
config_deposit_limit,
config_borrow_limit,
config_fee_receiver,
_padding,
) = mut_array_refs![
output,
Expand All @@ -771,7 +768,6 @@ impl Pack for Reserve {
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
8,
16,
16,
Expand All @@ -791,6 +787,7 @@ impl Pack for Reserve {
1,
8,
8,
PUBKEY_BYTES,
248
];

Expand All @@ -804,7 +801,6 @@ impl Pack for Reserve {
liquidity_mint_pubkey.copy_from_slice(self.liquidity.mint_pubkey.as_ref());
*liquidity_mint_decimals = self.liquidity.mint_decimals.to_le_bytes();
liquidity_supply_pubkey.copy_from_slice(self.liquidity.supply_pubkey.as_ref());
liquidity_fee_receiver.copy_from_slice(self.liquidity.fee_receiver.as_ref());
liquidity_pyth_oracle_pubkey.copy_from_slice(self.liquidity.pyth_oracle_pubkey.as_ref());
liquidity_switchboard_oracle_pubkey
.copy_from_slice(self.liquidity.switchboard_oracle_pubkey.as_ref());
Expand Down Expand Up @@ -837,6 +833,7 @@ impl Pack for Reserve {
*config_fees_host_fee_percentage = self.config.fees.host_fee_percentage.to_le_bytes();
*config_deposit_limit = self.config.deposit_limit.to_le_bytes();
*config_borrow_limit = self.config.borrow_limit.to_le_bytes();
config_fee_receiver.copy_from_slice(self.config.fee_receiver.as_ref());
}

/// Unpacks a byte buffer into a [ReserveInfo](struct.ReserveInfo.html).
Expand All @@ -851,7 +848,6 @@ impl Pack for Reserve {
liquidity_mint_pubkey,
liquidity_mint_decimals,
liquidity_supply_pubkey,
liquidity_fee_receiver,
liquidity_pyth_oracle_pubkey,
liquidity_switchboard_oracle_pubkey,
liquidity_available_amount,
Expand All @@ -873,6 +869,7 @@ impl Pack for Reserve {
config_fees_host_fee_percentage,
config_deposit_limit,
config_borrow_limit,
config_fee_receiver,
_padding,
) = array_refs![
input,
Expand All @@ -885,7 +882,6 @@ impl Pack for Reserve {
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
PUBKEY_BYTES,
8,
16,
16,
Expand All @@ -905,6 +901,7 @@ impl Pack for Reserve {
1,
8,
8,
PUBKEY_BYTES,
248
];

Expand All @@ -925,7 +922,6 @@ impl Pack for Reserve {
mint_pubkey: Pubkey::new_from_array(*liquidity_mint_pubkey),
mint_decimals: u8::from_le_bytes(*liquidity_mint_decimals),
supply_pubkey: Pubkey::new_from_array(*liquidity_supply_pubkey),
fee_receiver: Pubkey::new_from_array(*liquidity_fee_receiver),
pyth_oracle_pubkey: Pubkey::new_from_array(*liquidity_pyth_oracle_pubkey),
switchboard_oracle_pubkey: Pubkey::new_from_array(
*liquidity_switchboard_oracle_pubkey,
Expand Down Expand Up @@ -955,6 +951,7 @@ impl Pack for Reserve {
},
deposit_limit: u64::from_le_bytes(*config_deposit_limit),
borrow_limit: u64::from_le_bytes(*config_borrow_limit),
fee_receiver: Pubkey::new_from_array(*config_fee_receiver),
},
})
}
Expand Down
Loading

0 comments on commit afd84d9

Please sign in to comment.