Contract Design – Can a Smart Contract Autonomously Hold ERC20 Tokens or ETH?

contract-designerc-20

I want to make a simple smart contract, but I don't know if smart contracts are capable of what I desire. Please tell me if they are:

  1. a user can send ETH (or any ERC20 token) to the smart contract address and it will mint some of it's own token and send it back in exchange.
  2. a user can then send in the minted token at any time in the future and the smart contract will return the original assets that were sent in, to the sender.

I was told this couldn't be done because the smart contract would need to sign the transaction, and it can't sign it or else the private key would be viewable. Is this true? this behavior seems like such a simple thing, I'd be surprised if it's impossible for it to do this autonomously. But if so, I just don't understand what a smart contract really is.

Thank you for helping!

Best Answer

You can do that.

A contract is a full participant and it can hold assets. What it cannot do is initiate a transaction. That has to be done by someone or something (a server) that can sign. This influences the flows.

Think of the contract like a vending machine. The user puts something in and something else comes out. The contract defines the internal mechanisms of the machine.

Eth goes in, tokens come out, or tokens go in and eth comes out. Neither is a problem - the user starts by signing a transaction that puts something in. The contract merely reacts by performing its calculations and giving something back.

I would be remiss if I didn't mention there are non-trivial challenges ahead to get the exchange rates right, but the process is feasible.

Hope it helps.

Related Topic