### AshSwap Aggregator Data Structures (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Defines the core data structures used within the AshSwap aggregator contract. This includes `AggregatorStep` for defining swap operations and `TokenAmount` for representing token quantities, along with usage examples. ```rust // AggregatorStep: Defines a single swap step in multi-hop exchange pub struct AggregatorStep { pub token_in: TokenIdentifier, pub token_out: TokenIdentifier, pub amount_in: BigUint, // Use 0 to swap all available tokens pub pool_address: ManagedAddress, pub function_name: ManagedBuffer, pub arguments: ManagedVec>, } // TokenAmount: Represents token identifier and amount pair pub struct TokenAmount { pub token: TokenIdentifier, pub amount: BigUint, } // Usage example: let step = AggregatorStep { token_in: TokenIdentifier::from("USDC-abcdef"), token_out: TokenIdentifier::from("BUSD-abcdef"), amount_in: BigUint::from(1000000u64), pool_address: ManagedAddress::from("erd1pool..."), function_name: ManagedBuffer::from("exchange"), arguments: ManagedVec::from(vec![ ManagedBuffer::from("BUSD-abcdef"), ManagedBuffer::from("1") // Additional pool-specific args ]) }; let limit = TokenAmount { token: TokenIdentifier::from("BUSD-abcdef"), amount: BigUint::from(950000u64) // Minimum acceptable output }; ``` -------------------------------- ### Get Claimable AshSwap Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Retrieves AshSwap fee amounts in paginated batches to efficiently manage potentially large datasets and avoid memory issues. It takes start and end indices to specify the range of fees to retrieve. ```rust #[view(getClaimabeAshswapFee)] fn get_claimable_ashswap_fee( &self, from_idx: u64, to_idx: u64 ) -> ManagedVec> // Usage example: // Query AshSwap fees page by page let batch1 = contract.get_claimable_ashswap_fee(0, 50); // Returns: [TokenAmount(WEGLD, 5000), TokenAmount(USDC, 3000), ...] let batch2 = contract.get_claimable_ashswap_fee(50, 100); // Continues from index 50 to get more token fees ``` -------------------------------- ### Get Claimable Protocol Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Retrieves protocol fee amounts in paginated batches to prevent memory overflow, especially when dealing with a large number of token types. It requires the protocol address and start and end indices for the desired range of fees. ```rust #[view(getClaimabeProtocolFee)] fn get_claimable_protocol_fee( &self, protocol: ManagedAddress, from_idx: u64, to_idx: u64 ) -> ManagedVec> // Usage example: // Query protocol fees in batches let protocol_address = ManagedAddress::from("erd1protocol..."); // Get first 10 token fees (index 0-9) let batch1 = contract.get_claimable_protocol_fee( protocol_address.clone(), 0, 10 ); // Returns: [TokenAmount(USDC, 1000), TokenAmount(USDT, 2000), ...] // Get next 10 token fees (index 10-19) let batch2 = contract.get_claimable_protocol_fee( protocol_address, 10, 20 ); // Pagination prevents memory overflow with many token types ``` -------------------------------- ### POST /aggregate Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Performs multi-step token exchanges starting with ESDT token payments. This endpoint allows users to swap one ESDT token for another through a series of predefined steps, with slippage protection. ```APIDOC ## POST /aggregate ### Description Performs multi-step token exchanges starting with ESDT token payments. Users can define a sequence of token swaps, and the contract will route them through specified pools to achieve the best rate, while respecting minimum output limits. ### Method `POST` ### Endpoint `/aggregate` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **steps** (ManagedVec>) - Required - A vector defining the sequence of token swaps. Each step includes input/output tokens, amount, pool address, function name, and arguments. - **limits** (MultiValueEncoded>) - Required - A vector of `TokenAmount` specifying the minimum expected output amount for certain tokens to enforce slippage protection. ### Request Example ```rust let steps = vec![ AggregatorStep { token_in: TokenIdentifier::from("USDC-abcdef"), token_out: TokenIdentifier::from("USDT-abcdef"), amount_in: BigUint::from(1_000_000u64), pool_address: ManagedAddress::from("erd1pool1..."), function_name: ManagedBuffer::from("swapTokensFixedInput"), arguments: ManagedVec::from(vec![ManagedBuffer::from("USDT-abcdef")]) }, AggregatorStep { token_in: TokenIdentifier::from("USDT-abcdef"), token_out: TokenIdentifier::from("BUSD-abcdef"), amount_in: BigUint::zero(), // Use all available USDT pool_address: ManagedAddress::from("erd1pool2..."), function_name: ManagedBuffer::from("exchange"), arguments: ManagedVec::from(vec![ManagedBuffer::from("BUSD-abcdef")]) } ]; let limits = vec![ TokenAmount::new(TokenIdentifier::from("USDC-abcdef"), BigUint::zero()), TokenAmount::new(TokenIdentifier::from("BUSD-abcdef"), BigUint::from(900_000u64)) // Min output ]; // User sends 1,000,000 USDC let payment = EsdtTokenPayment::new( TokenIdentifier::from("USDC-abcdef"), 0, BigUint::from(1_000_000u64) ); let result = contract.aggregate(steps, limits); ``` ### Response #### Success Response (200) - **result** (ManagedVec) - A vector containing the final `EsdtTokenPayment` received by the user after all swap steps are completed. #### Response Example ```json { "result": [ { "tokenIdentifier": "BUSD-abcdef", "amount": "950000" } ] } ``` ``` -------------------------------- ### Aggregate ESDT Tokens in AshSwap Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Performs multi-step token exchanges starting with ESDT token payments. It takes a vector of aggregation steps and a list of minimum token amounts to ensure. The function returns the final ESDT payment after all swaps are completed. ```rust #[payable("*", "ESDT")] #[endpoint] fn aggregate( &self, steps: ManagedVec>, limits: MultiValueEncoded> ) -> ManagedVec // Usage example: // User swaps USDC -> USDT -> BUSD in two steps let steps = vec![ AggregatorStep { token_in: TokenIdentifier::from("USDC-abcdef"), token_out: TokenIdentifier::from("USDT-abcdef"), amount_in: BigUint::from(1_000_000u64), pool_address: ManagedAddress::from("erd1pool1..."), function_name: ManagedBuffer::from("swapTokensFixedInput"), arguments: ManagedVec::from(vec![ ManagedBuffer::from("USDT-abcdef") ]) }, AggregatorStep { token_in: TokenIdentifier::from("USDT-abcdef"), token_out: TokenIdentifier::from("BUSD-abcdef"), amount_in: BigUint::zero(), // Use all available USDT pool_address: ManagedAddress::from("erd1pool2..."), function_name: ManagedBuffer::from("exchange"), arguments: ManagedVec::from(vec![ ManagedBuffer::from("BUSD-abcdef") ]) } ]; let limits = vec![ TokenAmount::new(TokenIdentifier::from("USDC-abcdef"), BigUint::zero()), TokenAmount::new(TokenIdentifier::from("BUSD-abcdef"), BigUint::from(900_000u64)) // Min output ]; // User sends 1,000,000 USDC let payment = EsdtTokenPayment::new( TokenIdentifier::from("USDC-abcdef"), 0, BigUint::from(1_000_000u64) ); let result = contract.aggregate(steps, limits); // Returns: ManagedVec containing final BUSD payment ``` -------------------------------- ### Initialize Contract Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Initializes the AshSwap Aggregator smart contract with the EGLD wrapper address and token identifier. ```APIDOC ## Initialize Contract ### Description Initializes the aggregator contract with EGLD wrapper configuration. ### Method `init` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **egld_wrapper_address** (ManagedAddress) - Required - The address of the EGLD wrapper contract. - **egld_wrapped_token_id** (TokenIdentifier) - Required - The identifier of the wrapped EGLD token. ### Request Example ```rust let wrapper_address = ManagedAddress::from("erd1..."); let wegld_token = TokenIdentifier::from("WEGLD-abcdef"); contract.init(wrapper_address, wegld_token); ``` ### Response #### Success Response (200) None (Initialization is a contract deployment/setup function) #### Response Example None ``` -------------------------------- ### Register Protocol Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Configures the fee percentage and whitelist address for a specific protocol. This function can only be called by the owner. It takes the fee percentage and the protocol's address as input. ```rust #[only_owner] #[endpoint(registerProtocolFee)] fn register_protocol_fee(&self, fee_percent: u64, whitelist_address: ManagedAddress) ``` ```rust // Usage example: // Owner registers a DEX protocol with 0.5% fee (500 basis points out of 100,000) let protocol_address = ManagedAddress::from("erd1protocol123..."); let fee_percent = 500u64; // 0.5% = 500/100,000 contract.register_protocol_fee(fee_percent, protocol_address); // Protocol can now be used in aggregate calls with fee deduction // MAX_FEE_PERCENT = 100,000 (representing 100%) ``` -------------------------------- ### Register AshSwap Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Configures the fee percentage and destination address for AshSwap service fees. This function can only be called by the owner. It takes the fee percentage and the fee collection address as input. ```rust #[only_owner] #[endpoint(registerAshswapFee)] fn register_ashswap_fee(&self, fee_percent: u64, whitelist_address: ManagedAddress) ``` ```rust // Usage example: // Owner sets AshSwap fee to 0.3% with fee collection address let ashswap_fee_address = ManagedAddress::from("erd1ashswap..."); let ashswap_fee_percent = 300u64; // 0.3% = 300/100,000 contract.register_ashswap_fee(ashswap_fee_percent, ashswap_fee_address); // AshSwap fees are automatically deducted from protocol fees ``` -------------------------------- ### Initialize AshSwap Aggregator Contract Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Initializes the aggregator contract, setting the EGLD wrapper address and the token identifier for wrapped EGLD. This function is typically called once during contract deployment. ```rust #[init] fn init(&self, egld_wrapper_address: ManagedAddress, egld_wrapped_token_id: TokenIdentifier) // Usage example: // Owner deploys and initializes the contract let wrapper_address = ManagedAddress::from("erd1..."); let wegld_token = TokenIdentifier::from("WEGLD-abcdef"); contract.init(wrapper_address, wegld_token); ``` -------------------------------- ### POST /aggregateEgld Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Handles aggregation when the user sends native EGLD, automatically wrapping it to WEGLD before performing the specified token swaps. Supports optional protocol fees. ```APIDOC ## POST /aggregateEgld ### Description Handles aggregation when a user sends native EGLD. The contract automatically wraps EGLD to WEGLD and then performs the specified token swaps. It also allows for an optional protocol address to deduct service fees. ### Method `POST` ### Endpoint `/aggregateEgld` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **steps** (ManagedVec>) - Required - A vector defining the sequence of token swaps. Each step includes input/output tokens, amount, pool address, function name, and arguments. The first step typically involves swapping WEGLD. - **limits** (ManagedVec>) - Required - A vector of `TokenAmount` specifying the minimum expected output amount for certain tokens to enforce slippage protection. - **protocol** (OptionalValue) - Optional - The address of a protocol to which a service fee should be sent. ### Request Example ```rust let steps = vec![ AggregatorStep { token_in: TokenIdentifier::from("WEGLD-abcdef"), token_out: TokenIdentifier::from("USDC-abcdef"), amount_in: BigUint::from(1_000_000_000_000_000_000u64), // 1 EGLD pool_address: ManagedAddress::from("erd1pool..."), function_name: ManagedBuffer::from("swapEgldFixedInput"), arguments: ManagedVec::new() } ]; let limits = vec![ TokenAmount::new( TokenIdentifier::from("WEGLD-abcdef"), BigUint::zero() ), TokenAmount::new( TokenIdentifier::from("USDC-abcdef"), BigUint::from(1900_000_000u64) // Min 1900 USDC expected ) ]; let protocol_addr = OptionalValue::Some(ManagedAddress::from("erd1protocol...")); // User sends 1 EGLD with transaction let result = contract.aggregate_egld(steps, limits, protocol_addr); ``` ### Response #### Success Response (200) - **result** (ManagedVec) - A vector containing the final `EsdtTokenPayment` received by the user after all swap steps are completed (e.g., USDC payment). #### Response Example ```json { "result": [ { "tokenIdentifier": "USDC-abcdef", "amount": "1950000000" } ] } ``` ``` -------------------------------- ### Claim Protocol Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Allows registered protocols to claim accumulated fees in batches. The function takes the protocol's address as input and sends accumulated fees directly to that address. A maximum of 50 different token types can be claimed per call. ```rust #[endpoint(claimProtocolFee)] fn claim_protocol_fee(&self, protocol: ManagedAddress) ``` ```rust // Usage example: // Protocol claims accumulated fees (max 50 tokens per call) let protocol_address = ManagedAddress::from("erd1protocol..."); // First claim iteration - gets up to 50 different token types contract.claim_protocol_fee(protocol_address.clone()); // Sends accumulated fees directly to protocol address // If more than 50 token types have fees, call again contract.claim_protocol_fee(protocol_address.clone()); // CLAIM_BATCH_SIZE = 50 to prevent memory overflow ``` -------------------------------- ### Claim Protocol Fee by Tokens (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Allows protocols to claim fees for specific token identifiers. It takes the protocol's address and a vector of token identifiers as input. The accumulated fees for the specified tokens are transferred to the protocol address, and those tokens are removed from fee storage. ```rust #[endpoint(claimProtocolFeeByTokens)] fn claim_protocol_fee_by_tokens( &self, protocol: ManagedAddress, tokens: ManagedVec ) ``` ```rust // Usage example: // Protocol selectively claims fees for specific tokens let protocol_address = ManagedAddress::from("erd1protocol..."); let tokens_to_claim = ManagedVec::from(vec![ TokenIdentifier::from("USDC-abcdef"), TokenIdentifier::from("USDT-abcdef"), TokenIdentifier::from("BUSD-abcdef") ]); contract.claim_protocol_fee_by_tokens(protocol_address, tokens_to_claim); // Transfers accumulated USDC, USDT, and BUSD fees to protocol // Removes claimed tokens from fee storage ``` -------------------------------- ### AshSwap Aggregator Error Constants (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Lists the predefined error constants returned by the AshSwap aggregator contract upon validation failures. These constants provide specific codes for various error conditions, aiding in client-side error handling. ```rust pub const ERROR_EMPTY_PAYMENTS: &str = "Empty payments"; pub const ERROR_ZERO_AMOUNT: &str = "Invalid amount zero"; pub const ERROR_ZERO_TOKEN_NONCE: &str = "Invalid token nonce"; pub const ERROR_SLIPPAGE_SCREW_YOU: &str = "Slippage too high"; pub const ERROR_INVALID_AMOUNT_IN: &str = "Invalid amount in"; pub const ERROR_INVALID_TOKEN_IN: &str = "Invalid token in"; pub const ERROR_OUTPUT_LEN_MISMATCH: &str = "Output length mismatch"; pub const ERROR_INVALID_POOL_ADDR: &str = "Invalid pool address"; pub const ERROR_PROTOCOL_NOT_REGISTED: &str = "Protocol unregistered"; pub const ERROR_INVALID_FEE_PERCENT: &str = "Invalid fee percent"; pub const ERROR_INVALID_ADDRESS: &str = "Invalid contract"; // Usage example - handling errors in client: // If amount_in exceeds vault balance: ERROR_INVALID_AMOUNT_IN // If token_in not found in vault: ERROR_INVALID_TOKEN_IN // If output < limit amount: ERROR_SLIPPAGE_SCREW_YOU // If protocol not registered but provided: ERROR_PROTOCOL_NOT_REGISTED ``` -------------------------------- ### Claim AshSwap Fees by Tokens (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Allows AshSwap to claim fees for specified token types. It takes a vector of token identifiers as input and transfers the corresponding fees to the ashswap_fee_address. This function is crucial for managing and collecting transaction fees within the AshSwap ecosystem. ```rust #[endpoint(claimAshswapFeeByTokens)] fn claim_ashswap_fee_by_tokens(&self, tokens: ManagedVec) // Usage example: // AshSwap claims fees for high-value tokens let priority_tokens = ManagedVec::from(vec![ TokenIdentifier::from("WEGLD-abcdef"), TokenIdentifier::from("USDC-abcdef"), TokenIdentifier::from("USDT-abcdef") ]); contract.claim_ashswap_fee_by_tokens(priority_tokens); // Transfers specified token fees to ashswap_fee_address ``` -------------------------------- ### Aggregate ESDT with Optional EGLD Return (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Performs token aggregation with an option to unwrap the final WEGLD back to EGLD. It takes aggregation steps, token limits, an egld_return flag, and an optional protocol address as input. Returns the amount of EGLD sent and a vector of other ESDT tokens. ```rust #[payable("*")] #[endpoint(aggregateEsdt)] fn aggregate_esdt( &self, steps: ManagedVec>, limits: ManagedVec>, egld_return: bool, protocol: OptionalValue ) -> MultiValue2> ``` ```rust // Usage example: // User swaps USDC -> WEGLD and wants native EGLD back let steps = vec![ AggregatorStep { token_in: TokenIdentifier::from("USDC-abcdef"), token_out: TokenIdentifier::from("WEGLD-abcdef"), amount_in: BigUint::from(2000_000_000u64), // 2000 USDC pool_address: ManagedAddress::from("erd1pool..."), function_name: ManagedBuffer::from("swapTokensFixedInput"), arguments: ManagedVec::from(vec![ ManagedBuffer::from("WEGLD-abcdef") ]) } ]; let limits = vec![ TokenAmount::new(TokenIdentifier::from("USDC-abcdef"), BigUint::zero()), TokenAmount::new( TokenIdentifier::from("WEGLD-abcdef"), BigUint::from(950_000_000_000_000_000u64) // Min 0.95 WEGLD ) ]; let payment = EsdtTokenPayment::new( TokenIdentifier::from("USDC-abcdef"), 0, BigUint::from(2000_000_000u64) ); let result = contract.aggregate_esdt( steps, limits, true, // egld_return = true, unwrap WEGLD to EGLD OptionalValue::None ); // Returns: (BigUint: EGLD amount sent, ManagedVec: other ESDT tokens) // User receives native EGLD instead of WEGLD token ``` -------------------------------- ### Claim AshSwap Fee (Rust) Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Allows AshSwap to claim accumulated service fees in batches. This function does not require any parameters as it uses the stored fee address. It sends accumulated fees to the registered ashswap_fee_address. Multiple calls may be needed if many token types have accumulated fees. ```rust #[endpoint(claimAshswapFee)] fn claim_ashswap_fee(&self) ``` ```rust // Usage example: // AshSwap claims accumulated fees (up to 50 token types per call) contract.claim_ashswap_fee(); // Sends fees to registered ashswap_fee_address // No parameters needed - uses stored fee address // If many token types accumulated, call multiple times contract.claim_ashswap_fee(); contract.claim_ashswap_fee(); ``` -------------------------------- ### Aggregate with EGLD Input in AshSwap Source: https://context7.com/ashswap/ash-aggregator-sc/llms.txt Handles token aggregations when the user sends native EGLD. This function automatically wraps the EGLD to WEGLD before executing the swap steps. It also supports an optional protocol address for fee deduction and returns the final ESDT payment. ```rust #[payable("EGLD")] #[endpoint(aggregateEgld)] fn aggregate_egld( &self, steps: ManagedVec>, limits: ManagedVec>, protocol: OptionalValue ) -> ManagedVec // Usage example: // User swaps EGLD -> WEGLD -> USDC with protocol fee let steps = vec![ AggregatorStep { token_in: TokenIdentifier::from("WEGLD-abcdef"), token_out: TokenIdentifier::from("USDC-abcdef"), amount_in: BigUint::from(1_000_000_000_000_000_000u64), // 1 EGLD pool_address: ManagedAddress::from("erd1pool..."), function_name: ManagedBuffer::from("swapEgldFixedInput"), arguments: ManagedVec::new() } ]; let limits = vec![ TokenAmount::new( TokenIdentifier::from("WEGLD-abcdef"), BigUint::zero() ), TokenAmount::new( TokenIdentifier::from("USDC-abcdef"), BigUint::from(1900_000_000u64) // Min 1900 USDC expected ) ]; let protocol_addr = OptionalValue::Some(ManagedAddress::from("erd1protocol...")); // User sends 1 EGLD with transaction let result = contract.aggregate_egld(steps, limits, protocol_addr); // Contract wraps EGLD, deducts fees, executes swap, returns USDC ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.