2

I have a contract (ERC-20) created on the Binance Smart Chain (BSC) with a limited supply.

I want my users to be able to claim some tokens from the wallet contract that they earn on my website (server decide how much tokens they can claim), but i want my users to pay for the transaction fees.

If i understand this correctly, for this to work i need my server to sign parameters transaction (parameter containing the amount of coins the user is allowed to claim and its destination address) with the contract privatekey and then pass the signed parameters to the user so he can sign the transaction in Metamask and pay the fee to send it.

Then the contract must somehow be able to retrieve/decode parameters to know how much tokens to give and to who.

Is this the correct way ? I didn't find any good ressources that explain this.. I ve managed to handle everything else (deposit, confirmations, signing transactions..).

I'm using web3 on both server (nodejs) and client (js) side.

2 Answers 2

3

Yes, your line of reasoning is correct. You can find a way to do this via this link: https://solidity-by-example.org/signature/

Your server will prepare a request, sign it with your private key, using javascript. You will then pass the request to the user, along with the signature, which they will execute in metamask. The contract will check if the signature is valid and, if valid, will execute the transaction (in this case, send the tokens to the user)

You have to be very careful with the use of this algorithm. Recently an NFT game lost US 250k through a failure to use this algorithm. See for example this hack -> https://solidity-by-example.org/hacks/signature-replay/

1
  • Thank you, i have almost managed to do it but i have one last issue here
    – Owow
    Commented Mar 11, 2022 at 22:40
1

I think your approach is correct (and it's the same thing I would do considering the requirements you wrote).

Check EIP-712 to understand how it works (OpenZeppelin provides an implementation for that).

Only a note: your contract should also remember that the user claimed the tokens, I would store a mapping variable bytes signature => bool alreadyClaimed in the contract to do that.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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