Created
December 26, 2020 13:06
-
-
Save aviexk/ddca8d556f48d5a700a97315824b0bc3 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.12+commit.27d51765.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0-solc-0.7/contracts/token/ERC20/ERC20.sol'; | |
contract CollateralToken is ERC20 { | |
constructor() ERC20("METH", "METH") { | |
_setupDecimals(0); | |
_mint(0x5B38Da6a701c568545dCfcB03FcB875f56beddC4, 100000); //tokenSponser | |
_mint(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, 100000); //Liquidator | |
_mint(0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db, 100000); //Disputor | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
import "./SyntheticToken.sol"; | |
import "./TokenFactory.sol"; | |
import "./CollateralToken.sol"; | |
import './Timer.sol'; | |
contract FinancialContract { | |
SyntheticToken public syntheticToken; | |
TokenFactory public tokenFactory; | |
CollateralToken public collateralToken; | |
Timer public timer; | |
//store created positions | |
struct Position { | |
uint256 tokensOutstanding; | |
uint256 collateralAmount; | |
} | |
mapping(address => Position) public positions; | |
uint256 public minimumNumberOfTokens; | |
constructor() { | |
minimumNumberOfTokens = 100; | |
tokenFactory = TokenFactory(0x9ecEA68DE55F316B702f27eE389D10C2EE0dde84); | |
syntheticToken = tokenFactory.createToken("SyntheticToken", "SNT"); | |
collateralToken = CollateralToken(0xd7B63981A38ACEB507354DF5b51945bacbe28414); | |
} | |
//createPosition | |
function createPosition(uint256 _numberOfTokens, uint256 _collateralAmount) public { | |
require(_numberOfTokens >= minimumNumberOfTokens, "Below minimum amount of tokens"); | |
Position storage currentPosition = positions[msg.sender]; | |
currentPosition.tokensOutstanding = _numberOfTokens; | |
currentPosition.collateralAmount = _collateralAmount; | |
syntheticToken.mint(msg.sender, _numberOfTokens); | |
collateralToken.transferFrom(msg.sender, address(this), _collateralAmount); | |
} | |
struct Liquidation { | |
address sponsor; | |
address liquidator; | |
uint256 liquidationTime; | |
address disputer; | |
uint256 settlementPrice; | |
uint256 collateralLocked; | |
uint256 liquidationCollateral; | |
} | |
uint256 public numberOfLiquidations; | |
mapping(uint256 => Liquidation) public liquidations; | |
//createLiquidation | |
function createLiquidation(address _tokenSponsor) public { | |
numberOfLiquidations = numberOfLiquidations + 1; | |
Position storage positionToLiquidate = positions[_tokenSponsor]; | |
Liquidation storage newLiquidation = liquidations[numberOfLiquidations]; | |
newLiquidation.sponsor = _tokenSponsor; | |
newLiquidation.liquidator = msg.sender; | |
newLiquidation.liquidationTime = timer.getTime(); | |
newLiquidation.disputer = address(0); | |
newLiquidation.settlementPrice = 0; | |
newLiquidation.collateralLocked = positionToLiquidate.collateralAmount; | |
newLiquidation.liquidationCollateral = positionToLiquidate.collateralAmount; | |
uint256 tokensToLiquidate = positionToLiquidate.tokensOutstanding; | |
syntheticToken.transferFrom(msg.sender, address(this), tokensToLiquidate); | |
syntheticToken.burn(tokensToLiquidate); | |
collateralToken.transferFrom(msg.sender, address(this), positionToLiquidate.collateralAmount); | |
delete positions[_tokenSponsor]; | |
} | |
//disputeLiqudation | |
//settlePosition | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
contract Oracle { | |
//save priceidentifiers - eth/usd, btc/usd | |
mapping(string => uint256) private prices; | |
//make identifiers supported | |
mapping(string => bool) private supportedIdentifiers; | |
function setPrice(string memory _identifier, uint256 _price) public { | |
require(isSupportedIdentifier(_identifier), "Identifier not supported"); | |
prices[_identifier] = _price; | |
} | |
function getPrice(string memory _identifier) public view returns (uint256) { | |
return prices[_identifier]; | |
} | |
function addIdentifier(string memory _identifier) public { | |
supportedIdentifiers[_identifier] = true; | |
} | |
function isSupportedIdentifier(string memory _identifier) public view returns (bool) { | |
return supportedIdentifiers[_identifier]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0-solc-0.7/contracts/token/ERC20/ERC20.sol'; | |
contract SyntheticToken is ERC20 { | |
constructor(string memory _tokenName, string memory _tokenSymbol) ERC20(_tokenName, _tokenSymbol) { | |
_setupDecimals(0); | |
} | |
function mint(address _recipient, uint256 _value) public returns (bool) { | |
_mint(_recipient, _value); | |
return true; | |
} | |
function burn(uint256 _value) public { | |
_burn(msg.sender, _value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
contract Timer { | |
uint256 private currentTime; | |
constructor() { | |
currentTime = 0; | |
} | |
function setTime(uint256 _newTime) public{ | |
currentTime = _newTime; | |
} | |
function getTime() public view returns (uint256) { | |
return currentTime; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///SPDX-License-Identifier: MIT | |
pragma solidity ^0.7.3; | |
import './SyntheticToken.sol'; | |
contract TokenFactory { | |
function createToken(string memory _name, string memory _symbol) public returns (SyntheticToken) { | |
SyntheticToken newSyntheticToken = new SyntheticToken(_name, _symbol); | |
return newSyntheticToken; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment