From fc6f49523b9f8952a1c8c8818a8ea30ef2ee956f Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 31 Jul 2018 10:03:31 -0400 Subject: [PATCH] Added token to convert from ERC20 to ERC721 --- config/contracts.json | 12 +++--- contracts/plasma/PlasmaERC20.sol | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 contracts/plasma/PlasmaERC20.sol diff --git a/config/contracts.json b/config/contracts.json index 60902c7..615c07b 100644 --- a/config/contracts.json +++ b/config/contracts.json @@ -59,13 +59,13 @@ "RootChain": { "args": ["$ValidatorManagerContract"] }, - "CryptoCards": { - "args": ["$RootChain"], - "onDeploy": ["ValidatorManagerContract.methods.toggleToken($CryptoCards).send()"] + "CryptoCards": { "deploy": false }, + "PlasmaERC20":{ "deploy": false }, + "PlasmaSNT": { + "instanceOf": "PlasmaERC20", + "args": ["$RootChain", "$SNT", "5000000000000000000", "PlasmaSNT", "PSNT"], + "onDeploy": ["ValidatorManagerContract.methods.toggleToken($PlasmaSNT).send()"] } - - - } }, "testnet":{ diff --git a/contracts/plasma/PlasmaERC20.sol b/contracts/plasma/PlasmaERC20.sol new file mode 100644 index 0000000..f07783b --- /dev/null +++ b/contracts/plasma/PlasmaERC20.sol @@ -0,0 +1,63 @@ +pragma solidity ^0.4.24; + +import "openzeppelin-solidity/contracts/token/ERC721/ERC721Token.sol"; +import "../common/Owned.sol"; +import "../token/ERC20Token.sol"; + +contract PlasmaERC20 is ERC721Token, Owned { + + address plasma; + ERC20Token token; + uint exchangeRate; + + constructor (address _plasma, + address _token, + uint _exchangeRate, + string _name, + string _symbol + ) + ERC721Token(_name, _symbol) + public { + plasma = _plasma; + token = ERC20Token(_token); + exchangeRate = _exchangeRate; + } + + mapping(uint => uint) ERC20Balances; + + function depositERC20() public { + require(token.allowance(msg.sender, address(this)) >= exchangeRate); + require(token.transferFrom(msg.sender, address(this), exchangeRate)); + + uint256 tokenId = allTokens.length + 1; + ERC20Balances[tokenId] = exchangeRate; + _mint(msg.sender, tokenId); + } + + function withdrawERC20(uint tokenId) public { + require(ownerOf(tokenId) == msg.sender); + uint erc20Value = ERC20Balances[tokenId]; + ERC20Balances[tokenId] = 0; + _burn(msg.sender, tokenId); + require(token.transferFrom(address(this), msg.sender, erc20Value)); + } + + function depositToPlasmaWithData(uint tokenId, bytes _data) public { + require(plasma != address(0)); + safeTransferFrom( + msg.sender, + plasma, + tokenId, + _data); + } + + function depositToPlasma(uint tokenId) public { + require(plasma != address(0)); + safeTransferFrom(msg.sender, plasma, tokenId); + } + + function setExchangeRate(uint _newRate) onlyOwner public { + exchangeRate = _newRate; + } + +} \ No newline at end of file