D2D swap directly in gnosis safe app
Guide to creating D2D swaps with time locks using a direct smart contract. Walk through interacting with the smart contract using a Gnosis multisig directly through a transaction builder.
Last updated
Guide to creating D2D swaps with time locks using a direct smart contract. Walk through interacting with the smart contract using a Gnosis multisig directly through a transaction builder.
Last updated
Before we begin, we suggest that you have all of the details handy in a spreadsheet or notepad so that it is easy to copy & paste into the gnosis safe. We will use the terminology of DAO_A and DAO_B with TokenA and TokenB, where DAO_A is considered the โinitiatorโ and DAO_B is considered the โexecutorโ.
The details that you will need are:
Address of TokenA
Address of TokenB
Amount of TokenA to swap (pro tip - put the amount in a spreadsheet and next to it multiply the amount by (10 ** decimals of tokenA) - this is the amount of tokens that DAO_A will be delivering and DAO_B will receive.
Amount of TokenB to swap (also account for decimals like with tokenA), the amount of tokens DAO_B will deliver and the amount DAO_A will receive
UnlockDate - this is the unix time stamp that both sets of tokens will unlock. We use this site for easy referencehttps://www.epochconverter.com/.
If you don not want the tokens to be locked here, then this value will be set to 0.
Address of DAO_B (executor)
Address of the nftLocker: 0x2AA5d15Eb36E5960d056e8FeA6E7BB3e2a06A351
address of the Swapping smart contract: 0xf9e896b068cf9d71F3e7e4A710E72D6bA4531212
For example:
With this information in hand, we are now ready to initiate the DAO<>DAO swap. The initiator DAO_A will start this process and initiate the swap (the hard job), and the DAO_B will execute the swap (the easy job).
Step 1: Open the gnosis safe app you are going to use and ensure that you have sufficient tokens in the multi-sig to perform the swap (you can click the Assets link to double check).
Step 2: We will use the app Transaction Builder in the Apps section to create the two necessary transactions; 2(a), the approval and 2(b) the initiation of the swap.
Step 2(a): Click on the โNew Transactionโ button below your total balance on the left hand side of the app:
Click the โContract Interactionโ button.
Here we will enter in our Token Address for TokenA.
The ABI should auto-populate, but if it does not you can go to Arbiscan.io and plug in your token address, and pull the contract ABI from here (assuming the code has been verified):
Paste the ABI into the section where it says โEnter ABIโ (if this was not already pre-populated) and your gnosis app should now look like this:
Scroll down to Method dropdown and choose approve.
There are two inputs, the โspender (address)โ and the amount (uint256). For the first input, you should use the spender address of the Swap contract which is 0xB3d4EFE7ECF102afCd3262cF4d5fc768D0c55459. Then for the amount, you need to input the amount of tokens you are going to swap (this is the amount you are delivering and sending). Assuming this is handy in your spreadsheet paste that number into the amount field (making sure it has all of the zeroes, typically 18 of them).
Now click the Review button, and we can submit this transaction. Before submitting you can simulate the transaction to ensure it will be successful as well. When youโre ready, click submit.
This will prompt your metamask to sign the transaction, and once signed this transaction will show up in the queue for another signer to approve and execute. You can either have the second multi-sig signer confirm this transaction now, or go ahead and continue to step 2(b) and let the other signer(s) confirm the transactions in order all at once (in a batch).
Step 2(b): Now the fun begins. Again we click on the New Transaction button and select Contract Interaction. This time, however, we are going to input the Swap Contract address: 0xB3d4EFE7ECF102afCd3262cF4d5fc768D0c55459
into the Contract address field. The ABI should auto-populate. Scrolling down to the Method dropdown - we select initSwap
.
We will see a list of several different parameters that weโll need to input - and this is where having all our data in the spreadsheet comes in handy. If your spreadsheet is in the same order as above mentioned you can simply paste them in order:
TokenA Address โ tokenA
TokenB Address โ tokenB
Amount of token A (which we just approved for and are delivering) โ amountA
Amount of token B (the amount we are receiving) โ amountB
Unlock date (unix helper site:https://www.epochconverter.com/) โ unlockDate
Set this value to 0 if this is not going to be locked.
Address of DAO_B (executor) โ executor
Address of the nftLocker: 0x2AA5d15Eb36E5960d056e8FeA6E7BB3e2a06A351
โ nftLocker
It should look like this when filled out:
Now we click on Review button. And on the next screen click Submit.
Note: If you are Simulating the transaction, it will fail simulation if you have not already completed and executed the first part of 2(a) in approving the spender address. You can check on the Tenderly results and see that it will indicate a failure with the SafeERC20 methods.
You should see two transactions in queue (or if the first has been approved then just one) that are ready for the other signers to approve and submit. The final signer will click the Execute button and send the transaction off.
The final step will be to retrieve the transaction from Etherscan and deliver the index of the swap to the executor DAO_B. In this example transaction, we go to the tab that says โLogs (5)โ and we scroll down to find one with the name of โNewSwapโ:
We are looking for the value at index: 1
In this case - it was the first transaction so the index = 1. But your transaction will have a unique ID that is associated only with your swap. Give this index to your DAO_B and your part is done.
The Executor DAO has the easy part. They need to simply verify that the swap information is accurate, and then execute the swap. So first, we can verify that the information is accurate by taking the index of the swap that DAO_A has delivered to us, and viewing the information on arbiscan. First we pull up the contract on arbiscan: https://arbiscan.io/address/0xb3d4efe7ecf102afcd3262cf4d5fc768d0c55459
Next, click onto the Contract tab and the โRead Contractโ sub tab.
The first method is getSwapDetails and we should plug in the swap ID Index to that and click Query.
Here we can verify that all of the information is accurate and we are going to accept this swap transaction. IF ANY PIECE OF THE SWAP INFORMATION IS NOT 100% ACCURATE, DO NOT EXECUTE THE SWAP. INFORM THE DAO_A THAT THEIR INFORMATION IS INACCURATE AND TO CANCEL AND REDO THEIR SWAP.
Assuming that the information is accurate we will not execute the swap in steps 3(a) and 3(b).
Step 3(a): Approving the ERC20 spend for the swap.
We go to our gnosis safe app, and on the left click โNew Transactionโ button, and then select โContract interactionโ:
Next - paste into the Contract Address input your ERC20 token that you will be delivering, what we have referred to as TokenB. Scroll to the Method dropdown and select approve:
For the spender field; we input the swap contract address: 0xB3d4EFE7ECF102afCd3262cF4d5fc768D0c55459
and for the amount we input the amount we are going to be delivering.
Then weโll click on the โReviewโ button and on the next screen click the Submit button at the bottom. You can simulate the transaction to ensure it will be successful before submitting to the other signers.
Step 3(b): Executing the swap.
While the other signers are confirming that first transaction, we can submit the second to execute the swap. Click again on the New Transaction button and select Contract Interaction again. Now we input the Swap contract address 0xB3d4EFE7ECF102afCd3262cF4d5fc768D0c55459
and in the Method dropdown weโll select executeSwap
All we need to execute this swap is the swap index ID we received from DAO_A - the initiator. We simply input that ID and, click review and submit.
The last signer can simulate the transaction and Submit and execute.
We can now review the transaction (our example one) to verify that the swap occurred and to congratulate everyone involved in a successful transaction. We can then view the transactions on the Hedgey App.
To view our locked DAO tokens after the swap has been executed, we first navigate to our safe app and select the Wallet Connect app in the Apps section:
After clicking through the disclaimers, we go to https://app.hedgey.finance/#/portfolio - the portfolio page. Now click on the connect button and select Wallet Connect on the popup:
You should see a QR code that youโll copy on your clipboard.
and now switching back to the Gnosis Safe - paste this code into the connection link spot:
You should see that the app is connected with the confirmation screen above in Gnosis Safe.
Flipping back to the hedgey app - we can see the Locked Tokens that we received in the DAO swap. Weโll notice that these are wrapped in a Hedgey NFT, and so there may be some special art that youโll see in addition to the token details that include the amount of Tokens, and date that they unlock.
After the date has passed where the tokens are unlocked - the โUnlockโ button will become available and with this wallet connection we can unlock and redeem the tokens.
In case there was a mistake on the swap initiation, you can cancel that swap before it has been executed by DAO_B. To do so, we navigate to the New Transaction button in our gnosis safe app, and click on Contract Interaction. Now we input the Swap contract address 0xB3d4EFE7ECF102afCd3262cF4d5fc768D0c55459
and in the Method dropdown select cancelSwap
. The only value we need is the swap index that we identified in the event logs from our transaction on arbiscan. Input that swap ID into the field _swapId
and now we can review and submit, and once confirmed by the other signer(s) this swap will be cancelled and the tokens returned back to your multi-sig wallet.