-
Notifications
You must be signed in to change notification settings - Fork 4
/
MyToken.sol
65 lines (57 loc) · 1.99 KB
/
MyToken.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pragma solidity ^0.4.24;
import "../../scripts/contracts/src/native/CrossChain.sol";
contract MyToken is CrossChain {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract */
function MyToken(uint256 _balance) public {
balanceOf[msg.sender] = _balance;
}
/* Send coins */
function transfer(address _to, uint256 _value) public {
require(balanceOf[msg.sender] >= _value);
// Check if the sender has enough
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Check for overflows
balanceOf[msg.sender] -= _value;
// Subtract from the sender
balanceOf[_to] += _value;
// Add the same to the recipient
}
function getBalance(address addr) public view returns (uint256) {
return balanceOf[addr];
}
uint256 txDataSize = 0x20;
function sendToSideChain(
uint toChainId,
address destContract,
bytes txData
) public {
require(txData.length == txDataSize);
uint256 value;
assembly {
value := mload(add(txData, 0x20))
}
require(balanceOf[msg.sender] >= value);
bytes4 destFuncHasher = bytes4(keccak256("recvFromSideChain(bytes)"));
sendTransaction(toChainId, destContract, destFuncHasher);
balanceOf[msg.sender] -= value;
}
// verify_proof need:
// check from_chain_id in ChainManager.sideChains
// check to_chain_id == my chain_id
// check dest_contract == this
// check hasher == RECV_FUNC_HASHER
// check cross_chain_nonce == cross_chain_nonce
// extract origin tx sender and origin tx data
function recvFromSideChain(bytes txProof) public {
address sender;
bytes memory txData;
(sender, txData) = verifyTransaction(txProof, txDataSize);
uint256 value;
assembly {
value := mload(add(txData, 0x20))
}
balanceOf[sender] += value;
}
}