Skip to content

Commit

Permalink
add util for debugging live swap amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
parketh committed Aug 21, 2024
1 parent 762c849 commit 379f14c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion models/src/libraries/SpreadMath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Decimal from "decimal.js";
import { baseToLiquidity, quoteToLiquidity } from "../math/liquidityMath";
import { PRECISION, ROUNDING } from "../config";
import { baseToLiquidity, quoteToLiquidity } from "../math/liquidityMath";
import { limitToSqrtPrice, priceToLimit } from "../math/priceMath";

type PositionInfo = {
Expand Down
45 changes: 45 additions & 0 deletions models/tests/solver/DebugSwap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
getDelta,
getVirtualPosition,
getVirtualPositionRange,
} from "../../src/libraries/SpreadMath";
import { getSwapAmounts } from "../../src/libraries/SwapLib";

const isBuy = false;
const exactInput = true;
const amount = "1000";
const minSpread = 25;
const range = 5000;
const maxDelta = 500;
const oraclePrice = "0.00013734";
const baseReserves = "80000";
const quoteReserves = "11.428658419735305956";

const delta = getDelta(maxDelta, baseReserves, quoteReserves, oraclePrice);
console.log({ delta });
const { lowerLimit, upperLimit } = getVirtualPositionRange(
!isBuy,
minSpread,
delta,
range,
oraclePrice
);
console.log({ lowerLimit, upperLimit });
const { lowerSqrtPrice, upperSqrtPrice, liquidity } = getVirtualPosition(
!isBuy,
lowerLimit,
upperLimit,
isBuy ? baseReserves : quoteReserves
);
console.log({ lowerSqrtPrice, upperSqrtPrice, liquidity });
const { amountIn, amountOut } = getSwapAmounts(
isBuy,
exactInput,
amount,
null,
null,
lowerSqrtPrice,
upperSqrtPrice,
liquidity
);
console.log({ amountIn, amountOut });
3 changes: 3 additions & 0 deletions packages/replicating/src/tests/solver.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub mod test_swap;
pub mod test_withdraw;
pub mod test_market_params;
pub mod test_oracle;
// Disabled - used for debugging of live vaults
// pub mod debug_swap;

91 changes: 91 additions & 0 deletions packages/replicating/src/tests/solver/debug_swap.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Core lib imports.
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::class_hash::ClassHash;

// Local imports.
use haiko_solver_core::{
interfaces::ISolver::{ISolverDispatcher, ISolverDispatcherTrait}, types::SwapParams,
};
use haiko_solver_replicating::{
contracts::mocks::{
upgraded_replicating_solver::{
UpgradedReplicatingSolver, IUpgradedReplicatingSolverDispatcher,
IUpgradedReplicatingSolverDispatcherTrait
},
mock_pragma_oracle::{IMockPragmaOracleDispatcher, IMockPragmaOracleDispatcherTrait},
},
interfaces::{
IReplicatingSolver::{IReplicatingSolverDispatcher, IReplicatingSolverDispatcherTrait},
pragma::{DataType, PragmaPricesResponse},
},
types::MarketParams,
tests::{
helpers::{actions::{deploy_replicating_solver, deploy_mock_pragma_oracle}, utils::before,},
},
};

// Haiko imports.
use haiko_lib::helpers::{
params::{owner, alice, bob, treasury, default_token_params},
actions::{
market_manager::{create_market, modify_position, swap},
token::{deploy_token, fund, approve},
},
utils::{to_e18, to_e18_u128, to_e28, approx_eq, approx_eq_pct},
};

// External imports.
use openzeppelin::token::erc20::interface::{ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use snforge_std::{
declare, start_warp, start_prank, stop_prank, CheatTarget, spy_events, SpyOn, EventSpy,
EventAssertions, EventFetcher, ContractClass, ContractClassTrait
};

#[test]
fn test_debug_swap() {
let (
_base_token, _quote_token, oracle, _vault_token_class, solver, market_id, _vault_token_opt
) =
before(
true
);

// Set params.
start_warp(CheatTarget::One(solver.contract_address), 1000);
let repl_solver = IReplicatingSolverDispatcher { contract_address: solver.contract_address };
let market_params = MarketParams {
min_spread: 25,
range: 5000,
max_delta: 500,
max_skew: 6000,
base_currency_id: 1398035019,
quote_currency_id: 4543560,
min_sources: 2,
max_age: 1000
};
repl_solver.queue_market_params(market_id, market_params);
repl_solver.set_market_params(market_id);

// Set oracle price.
start_warp(CheatTarget::One(oracle.contract_address), 1000);
oracle.set_data_with_USD_hop('STRK', 'ETH', 13717, 8, 999, 5);

// Deposit initial.
start_prank(CheatTarget::One(solver.contract_address), owner());
let (base_deposit_init, quote_deposit_init, shares_init) = solver
.deposit_initial(market_id, 80000000000000000000000, 11428658419735305956);

// Swap.
start_prank(CheatTarget::One(solver.contract_address), owner());
let params = SwapParams {
is_buy: false,
amount: to_e18(1000),
exact_input: true,
threshold_sqrt_price: Option::None(()),
threshold_amount: Option::None(()),
deadline: Option::None(()),
};
let (amount_in, amount_out) = solver.swap(market_id, params);
println!("amount_in: {}, amount_out: {}", amount_in, amount_out);
}

0 comments on commit 379f14c

Please sign in to comment.