0

I need to build a smart contract where users can lock their ERC20 tokens for a fixed amount of time (same amount of time for all users, e.g. 1 year). Lets say a user locks 100 tokens and half a year later he locks another 100 tokens, the first 100 tokens should still unlock after 6 months and the second batch of tokens after a year.

My current idea is to use an ERC1155 token contract to keep track of the time locks. From the example above, when the first user deposits their tokens, 100 ERC1155 tokens with the token id 0 would be minted that unlock after 1 year. When they deposit the next 100 tokens another 100 ERC1155 tokens with the token id 1 would be minted, and so on. The solution is not ideal because in the worst case scenario a user would have to withdraw multiple times and it would be harder to keep track of all tokens.

Another idea I had that you could have the tokens unlock linearly, so after 3 months 1/4 of the tokens are unlocked, after half a year 1/2 of the tokens are unlocked, etc. When a user deposits more tokens, this equation is adjusted so that the full amount of tokens releases after 1 year but they can still withdraw the same amount as before after depositing more tokens. Would need to figure out the Math behind this one though. This would have the benefit that the total amount of unlocked tokens could be withdrawn at once and it's easy to keep track of the users balance if tokens are deposited multiple times.

Does anyone have another idea to tackle this issue or maybe is there a project that has similar mechanics already I could have a look at?

3
  • Why use ERC1155 isn't that a lot of work?Isn't it a good idea to write a contract?
    – zmy
    Aug 9 at 2:11
  • using an ERC1155 i can tokenize the balances inside the smart contract Aug 9 at 13:06
  • Maybe this is a good idea. I once wrote a contract that allowed ERC721 to be associated with multiple ERC20's.ERC20 recharge, ERC20 extraction and other functions. ERC721 is like a card, this card has a lot of money on it (ERC20), as long as you have the card you can use the money!
    – zmy
    Aug 10 at 1:17
0

I would not involve another token standard in this just to keep track of the batches.

A crude approach would be to simply store the batches in an array inside a user mapping, so something like this:

struct Batch {
    uint toBeReleasedTimeStamp;
    uint amount;
}

mapping(address => Batch[]) userBatches;

When user saves a batch, a release timestamp is assigned for it. Each batch would be a new entry in the array.

Depending on further needs, you can add more data in the struct.

1
  • I would like to avoid unbounded arrays if possible, this would in theory result in the same problem I have with my first approach Aug 10 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.