Skip to content

Commit

Permalink
1.1 - New API (#18)
Browse files Browse the repository at this point in the history
* fix: callback-contract checking (#14)
* fix: tx-sender vulnerability (#15)
* Ref/remove attestors (#16)
* ref: update dlc-manager API
* ref: add refund-delay and value-locked to DLC objects
* BTC Fee updates (#17)
* update scripts
* ref: new versioning and deployments
* update README
  • Loading branch information
scolear authored Jan 30, 2024
1 parent a0e37cb commit c849207
Show file tree
Hide file tree
Showing 46 changed files with 979 additions and 936 deletions.
27 changes: 16 additions & 11 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,38 @@ telemetry = false
cache_dir = '.cache'

[[project.requirements]]
contract_id = 'STDBEG5X8XD50SPM1JJH0E5CTXGDV5NJTJTTH7YB.redstone-verify'
contract_id = 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard'

[[project.requirements]]
contract_id = 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard'
contract_id = 'ST1NXBK3K5YYMD6FD41MVNP3JS1GABZ8TRVX023PT.sip-010-trait-ft-standard'

[contracts.callback-contract-v1-1]
path = 'tests/callback-contract-mock.clar'
clarity_version = 2
epoch = 2.3

[contracts.callback-contract-v1]
path = 'tests/callback-contract-mock-v1.clar'
[contracts.dlc-link-callback-trait-v1-1]
path = 'contracts/dlc-link-callback-trait.clar'
clarity_version = 2
epoch = 2.3

[contracts.dlc-link-callback-trait-v1]
path = 'contracts/dlc-link-callback-trait-v1.clar'
[contracts.dlc-manager-trait-v1-2]
path = 'contracts/dlc-manager-trait.clar'
clarity_version = 2
epoch = 2.3

[contracts.dlc-manager-v1]
path = 'contracts/dlc-manager-v1.clar'
[contracts.dlc-manager-v1-1]
path = 'contracts/dlc-manager.clar'
clarity_version = 2
epoch = 2.3

[contracts.dlc-stablecoin]
[contracts.dlc-stablecoin-v1-1]
path = 'contracts/dlc-stablecoin.clar'
clarity_version = 2
epoch = 2.3

[contracts.sample-contract-loan-v1]
path = 'examples/sample-contract-loan-v1.clar'
[contracts.sample-contract-loan-v1-1]
path = 'examples/sample-contract-loan.clar'
clarity_version = 2
epoch = 2.3

Expand Down
100 changes: 44 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
[![Build & Push to S3](https://github.com/DLC-link/dlc-clarity/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/DLC-link/dlc-clarity/actions/workflows/build.yml)

Join our Discord server for news and support!

[![Discord Banner](https://discordapp.com/api/guilds/887360470955208745/widget.png?style=banner2)](https://discord.gg/JAkbs92N5H)

## Deployed contracts

### Testnet

You can find the latest deployed versions in the [deployments/custom.testnet-plan.yaml](deployments/custom.testnet-plan.yaml) file.

### DLC.Link Devnet

You can find the latest deployed versions in the [deployments/custom.devnet-plan.yaml](deployments/custom.devnet-plan.yaml) file.

# DLC.Link Stacks Smart Contracts

This smart contract is the interface for creating, closing and otherwise managing DLCs via the DLC.Link infrastructure.

Learn more about [DLCs](https://github.com/DLC-link/stacks-contracts-all/#what-are-dlcs) and [DLC.Link](https://github.com/DLC-link/stacks-contracts-all/#about-dlc-link) below.
Learn more about [DLCs](https://github.com/DLC-link/dlc-clarity/#what-are-dlcs) and [DLC.Link](https://github.com/DLC-link/dlc-clarity/#about-dlc-link) below.

# Overview

Expand All @@ -16,97 +28,71 @@ This contract acts to feed the outcome of the DLC. By using a smart contract for

# How to interact with this contract

An example lending/borrowing contract is provided in `/examples/sample-contract-loan.clar`. This contract is a simple example of how to interact with the DLC Manager contract.

## Clarity Traits
The following Clarity Traits must be implemented to interact with this contract. See `/contracts/dlc-link-callback-trait.clar`
### `post-create-dlc-handler`
Used to callback into the calling/protocol contract to provide the uuid of the created DLC, which will be used to reference the DLC going forward.

Parameters:
The following Clarity Traits must be implemented to interface with our dlc-manager contract. See `/contracts/dlc-link-callback-trait.clar`

* `nonce`: uint - an ID used to associate a create function call with the callback
* `uuid`: string - UUID of the DLC
### `set-status-funded`

### `post-close-dlc-handler`
Used to callback into the calling/protocol contract to notify when a DLC is closed successfully.
When a DLC is properly set up on the _Bitcoin_ blockchain, the DLC.Link infrastructure will use this function to notify Stacks of it's success.
This allows the contracts to have confirmation about the DLC's status.

Parameters:

* `uuid`: string - UUID of the DLC
- `uuid`: string - UUID of the DLC that was succesfully funded on BTC
- `btc-tx-id`: string - BTC funding transaction ID
-

### `get-btc-price-callback`
This callback is used to provide a way to hand back BTC price data, validated by Redstone. This value then can be used on the protocol side to decide liquidations, etc.

Parameters:

* `price`: uint - Redstone validated BTC price. Shifted by 10**8
* `uuid`: string - DLC UUID
### `post-close-dlc-handler`

### `set-staus-funded`
When a DLC is properly set up on the _Bitcoin_ blockchain, the DLC.Link infrastructure will use this function to notify Stacks of it's success.
This allows the contracts to have confirmation about the DLC's status.
Used to callback into the calling/protocol contract to notify when a DLC is closed successfully.

Parameters:

* `uuid`: string - UUID of the DLC that was succesfully funded on BTC
- `uuid`: string - UUID of the DLC
- `btc-tx-id`: string - BTC closing transaction ID

## Functions to call

### Register contract

This must be run first by a DLC.Link admin. This authorizes your contract to interact with our DLC Manager contract and to be listened to by our listeners. This happens once, and should happen first before anything else.
This must be run first by a DLC.Link admin. This authorizes your contract to interact with our DLC Manager contract. This happens once, and should happen first before anything else.

Parameters:
`contract-address`:Principal
`contract-address`: Principal

### Unregister Contract

Used to unregister a contract from the list of authorized contracts

Parameters
`contract-address`:Principal
`contract-address`: Principal

### Opening a DLC

When you register a DLC with this contract using the `create-dlc` function, a DLC is set up on our DLC Oracles with the associated outcomes (CETs).
When you register a DLC with this contract using the `create-dlc` function, a DLC is set up on our DLC Attestors with the associated outcomes (CETs).

See the comments in the contract for further information about using this function.

Parameters:

* `emergency-refund-time`:uint - UNIX timestamp after which the DLC can be reclaimed by either party
* `callback-contract`:principal - The contract that sends the request, and will accept the callback.
* `nonce`:uint - the protocol/user provided ID to associate with the UUID
- `value-locked`: uint - amount of BTC to lock in the DLC in Satoshis
- `callback-contract`: principal - The contract that sends the request, and will accept the callback.
- `protocol-wallet`: principal - router-wallet public key, that will trigger `set-status-funded` and `post-close-dlc` (see [dlc-stack](https://github.com/DLC-link/dlc-stack) repo for more information)
- `refund-delay`: uint - seconds until an emergency refund transaction can be broadcasted. Set 0 to disable this feature.
- `btc-fee-recipient`: (string-ascii 64) - BTC address to send the BTC fees to (sends the fees to the protocol wallet if set to "0x")
- `btc-fee-basis-points`: uint - BTC fee basis points (1/100 of a percent) to send to the fee recipient (set 0 to disable this feature)

### Closing the DLC

The `close-dlc` function initiates the DLC closing flow. The supplied outcome will be relayed to the DLC Oracles through the DLC.Link Infrastructure, and it's value signed on the payout curve. The outcome must be between 0-10000: this represents 0-100.00% payouts, with two decimals precision. A value of 0 represents all value locked in the DLC to return to the user, 10000 means all BTC goes to the protocol (the other party in the DLC).

**_NOTE:_** To close a DLC successfully you have to set a trusted oracle first (the oracle used in the deployed contract and scripts is already set on Testnet)

Parameters:

* `uuid`:string
* `outcome`:number - a value between 0-10000 inclusive

### Other noteworthy functions

`get-btc-price(uuid)` : calling this function initiates a price-fetching flow on our backend. BTC price is fetched and validated by Redstone, and returned through the `get-btc-price-callback` callback function. This value then can be used to check liquidations, etc. See our sample contract for a full loan implementation: [sample-contract-loan](examples/sample-contract-loan.clar)

## About Redstone

- [Intro article](https://stacks.org/redstone)
- [Reference implementation](https://github.com/Clarity-Innovation-Lab/redstone-clarity-connector)

**_NOTE:_** the integration so this implementation as well depends on [micro-stacks](https://github.com/fungible-systems/micro-stacks) which is in alpha state and not audited, so use this in production with caution.

Flow of the Redstone oracle requests:

1. submit trusted oracle (node public keys can be found [here](https://github.com/redstone-finance/redstone-node/blob/main/src/config/nodes.json), this repo uses `redstone`)
2. when trying to close a DLC, submit a `timestamp`, `data package` and a `signature` as well with the UUID, which can be obtained from the [redstone-api-extended](https://www.npmjs.com/package/redstone-api-extended) module. For reference check the `close-dlc-internal.ts` script.

The Redstone data-package verification contract is included in the `.requirements/` folder for the purposes of deploying it during Mocknet testing. In production and testnet, the on-chain contracts are used, which can be found here:

[redstone-verify on Testnet](https://explorer.stacks.co/txid/0x35952be366691c79243cc0fc43cfcf90ae71ed66a9b6d9578b167c28965bbf7e?chain=testnet)

[redstone-verify on Mainnet](https://explorer.stacks.co/txid/0x8de1fb0a41d6a8a962c8016c3a5178176fc51c206afa72f71f5747a6246a37bb?chain=mainnet)
- `uuid`:string
- `outcome`:number - a value between 0-10000 inclusive

# Contributing

Expand All @@ -133,14 +119,16 @@ brew install lcov
genhtml coverage.lcov
open index.html
```

## Useful to note

### Scripts

This repo contains scripts that help interacting with our contracts. Create a `.env` in the root folder, following the `.env.template` file. Then, after an `npm install` and an `npm run build`, scripts can be ran using `dlc-link-stacks`. Run `dlc-link-stacks --help` to see the possibilities. Scripts can be found [here](scripts/).

### Mocknet deployment

This repo temporarily contains the older versions of our contracts in the `legacy-contracts` folder.
The following command deploys all our contracts and maked the necessary setups for testing.
The following command deploys all our contracts and makes the necessary setups for testing.

```bash
$ clarinet integrate -p deployments/custom.devnet-plan.yaml
Expand Down
6 changes: 0 additions & 6 deletions contracts/dlc-link-callback-trait-v1.clar

This file was deleted.

6 changes: 6 additions & 0 deletions contracts/dlc-link-callback-trait.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(define-trait dlc-link-callback-trait-v1-1
(
(set-status-funded ((buff 32) (string-ascii 64)) (response bool uint))
(post-close-dlc-handler ((buff 32) (string-ascii 64)) (response bool uint))
)
)
10 changes: 10 additions & 0 deletions contracts/dlc-manager-trait.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

(use-trait cb-trait .dlc-link-callback-trait-v1-1.dlc-link-callback-trait-v1-1)
(define-trait dlc-manager-trait-v1-2
(
(create-dlc (uint principal principal uint (string-ascii 64) uint) (response (buff 32) uint))
(set-status-funded ((buff 32) (string-ascii 64) <cb-trait>) (response bool uint))
(close-dlc ((buff 32) uint) (response bool uint))
(post-close ((buff 32) (string-ascii 64) <cb-trait>) (response bool uint))
)
)
Loading

0 comments on commit c849207

Please sign in to comment.