diff --git a/developers/contracts/README.md b/developers/contracts/README.md index 39b8da8..1a9afe3 100644 --- a/developers/contracts/README.md +++ b/developers/contracts/README.md @@ -20,7 +20,7 @@ The XLink ecosystem offers three main features that are implemented along differ #### BTC Peg-In Endpoint -- Contract names: `btc-peg-in-endpoint-v2-05`, `btc-peg-in-endpoint-v2-05-lisa`, `btc-peg-in-v2-05-launchpad`, `btc-peg-in-v2-07-swap`. +- Contract names: `btc-peg-in-endpoint-v2-05`, `btc-peg-in-endpoint-v2-05-lisa`, `btc-peg-in-v2-05-launchpad`, `btc-peg-in-v2-07-swap`, `btc-peg-in-v2-07a-agg`. - [Complete technical documentation](btc-peg-in-endpoint.md) This endpoint is responsible for managing the bridging of BTC from Bitcoin chain into Stacks chain as bridged BTC (aBTC). @@ -69,7 +69,7 @@ This endpoint is responsible for managing the transfer of assets from other EVM- #### Cross Peg-Out Endpoint -- Contract name: `cross-peg-out-endpoint-v2-01` +- Contract names: `cross-peg-out-endpoint-v2-01`, `cross-peg-out-v2-01-agg`. - [Complete technical documentation](cross-peg-out-endpoint.md) This endpoint is responsible for managing the transfer of `SIP-010` bridged tokens from the Stacks network to EVM-like blockchains. diff --git a/developers/contracts/btc-peg-in-endpoint.md b/developers/contracts/btc-peg-in-endpoint.md index 7e7cd5b..35862d5 100644 --- a/developers/contracts/btc-peg-in-endpoint.md +++ b/developers/contracts/btc-peg-in-endpoint.md @@ -1,8 +1,8 @@ # btc-peg-in-endpoint - Location: `xlink/packages/contracts/bridge-stacks/contracts` -- Deployed contracts: [btc-peg-in-endpoint-v2-05](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-endpoint-v2-05?chain=mainnet), [btc-peg-in-endpoint-v2-05-lisa](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-endpoint-v2-05-lisa?chain=mainnet), [btc-peg-in-v2-05-launchpad](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-v2-05-launchpad?chain=mainnet), [btc-peg-in-v2-07-swap](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-v2-07-swap?chain=mainnet). +- Deployed contracts: [btc-peg-in-endpoint-v2-05](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-endpoint-v2-05?chain=mainnet), [btc-peg-in-endpoint-v2-05-lisa](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-endpoint-v2-05-lisa?chain=mainnet), [btc-peg-in-v2-05-launchpad](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-v2-05-launchpad?chain=mainnet), [btc-peg-in-v2-07-swap](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-v2-07-swap?chain=mainnet), [btc-peg-in-v2-07a-agg](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.btc-peg-in-v2-07a-agg?chain=mainnet) -This technical document provides a detailed overview of the contracts responsible for managing the peg-in process, enabling the transfer of BTC from the Bitcoin network to the Stacks network. In this process, BTC is represented as bridged tokens on Stacks (aBTC). The module's core functionality is implemented through a series of public functions distributed across three specialized contracts. Each contract addresses specific aspects of the BTC peg-in process. +This technical document provides a detailed overview of the contracts responsible for managing the peg-in process, enabling the transfer of BTC from the Bitcoin network to the Stacks network. In this process, BTC is represented as bridged tokens on Stacks (aBTC). The module's core functionality is implemented through a series of public functions distributed across five specialized contracts. Each contract addresses specific aspects of the BTC peg-in process. This functionality is implemented and distributed across the following contracts: @@ -10,6 +10,7 @@ This functionality is implemented and distributed across the following contracts - `btc-peg-in-endpoint-v2-05-lisa`: extends Bitcoin peg-in operations by converting BTC into LiaBTC through intermediate bridging steps, ultimately enabling the issuance of BRC-20 tokens on Bitcoin. - `btc-peg-in-v2-05-launchpad`: facilitates BTC peg-ins specifically for participation in launchpad projects on Stacks. - `btc-peg-in-v2-07-swap`: enables the bridging of BTC into the Stacks network while enabling token swaps to convert BTC into other predefined assets during the process. +- `btc-peg-in-v2-07a-agg`: facillitates the BTC peg-in process for swaps with non-ALEX liquidity aggregators. Liquidity aggregators optimize token exchanges by accessing multiple liquidity sources. This allows ALEX to execute swaps even when there are no ALEX pools for a specific token pair. ## Storage ###### _(all contracts include the following variables unless otherwise specified)_ @@ -46,7 +47,7 @@ The percentage fee charged for peg-in transactions. By default, this value is `0 The minimum fee required for a peg-in transaction, regardless of the transaction amount. For each transaction, the fee is the maximum value between the calculated fee amount and the `peg-in-min-fee`. By default, this value is `0`. ### `btc-peg-outfee` -###### _(only present in btc-peg-in-v2-07-swap)_ +###### _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ | Data | Type | | -------- | ------ | @@ -55,7 +56,7 @@ The minimum fee required for a peg-in transaction, regardless of the transaction This variable represents the percentage fee applied to BTC peg-out operations during cross-swap transactions, where BTC is swapped and routed across chains to reach the final recipient. By default, it is initialized to `0` and can be updated via governance functions. ### `btc-peg-out-min-fee` -###### _(only present in btc-peg-in-v2-07-swap)_ +###### _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ | Data | Type | | -------- | ------ | @@ -68,7 +69,7 @@ This variable sets the minimum fee required for BTC peg-out operations during cr #### `finalize-peg-in-cross` ###### _(in contract btc-peg-in-endpoint-v2-05)_ This function manages the peg-in process for transferring BTC to Stacks with support for cross-chain routing. It validates the provided Bitcoin transaction (which represents the transfer of BTC to a peg-in address on the Bitcoin network), ensuring it has been mined and meets the necessary conditions including an approved peg-in address. The function also performs additional checks involving a "reveal transaction," which specifies the token and chain-id for the destination chain. -Once the transaction is validated, the function calculates fees, verifies that the asset being transferred is approved for bridging operations in the `.btc-bridge-registry-v2-01` contract, and registers the transaction status. Once validated, the function mints bridged BTC tokens and sends them to the recipient specified in the transaction details via the `.cross-router-v2-03 contract`. If the validation or routing fails, a refund is executed. +Once the transaction is validated, the function calculates fees, verifies that the asset being transferred is approved for bridging operations in the `.btc-bridge-registry-v2-01` contract, and registers the transaction status. Once validated, the function mints bridged BTC tokens and sends them to the recipient specified in the transaction details via the `.cross-router-v2-03`. If the validation or routing fails, a refund is executed. ##### Parameters ```lisp @@ -135,6 +136,25 @@ In case of any error, it invokes the internal [refund](btc-peg-in-endpoint.md#re (order-idx uint) ``` +### `finalize-peg-in-agg` + +###### _(in contract btc-peg-in-v2-07a-agg)_ + +This function facilitates a BTC peg-in operation designed for aggregated cross-chain routing. Unlike standard peg-in processes where users receive aBTC directly on Stacks, this function integrates a routing mechanism that forwards the bridged tokens for immediate cross-chain processing. +The process begins by verifying that the provided Bitcoin transaction has been mined and meets all peg-in validation criteria. It checks that the peg-in address is approved and calculates the required transaction fees. Once validated, the function mints aBTC for the net amount (after deducting fees) and registers the peg-in transaction in the `.btc-bridge-registry-v2-01` contract. +Instead of keeping the minted aBTC within the Stacks ecosystem, this function directly transfers it to the `.cross-peg-out-v2-01-agg` contract. This interaction enables automated routing and potential asset swaps to facilitate seamless movement of assets across blockchains. The function logs transaction details and ensures that any failure in the process triggers a refund mechanism. + +##### Parameters +```lisp +(tx (buff 32768)) +(block { header: (buff 80), height: uint }) +(proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint }) +(output-idx uint) +(reveal-tx { tx: (buff 32768), order-idx: uint }) +(reveal-block { header: (buff 80), height: uint }) +(reveal-proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint }) +``` + ### Governance features #### `is-dao-or-extension` This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract. @@ -175,7 +195,7 @@ This feature allows to set the minimum fee required for a peg-in transaction. ``` #### `set-btc-peg-out-fee` -###### _(only present in btc-peg-in-v2-07-swap)_ +###### _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ This feature sets the percentage fee applied to BTC peg-out operations. ##### Parameters @@ -184,7 +204,7 @@ This feature sets the percentage fee applied to BTC peg-out operations. ``` #### `set-btc-peg-out-min-fee` -###### _(only present in btc-peg-in-v2-07-swap)_ +###### _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ This feature establishes the minimum fee required for BTC peg-out operations. ##### Parameters @@ -195,8 +215,8 @@ This feature establishes the minimum fee required for BTC peg-out operations. ### Supporting features The following functions are tools to assist the off-chain activities. 1. Construct and destruct helpers (`destruct-principal`, `construct-principal`). -2. Order creation helpers (`create-order-cross-or-fail`, `create-order-cross-swap-or-fail`, `create-order-mint-liabtc-or-fail`, `create-order-launchpad-or-fail`). -3. Decoding helpers (`decode-order-cross-from-reveal-tx-or-fail`, `decode-order-cross-swap-from-reveal-tx-or-fail`). +2. Order creation helpers (`create-order-cross-or-fail`, `create-order-cross-swap-or-fail`, `create-order-mint-liabtc-or-fail`, `create-order-launchpad-or-fail`, `create-order-agg-or-fail`). +3. Decoding helpers (`decode-order-cross-from-reveal-tx-or-fail`, `decode-order-cross-swap-from-reveal-tx-or-fail`, `decode-order-agg-or-fail`, `decode-order-agg-from-reveal-tx-or-fail`). ### Relevant internal functions @@ -218,8 +238,8 @@ The following functions are tools to assist the off-chain activities. ```lisp (tx (buff 32768)) ``` -#### `get-btc-peg-out-fee` _(only present in btc-peg-in-v2-07-swap)_ -#### `get-btc-peg-out-min-fee` _(only present in btc-peg-in-v2-07-swap)_ +#### `get-btc-peg-out-fee` _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ +#### `get-btc-peg-out-min-fee` _(only present in btc-peg-in-v2-07-swap and btc-peg-in-v2-07a-agg)_ #### `get-liabtc-decimals` _(only present in btc-peg-in-endpoint-v2-05-lisa)_ ## Contract calls (interactions) @@ -234,6 +254,7 @@ The following functions are tools to assist the off-chain activities. - `liabtc-mint-endpoint`: this contract is called to validate and mint liabtc during peg-in operations. - `token-wvliabtc`: this contract handles the management of wvliabtc tokens, the wrapped representation of liabtc on the Stacks network. It is called to mint and manage wvliabtc during peg-in operations. - `meta-peg-out-endpoint-v2-04`: this contract is called to bridge wvLiaBTC from the Stacks network to Bitcoin as a BRC-20 token, transferring it to a specified address. +- `cross-peg-out-v2-01-agg`: this contract is called to finalize the transaction by preparing the swap to be executed in an EVM-compatible blockchain. ## Errors diff --git a/developers/contracts/cross-peg-out-endpoint.md b/developers/contracts/cross-peg-out-endpoint.md index 5b085f5..c9c1847 100644 --- a/developers/contracts/cross-peg-out-endpoint.md +++ b/developers/contracts/cross-peg-out-endpoint.md @@ -1,9 +1,14 @@ -# cross-peg-out-endpoint-v2-01.clar +# cross-peg-out-endpoint -- Location: `xlink/packages/contracts/bridge-stacks/contracts/cross-peg-out-endpoint-v2-01.clar` -- [Deployed contract](https://explorer.hiro.so/txid/SP2XD7417HGPRTREMKF748VNEQPDRR0RMANB7X1NK.cross-peg-out-endpoint-v2-01?chain=mainnet) +- Location: `xlink/packages/contracts/bridge-stacks/contracts` +- Deployed contracts:[cross-peg-out-endpoint-v2-01](https://explorer.hiro.so/txid/SP2XD7417HGPRTREMKF748VNEQPDRR0RMANB7X1NK.cross-peg-out-endpoint-v2-01?chain=mainnet), [cross-peg-out-v2-01-agg](https://explorer.hiro.so/txid/SP673Z4BPB4R73359K9HE55F2X91V5BJTN5SXZ5T.cross-peg-out-v2-01-agg?chain=mainnet) -This technical document provides a detailed overview of the contract responsible for managing the peg-out process, enabling the transfer of `SIP-010` bridged tokens from the Stacks network to EVM-compatible blockchain networks as the EVM-based assets. The contract manages token transfers by validating amounts, applying fees, and maintaining a whitelist for authorized users if enabled. During the process, tokens are either burned or transferred (depending on the token's configuration) to a designated address on the EVM chain. The core functionalities of the contract are implemented through a series of public and governance functions, as described below. +This technical document provides a detailed overview of the contracts responsible for managing the peg-out process, enabling the transfer of `SIP-010` bridged tokens from the Stacks network to EVM-compatible blockchain networks as EVM-based assets. The contracts manage token transfers by validating amounts and applying fees. During the process, tokens are either burned or transferred (depending on the token's configuration) to a designated address on the EVM chain. + +The core functionalities of the module are implemented through public and governance functions in the following contracts: + +- `cross-peg-out-endpoint-v2-01`: handles bridging of aBTC tokens from Stacks to EVM-compatible blockchains. +- `cross-peg-out-v2-01-agg`: enables the bridging of tokens from Stacks to an EVM-compatible blockchain to perform a swap on a non-ALEX liquidity aggregator. ## Storage ### `is-paused` @@ -14,6 +19,8 @@ This technical document provides a detailed overview of the contract responsible A flag that indicates whether the peg-out process is active. If set to `true`, all peg-out operations are paused, preventing any new transactions. The contract is deployed in a paused state by default. ### `use-whitelist` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + | Data | Type | | -------- | ------ | | Variable | `bool` | @@ -21,6 +28,8 @@ A flag that indicates whether the peg-out process is active. If set to `true`, a A flag that determines whether a whitelist is enforced for peg-out operations. If set to `true`, only addresses explicitly whitelisted can execute peg-out transactions. By default, this feature is disabled. ### `whitelisted-users` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + | Data | Type | | -------- | ------ | | Map | `principal → bool` | @@ -46,11 +55,30 @@ At the end of the process, the function logs key destination details, including (settle-address (buff 256)) ``` +#### `transfer-to-swap` + +###### _(in contract cross-peg-out-v2-01-agg)_ +This function handles the peg-out process from Stacks to an EVM-compatible blockchain. It prepares the transaction to be sent to an external liquidity aggregator in the `BridgeEndpointWithSwap.sol` contract. Its core functionality consists of validating amounts, deducting fees, transferring tokens and emitting an event with the transaction details. +The function begins by validating the transfer through the `validate-transfer-to-swap` function, performing checks such as token and chain approval, amount thresholds, and sufficient reserves for the operation. +Once validated, the function calculates the required fee and determines the net amount to be transferred. Depending on the token's properties, the function either burns the tokens or transfers them to the `cross-bridge-registry-v2-01` contract. This action records the transaction and prepares it for further processing. +At this point, an event is emitted, signaling relayers to detect and forward the transaction to `BridgeEndpointWithSwap`, which completes the operation through an external liquidity aggregator. + +```lisp +(amount-in-fixed uint) +(token-in-trait ) +(token-out principal) +(min-amount-out (optional uint)) +(dest-chain-id uint) +(settle-details { address: (buff 256), chain-id: (optional uint) }) +``` + ### Governance features #### `is-dao-or-extension` This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract. #### `is-whitelisted` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + A read-only function that checks whether a specific user is included in the `whitelisted-users` map. It returns `true` if the user is whitelisted; otherwise, it returns `false`. ##### Parameters @@ -70,6 +98,8 @@ A public function, governed through the `is-dao-or-extension`, that can change t ``` #### `apply-whitelist` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + A public function, governed through the `is-dao-or-extension`, that enables or disables the whitelist mechanism for peg-out transactions. When enabled, only users listed in the `whitelisted-users` map can perform peg-out operations. ##### Parameters @@ -78,6 +108,8 @@ A public function, governed through the `is-dao-or-extension`, that enables or d ``` #### `whitelist` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + A public function, governed through the `is-dao-or-extension`, that allows the DAO to manage user access to the peg-out feature. It updates the `whitelisted-users` map to either include or remove a specific user. If the `whitelisted` parameter is set to `true`, the user is added to the whitelist; if set to `false`, the user is removed. ##### Parameters @@ -87,6 +119,8 @@ A public function, governed through the `is-dao-or-extension`, that allows the D ``` #### `whitelist-many` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + A public function, governed through the `is-dao-or-extension`, that allows the DAO to update the whitelist for multiple users in a single call. It works as a batch operation for the `whitelist` function, applying the specified whitelist status (`true` to add or `false` to remove) for each user in the provided list. The `whitelisted-users` map is updated. ##### Parameters @@ -98,6 +132,8 @@ A public function, governed through the `is-dao-or-extension`, that allows the D ### Getters #### `get-use-whitelist` +###### _(only present in cross-peg-out-endpoint-v2-01)_ + #### `get-approved-chain-or-fail` ##### Parameters ```lisp