Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deposit_into_spot_market_revenue_pool #520

Merged
merged 10 commits into from
Aug 1, 2023
56 changes: 56 additions & 0 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::state::perp_market::MarketStatus;
use crate::state::perp_market_map::{get_writable_perp_market_set, MarketSet};
use crate::state::spot_fulfillment_params::SpotFulfillmentParams;
use crate::state::spot_market::SpotBalanceType;
use crate::state::spot_market::SpotMarket;
use crate::state::spot_market_map::{
get_writable_spot_market_set, get_writable_spot_market_set_from_many,
};
Expand Down Expand Up @@ -1834,6 +1835,38 @@ pub fn handle_delete_user(ctx: Context<DeleteUser>) -> Result<()> {
Ok(())
}

#[access_control(
deposit_not_paused(&ctx.accounts.state)
)]
pub fn handle_deposit_into_spot_market_revenue_pool(
ctx: Context<RevenuePoolDeposit>,
amount: u64,
) -> Result<()> {
if amount == 0 {
return Err(ErrorCode::InsufficientDeposit.into());
}

let mut spot_market = load_mut!(ctx.accounts.spot_market)?;

controller::spot_balance::update_revenue_pool_balances(
amount.cast::<u128>()?,
&SpotBalanceType::Deposit,
&mut spot_market,
)?;

controller::token::receive(
&ctx.accounts.token_program,
&ctx.accounts.user_token_account,
&ctx.accounts.spot_market_vault,
&ctx.accounts.authority,
amount,
)?;

spot_market.validate_max_token_deposits()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would validate vault amounts too


Ok(())
}

#[derive(Accounts)]
#[instruction(
sub_account_id: u16,
Expand Down Expand Up @@ -1940,6 +1973,29 @@ pub struct Deposit<'info> {
pub token_program: Program<'info, Token>,
}

#[derive(Accounts)]
#[instruction(market_index: u16,)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong i think, as is it it would use the amount as the market_index, im surprised the ts test passed

i'd get wrong of the market index here and just pull it from the spot market using
spot_market.load()?.market_index

pub struct RevenuePoolDeposit<'info> {
#[account(mut)]
pub spot_market: AccountLoader<'info, SpotMarket>,
pub state: Box<Account<'info, State>>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
mut,
seeds = [b"spot_market_vault".as_ref(), market_index.to_le_bytes().as_ref()],
bump,
)]
pub spot_market_vault: Box<Account<'info, TokenAccount>>,
#[account(
mut,
constraint = &spot_market_vault.mint.eq(&user_token_account.mint),
token::authority = authority
)]
pub user_token_account: Box<Account<'info, TokenAccount>>,
pub token_program: Program<'info, Token>,
}

#[derive(Accounts)]
#[instruction(market_index: u16,)]
pub struct Withdraw<'info> {
Expand Down
7 changes: 7 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,13 @@ pub mod drift {
handle_deposit_into_perp_market_fee_pool(ctx, amount)
}

pub fn deposit_into_spot_market_revenue_pool(
ctx: Context<RevenuePoolDeposit>,
amount: u64,
) -> Result<()> {
handle_deposit_into_spot_market_revenue_pool(ctx, amount)
}

pub fn repeg_amm_curve(ctx: Context<RepegCurve>, new_peg_candidate: u128) -> Result<()> {
handle_repeg_amm_curve(ctx, new_peg_candidate)
}
Expand Down
27 changes: 27 additions & 0 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5493,6 +5493,33 @@ export class DriftClient {
);
}

public async depositIntoSpotMarketRevenuePool(
marketIndex: number,
amount: BN,
userTokenAccountPublicKey: PublicKey
): Promise<TransactionSignature> {
const spotMarket = await this.getSpotMarketAccount(marketIndex);
const tx = await this.program.transaction.depositIntoSpotMarketRevenuePool(
amount,
{
accounts: {
state: await this.getStatePublicKey(),
spotMarket: await getSpotMarketPublicKey(
this.program.programId,
marketIndex
),
userTokenAccount: userTokenAccountPublicKey,
authority: this.wallet.publicKey,
spotMarketVault: spotMarket.vault,
tokenProgram: TOKEN_PROGRAM_ID,
},
}
);

const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}

public getPerpMarketExtendedInfo(
marketIndex: number
): PerpMarketExtendedInfo {
Expand Down
41 changes: 41 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,47 @@
}
]
},
{
"name": "depositIntoSpotMarketRevenuePool",
"accounts": [
{
"name": "spotMarket",
"isMut": true,
"isSigner": false
},
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "authority",
"isMut": true,
"isSigner": true
},
{
"name": "spotMarketVault",
"isMut": true,
"isSigner": false
},
{
"name": "userTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
},
{
"name": "repegAmmCurve",
"accounts": [
Expand Down
31 changes: 31 additions & 0 deletions tests/spotSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
EventSubscriber,
OracleSource,
OracleInfo,
getTokenAmount,
SpotBalanceType,
} from '../sdk/src';

import {
Expand All @@ -37,6 +39,7 @@ import {
getSerumSignerPublicKey,
QUOTE_PRECISION,
} from '../sdk';
import { ZERO } from '@drift-labs/sdk';

describe('spot swap', () => {
const provider = anchor.AnchorProvider.local(undefined, {
Expand Down Expand Up @@ -596,4 +599,32 @@ describe('spot swap', () => {
}
assert(failed);
});

it('donate to revenue pool for a great feature!', async () => {
const solSpotMarket = takerDriftClient.getSpotMarketAccount(1);

const solRevPool = getTokenAmount(
solSpotMarket.revenuePool.scaledBalance,
solSpotMarket,
SpotBalanceType.DEPOSIT
);
assert(solRevPool.eq(ZERO));

const charity = new BN(1);
await takerDriftClient.depositIntoSpotMarketRevenuePool(
1,
charity,
takerWSOL
);
await takerDriftClient.fetchAccounts();
const solSpotMarketAfter = takerDriftClient.getSpotMarketAccount(1);

const solRevPoolAfter = getTokenAmount(
solSpotMarketAfter.revenuePool.scaledBalance,
solSpotMarketAfter,
SpotBalanceType.DEPOSIT
);
assert(solRevPoolAfter.gt(solRevPool));
assert(solRevPoolAfter.eq(charity));
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading