github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/xc/eth-v1.0.3/XC.sol (about) 1 pragma solidity ^0.4.19; 2 3 import "./XCInterface.sol"; 4 5 import "./Token.sol"; 6 7 import "./XCPlugin.sol"; 8 9 import "./SafeMath.sol"; 10 11 contract XC is XCInterface { 12 13 /** 14 * Contract Administrator 15 * @field status Contract external service status. 16 * @field platformName Current contract platform name. 17 * @field account Current contract administrator. 18 */ 19 struct Admin { 20 uint8 status; 21 bytes32 platformName; 22 address account; 23 } 24 25 Admin private admin; 26 27 uint public lockBalance; 28 29 Token private token; 30 31 XCPlugin private xcPlugin; 32 33 event Lock(bytes32 toPlatform, address toAccount, bytes32 value, bytes32 tokenSymbol); 34 35 event Unlock(string txid, bytes32 fromPlatform, address fromAccount, bytes32 value, bytes32 tokenSymbol); 36 37 constructor() public { 38 init(); 39 } 40 41 /** 42 * TODO Parameters that must be set before compilation 43 * $Init admin.status 44 * $Init admin.platformName 45 * $Init admin.account 46 * $Init lockBalance 47 * $Init token 48 * $Init xcPlugin 49 */ 50 function init() internal { 51 // Admin {status | platformName | account} 52 admin.status = 3; 53 admin.platformName = "ETH"; 54 admin.account = msg.sender; 55 lockBalance = 344737963881081236; 56 token = Token(0xf4c90e18727c5c76499ea6369c856a6d61d3e92e); 57 xcPlugin = XCPlugin(0x15782cc68d841416f73e8f352f27cc1bc5e76e11); 58 } 59 60 function setStatus(uint8 status) onlyAdmin external { 61 require(status <= 3); 62 if (admin.status != status) { 63 admin.status = status; 64 } 65 } 66 67 function getStatus() external view returns (uint8) { 68 return admin.status; 69 } 70 71 function getPlatformName() external view returns (bytes32) { 72 return admin.platformName; 73 } 74 75 function setAdmin(address account) onlyAdmin nonzeroAddress(account) external { 76 if (admin.account != account) { 77 admin.account = account; 78 } 79 } 80 81 function getAdmin() external view returns (address) { 82 return admin.account; 83 } 84 85 function setToken(address account) onlyAdmin nonzeroAddress(account) external { 86 if (token != account) { 87 token = Token(account); 88 } 89 } 90 91 function getToken() external view returns (address) { 92 return token; 93 } 94 95 function setXCPlugin(address account) onlyAdmin nonzeroAddress(account) external { 96 if (xcPlugin != account) { 97 xcPlugin = XCPlugin(account); 98 } 99 } 100 101 function getXCPlugin() external view returns (address) { 102 return xcPlugin; 103 } 104 105 function lock(address toAccount, uint value) nonzeroAddress(toAccount) external { 106 require(admin.status == 2 || admin.status == 3); 107 require(xcPlugin.getStatus()); 108 require(value > 0); 109 uint allowance = token.allowance(msg.sender, this); 110 require(allowance >= value); 111 bool success = token.transferFrom(msg.sender, this, value); 112 require(success); 113 lockBalance = SafeMath.add(lockBalance, value); 114 emit Lock(xcPlugin.getTrustPlatform(), toAccount, bytes32(value), xcPlugin.getTokenSymbol()); 115 } 116 117 function unlock(string txid, address fromAccount, address toAccount, uint value) nonzeroAddress(toAccount) external { 118 require(admin.status == 1 || admin.status == 3); 119 require(xcPlugin.getStatus()); 120 require(value > 0); 121 bool complete; 122 bool verify; 123 (complete, verify) = xcPlugin.verifyProposal(fromAccount, toAccount, value, txid); 124 require(verify && !complete); 125 uint balance = token.balanceOf(this); 126 require(balance >= value); 127 require(token.transfer(toAccount, value)); 128 require(xcPlugin.commitProposal(txid)); 129 lockBalance = SafeMath.sub(lockBalance, value); 130 emit Unlock(txid, xcPlugin.getTrustPlatform(), fromAccount, bytes32(value), xcPlugin.getTokenSymbol()); 131 } 132 133 function withdraw(address account, uint value) onlyAdmin nonzeroAddress(account) external { 134 require(value > 0); 135 uint balance = token.balanceOf(this); 136 require(SafeMath.sub(balance, lockBalance) >= value); 137 bool success = token.transfer(account, value); 138 require(success); 139 } 140 141 modifier onlyAdmin { 142 require(admin.account == msg.sender); 143 _; 144 } 145 146 modifier nonzeroAddress(address account) { 147 require(account != address(0)); 148 _; 149 } 150 }