Agent contract

James Sangalli started this conversation on how to model Agent Contracts in TokenScript. Agent contract is a contract with approved access to ERC20 tokens (or any that supports something akin to approve()).

Let's say one of such a contract is called "Replenisher". Once approved, it converts from other ERC20 tokens like Wrapped Eth to DAI through something like uniswap, so that the user's DAI's balance is no less than 200USD. This way, the user always has enough pocket money with the rest of the capital invested.

The nitty-gritty in Replenisher:

  1. The user (who has an address addr) approves Replenisher to access his Wrapped Eth contract (as spender in approve().
  2. Anyone can call the function replanish(address addr), which examines the DAI balance of account denoted by addr.
  3. If the balance is below 200DAI, the function calls Wrapped Ether contract (with call()) to convert some of the wrapped ethers to DAI, emitting a "thank you" fee to msg.sender (suppose it holds any).

Translated to English, either the owner of the tokens or anyone else who spotted the balance falling below 200USD can activate this Replenisher, which doles a small reward.

You can imagine that this kind of agent doing all sorts of useful things, for example, the agent can issue a stop-loss order when the owner is asleep.

There is a risk that when Alice calls the Agent contract, she gets no reward because Bob was calling it at the same moment and Bob's transaction is ordered before hers. If the reward is too small, Alice might think it's not worth the risk. If the reward is too big, Alice might think too many people will compete, and most likely she will end up sending the transaction fee and getting nothing in return. Either way, Alice might not join the game. The sure way to win money is for the miners to participate since a miner can make sure his transaction is included before anyone else's if he is lucky enough to mind the block.

Because an agent contract needs to be approved to access a user's ERC20 tokens, the TokenScript has to be made with the capacity to send a transaction to approve. Intuitively, the best place to control the approval and revocation is the Token, like in this example:

agent contract at work

[Clearly, I used a different agent than "Replenisher" in this illustration. The agent contract, in this case, is Subscription Authoriser, which authorises a 3rd party to withdraw a limited amount of money at an interval.]