Skip to content

Commit

Permalink
Feat: decrease order handler size (#642)
Browse files Browse the repository at this point in the history
* feat: convert order_utils to a lib

* fix order_utils to contract

* fix swap/increase/decrease order utils to contract

* feat: update deployment scripts

* test: uncomment tests

* refacto: format

---------

Co-authored-by: sparqet <[email protected]>
  • Loading branch information
zarboq and sparqet authored Apr 25, 2024
1 parent b5416be commit f54610c
Show file tree
Hide file tree
Showing 17 changed files with 1,551 additions and 782 deletions.
48 changes: 47 additions & 1 deletion scripts/app/deployApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,51 @@ async function deploy() {
})
console.log("ReferralStorage Deployed.")

console.log("Deploying IncreaseOrderUtils")
const compiledIncreaseOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_IncreaseOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledIncreaseOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_IncreaseOrderUtils.contract_class.json").toString( "ascii"))
const increaseOrderUtilsCallData: CallData = new CallData(compiledIncreaseOrderUtilsSierra.abi)
const increaseOrderUtilsConstructor: Calldata = increaseOrderUtilsCallData.compile("constructor", {})
const deployIncreaseOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledIncreaseOrderUtilsSierra,
casm: compiledIncreaseOrderUtilsCasm,
})

console.log("Deploying DecreaseOrderUtils")
const compiledDecreaseOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_DecreaseOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledDecreaseOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_DecreaseOrderUtils.contract_class.json").toString( "ascii"))
const decreaseOrderUtilsCallData: CallData = new CallData(compiledDecreaseOrderUtilsSierra.abi)
const decreaseOrderUtilsConstructor: Calldata = decreaseOrderUtilsCallData.compile("constructor", {})
const deployDecreaseOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledDecreaseOrderUtilsSierra,
casm: compiledDecreaseOrderUtilsCasm,
})

console.log("Deploying SwapOrderUtils")
const compiledSwapOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_SwapOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledSwapOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_SwapOrderUtils.contract_class.json").toString( "ascii"))
const swapOrderUtilsCallData: CallData = new CallData(compiledSwapOrderUtilsSierra.abi)
const swapOrderUtilsConstructor: Calldata = swapOrderUtilsCallData.compile("constructor", {})
const deploySwapOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledSwapOrderUtilsSierra,
casm: compiledSwapOrderUtilsCasm,
})

console.log("Deploying OrderUtils")
const compiledOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_OrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_OrderUtils.contract_class.json").toString( "ascii"))
const orderUtilsCallData: CallData = new CallData(compiledOrderUtilsSierra.abi)
const orderUtilsConstructor: Calldata = orderUtilsCallData.compile("constructor", {
increase_order_address: deployIncreaseOrderUtilsResponse.deploy.contract_address,
decrease_order_address: deployDecreaseOrderUtilsResponse.deploy.contract_address,
swap_order_address: deploySwapOrderUtilsResponse.deploy.contract_address
})
const deployOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledOrderUtilsSierra,
casm: compiledOrderUtilsCasm,
constructorCalldata: orderUtilsConstructor
})

console.log("Deploying OrderHandler...")
const compiledOrderHandlerCasm = json.parse(fs.readFileSync( "./target/dev/satoru_OrderHandler.compiled_contract_class.json").toString( "ascii"))
const compiledOrderHandlerSierra = json.parse(fs.readFileSync( "./target/dev/satoru_OrderHandler.contract_class.json").toString( "ascii"))
Expand All @@ -145,7 +190,8 @@ async function deploy() {
order_vault_address: deployOrderVaultResponse.deploy.contract_address,
oracle_address: deployOracleResponse.deploy.contract_address,
swap_handler_address: deploySwapHandlerResponse.deploy.contract_address,
referral_storage_address: deployReferralStorageResponse.deploy.contract_address
referral_storage_address: deployReferralStorageResponse.deploy.contract_address,
order_utils_address: deployOrderUtilsResponse.deploy.contract_address
})
const deployOrderHandlerResponse = await account0.declareAndDeploy({
contract: compiledOrderHandlerSierra,
Expand Down
88 changes: 88 additions & 0 deletions scripts/app/testDeploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Account, hash, Contract, json, Calldata, CallData, RpcProvider, shortString, ec } from "starknet"
import fs from 'fs'
import dotenv from 'dotenv'

dotenv.config()

async function deploy() {
// connect provider
const providerUrl = process.env.PROVIDER_URL
const provider = new RpcProvider({ nodeUrl: providerUrl! })
// connect your account. To adapt to your own account :
const privateKey0: string = process.env.ACCOUNT_PRIVATE as string
const account0Address: string = process.env.ACCOUNT_PUBLIC as string
const account0 = new Account(provider, account0Address!, privateKey0!)
console.log("Deploying with Account: " + account0Address)
console.log("RPC: " + providerUrl)

console.log("Deploying IncreaseOrderUtils")
const compiledIncreaseOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_IncreaseOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledIncreaseOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_IncreaseOrderUtils.contract_class.json").toString( "ascii"))
const increaseOrderUtilsCallData: CallData = new CallData(compiledIncreaseOrderUtilsSierra.abi)
const increaseOrderUtilsConstructor: Calldata = increaseOrderUtilsCallData.compile("constructor", {})
const deployIncreaseOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledIncreaseOrderUtilsSierra,
casm: compiledIncreaseOrderUtilsCasm,
})

console.log("Deploying DecreaseOrderUtils")
const compiledDecreaseOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_DecreaseOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledDecreaseOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_DecreaseOrderUtils.contract_class.json").toString( "ascii"))
const decreaseOrderUtilsCallData: CallData = new CallData(compiledDecreaseOrderUtilsSierra.abi)
const decreaseOrderUtilsConstructor: Calldata = decreaseOrderUtilsCallData.compile("constructor", {})
const deployDecreaseOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledDecreaseOrderUtilsSierra,
casm: compiledDecreaseOrderUtilsCasm,
})

console.log("Deploying SwapOrderUtils")
const compiledSwapOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_SwapOrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledSwapOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_SwapOrderUtils.contract_class.json").toString( "ascii"))
const swapOrderUtilsCallData: CallData = new CallData(compiledSwapOrderUtilsSierra.abi)
const swapOrderUtilsConstructor: Calldata = swapOrderUtilsCallData.compile("constructor", {})
const deploySwapOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledSwapOrderUtilsSierra,
casm: compiledSwapOrderUtilsCasm,
})

console.log("Deploying OrderUtils")
const compiledOrderUtilsCasm = json.parse(fs.readFileSync( "./target/dev/satoru_OrderUtils.compiled_contract_class.json").toString( "ascii"))
const compiledOrderUtilsSierra = json.parse(fs.readFileSync( "./target/dev/satoru_OrderUtils.contract_class.json").toString( "ascii"))
const orderUtilsCallData: CallData = new CallData(compiledOrderUtilsSierra.abi)
const orderUtilsConstructor: Calldata = orderUtilsCallData.compile("constructor", {
increase_order_address: deployIncreaseOrderUtilsResponse.deploy.contract_address,
decrease_order_address: deployDecreaseOrderUtilsResponse.deploy.contract_address,
swap_order_address: deploySwapOrderUtilsResponse.deploy.contract_address
})
const deployOrderUtilsResponse = await account0.declareAndDeploy({
contract: compiledOrderUtilsSierra,
casm: compiledOrderUtilsCasm,
constructorCalldata: orderUtilsConstructor
})


const compiledOrderHandlerCasm = json.parse(fs.readFileSync( "./target/dev/satoru_OrderHandler.compiled_contract_class.json").toString( "ascii"))
const compiledOrderHandlerSierra = json.parse(fs.readFileSync( "./target/dev/satoru_OrderHandler.contract_class.json").toString( "ascii"))
const orderHandlerCallData: CallData = new CallData(compiledOrderHandlerSierra.abi)
const orderHandlerConstructor: Calldata = orderHandlerCallData.compile("constructor", {
data_store_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
role_store_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
event_emitter_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
order_vault_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
oracle_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
swap_handler_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
referral_storage_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691",
order_utils_address: "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691"
})
const deployOrderHandlerResponse = await account0.declareAndDeploy(
{
contract: compiledOrderHandlerSierra,
casm: compiledOrderHandlerCasm ,
constructorCalldata: orderHandlerConstructor,
},
//{ maxFee: 1485894175412100 }
)
console.log("OrderHandler Deployed at: " + deployOrderHandlerResponse.deploy.contract_address)
}

deploy()
12 changes: 8 additions & 4 deletions src/exchange/adl_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mod AdlHandler {
use satoru::exchange::base_order_handler::BaseOrderHandler::{
data_store::InternalContractMemberStateTrait as DataStoreStateTrait,
event_emitter::InternalContractMemberStateTrait as EventEmitterStateTrait,
order_utils::InternalContractMemberStateTrait as OrderUtilsTrait,
oracle::InternalContractMemberStateTrait as OracleStateTrait,
InternalTrait as BaseOrderHandleInternalTrait,
};
Expand All @@ -91,7 +92,8 @@ mod AdlHandler {
use satoru::order::{
order::{SecondaryOrderType, OrderType, Order},
order_vault::{IOrderVaultDispatcher, IOrderVaultDispatcherTrait},
base_order_utils::{ExecuteOrderParams, ExecuteOrderParamsContracts}, order_utils
base_order_utils::{ExecuteOrderParams, ExecuteOrderParamsContracts},
order_utils::{IOrderUtilsDispatcher}
};
use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait};
use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait};
Expand Down Expand Up @@ -151,7 +153,7 @@ mod AdlHandler {
oracle_address: ContractAddress,
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress,
order_handler_address: ContractAddress
order_utils_address: ContractAddress
) {
let mut state: BaseOrderHandler::ContractState =
BaseOrderHandler::unsafe_new_contract_state();
Expand All @@ -163,8 +165,10 @@ mod AdlHandler {
order_vault_address,
oracle_address,
swap_handler_address,
referral_storage_address
referral_storage_address,
order_utils_address
);
self.order_utils.write(IOrderUtilsDispatcher { contract_address: order_utils_address });
}

// *************************************************************************
Expand Down Expand Up @@ -282,7 +286,7 @@ mod AdlHandler {
)
);

order_utils::execute_order_utils(params);
base_order_handler_state.order_utils.read().execute_order_utils(params);

// validate that the ratio of pending pnl to pool value was decreased
cache
Expand Down
18 changes: 13 additions & 5 deletions src/exchange/base_order_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ trait IBaseOrderHandler<TContractState> {
order_vault_address: ContractAddress,
oracle_address: ContractAddress,
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress
referral_storage_address: ContractAddress,
order_utils_address: ContractAddress
);
}

Expand Down Expand Up @@ -69,6 +70,7 @@ mod BaseOrderHandler {
error::OrderError, order::{SecondaryOrderType, OrderType, Order, DecreasePositionSwapType},
order_vault::{IOrderVaultDispatcher, IOrderVaultDispatcherTrait},
base_order_utils::{ExecuteOrderParams, ExecuteOrderParamsContracts},
order_utils::{IOrderUtilsDispatcher, IOrderUtilsDispatcherTrait}
};
use satoru::swap::swap_handler::{ISwapHandlerDispatcher, ISwapHandlerDispatcherTrait};
use satoru::exchange::error::ExchangeError;
Expand Down Expand Up @@ -96,7 +98,9 @@ mod BaseOrderHandler {
/// Interface to interact with the `Oracle` contract.
oracle: IOracleDispatcher,
/// Interface to interact with the `ReferralStorage` contract.
referral_storage: IReferralStorageDispatcher
referral_storage: IReferralStorageDispatcher,
/// Interface to interact with the `OrderUtils` contract.
order_utils: IOrderUtilsDispatcher
}

// *************************************************************************
Expand All @@ -121,7 +125,8 @@ mod BaseOrderHandler {
order_vault_address: ContractAddress,
oracle_address: ContractAddress,
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress
referral_storage_address: ContractAddress,
order_utils_address: ContractAddress
) {
self
.initialize(
Expand All @@ -131,7 +136,8 @@ mod BaseOrderHandler {
order_vault_address,
oracle_address,
swap_handler_address,
referral_storage_address
referral_storage_address,
order_utils_address
);
}

Expand All @@ -149,7 +155,8 @@ mod BaseOrderHandler {
order_vault_address: ContractAddress,
oracle_address: ContractAddress,
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress
referral_storage_address: ContractAddress,
order_utils_address: ContractAddress
) {
// Make sure the contract is not already initialized.
assert(
Expand All @@ -170,6 +177,7 @@ mod BaseOrderHandler {
self
.referral_storage
.write(IReferralStorageDispatcher { contract_address: referral_storage_address });
self.order_utils.write(IOrderUtilsDispatcher { contract_address: order_utils_address });
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/exchange/liquidation_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod LiquidationHandler {
oracle_utils::SetPricesParams
};
use satoru::order::{
order_utils, order::{SecondaryOrderType, OrderType, Order},
order_utils::{IOrderUtilsDispatcher}, order::{SecondaryOrderType, OrderType, Order},
order_vault::{IOrderVaultDispatcher, IOrderVaultDispatcherTrait},
base_order_utils::{ExecuteOrderParams, ExecuteOrderParamsContracts}
};
Expand All @@ -77,6 +77,7 @@ mod LiquidationHandler {
use satoru::exchange::base_order_handler::BaseOrderHandler::{
event_emitter::InternalContractMemberStateTrait,
data_store::InternalContractMemberStateImpl,
order_utils::InternalContractMemberStateTrait as OrderUtilsTrait,
oracle::InternalContractMemberStateTrait as OracleStateTrait,
};

Expand Down Expand Up @@ -107,7 +108,8 @@ mod LiquidationHandler {
order_vault_address: ContractAddress,
oracle_address: ContractAddress,
swap_handler_address: ContractAddress,
referral_storage_address: ContractAddress
referral_storage_address: ContractAddress,
order_utils_address: ContractAddress
) {
let mut state: BaseOrderHandler::ContractState =
BaseOrderHandler::unsafe_new_contract_state();
Expand All @@ -119,9 +121,9 @@ mod LiquidationHandler {
order_vault_address,
oracle_address,
swap_handler_address,
referral_storage_address
referral_storage_address,
order_utils_address
);

let mut state: RoleModule::ContractState = RoleModule::unsafe_new_contract_state();
IRoleModule::initialize(ref state, role_store_address,);
}
Expand Down Expand Up @@ -181,7 +183,7 @@ mod LiquidationHandler {
params.contracts.data_store,
execute_order_feature_disabled_key(get_contract_address(), params.order.order_type)
);
order_utils::execute_order_utils(params);
state_base.execute_order_utils(params);
// with_oracle_prices_after(state_base.oracle.read());

global_reentrancy_guard::non_reentrant_after(state_base.data_store.read());
Expand Down
Loading

0 comments on commit f54610c

Please sign in to comment.