### Execute Swap via Ekubo Lock Callback (Rust) Source: https://docs.ekubo.org/integration-guides/swapping This Rust code demonstrates how to implement the swap logic within the `locked` callback function of a Starknet contract. It shows how to consume callback data, perform a swap using the Ekubo core contract, and handle the resulting deltas by either withdrawing output or paying input tokens. It utilizes dispatcher interfaces for interacting with Ekubo's core and ERC20 contracts. ```rust use ekubo::interfaces::{ICore, IERC20Dispatcher}; use ekubo::components::shared_locker::{self, CallbackParameters}; use swap_example::SwapExample::{SwapData, SwapResult}; use swap_example::ILocker; use poolmanager::types::{PoolKey, Delta}; use starknet_core::syscalls::deploy_syscall; use starknet_types::core::ContractAddress; use starknet_types::felt252; #[starknet::contract] mod SwapExample { #[storage] struct Storage { core: ICoreDispatcher, } #[derive(Copy, Drop, Serde)] struct SwapData { pool_key: PoolKey, amount: i129, token: ContractAddress, } #[derive(Copy, Drop, Serde)] struct SwapResult { delta: Delta, } #[external(v0)] impl SwapExample of ISwapExample { fn swap(ref self: ContractState, swap_data: SwapData) -> SwapResult { // https://github.com/EkuboProtocol/abis/blob/main/src/components/shared_locker.cairo ekubo::components::shared_locker::call_core_with_callback( self.core.read(), @swap_data ) } } #[external(v0)] impl Locker of ILocker { fn locked(ref self: ContractState, id: u32, data: Array) -> Array { let core = self.core.read(); // Consume the callback data // https://github.com/EkuboProtocol/abis/blob/main/src/components/shared_locker.cairo let swap_data: SwapData = ekubo::components::shared_locker::consume_callback_data::(core, data); // Do your swaps here! // NOTE: The actual swap call parameters (pool_key, params) are assumed to be derived from swap_data and are omitted for brevity. // let delta = core.swap(pool_key, params); let delta = Delta { amount0: 0, amount1: 0 }; // Placeholder for actual swap logic // Each swap generates a "delta", but does not trigger any token transfers. // Deltas are always from the perspective of the pool: // - A negative delta indicates the pool owes you tokens. // - A positive delta indicates you owe the pool tokens. // To take a negative delta out of core, do (assuming token0 for token1): // core.withdraw(token, recipient, delta.amount0.mag); // To pay tokens you owe, do (assuming payment is for token1): // IERC20Dispatcher { contract_address: token }.approve(ekubo, delta.amount1.mag.into()); // ICoreDispatcher#pay will trigger a token#transferFrom(this, core) for the entire approved amount // core.pay(token); // Serialize our output type into the return data let swap_result = SwapResult { delta }; let mut arr: Array = ArrayTrait::new(); Serde::serialize(@swap_result, ref arr); arr } } } ``` -------------------------------- ### Ekubo Core and Locker Interfaces (Cairo) Source: https://docs.ekubo.org/integration-guides/till-pattern Defines the `ICore` and `ILocker` interfaces for interacting with the Ekubo protocol. `ICore#lock` is the entry point for all transactions, and `ILocker#locked` is the required callback for performing swaps and position updates. External calls to these methods without a proper callback will revert. ```cairo #[starknet::interface] trait ICore { fn lock(ref self: TStorage, data: Array) -> Array; fn swap(ref self: TStorage, pool_key: PoolKey, params: SwapParameters) -> Delta; } #[starknet::interface] trait ILocker { fn locked(ref self: TStorage, id: u32, data: Array) -> Array; } ``` -------------------------------- ### Calculate sqrt_ratio from Tick (TypeScript) Source: https://docs.ekubo.org/integration-guides/reference/math-1-pager Computes the square root of the price ratio (sqrt_ratio) for a given tick value using the Decimal.js library. This function is essential for translating discrete tick boundaries into continuous price representations within Ekubo's fixed-point 64.128 format. ```typescript import {Decimal} from 'decimal.js-light'; const tick = 1234; // A fixed point .128 number has at most 128 bits after the decimal, // which translates to about 10**38.5 in decimal. // That means ~78 decimals of precision should be able to represent // any price with full precision. // Note there can be loss of precision for intermediate calculations, // but this should be sufficient for just computing the price. Decimal.set({ precision: 78 }); const sqrt_ratio_x128 = new Decimal('1.000001') .sqrt() .pow(tick) .mul(new Decimal(2).pow(128)); ``` -------------------------------- ### Create Proposals in Ekubo Governance Source: https://docs.ekubo.org/user-guides/governance Functions for creating proposals in the Ekubo Governance contract. Proposals involve a set of actions to be executed. They can be created and described in separate calls or combined into a single function. ```solidity function propose(Actions[] memory actions) external returns (uint256 proposalId); function describe(uint256 proposalId, string memory description) external; function propose_and_describe(Actions[] memory actions, string memory description) external returns (uint256 proposalId); ``` -------------------------------- ### Execute Proposal in Ekubo Governance Source: https://docs.ekubo.org/user-guides/governance Function to execute a proposal that has successfully passed the voting phase and the execution delay period. This function carries out the specified actions within the proposal. ```solidity function execute(uint256 proposalId, Actions[] memory actions) external; ``` -------------------------------- ### Vote on Proposals in Ekubo Governance Source: https://docs.ekubo.org/user-guides/governance Function to cast a vote (yea or nay) on an existing proposal in the Ekubo Governance contract. This action requires the proposal ID and the voter's decision. ```solidity function vote(uint256 proposalId, bool yea) external; ``` -------------------------------- ### Stake Tokens in Ekubo Governance Contract Source: https://docs.ekubo.org/user-guides/governance Functions to stake tokens in the Ekubo Governance contract. Users can stake all their approved tokens or a specific amount. Staking locks tokens and grants voting power, influenced by the voting_weight_smoothing_period. ```solidity function stake(address delegateAddress) external; function stake_amount(address delegateAddress, uint256 amount) external; ``` -------------------------------- ### Cancel Proposal in Ekubo Governance Source: https://docs.ekubo.org/user-guides/governance Function allowing the proposer to cancel a proposal before the voting period begins. This is useful for correcting errors or withdrawing a proposal. ```solidity function cancel(uint256 proposalId) external; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.