-1

I want to deploy a smart contract (ERC20) for the game, so the purpose is to keep points.

  • when someone enters the game, we will ask for some crypto coin (ex. ETH) and give some of our own points
  • while playing the game, the user will earn some points.
  • Then that user can get crypto coins (ex. ETH) from that points.

I can write a smart contract to manage points. But I wonder if I can have a function to exchange our points to existing crypto coins (ex ETH) inside of our smart contract.

Does someone know the right way to do it?

Talent
  • 23
  • 4

1 Answers1

0

You can calculate the amount of ETH based on a price, and then use the transfer() native function of address payable type.

mapping (address => uint256) pointBalances;

// 1 point for 100 wei, assuming the points have 0 decimals
uint256 price = 100;

function sellPoints(uint256 _amount) external {
    require(pointBalances[msg.sender] >= _amount, "Insufficient balance");
    pointBalances[msg.sender] -= _amount;
    uint256 weiAmount = _amount * price;
    payable(msg.sender).transfer(weiAmount);
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Can't we transfer another token like MIM or ADA? – Talent Jan 10 '22 at 06:43
  • @Talent You can. An easy way is to invoke the `.transfer()` function of the token contract (not of the `address payable` type as in the answer), which would send the tokens from your contract address to the specified recipient. – Petr Hejda Jan 10 '22 at 09:14