0
votes

So the last couple of weeks I spent my freetime on working on a dApp and I'm almost finished there's one last thing I have to implement but I really struggle to do so.

So my dApp is basically a game. To get more players I added a token which the players can earn. I want them to stake their tokens to receive some of the profit the game makes. All the ETH the game makes goes into a treasury contract. That's the same contract where people can stake their tokens. I want them to have a "pending ETH reward" which they can withdraw on their cost. Similar to sushiswap with the difference that there isn't a fixed amount of reward per block. So basically I want the smart contract to assign the Ether it receives to the token stakers based on how much they stake. I don't want the Eth to directly get send to the addresses of the stakers because this would be to pricy.

At first I thought this would be easy but than I looked into it, wasted many hours and found out that I have no clue how to do this. How can I let my contract know that it received ETH and that it should do something evertime it receives Eth? Every type of help is highly appreciated!

My current idea is to make a struct array called "Users". Each User stores the adress, pendingETHreward and amount of staked tokens. Each User should be mapped to the addres of the user. The Problem: I dont know how to update the variable pendingETHreward of each user everytime the contract receives ETH.

1

1 Answers

0
votes
  1. You will write a smart contract function that does the logic you described -> This is simply matter of programming and outside the scope of this question - if you are not on the level of Solidity programming yet that you can do this, then I suggest tutorials and online courses
  2. Mark your function as payable in Solidity
  3. Users will call your smart contract function from their MetaMask and then they attach any number of ETH as the payload for the transaction
  4. Smart contract can then hold this ETH and have internal accounting done for who it belongs based on msg.sender address in Solidity code