For the birth of my niece i wanted to gift her a little bit of money in bitcoin. Of course a newborn can’t buy anything with it, but maybe in the future. I got a savings book when I was young, so I thought it would be a good idea to give her something similar. In the blockchain world this is done by a smart contract. We don’t have to trust banks or the government to keep the inflation low. However, we need to trust those guys developing smart contracts.
There are a lot of projects out there that let you stack your bitcoin for several years and gain some rewards for it. However, I don’t trust them enough to lend them my bitcoin for 18 years until my niece is an adult. So I created a contract that made several things possible:
- Multiple individual payments
- Multiple withdraw dates
- Token support
- Multiple participants
- Definable payout dates
A smart contract on ethereum or the smart chain can do all of that. The only problem is the bitcoin payout. I decided to just link to an ERC20 Token. This of course could be an wrapped bitcoin contract. Bitcoin is important, because nobody knows if a token like Doge will keep his price for 18 years. Bitcoin is the most secure coin in regards to price stability in the long run. Yes, those wrapped coins are pretty insecure, but I trust them enough to give it a try. The complete smart contract can be found in my github. The interface looks like this:
constructor(address coin); function depositToken(uint256 amount, address receiver, uint256 timestamp) public; function withdrawToken() public returns (uint256); function totalBalanceOf(address user) public view returns (uint256); function nextWithdrawal(address user, uint from) public view returns (uint256, uint256)
To setup the coin the constructor needs a token address. This could be WBTC or some other coin.
To send someone tokens as a birthday or Christmas present you first need to give the token contract the allowance to spend the tokens in your wallet. This can be done with the allowance-method every ERC20 token has. The next step would be to call depositToken
with
amount
: The amount in token you are willing to give. Please keep in mind that smart contracts don’t work with decimals. You have to multiply your amount with the tenfold number of decimals of that token. If your tokens has 18 decimals and you want to spend 1 token you have to spend 1000000000000000000.receiver
: The wallet address of the person who should receive the token after the period is over.timestmap
: The epoch timestamp. Those are the seconds since the year 1970. Tokens can be claimed once this time is reached.
Please keep in mind that a wrong input can make your tokens unusable. There is no possibility to revert an action once done. It is also not possible to receive tokens earlier or give them back to the previous owner. This is on purpose.
If the contract time is over the receiver can receive the tokens by calling withdrawToken
with his wallet. Only the wallet address specified during the deposit is able to do so.
Because I can’t guarantee that this piece of code is bug free it should only be used for testing and never with real tokens of value.