From e2b3a2b3d18a9c379576d9b6d5c572eb8bbb2234 Mon Sep 17 00:00:00 2001 From: simsbluebox Date: Tue, 17 Dec 2024 18:07:23 -0300 Subject: [PATCH 1/5] meta-peg-out technical documentation --- SUMMARY.md | 1 + developers/contracts/README.md | 2 + .../contracts/meta-peg-out-endpoint-v2-03.md | 242 ++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 developers/contracts/meta-peg-out-endpoint-v2-03.md diff --git a/SUMMARY.md b/SUMMARY.md index 5812f45..a74198d 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -28,6 +28,7 @@ * [Smart Contracts](developers/contracts/README.md) * [btc peg-in endpoint v2-03](developers/contracts/btc-peg-in-endpoint-v2-03.md) * [meta peg-in endpoint v2-02](developers/contracts/meta-peg-in-endpoint-v2-02.md) + * [meta peg-out endpoint v2-03](developers/contracts/meta-peg-out-endpoint-v2-03.md) From d4ec7762a65e5a39f9e2383a61672f17d5cbb3a3 Mon Sep 17 00:00:00 2001 From: "ignacio.pena@coinfabrik.com" Date: Tue, 17 Dec 2024 18:10:54 -0300 Subject: [PATCH 2/5] btc-peg-out technical documentation --- SUMMARY.md | 1 + developers/contracts/README.md | 2 + .../contracts/btc-peg-out-endpoint-v2-01.md | 151 ++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 developers/contracts/btc-peg-out-endpoint-v2-01.md diff --git a/SUMMARY.md b/SUMMARY.md index a74198d..310caee 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -27,6 +27,7 @@ ## 🎮 Developers * [Smart Contracts](developers/contracts/README.md) * [btc peg-in endpoint v2-03](developers/contracts/btc-peg-in-endpoint-v2-03.md) + * [btc-peg-out-endpoint-v2-01](developers/contracts/btc-peg-out-endpoint-v2-01.md) * [meta peg-in endpoint v2-02](developers/contracts/meta-peg-in-endpoint-v2-02.md) * [meta peg-out endpoint v2-03](developers/contracts/meta-peg-out-endpoint-v2-03.md) diff --git a/developers/contracts/README.md b/developers/contracts/README.md index c978c4b..5b85ede 100644 --- a/developers/contracts/README.md +++ b/developers/contracts/README.md @@ -2,6 +2,8 @@ ## [btc-peg-in-endpoint-v2-03](btc-peg-in-endpoint-v2-03.md) +## [btc-peg-out-endpoint-v2-01](btc-peg-out-endpoint-v2-01.md) + ## [meta-peg-in-endpoint-v2-02](meta-peg-in-endpoint-v2-02.md) ## [meta-peg-out-endpoint-v2-03](meta-peg-out-endpoint-v2-03.md) diff --git a/developers/contracts/btc-peg-out-endpoint-v2-01.md b/developers/contracts/btc-peg-out-endpoint-v2-01.md new file mode 100644 index 0000000..3a99488 --- /dev/null +++ b/developers/contracts/btc-peg-out-endpoint-v2-01.md @@ -0,0 +1,151 @@ +# btc-peg-out-endpoint-v2-01.clar + +- Location: `xlink/packages/contracts/bridge-stacks/contracts/btc-peg-out-endpoint-v2-01.clar` +- [Deployed contract]() + +This technical document provides a detailed overview of the contract responsible for managing the peg-out process, enabling the transfer of bridged BTC from the Stacks network back to the Bitcoin network. In this process, aBTC (Bridged BTC tokens on Stacks) is burned or transferred, and BTC is released to a specified Bitcoin address. The contract's primary functionality is implemented through a series of public functions. Let's review this core operation. + + +## Storage +### `fee-to-address` +| Data | Type | +| -------- | ------ | +| Variable | `principal` | + +The address where the fees collected from peg-out operations are transferred. By default, this address is the `tx-sender` (the original deployer of the contract). + +### `peg-out-paused` +| Data | Type | +| -------- | ------ | +| Variable | `bool` | + +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. + +### `peg-out-fee` +| Data | Type | +| -------- | ------ | +| Variable | `uint` | + +The percentage fee charged for peg-out transactions. By default, this value is `u0`. + +### `peg-out-min-fee` +| Data | Type | +| -------- | ------ | +| Variable | `uint` | + +The minimum fee required for a peg-out transaction, regardless of the transaction amount. By default, this value is `u0`. + +## Features + +### Peg-out features + +#### `request-peg-out-0` +This function initiates the peg-out process, enabling users to transfer bridged BTC from the Stacks network to a specified Bitcoin address (peg-out-address). First, it validates the requested amount using `validate-peg-out-0` to ensure it meets the minimum requirements (the requested amount must be sufficient to cover both the peg-out fee and the gas fee, leaving a positive net amount available for transfer). The function registers the request by interacting with the `.btc-bridge-registry-v2-01` contract, storing details such as the requesting user, target Bitcoin address, calculated fees, and associated block heights (e.g., `block-height` and `burn-block-height`). A unique request ID is generated for tracking. Finally, the amount of aBTC is escrowed by transferring it from the user to the contract, securing the funds until the request is either completed or revoked. + +##### Parameters +```lisp +(peg-out-address (buff 128)) +(amount uint) +``` + +#### `claim-peg-out` +This function allows a user to claim a specific peg-out request. The function first retrieves the request details and performs several validations to check that the request is active and valid. It checks that the peg-out process is not paused, the request has not been finalized, revoked, or already claimed, and that all conditions for claiming are met. Upon successful validation, the function registers the request as claimed in the `.btc-bridge-registry-v2-01` contract, updating the state with the claimer's identity, the Bitcoin address (`fulfilled-by`) responsible for completing the transaction, and the block height defining the claim's expiration period. Note that the expiration period defines a specific timeframe during which the claimed request must be finalized. And finally, the updated request details are logged, and the claimer is granted the right to proceed with finalizing the peg-out. + +##### Parameters +```lisp +(request-id uint) +(fulfilled-by (buff 128)) +``` + +#### `finalize-peg-out` +This function completes the peg-out process. It performs checks to finalize the peg-out, including verifying that the transaction has been mined, and verifies that the included details in the transaction (concerning the amount, recipient address, and fulfiller address) align with the request specifications. The function interacts with the `.btc-bridge-registry-v2-01` contract to mark the request as finalized. +Additionally, the function processes fees by transferring them to the designated governance address. It then handles the bridged tokens (aBTC) associated with the request in one of two ways: +1. Approved Peg-in Address: + - The tokens are burned, reducing the total supply. +2. Third-Party Address: + - The tokens are transferred from the contract to the claimer, maintaining the total supply. + +Once all operations are completed, the function logs the transaction details and confirms the successful finalization of the request. + +##### Parameters +```lisp +(request-id uint) +(tx (buff 32768)) +(block { header: (buff 80), height: uint }) +(proof { tx-index: uint, hashes: (list 14 (buff 32)), tree-depth: uint }) +(output-idx uint) +(fulfilled-by-idx uint) +``` + +#### `revoke-peg-out` +This function allows the user who created a peg-out request to cancel it, only if certain conditions are met. The function checks that the required `request-revoke-grace-period` has passed since the request was created, and verifies that the request has not already been claimed, finalized, or already revoked. Once these validations are passed, the function updates the request's status to "revoked" in the `.btc-bridge-registry-v2-01` contract. It then processes the refund by transferring the associated fees and the bridged tokens (aBTC) back to the requester. + +##### Parameters +```lisp +(request-id 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-peg-out-paused` +A read-only function that checks the operational status of the contract. + +#### `pause-peg-out` +A public function, governed through the `is-dao-or-extension`, that can change the contract's operational status. + +##### Parameters +```lisp +(paused bool) +``` + +### Getters + +#### `get-peg-out-fee` +#### `get-peg-out-min-fee` +#### `get-request-revoke-grace-period` +#### `get-request-claim-grace-period` +#### `get-request-or-fail` +##### Parameters +```lisp +(request-id uint) +``` +#### `get-peg-in-sent-or-default` +##### Parameters +```lisp +(tx (buff 32768)) +(output uint) +``` +#### `get-fee-to-address` +#### `get-txid` +##### Parameters +```lisp +(tx (buff 32768)) +``` + +## Contract calls (interactions) +- `executor-dao`: Calls are made to verify whether a certain contract-caller is designated as an extension. +- `btc-bridge-registry-v2-01`: This contract is called to register, update, and track peg-out requests. +- `clarity-bitcoin-v1-07`: This contract is called to validate Bitcoin transactions by verifying that they have been mined and extracting relevant transaction details, such as inputs, outputs, and script data. +- `token-abtc`: This contract represents the aBTC token on the Stacks network. It is directly responsible for managing the transfer, burning, and refunding of aBTC tokens during the peg-out process. + +## Errors + +| Error Name | Value | +| ---------------- | ------------- | +| `err-unauthorised` | `(err u1000)` | +| `err-paused` | `(err u1001)` | +| `err-invalid-amount` | `(err u1003)` | +| `err-invalid-tx` | `(err u1004)` | +| `err-already-sent` | `(err u1005)` | +| `err-address-mismatch` | `(err u1006)` | +| `err-request-already-revoked` | `(err u1007)` | +| `err-request-already-finalized` | `(err u1008)` | +| `err-revoke-grace-period` | `(err u1009)` | +| `err-request-already-claimed` | `(err u1010)` | +| `err-bitcoin-tx-not-mined` | `(err u1011)` | +| `err-tx-mined-before-request` | `(err u1013)` | +| `err-slippage` | `(err u1016)` | + + \ No newline at end of file From 7c4fc214d08346e95f1bb9645ab4add6ada11eb0 Mon Sep 17 00:00:00 2001 From: simsbluebox Date: Mon, 30 Dec 2024 13:07:58 -0300 Subject: [PATCH 3/5] meta-peg-out technical documentation: @tenuki review --- developers/contracts/meta-peg-out-endpoint-v2-03.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/developers/contracts/meta-peg-out-endpoint-v2-03.md b/developers/contracts/meta-peg-out-endpoint-v2-03.md index 3040a69..ab0cd71 100644 --- a/developers/contracts/meta-peg-out-endpoint-v2-03.md +++ b/developers/contracts/meta-peg-out-endpoint-v2-03.md @@ -23,7 +23,7 @@ This data variable serves as a flag to control the operational status of the con | -------- | ------ | | Variable | `principal` | -This variable represents the address to which fees are paid. In this contract, there are two categories of fees: peg-out fees and gas fees. Peg-out fees are transactioned with the relevant token, while gas fees are handled using the Bridged BTC token (`token-abtc`). For more details on these transactions, refer to the [finalize peg-out feature](meta-peg-out-endpoint-v2-03.md#finalize-peg-out-on-index). By default, the address assigned to receive these fees is the `tx-sender` (the original deployer of the contract). +This variable represents the address to which fees are paid. In this contract, there are two categories of fees: peg-out fees and gas fees. Peg-out fees are transactioned with the relevant token, while gas fees are handled using the Bridged BTC token (`token-abtc`). For more details on these transactions, refer to the [finalize peg-out feature](meta-peg-out-endpoint-v2-03.md#finalize-peg-out-on-index). By default, the address assigned to receive these fees is the `tx-sender` address of the contract deployer. ### Relevant constants @@ -64,7 +64,7 @@ Once these validations are met, the operation is registered in the meta registry #### `claim-peg-out` -Following the peg-out request, the next potential step is for the `tx-sender` (claimer) to claim the request. This step is critical, as it marks the request as ready for finalization. This action is executed by calling the function with the `request-id`, along with the `fulfilled-by` parameter, which designates the address responsible for executing the burn blockchain operation. +Following the peg-out request, a next potential step is to claim the request. This step is critical, as it marks the request as ready for finalization. This action is executed by calling the function with the `request-id` of the previously created request, along with the `fulfilled-by` parameter, which designates the address responsible for executing the burn blockchain operation. The claim must satisfy the following validations: @@ -89,10 +89,10 @@ Key validations include: - The request must exist. - The token pair must be approved and operational (not paused for peg-out). - The transaction cannot be indexed to a time before the contract deployment. -- The transaction's metaprotocol token (BRC-20) must match the requested details (`tick`, `amount`), with `from` and `to` addresses aligning with the fulfilled-by (registered by the claimer) and the peg-out-address (initiated by the requester) respectively. +- The transaction's metaprotocol token (BRC-20) must match the requested details (`tick` and `amount`). Additionally, the `from` address must correspond to the `fulfilled-by address`, and the `to` address must match the `peg-out-address`. - The request must not be revoked or already finalized. -The procedure proceeds as follows based on who finalizes it: +The procedure is as follows, based on who finalizes it: 1) Peg-In Address - The net amount of the peg-in token requested is burned from the contract balance, affecting the overall total supply. From 1f4e38c27fe552834aef7490f035f13d835f8c4e Mon Sep 17 00:00:00 2001 From: simsbluebox Date: Mon, 30 Dec 2024 13:46:58 -0300 Subject: [PATCH 4/5] meta-peg-out technical documentation: @tenuki review - minor fix --- developers/contracts/meta-peg-out-endpoint-v2-03.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/developers/contracts/meta-peg-out-endpoint-v2-03.md b/developers/contracts/meta-peg-out-endpoint-v2-03.md index ab0cd71..e9611ed 100644 --- a/developers/contracts/meta-peg-out-endpoint-v2-03.md +++ b/developers/contracts/meta-peg-out-endpoint-v2-03.md @@ -89,7 +89,7 @@ Key validations include: - The request must exist. - The token pair must be approved and operational (not paused for peg-out). - The transaction cannot be indexed to a time before the contract deployment. -- The transaction's metaprotocol token (BRC-20) must match the requested details (`tick` and `amount`). Additionally, the `from` address must correspond to the `fulfilled-by address`, and the `to` address must match the `peg-out-address`. +- The transaction's metaprotocol token (BRC-20) must match the requested details (`tick` and `amount`). Additionally, the `from` address must correspond to the `fulfilled-by` address, and the `to` address must match the `peg-out-address`. - The request must not be revoked or already finalized. The procedure is as follows, based on who finalizes it: From 58870edddcee9d812dcb347e3f63e1f6bafed746 Mon Sep 17 00:00:00 2001 From: "ignacio.pena@coinfabrik.com" Date: Thu, 2 Jan 2025 11:22:11 -0300 Subject: [PATCH 5/5] btc-peg-out technical documentation: @tenuki review --- developers/contracts/btc-peg-out-endpoint-v2-01.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/developers/contracts/btc-peg-out-endpoint-v2-01.md b/developers/contracts/btc-peg-out-endpoint-v2-01.md index 3a99488..9218ca3 100644 --- a/developers/contracts/btc-peg-out-endpoint-v2-01.md +++ b/developers/contracts/btc-peg-out-endpoint-v2-01.md @@ -3,7 +3,7 @@ - Location: `xlink/packages/contracts/bridge-stacks/contracts/btc-peg-out-endpoint-v2-01.clar` - [Deployed contract]() -This technical document provides a detailed overview of the contract responsible for managing the peg-out process, enabling the transfer of bridged BTC from the Stacks network back to the Bitcoin network. In this process, aBTC (Bridged BTC tokens on Stacks) is burned or transferred, and BTC is released to a specified Bitcoin address. The contract's primary functionality is implemented through a series of public functions. Let's review this core operation. +This technical document provides a detailed overview of the contract responsible for managing the peg-out process, enabling the transfer of bridged BTC from the Stacks network back to the Bitcoin network. In this process, aBTC (Bridged BTC tokens on Stacks) is burned or transferred, depending on the context, and BTC is released to a specified Bitcoin address. The contract's primary functionality is implemented through a series of public functions. Let's review this core operation. ## Storage @@ -12,7 +12,7 @@ This technical document provides a detailed overview of the contract responsible | -------- | ------ | | Variable | `principal` | -The address where the fees collected from peg-out operations are transferred. By default, this address is the `tx-sender` (the original deployer of the contract). +The address where the fees collected from peg-out operations are transferred. By default, the address assigned to receive these fees is the `tx-sender` address of the contract deployer. ### `peg-out-paused` | Data | Type | @@ -26,7 +26,7 @@ A flag that indicates whether the peg-out process is active. If set to true, all | -------- | ------ | | Variable | `uint` | -The percentage fee charged for peg-out transactions. By default, this value is `u0`. +The percentage fee charged for peg-out transactions. The fee is represented in a fixed-point format with 6 decimal places. This means that 100% is represented as `u100000000`and 1% is represented as `u1000000`. By default, this value is `u0`. ### `peg-out-min-fee` | Data | Type |