Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ solana-system-interface = "3"
spl-associated-token-account-interface = "2"
spl-token = "9"
spl-token-2022-interface = "3"
# Only the test CLI still builds against the legacy interface; it moves over
# together with its Token-2022 support.
spl-token-interface = "3"

[workspace.metadata.cli]
# Used to help solana-verify identify the correct build image.
Expand Down
2 changes: 1 addition & 1 deletion test-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ solana-sdk.workspace = true
solana-system-interface = { workspace = true, features = ["bincode"] }
spl-associated-token-account-interface.workspace = true
spl-token.workspace = true
spl-token-interface.workspace = true
spl-token-2022-interface.workspace = true

[lints]
workspace = true
2 changes: 1 addition & 1 deletion test-cli/src/cmd/create_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn execute(ctx: Context, parsed: ParsedOrder, common: CommonArgs) -> anyhow::Res
// Approve the settlement state PDA to pull sell tokens on the user's behalf.
ixs.push(crate::instructions::approve(
&ctx.program_id,
&sell.ta,
&sell,
&ctx.payer.pubkey(),
sell_amount,
)?);
Expand Down
32 changes: 18 additions & 14 deletions test-cli/src/cmd/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cow_settlement_client::{
cow_settlement_interface::{
data::{intent::OrderIntent, order::OrderAccount},
pda::buffer::find_buffer_pda,
token_program, Pubkey,
Pubkey,
},
instructions::{
BeginSettle, CreateBuffers, FinalizeSettle, FinalizedIntent, InitializedIntent, Pull,
Expand Down Expand Up @@ -235,16 +235,18 @@ fn resolve_intents(ctx: &Context, args: &SettleArgs) -> anyhow::Result<Vec<Resol
.collect()
}

/// Tally `amount` for `mint` in `tally`. The first time a mint is seen, also
/// check whether its buffer PDA already exists on-chain and register it for
/// creation if not.
/// Tally `amount` for `token`'s mint in `tally`. The first time a mint is seen,
/// also check whether its buffer PDA already exists on-chain and register it for
/// creation if not. Buffers are grouped by token program, since a single
/// `CreateBuffer` instruction creates buffers for only one of them.
fn tally_and_register_buffer(
ctx: &Context,
tally: &mut HashMap<Pubkey, u64>,
mint_buffers_to_create: &mut HashSet<Pubkey>,
mint: Pubkey,
mint_buffers_to_create: &mut HashMap<Pubkey, HashSet<Pubkey>>,
token: &ResolvedToken,
amount: u64,
) -> anyhow::Result<()> {
let mint = token.mint;
match tally.get(&mint) {
Some(cur_amount) => {
let new_amount = cur_amount
Expand All @@ -255,7 +257,10 @@ fn tally_and_register_buffer(
None => {
let (buffer_pda, _) = find_buffer_pda(&ctx.program_id, &mint);
if ctx.rpc.get_account(&buffer_pda).is_err() {
mint_buffers_to_create.insert(mint);
mint_buffers_to_create
.entry(token.token_program)
.or_default()
.insert(mint);
}
tally.insert(mint, amount);
}
Expand All @@ -273,7 +278,7 @@ fn prepare_setup_ixs(
) -> anyhow::Result<()> {
let mut sell_amount_pulled: HashMap<Pubkey, u64> = HashMap::new();
let mut buy_amount_pushed: HashMap<Pubkey, u64> = HashMap::new();
let mut mint_buffers_to_create: HashSet<Pubkey> = HashSet::new();
let mut mint_buffers_to_create: HashMap<Pubkey, HashSet<Pubkey>> = HashMap::new();

for intent in intents {
// for both the buy and sell token: we need to tally the total transfer amounts
Expand All @@ -282,28 +287,27 @@ fn prepare_setup_ixs(
ctx,
&mut sell_amount_pulled,
&mut mint_buffers_to_create,
intent.sell.mint,
&intent.sell,
intent.data.sell_amount,
)?;
tally_and_register_buffer(
ctx,
&mut buy_amount_pushed,
&mut mint_buffers_to_create,
intent.buy.mint,
&intent.buy,
intent.data.buy_amount,
)?;
}

ensure_cow_balance(&sell_amount_pulled, &buy_amount_pushed)?;

if !mint_buffers_to_create.is_empty() {
for (token_program, mints) in mint_buffers_to_create {
all_ixs.push(
CreateBuffers {
program_id: ctx.program_id,
payer: ctx.payer.pubkey(),
// The CLI only resolves tokens on the legacy program for now.
token_program: token_program::SPL_TOKEN_PROGRAM_ID,
mints: &mint_buffers_to_create.into_iter().collect::<Vec<_>>(),
token_program,
mints: &mints.into_iter().collect::<Vec<_>>(),
}
.into(),
);
Expand Down
14 changes: 7 additions & 7 deletions test-cli/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Context as _;
use cow_settlement_client::cow_settlement_interface::{pda::state::find_state_pda, Pubkey};
use solana_instruction::Instruction;
use solana_rpc_client::rpc_client::RpcClient;
use spl_token_interface::instruction::{self as token_ix};
use spl_token_2022_interface::instruction::{self as token_ix};

/// Build instructions that wrap `amount` lamports into the payer's WSOL ATA.
///
Expand All @@ -27,26 +27,26 @@ pub fn wrap_sol(
));

ixs.push(
token_ix::sync_native(&spl_token_interface::id(), &wsol.ta)
token_ix::sync_native(&wsol.token_program, &wsol.ta)
.context("failed to build SyncNative instruction")?,
);

Ok((wsol.ta, ixs))
}

/// Build an `Approve` instruction delegating `amount` tokens on `token_account`
/// to the PDA derived from `program_id`.
/// Build an `Approve` instruction delegating `amount` of `token` to the PDA
/// derived from `program_id`.
pub fn approve(
program_id: &Pubkey,
token_account: &Pubkey,
token: &token::ResolvedToken,
owner: &Pubkey,
amount: u64,
) -> anyhow::Result<Instruction> {
let (settlement_pda, _) = find_state_pda(program_id);

token_ix::approve(
&spl_token_interface::id(),
token_account,
&token.token_program,
&token.ta,
&settlement_pda,
owner,
&[],
Expand Down
Loading
Loading