Fuse: Simple MultiSig Contract with Hardhat
Fuse support is deprecated
Chainstack deprecated support for Fuse nodes. This page here is for legacy and in case you may find it useful.
You can create a simple multiSig contract that requires confirmations from multiple owners to withdraw funds from it.
In this tutorial, you will:
- Create a simple multiSig contract.
- Deploy the contract on the Fuse Spark testnet through a Fuse node.
- Interact with the deployed contract.
Prerequisites
- A public Fuse endpoint.
- Hardhat to compile and deploy the contract.
- Hardhat ABI Exporter to export the ABI to interact with the contract.
- MetaMask to interact with the contract through your Chainstack node.
Get your Fuse node endpoint
For example, get a community public endpoint.
Install Hardhat
Initialize a Hardhat project
In your project directory, run npx hardhat
. Select Create a JavaScript project.
Create and compile the multiSig contract
In the contracts
directory, create multiSig.sol
.
This is your multiSig contract:
- The addresses that the deployer passes as parameters are set as co-owners of the multiSig Wallet. Along with that, the deployer passes an integer as the parameter. This integer will be the minimum number of confirmations a transaction will require to be approved. This integer must be greater than zero and must be less than or equal to the number of co-owners of the contract.
receive
is a special function provided by Solidity that allows the contract to accept protocol native tokens without a specially defined function. It is declared without the keywordfunction
and must be external and payable.- After declaring a struct to store the transaction data, there are two mappings to store the validity of the contract owners and to store all confirmations to a particular transaction.
- The functions defined mostly restrict access to the owners, even though anyone can read the data. The function
executeTransaction
uses the low-level methodcall()
to transfer the protocol native tokens from the contract to the required address if the transaction is approved.
To compile the contract, run npx hardhat compile
.
Fund your account
Fund the account that you will use to deploy the contract with SPARK—the native token of the Fuse Spark testnet. Use the Fuse Spark testnet faucet.
Set up Hardhat to work through your Fuse node
In your project directory, open for editing hardhat.config.js
.
where
- YOUR_NODE_ENDPOINT — your Fuse node HTTPS endpoint. See also Fuse tooling.
- YOUR_PRIVATE_KEY — the private key of your Fuse account that will deploy the contract. The account must have enough funds to run the deployment. See also Fuse Spark testnet faucet.
Deploy the multiSig contract
Set up the deployment script at scripts/deploy.js
.
where
- OWNER — the addresses that co-own the contract and whose confirmations are required to withdraw the funds.
- CONFIRMATION_NUMBER — the number of confirmations required by the contract owners to withdraw the funds.
Example of a contract owned by 0x7B397Bd7042560cdaE08C674Ef554e5C3239bC10
and 0xFDa85C3404dC00fFBe2A18615ba55380cB42c8Fb
and that requires a confirmation from both owners (2
):
Deploy the contract by running:
Interact with the contract
Once your contract is deployed, you can view it online at Fuse Spark testnet explorer.
You are now going to verify the contract in the explorer to be able to use the explorer as a web app and easily interact with the contract online.
Verify the deployed contract on the explorer
Go to Fuse Spark testnet explorer.
Find your deployed contract. The address of your contract is printed in the terminal by Hardhat at the end of the deployment.
On the contract page in the explorer, click Code > Verify & Publish.
In Contract Name, provide the name of your contract. In our example, the name is multiSigWallet
.
In Compiler, select the same compiler version that was used in the Hardhat configuration file. In our example, it is v0.8.10
.
In EVM Version, select default.
In Optimization, select No.
In Enter the Solidity Contract Code, paste the contract code.
In ABI-encoded Constructor Arguments, provide constructor values:
-
Copy the ugly ABI version from your project directory. For this example, it is
abi/ugly/contracts/multiSig.sol/multiSigWallet.json
. -
Go to Online ABI Encoding Service.
-
Enter your ABI and click Parse.
Remove the first and last
{}
and"abi":
. -
Enter the contructor parameters that you provided in your deployment script in
deploy.js
. -
Copy the resulting value and put it in the ABI-encoded Constructor Arguments field in the explorer.
Click Verify & publish.
The explorer will take a few seconds to compile your contract, verify, and publish it.
Interact with the contract
Set up your MetaMask instance to work through your Fuse node. See Fuse tooling: MetaMask.
Using MetaMask, send some funds to the contract.
Now that your multiSig contract is verified, you can use the explorer to interact with it.
- In the explorer, on your contract, click Write Contract.
- In your MetaMask, make sure you have the same address selected as the one that one of the contract owners.
- Click Connect wallet. This will connect your MetaMask instance with the contract owner as the active address.
- In submitTransaction, provide an address to send some funds to and the amount of funds in Wei. You can also use the online unit converter.
- Click Write.
- Once the transaction is included in a block, confirm it by providing the transaction index in confirmTransaction and clicking Write. Since this is the first transaction on the contract, the index is
0
. - Connect to the contract on the explorer with the other account that you provided as owner when deploying the contract.
- Again, confirm the transaction through confirmTransaction and the index
0
. - Once the confirmation transaction is included in a block, execute it through executeTransaction and the index
0
.
This will withdraw the funds from the multiSig contract.
Conclusion
This tutorial guided you through the basics of creating and deploying a simple multiSig contract on the Fuse Spark testnet through your Fuse node.
You have also interacted with the contract, funded it, and withdrawn the funds with multiSig confirmations using the explorer as a web app and MetaMask as your interaction tool that works through your Fuse node.
This tutorial uses testnet, but the exact same instructions work on the mainnet.