Betting Tokens In A Prediction Market

June 12, 2018 by Todd Proebsting

This post will describe how to implement a prediction market where bets are for ERC20 tokens rather than ether.

My previous
post on prediction markets
supported betting ether. For some applications, it’s important to have the betting currency be ERC20 tokens, so I’m going to alter that previous implementation to accept tokens rather than ether. Note that it’s possible to
wrap ether in a token,
so this new contract will be strictly more general than the previous one.

Just like the previous prediction market contract, this contract will inherit the routines of the WagerBase contract, so all I need to do is to create the appropriate constructor and routines that accept and pay off bets. It’s a good idea to be familiar with that
post
before going further here.

Token-Based Prediction Market

Like the ether-based prediction market contract, this contract requires a proposition and possible outcomes. This contract also requires which ERC20 token will be the wagered token (as opposed to ether).

pragma solidity^0.4.24;import"wagerbase.sol";import"ierc20token.sol";contractTokenPredictionMarketisWagerBase{IERC20Tokenpublicwagertoken;constructor(string_proposition,bytes32[]_outcomes,bytes32[]_symbols,uint256timeoutDelay,IERC20Token_wagertoken)// Just forward the parameters to the base constructor.WagerBase(_proposition,_outcomes,_symbols,timeoutDelay)public{wagertoken=_wagertoken;}

Buying Bundles

Outcome tokens are now bought in exchange for wagered tokens. The bettor must have approved the transfer of those wagered tokens prior to calling buyBundle:

Refunding Bundles

Complete bundles of outcome tokens can be exchanged for wagered tokens. The bettor must approve the outcome tokens for transfer before calling refundBundle. Tokens representing each outcome are burned. The wagered tokens are transferred directly back to msg.sender.