Counterfactual Loan Repayment

exploring predictive smart contract deployments






Image for post
Image for post

Predictive Deployment Toolkit

To explore this idea, I patched together a script using the previous work of George Spasov and it’s available for anyone to use in this Gist:

node craft.js ABI_FILE BYTECODE_FILE GAS_LIMIT GAS_PRICE [...CONSTRUCTOR ARGUMENTS]
Image for post
Image for post

Image for post
Image for post

Predictive Deployment Loan Repayment

Now that we have this tool, what can we build with it? Well let’s go back to Nadav’s problem. Wouldn’t it be nice if Dharma could loan someone a grip of tokens and give them a simple address for repayment? Could this work without a trusted third party? Could we use a counterfactual contract to eventually sweep the funds and call the correct on-chain functions? 🤔🤔🤔

cd ~;git clone https://github.com/austintgriffith/counterfactual-token-repayment
docker run -ti --rm --name clevis -p 3000:3000 -p 8545:8545 -p 1337:1337 -v ~/counterfactual-token-repayment:/dapp austingriffith/clevis:latest
Image for post
Image for post
You will be prompted for what directories to use, just hit enter to use the defaults.
atom ~/counterfactual-token-repayment
Image for post
Image for post
http://localhost:3000
Image for post
Image for post
Hopefully by the time you are reading this we make this error message look better :)
🗜️ Clevis:/dapp 🗜️ clevis test full
Image for post
Image for post
Image for post
Image for post
Make sure you have MetaMask pointed to the local RPC (http://localhost:8545)

Image for post
Image for post

Image for post
Image for post
clevis test full
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
🗜️ Clevis:/dapp 🗜️ clevis compile Sweeper
🗜️ Clevis:/dapp 🗜️ node backend.js
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Notice we can use the Blockie (Identicon) as a hint that we have the address right.
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Pitfalls

There are few things to mention that I’d love to get some feedback on from the community. First, the counterfactual transaction is very sensitive. The gas constraints and nonce are all signed and you can’t adjust them. If the transaction runs before there are enough tokens or the gasLimit is wrong, there is a chance that the transaction will fail and the tokens will be locked up forever in an account we will never know the private key of. 😪😪😪

Image for post
Image for post

Conclusion

Using this method, we were able to simplify the process for the end user by adding a little extra complexity on our side. Instead of the user having to approve() the Loan contract and call the repay() function themselves. They were able to just transfer() the tokens to a repayment address and our off-chain cron will automatically sweep the tokens. This is a much easier task for a new user in the space. If we are going to drive mass adoption in Ethereum, we need to make the UX as smooth as possible. But maybe it isn’t worth it? Maybe just a good UX around the approve() and repay() is good enough?

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Gitcoin

Grow Open Source