Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 

Repository files navigation

Starknet DCA Integration

Integrate your own DCA primitive directly for your Madara App Chain.

Prerequisites

1. Edit your Madara node

Modify your node so that it does the following action:

A - Fetch data from the DCA smart contract

You need a runtime API that allows your node to fetch the current batch index and the pending_dca_ids from the smart contract. Retreive the pending_dca_ids by interacting with the smart contract's storage or by calling get_current_batch_index & get_pending_dca_ids.

B - Create transactions with the fetched data

For each dca_id in pending_dca_ids, create a transaction that calls the execute function on the smart contract with the dca_id as argument.

C - Insert the Transaction at the beginning of the Block

With a custom proposer, insert the constructed transactions at the beginning of the block during block production and ensure the transactions are valid according to your runtime's rules.

This logic can be implemented to execute at the beginning of each block to ensure DCA are executed at the correct timestamp.

2. Adapt the IDex and the IPriceOracle interface

Edit the interface of IDex 7 IPriceOracle in the file interface.cairo and adapt the lines 307-312.

let oracle = IPriceOracleDispatcher { contract_address: self.oracle_address.read() };
let price: u256 = oracle.get_price(source_asset, target_asset, amount).into();
let slippage: u256 = 50_u256;  // 0.5%
let min_out = (price * (10000_u256 - slippage)) / 10000_u256;
let dex = IDexDispatcher { contract_address: self.dex_address.read() };
let received: u256 = dex.swap(amount, source_asset, target_asset, min_out).into();

If the DEX doesn't require the fund to be previously sent to the DEX smart contract, edit the lines 299-304 to adapt to your mechanism.

let erc_in = IERC20Dispatcher { contract_address: source_asset };
let bal: u256 = erc_in.balance_of(owner).into();
assert(bal >= amount, 'INSUFFICIENT_FUNDS');
let allowance: u256 = erc_in.allowance(owner, get_contract_address()).into();
assert(allowance >= amount, 'INSUFFICIENT_ALLOWANCE');
erc_in.transfer_from(owner, get_contract_address(), amount);

3. Deploy the DCA smart-contract

Deploy the DCA smart-contract and initialize it with the correct addresses:

  • operator: Address linked to the node calling the execute function
  • dex_address: Address of the DEX the DCA is performed on
  • oracle_address: Address of the price oracle

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages