This is a Solidity example of a DCA (Dollar-Cost Averaging) Bot service using the Gearbox protocol. You can use your WBTC as collateral and borrow USDT to DCA buying in the WETH/USDT market.
It does not include backend and frontend services. However, you can easily build a server with two endpoints: one to store signatures in a database, and another to provide available signatures to keeper bots. For frontend, you need to calculate the health risk related stuff and form the Order structure. (Please check _prepareCreditAccountAndSetBotPermissions(), and _prepareXXXCollateralForDCABot in the GearboxDCA.t.sol or the below flow chart to know more about it.)
- Approve
creditManagerto spendNamount of collateral (EIP-2612-compatible token can skip this step)N > order.collateralAmount * order.parts(order is the struct in 4.) Otherwise, the order will not be be executed.
- Send
creditFacade.openCreditAccount(user, [addCollateral(WETH,10 ether)], 0) - Send
creditFacade.setBotPermissions(dcaBot, EXTERNAL_CALLS_PERMISSION) - Submit a DCA order to server
- By static calling
executeOrder()orgetOrderStatus()to check an order executability - Send
executeOrder()to execute an order.
Capital efficiency version
- User on-chain actions:
- Approve
dcaBotto spend 10 WETH (EIP-2612-compatible token can skip this step) creditFacade.openCreditAccount(user, [], 0)creditFacade.setBotPermissions(dcaBot, PERMISSIONS)
- Approve
- Pros:
- Capital efficiency: User can move their money easily. Funds will be moved when the order gets executed
- Cons: 1. Extra security risk: User needs to approve first to let dcaBot to spend their money
(Skip) Capital inefficiency version
- User on-chain actions:
- Approve
creditManagerto spend 10 WETH (EIP-2612-compatible token can skip this step) creditFacade.openCreditAccount(user, [addCollateral(WETH,10 ether)], 0)creditFacade.setBotPermissions(dcaBot, EXTERNAL_CALLS_PERMISSION)
- Approve
- Pros:
- Simple design: dcaBot only needs to care about the creditFacade's external calls
- Cons:
- Capital inefficiency: collateral stores in the credit account first
mv .env.example .env # please update .env
$ forge build$ forge test$ forge fmt