Skip to content

Instantly share code, notes, and snippets.

@justinbarry
Created February 11, 2021 23:40
Show Gist options
  • Save justinbarry/6e39785c2d9fc2131a9df5160be91408 to your computer and use it in GitHub Desktop.
Save justinbarry/6e39785c2d9fc2131a9df5160be91408 to your computer and use it in GitHub Desktop.
pragma solidity 0.5.15;
pragma experimental ABIEncoderV2;
import "ROOT/trading/wrappedShareToken/WrappedShareToken.sol";
import 'ROOT/para/interfaces/IParaShareToken.sol';
/**
* @dev This is a factory that creates Wrappers around ERC1155 shareTokens generated by Augur
* @author yashnaman
* as shares on outcomes of a markets.
* For every outcome there will be one wrapper.
*/
contract WrappedShareTokenFactory {
IParaShareToken public shareToken;
IERC20 public cash;
mapping(uint256 => address) public wrappers;
event WrapperCreated(uint256 indexed tokenId, address tokenAddress, string symbol);
/**@dev sets value for {shareToken} and {cash}
* @param _shareToken address of shareToken associated with a augur universe
*/
constructor(IParaShareToken _shareToken, IERC20 _cash) public {
shareToken = _shareToken;
cash = _shareToken.cash();
}
/**@dev creates new ERC20 wrappers for a outcome of a market
*@param _tokenId token id associated with a outcome of a market
*@param _name a descriptive name mentioning market and outcome
*@param _symbol symbol for the ERC20 wrapper
*@param decimals decimals for the ERC20 wrapper
*/
function newWrappedShareToken(
uint256 _tokenId,
string memory _name,
string memory _symbol,
uint8 _decimals
) public {
require(wrappers[_tokenId] == address(0), "Wrapper already created");
WrappedShareToken WrappedShareToken = new WrappedShareToken(
shareToken,
cash,
_tokenId,
_name,
_symbol,
_decimals
);
wrappers[_tokenId] = address(WrappedShareToken);
emit WrapperCreated(_tokenId, address(WrappedShareToken), _symbol);
}
/**@dev creates new ERC20 wrappers for multiple tokenIds*/
function newWrappedShareTokens(
uint256[] memory _tokenIds,
string[] memory _names,
string[] memory _symbols,
uint8[] memory _decimals
) public {
require(
_tokenIds.length == _names.length &&
_tokenIds.length == _symbols.length
);
for (uint256 i = 0; i < _tokenIds.length; i++) {
newWrappedShareToken(_tokenIds[i], _names[i], _symbols[i], _decimals[i]);
}
}
/**@dev A function that wraps ERC1155s shareToken into ERC20s
* Requirements:
*
* - msg.sender has setApprovalForAll to this contract
* @param _tokenId token id associated with a outcome of a market
* @param _account account the newly minted ERC20s will go to
* @param _amount amount of tokens to be wrapped
*/
function wrapTokens(
uint256 _tokenId,
address _account,
uint256 _amount
) public {
WrappedShareToken WrappedShareToken = WrappedShareToken(wrappers[_tokenId]);
shareToken.unsafeTransferFrom(
msg.sender,
address(WrappedShareToken),
_tokenId,
_amount
);
WrappedShareToken.wrapTokens(_account, _amount);
}
/**@dev A function that burns ERC20s and gives back ERC1155s
* Requirements:
*
* - msg.sender has more than _amount of ERC20 tokens associated with _tokenId.
* - if the market has finalized then it is advised that you call claim() on WrappedShareToken
* contract associated with the winning outcome
* @param _tokenId token id associated with a outcome of a market
* @param _amount amount of tokens to be unwrapped
*/
function unWrapTokens(uint256 _tokenId, uint256 _amount) public {
WrappedShareToken WrappedShareToken = WrappedShareToken(wrappers[_tokenId]);
WrappedShareToken.unWrapTokens(msg.sender, _amount);
}
/**@dev wraps multiple tokens */
function wrapMultipleTokens(
uint256[] memory _tokenIds,
address _account,
uint256[] memory _amounts
) public {
for (uint256 i = 0; i < _tokenIds.length; i++) {
wrapTokens(_tokenIds[i], _account, _amounts[i]);
}
}
/**@dev unwraps multiple tokens */
function unWrapMultipleTokens(
uint256[] memory _tokenIds,
uint256[] memory _amounts
) public {
for (uint256 i = 0; i < _tokenIds.length; i++) {
unWrapTokens(_tokenIds[i], _amounts[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment