Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] dApp-friendly contract deployment #403

Draft
wants to merge 41 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7cea4cf
Add a mock fn that register the ECDSA wallet
r-czajkowski Aug 10, 2022
729627b
Add hardhat task to create the mocked ECDSA wallet
r-czajkowski Aug 10, 2022
ce6f476
Add a mock function that sweeps a deposit
r-czajkowski Aug 10, 2022
6cab577
Add hardhat task to sweep a deposit
r-czajkowski Aug 10, 2022
4e1a1ee
Add a mock function to submit the redemption proof
r-czajkowski Aug 10, 2022
6b1e78e
Fix a `submitRedemptionProof` task
r-czajkowski Aug 15, 2022
36e5846
Update the mocked deposit tx fee
r-czajkowski Aug 16, 2022
cc22813
Update a hardhat task that creates a mocked wallet
r-czajkowski Aug 16, 2022
3af9b83
Update hardhat task
r-czajkowski Aug 16, 2022
ad1511c
Add hardhat task
r-czajkowski Aug 16, 2022
2f3e61b
Fix linting error
r-czajkowski Aug 16, 2022
e3c17cc
Use kebab case for the name of the hardhat task
r-czajkowski Aug 16, 2022
8cf4cdc
Merge remote-tracking branch 'origin/main' into dapp-development
michalinacienciala Aug 31, 2022
18ce7cb
Update contract dependencies
michalinacienciala Aug 31, 2022
7b5e7b6
Change version's suffix to `-dapp-dev-goerli`
michalinacienciala Sep 1, 2022
2560afc
Merge branch 'main' into dapp-development
r-czajkowski Sep 14, 2022
144e7ce
Update solidity config for ecdsa
r-czajkowski Sep 14, 2022
93de579
Update deploy bridge script
r-czajkowski Sep 14, 2022
8d043fe
Merge remote-tracking branch 'origin/main' into dapp-development
michalinacienciala Sep 19, 2022
607d806
Fix dapp hardhat tasks
r-czajkowski Dec 20, 2022
37494a7
Merge branch 'main' into dapp-development
r-czajkowski Jan 4, 2023
9a34b17
Update `yarn.lock` file
r-czajkowski Jan 4, 2023
a2c3074
Update `yarn.lock` file
r-czajkowski Jan 5, 2023
4450c03
Remove `tenderly` tag for goerli network
r-czajkowski Jan 5, 2023
ef7beb7
Merge branch 'main' into dapp-development
r-czajkowski Jan 5, 2023
93773eb
Add optimistic minting tasks
michalsmiarowski Jan 12, 2023
4d3a05d
Merge branch 'main' into dapp-development
r-czajkowski Jan 19, 2023
82a079a
Update `optimisticMintingDelay`
r-czajkowski Jan 19, 2023
91833bb
Remove `tenderly` tag for goerli network
r-czajkowski Jan 5, 2023
d0e2c3b
Set `GOVERNANCE_DELAY` in `VendingMachine` to `0`
r-czajkowski Jan 20, 2023
51ede5a
Add new deployment script
r-czajkowski Jan 20, 2023
a78a073
Merge branch 'main' into dapp-development
r-czajkowski Jan 24, 2023
fe01903
Update yarn.lock file
r-czajkowski Jan 24, 2023
65a2cfa
Remove unnecessary deployment script
r-czajkowski Jan 24, 2023
a12ca03
Merge branch 'main' into dapp-development
r-czajkowski Aug 3, 2023
bb6f174
Update mocked `submitRedemptionProof` fn\
r-czajkowski Aug 3, 2023
54be0a5
Skip script for goerli network
r-czajkowski Aug 3, 2023
39472bf
Merge remote-tracking branch 'origin/main' into dapp-development
michalinacienciala Nov 8, 2023
7d910df
Switch to dapp-dev-sepolia
michalinacienciala Nov 8, 2023
5b29ae0
Merge branch 'main' into dapp-development
michalsmiarowski Mar 7, 2024
9220c50
Move documentation to separate file
michalsmiarowski Mar 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions docs/dapp-development/dapp-development.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@

# Set up

Note: Make sure you have newest changes pulled on `dapp-development` branch

[start=1]
. Add proper dependencies to `solidity/package.json`:

+
```
"@keep-network/tbtc": "sepolia",
"@keep-network/tbtc-v2": "dapp-development-sepolia",
```

. Run `yarn install` (it's good to remove `node_modules` and `external` directories before doing that)

. If you don't have `typechain` directory in `solidity` folder then also run `npx hardhat compile`

. In `hardhat.config.ts`:

.. config -> network -> sepolia

... add proper infura url,
... add either mnemonic or private key as account (See `Set vault status` note for more info):

+
Syntax for private key:
```
account: [<private_key>]
```

Syntax for mnemonic:
```
account: {
mnemonic: <your_mnemonic>
}
```

... set gas and gasPrice to `2100000` and `8000000000` respectively

+
The final network config for sepolia should look similar to this:
```
sepolia: {
url: "https://sepolia.infura.io/v3/<your_infura_id>",
chainId: 11155111,
accounts: [<private_key>],
gas: 2100000,
gasPrice: 8000000000,
tags: ["tenderly"],
},
```

.. config -> external -> contracts

... add tbtc-v2 artifacts at the end of an array

+
```
{
artifacts: "node_modules/@keep-network/tbtc-v2/artifacts",
deploy: "node_modules/@keep-network/tbtc-v2/deploy",
}
```

.. config -> external -> deployments -> sepolia

... add tbtc-v2 artifacts to the array

+
```
sepolia: [
/* (...Other artifacts...) */
"node_modules/@keep-network/tbtc-v2/artifacts",
],
```

# Overview

## Wallets
We need the ECDSA wallet to create a deposit or request a redemption. The `mock__registerEcdsaWallet` function creates a mocked ECDSA wallet- to create wallet run:
```
npx hardhat dapp:register-wallet --network <network-name> --utxo-tx-hash 0x2f952bdc206bf51bb745b967cb7166149becada878d3191ffe341155ebcd4883 --utxo-tx-output-index 1 --utxo-tx-output-value 3933200
```

## Deposit

The `mock__submitDepositSweepProof` function sweeps deposit and mint TBTC tokens to a depositor address. We have to run this function after revealing a deposit. You can submit deposit sweep proof using:
```
npx hardhat dapp:submit-deposit-sweep-proof --wallet-pub-key-hash 0x486b0ee2eed761f069f327034eb2ae5e07580bf3 --funding-tx-hash 0xc7f953290caafec7f71c9a90f4c7b87a4d6cb63d3f016c0447662afe6c7e9847 --funding-output-index 0 --network <network-name>
```
To find the necessary parameters to run `dapp:submit-deposit-sweep-proof` task run:
```
npx hardhat dapp:get-revealed-deposits --depositor-address <depositor-address> --network <network-name>
```

## Redemption
The `mock__submitRedemptionProof` function submits the redemption proof. We have to run this function after requesting a redemption. To submit the redemption proof run:
```
npx hardhat dapp:submit-redemption-proof --wallet-pub-key-hash 0x486b0ee2eed761f069f327034eb2ae5e07580bf3 --redeemer-output-script 0x1600148db50eb52063ea9d98b3eac91489a90f738986f6 --network <network-name>
```
To find the necessary parameters to run `dapp:submit-redemption-proof` task run:
```
npx hardhat dapp:get-redemptions --redeemer-address <redeemer-address> --network <network-name>
```

# Optimistic Minting

## Set vault status

To be able to to optimistic minting we need to have trusted vault. To set a vault status to trusted (or not trusted) run:
```
npx hardhat dapp:set-vault-status --network goerli --address <vault_address> --is-trusted <true/false>
```
**Note:** This method can be called only by the owner of `BridgeGovernance` contract. To make this task work you will have to put a private key of the `dapp-friendly` contracts deployer inside `hardhat.config.ts` like this:
```
const config: HardhatUserConfig = {
(...)
networks: {
(...)
goerli: {
url: <your_infura_url>,
chainId: 5,
accounts: [<deployer_private_key>],
tags: ["tenderly"],
},
}
}
```
The `<deployer_private_key>` should be prefixed with `0x`!

## Add minter

Only minters can request and finalize an optimistic mint, so we've added a task to add the minter manually. To do that run:
```
npx hardhat dapp:add-minter --network goerli --address <your_address_that_will_become_a_minter>
```
**Note:** This method can be called only by the owner of `TBTCVault` contract. Please see the note from the `Set vault status` note to set the correct address as a deployer in hardhat config.

## Request Optimistic Mint

Allows a Minter to request for an optimistic minting of TBTC. To request an optimistic mint run:
```
npx hardhat dapp:request-optimistic-minting --network goerli --funding-tx-hash <funding_tx_hash> --funding-output-index <funding_output_index>
```
Both values, `funding_tx_hash` and `funding_output_index`, can be extracted from the `DepositRevealed` event.

## Finalize Optimistic Mint

Allows a Minter to finalize previously requested optimistic minting. To finalize the optimistic mint run:
```
npx hardhat dapp:finalize-optimistic-minting --network goerli --funding-tx-hash <funding_tx_hash> --funding-output-index <funding_output_index>
```
The `funding_tx_hash` and `funding_output_index` should be the same values that were used in `Request Optimistic Mint`.
38 changes: 38 additions & 0 deletions solidity/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ contract Bridge is
self.submitDepositSweepProof(sweepTx, sweepProof, mainUtxo, vault);
}

function mock__submitDepositSweepProof(
bytes20 walletPubKeyHash,
bytes32 fundingTxHash,
uint32 fundingOutputIndex
) external {
self.mock__submitDepositSweepProof(
walletPubKeyHash,
fundingTxHash,
fundingOutputIndex
);
}

/// @notice Requests redemption of the given amount from the specified
/// wallet to the redeemer Bitcoin output script. Handles the
/// simplest case in which the redeemer's balance is decreased in
Expand Down Expand Up @@ -649,6 +661,18 @@ contract Bridge is
);
}

function mock__submitRedemptionProof(
bytes20 walletPubKeyHash,
bytes calldata redeemerOutputScript,
bytes32 redemptionTxHash
) external {
self.mock__submitRedemptionProof(
walletPubKeyHash,
redeemerOutputScript,
redemptionTxHash
);
}

/// @notice Notifies that there is a pending redemption request associated
/// with the given wallet, that has timed out. The redemption
/// request is identified by the key built as
Expand Down Expand Up @@ -994,6 +1018,20 @@ contract Bridge is
self.registerNewWallet(ecdsaWalletID, publicKeyX, publicKeyY);
}

function mock__registerEcdsaWallet(
bytes32 ecdsaWalletID,
bytes32 publicKeyX,
bytes32 publicKeyY,
BitcoinTx.UTXO calldata walletMainUtxo
) external {
self.mock__registerEcdsaWallet(
ecdsaWalletID,
publicKeyX,
publicKeyY,
walletMainUtxo
);
}

/// @notice A callback function that is called by the ECDSA Wallet Registry
/// once a wallet heartbeat failure is detected.
/// @param publicKeyX Wallet's public key's X coordinate.
Expand Down
50 changes: 50 additions & 0 deletions solidity/contracts/bridge/DepositSweep.sol
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,56 @@ library DepositSweep {
}
}

function mock__submitDepositSweepProof(
BridgeState.Storage storage self,
bytes20 walletPubKeyHash,
bytes32 fundingTxHash,
uint32 fundingOutputIndex
) external {
Deposit.DepositRequest storage deposit = self.deposits[
uint256(
keccak256(abi.encodePacked(fundingTxHash, fundingOutputIndex))
)
];

require(deposit.sweptAt == 0, "Deposit already swept");

/* solhint-disable-next-line not-rely-on-time */
deposit.sweptAt = uint32(block.timestamp);
// Mokced deposit transaciton fee on the Bitcoin chain.
uint64 mockedDepositTxFeeIncurred = 400; // 0.000004 BTC.
uint64 depositedAmount = deposit.amount -
deposit.treasuryFee -
mockedDepositTxFeeIncurred;

emit DepositsSwept(walletPubKeyHash, 0x0);

address vault = deposit.vault;
address[] memory depositors = new address[](1);
depositors[0] = deposit.depositor;
uint256[] memory depositedAmounts = new uint256[](1);
depositedAmounts[0] = depositedAmount;

if (vault != address(0) && self.isVaultTrusted[vault]) {
// If the `vault` address is not zero and belongs to a trusted
// vault, route the deposits to that vault.
self.bank.increaseBalanceAndCall(
vault,
depositors,
depositedAmounts
);
} else {
// If the `vault` address is zero or belongs to a non-trusted
// vault, increase balances in the Bank individually for each
// depositor.
self.bank.increaseBalances(depositors, depositedAmounts);
}

uint256 totalTreasuryFee = deposit.treasuryFee;
// Pass the treasury fee to the treasury address.
self.bank.increaseBalance(self.treasury, totalTreasuryFee);
}

/// @notice Resolves sweeping wallet based on the provided wallet public key
/// hash. Validates the wallet state and current main UTXO, as
/// currently known on the Ethereum chain.
Expand Down
22 changes: 22 additions & 0 deletions solidity/contracts/bridge/Redemption.sol
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,28 @@ library Redemption {
}
}

function mock__submitRedemptionProof(
BridgeState.Storage storage self,
bytes20 walletPubKeyHash,
bytes calldata redeemerOutputScript,
bytes32 redemptionTxHash
) external {
uint256 redemptionKey = getRedemptionKey(
walletPubKeyHash,
redeemerOutputScript
);
RedemptionRequest storage request = self.pendingRedemptions[
redemptionKey
];

uint64 redeemableAmount = request.requestedAmount - request.treasuryFee;
delete self.pendingRedemptions[redemptionKey];
emit RedemptionsCompleted(walletPubKeyHash, redemptionTxHash);

self.bank.decreaseBalance(redeemableAmount);
self.bank.transferBalance(self.treasury, request.treasuryFee);
}

/// @notice Resolves redeeming wallet based on the provided wallet public
/// key hash. Validates the wallet state and current main UTXO, as
/// currently known on the Ethereum chain.
Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/bridge/VendingMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract VendingMachine is Ownable, IReceiveApproval {

/// @notice The time delay that needs to pass between initializing and
/// finalizing update of any governable parameter in this contract.
uint256 public constant GOVERNANCE_DELAY = 7 days;
uint256 public constant GOVERNANCE_DELAY = 0;

/// @notice Divisor for precision purposes. Used to represent fractions
/// in parameter values.
Expand Down
36 changes: 36 additions & 0 deletions solidity/contracts/bridge/Wallets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,42 @@ library Wallets {
emit NewWalletRegistered(ecdsaWalletID, walletPubKeyHash);
}

function mock__registerEcdsaWallet(
BridgeState.Storage storage self,
bytes32 ecdsaWalletID,
bytes32 publicKeyX,
bytes32 publicKeyY,
BitcoinTx.UTXO calldata walletMainUtxo
) external {
bytes20 walletPubKeyHash = bytes20(
EcdsaLib.compressPublicKey(publicKeyX, publicKeyY).hash160View()
);

Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
require(
wallet.state == WalletState.Unknown,
"ECDSA wallet has been already registered"
);
wallet.ecdsaWalletID = ecdsaWalletID;
wallet.state = WalletState.Live;
/* solhint-disable-next-line not-rely-on-time */
wallet.createdAt = uint32(block.timestamp);
wallet.mainUtxoHash = keccak256(
abi.encodePacked(
walletMainUtxo.txHash,
walletMainUtxo.txOutputIndex,
walletMainUtxo.txOutputValue
)
);

// Set the freshly created wallet as the new active wallet.
self.activeWalletPubKeyHash = walletPubKeyHash;

self.liveWalletsCount++;

emit NewWalletRegistered(ecdsaWalletID, 0x0);
}

/// @notice Handles a notification about a wallet redemption timeout.
/// Triggers the wallet moving funds process only if the wallet is
/// still in the Live state. That means multiple action timeouts can
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/vault/TBTCOptimisticMinting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract contract TBTCOptimisticMinting is Ownable {

/// @notice The time delay that needs to pass between initializing and
/// finalizing the upgrade of governable parameters.
uint256 public constant GOVERNANCE_DELAY = 24 hours;
uint256 public constant GOVERNANCE_DELAY = 120;

/// @notice Multiplier to convert satoshi to TBTC token units.
uint256 public constant SATOSHI_MULTIPLIER = 10**10;
Expand Down Expand Up @@ -73,7 +73,7 @@ abstract contract TBTCOptimisticMinting is Ownable {
/// @notice The time that needs to pass between the moment the optimistic
/// minting is requested and the moment optimistic minting is
/// finalized with minting TBTC.
uint32 public optimisticMintingDelay = 3 hours;
uint32 public optimisticMintingDelay = 120;

/// @notice Indicates if the given address is a Minter. Only Minters can
/// request optimistic minting.
Expand Down
2 changes: 1 addition & 1 deletion solidity/deploy/90_initialize_wallet_owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ func.dependencies = ["Bridge"]
func.runAtTheEnd = true

func.skip = async (hre: HardhatRuntimeEnvironment): Promise<boolean> =>
hre.network.name === "mainnet"
hre.network.name === "sepolia"
6 changes: 3 additions & 3 deletions solidity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keep-network/tbtc-v2",
"version": "1.6.0-dev",
"version": "1.6.0-dapp-dev-sepolia",
"license": "GPL-3.0-only",
"files": [
"artifacts/",
Expand Down Expand Up @@ -31,8 +31,8 @@
},
"dependencies": {
"@keep-network/bitcoin-spv-sol": "3.4.0-solc-0.8",
"@keep-network/ecdsa": "development",
"@keep-network/random-beacon": "development",
"@keep-network/ecdsa": "dapp-development-sepolia",
"@keep-network/random-beacon": "dapp-development-sepolia",
"@keep-network/tbtc": "development",
"@openzeppelin/contracts": "^4.8.1",
"@openzeppelin/contracts-upgradeable": "^4.8.1",
Expand Down
Loading
Loading