Transactions
Token Transfers
Internal Transactions
Coin Balance History
Logs
Code
Read Contract
Write Contract
Contract is not verified. However, we found a verified contract with the same bytecode in our database 0x95aa50e3ec26d4da1e98196d8dfc76583bbb9766.
All metadata displayed below is from that contract. In order to verify current contract, click Verify & Publish button
Verify & Publish
All metadata displayed below is from that contract. In order to verify current contract, click Verify & Publish button
- Contract name:
- User
- Optimization enabled
- true
- Compiler version
- v0.8.9+commit.e5eed63a
- Optimization runs
- 200
- Verified at
- 2022-06-22T09:13:18.520174Z
contracts/User.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; import {Bytes} from "./libs/Bytes.sol"; import {IRegistry, Registrable} from "./Registrable.sol"; import {IERC20} from "./Asset.sol"; contract User is Registrable { using Bytes for bytes; event ProcessCalled( address indexed process, bytes input, bool result, bytes output ); bytes public members; constructor(bytes memory _members) { members = _members; } function run( address asset, uint256 amount, bytes memory extra ) external onlyRegistry returns (bool) { if (extra.length < 28) { IRegistry(registry).claim(asset, amount); return true; } uint16 count = extra.toUint16(0); if (count < 1 || count > 16) { IRegistry(registry).claim(asset, amount); return true; } for (uint256 offset = 2; count >= 0 && offset < extra.length; count--) { bool primary = offset == 2; bytes memory data = extra.slice(offset, extra.length - offset); (uint256 size, bool success) = handle(data, asset, amount, primary); if (!success) { break; } offset = offset + size; } try IRegistry(registry).claim(asset, amount) {} catch {} return true; } function handle( bytes memory extra, address asset, uint256 amount, bool primary ) internal returns (uint256, bool) { uint256 offset = 0; if (offset + 20 > extra.length) { return (offset, false); } address process = extra.toAddress(offset); offset = offset + 20; if (primary) { IERC20(asset).approve(process, 0); IERC20(asset).approve(process, amount); } if (offset + 2 > extra.length) { return (offset, false); } uint256 size = extra.toUint16(offset); offset = offset + 2; if (offset + size > extra.length) { return (offset, false); } bytes memory input = extra.slice(offset, size); (bool result, bytes memory output) = process.call(input); offset = offset + size; emit ProcessCalled(process, input, result, output); return (offset, result); } }
contracts/Asset.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; import {IRegistry, Registrable} from "./Registrable.sol"; interface IERC20 { function balanceOf(address who) external view returns (uint256); function transfer(address to, uint256 value) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function transferFrom( address from, address to, uint256 value ) external returns (bool); function approve(address spender, uint256 value) external returns (bool); event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); } abstract contract StandardToken is IERC20 { mapping(address => uint256) balances; mapping(address => mapping(address => uint256)) allowed; function balanceOf(address _owner) public view override returns (uint256 balance) { return balances[_owner]; } function _transfer( address _from, address _to, uint256 _value ) internal { balances[_from] = balances[_from] - _value; balances[_to] = balances[_to] + _value; emit Transfer(_from, _to, _value); } function _transferFrom( address _from, address _to, uint256 _value ) internal { uint256 _allowance = allowed[_from][msg.sender]; allowed[_from][msg.sender] = _allowance - _value; _transfer(_from, _to, _value); } function approve(address _spender, uint256 _value) public override returns (bool) { // To change the approve amount you first have to reduce the addresses` // allowance to zero by calling `approve(_spender, 0)` if it is not // already 0 to mitigate the race condition described here: // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 require( (_value == 0) || (allowed[msg.sender][_spender] == 0), "approve on a non-zero allowance" ); allowed[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); return true; } function allowance(address _owner, address _spender) public view override returns (uint256 remaining) { return allowed[_owner][_spender]; } } contract Asset is Registrable, StandardToken { uint128 public immutable id; string public name; string public symbol; uint256 public totalSupply; uint8 public constant decimals = 8; constructor( uint128 _id, string memory _name, string memory _symbol ) { id = _id; name = _name; symbol = _symbol; } function transferWithExtra( address to, uint256 value, bytes memory extra ) public returns (bool) { _transfer(msg.sender, to, value); IRegistry(registry).burn(to, value, extra); return true; } function transfer(address to, uint256 value) public override returns (bool) { return transferWithExtra(to, value, new bytes(0)); } function transferFrom( address from, address to, uint256 value ) public override returns (bool) { _transferFrom(from, to, value); IRegistry(registry).burn(to, value, new bytes(0)); return true; } function mint(address to, uint256 value) external onlyRegistry { balances[to] = balances[to] + value; totalSupply = totalSupply + value; emit Transfer(registry, to, value); } function burn(address to, uint256 value) external onlyRegistry { balances[to] = balances[to] - value; totalSupply = totalSupply - value; emit Transfer(to, registry, value); } }
contracts/Factory.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; import {Asset} from "./Asset.sol"; import {User} from "./User.sol"; abstract contract Factory { uint256 public constant VERSION = 1; event UserCreated(address indexed at, bytes members); event AssetCreated(address indexed at, uint256 id); mapping(address => bytes) public users; mapping(address => uint128) public assets; mapping(uint256 => address) public contracts; function getOrCreateAssetContract( uint128 id, string memory symbol, string memory name ) internal returns (address) { address old = contracts[id]; if (old != address(0)) { return old; } bytes memory code = getAssetContractCode(id, symbol, name); address asset = getContractAddress(code); if (assets[asset] > 0) { return asset; } address addr = deploy(code, VERSION); require(addr == asset, "malformed asset contract address"); assets[asset] = id; contracts[id] = asset; emit AssetCreated(asset, id); return asset; } function getOrCreateUserContract(bytes memory members) internal returns (address) { uint256 id = uint256(keccak256(members)); address old = contracts[id]; if (old != address(0)) { return old; } bytes memory code = getUserContractCode(members); address user = getContractAddress(code); if (users[user].length > 0) { return user; } address addr = deploy(code, VERSION); require(addr == user, "malformed user contract address"); users[user] = members; contracts[id] = user; emit UserCreated(user, members); return user; } function getUserContractCode(bytes memory members) internal pure returns (bytes memory) { bytes memory code = type(User).creationCode; bytes memory args = abi.encode(members); return abi.encodePacked(code, args); } function getAssetContractCode( uint256 id, string memory symbol, string memory name ) internal pure returns (bytes memory) { bytes memory code = type(Asset).creationCode; bytes memory args = abi.encode(id, name, symbol); return abi.encodePacked(code, args); } function getContractAddress(bytes memory code) internal view returns (address) { code = abi.encodePacked( bytes1(0xff), address(this), VERSION, keccak256(code) ); return address(uint160(uint256(keccak256(code)))); } function deploy(bytes memory bytecode, uint256 _salt) internal returns (address) { address addr; assembly { addr := create2( callvalue(), add(bytecode, 0x20), mload(bytecode), _salt ) if iszero(extcodesize(addr)) { revert(0, 0) } } return addr; } }
contracts/Registrable.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; interface IRegistry { function claim(address asset, uint256 amount) external returns (bool); function burn(address user, uint256 amount, bytes memory extra) external returns (bool); } abstract contract Registrable { address public registry; modifier onlyRegistry() { require(msg.sender == registry, "not registry"); _; } constructor() { registry = msg.sender; } }
contracts/Registry.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; import {Integer} from "./libs/Integer.sol"; import {Bytes} from "./libs/Bytes.sol"; import {BLS} from "./libs/BLS.sol"; import {Storage} from "./Storage.sol"; import {IRegistry, Registrable} from "./Registrable.sol"; import {Factory} from "./Factory.sol"; import {Asset} from "./Asset.sol"; import {User} from "./User.sol"; contract Registry is IRegistry, Factory { using Bytes for bytes; using BLS for uint256[2]; using BLS for bytes; event Halted(bool state); event Iterated(uint256[4] from, uint256[4] to); event MixinTransaction(bytes raw); event MixinEvent( uint64 indexed nonce, address indexed user, address indexed asset, uint256 amount, bytes extra, uint64 timestamp ); uint128 public immutable PID; uint256[4] public GROUP; uint64 public INBOUND = 0; uint64 public OUTBOUND = 0; bool public HALTED = false; mapping(uint128 => uint256) public balances; struct Event { uint64 nonce; address user; address asset; uint256 amount; bytes extra; uint64 timestamp; uint256[2] sig; } constructor(bytes memory raw, uint128 pid) { require(raw.length == 128); require(pid > 0); GROUP = [ raw.toUint256(0), raw.toUint256(32), raw.toUint256(64), raw.toUint256(96) ]; PID = pid; } function iterate(bytes memory raw) public { require(HALTED, "invalid state"); require(raw.length == 256, "invalid input size"); uint256[4] memory group = [ raw.toUint256(0), raw.toUint256(32), raw.toUint256(64), raw.toUint256(96) ]; uint256[2] memory sig1 = [raw.toUint256(128), raw.toUint256(160)]; uint256[2] memory sig2 = [raw.toUint256(192), raw.toUint256(224)]; uint256[2] memory message = raw.slice(0, 128).hashToPoint(); require(sig1.verifySingle(GROUP, message), "invalid signature"); require(sig2.verifySingle(group, message), "invalid signature"); emit Iterated(GROUP, group); GROUP = group; } function halt(bytes memory raw) public { bytes memory input = bytes("HALT").concat( Integer.uint64ToFixedBytes(INBOUND) ); uint256[2] memory sig = [raw.toUint256(0), raw.toUint256(32)]; uint256[2] memory message = input.hashToPoint(); require(sig.verifySingle(GROUP, message), "invalid signature"); HALTED = !HALTED; emit Halted(HALTED); } function claim(address asset, uint256 amount) external returns (bool) { require(users[msg.sender].length > 0, "invalid user"); require(assets[asset] > 0, "invalid asset"); Asset(asset).burn(msg.sender, amount); sendMixinTransaction(msg.sender, asset, amount, new bytes(0)); return true; } function burn( address user, uint256 amount, bytes memory extra ) external returns (bool) { require(assets[msg.sender] > 0, "invalid asset"); if (users[user].length == 0) { return true; } Asset(msg.sender).burn(user, amount); sendMixinTransaction(user, msg.sender, amount, extra); return true; } function sendMixinTransaction( address user, address asset, uint256 amount, bytes memory extra ) internal { uint256 balance = balances[assets[asset]]; bytes memory log = buildMixinTransaction( OUTBOUND, users[user], assets[asset], amount, extra ); emit MixinTransaction(log); balances[assets[asset]] = balance - amount; OUTBOUND = OUTBOUND + 1; } // process || nonce || asset || amount || extra || timestamp || members || threshold || sig function buildMixinTransaction( uint64 nonce, bytes memory receiver, uint128 asset, uint256 amount, bytes memory extra ) internal returns (bytes memory) { if (extra.length >= 68 && extra.toUint128(0) == PID) { Storage stg = Storage(extra.toAddress(16)); bytes memory data = extra.slice(68, extra.length - 68); stg.write(extra.toUint256(36), data); extra = extra.slice(0, 68); } bytes memory raw = Integer.uint128ToFixedBytes(PID); raw = raw.concat(Integer.uint64ToFixedBytes(nonce)); raw = raw.concat(Integer.uint128ToFixedBytes(asset)); (bytes memory ab, uint16 al) = Integer.uint256ToVarBytes(amount); raw = raw.concat(Integer.uint16ToFixedBytes(al)); raw = raw.concat(ab); raw = raw.concat(Integer.uint16ToFixedBytes(uint16(extra.length))); raw = raw.concat(extra); raw = raw.concat(Integer.uint64ToFixedBytes(uint64(block.timestamp))); raw = raw.concat(receiver); raw = raw.concat(new bytes(2)); return raw; } // process || nonce || asset || amount || extra || timestamp || members || threshold || sig function mixin(bytes memory raw) public returns (bool) { require(!HALTED, "invalid state"); require(raw.length >= 141, "event data too small"); Event memory evt; uint256 offset = 0; uint128 id = raw.toUint128(offset); require(id == PID, "invalid process"); offset = offset + 16; evt.nonce = raw.toUint64(offset); require(evt.nonce == INBOUND, "invalid nonce"); INBOUND = INBOUND + 1; offset = offset + 8; (offset, id, evt.amount) = parseEventAsset(raw, offset); (offset, evt.extra, evt.timestamp) = parseEventExtra(raw, offset); (offset, evt.user) = parseEventUser(raw, offset); (evt.asset, evt.extra) = parseEventInput(id, evt.extra); offset = offset + 2; evt.sig = [raw.toUint256(offset), raw.toUint256(offset + 32)]; uint256[2] memory message = raw .slice(0, offset - 2) .concat(new bytes(2)) .hashToPoint(); require(evt.sig.verifySingle(GROUP, message), "invalid signature"); offset = offset + 64; require(raw.length == offset, "malformed event encoding"); uint256 balance = balances[assets[evt.asset]]; balances[assets[evt.asset]] = balance + evt.amount; emit MixinEvent( evt.nonce, evt.user, evt.asset, evt.amount, evt.extra, evt.timestamp ); Asset(evt.asset).mint(evt.user, evt.amount); return User(evt.user).run(evt.asset, evt.amount, evt.extra); } function parseEventExtra(bytes memory raw, uint256 offset) internal pure returns ( uint256, bytes memory, uint64 ) { uint256 size = raw.toUint16(offset); offset = offset + 2; bytes memory extra = raw.slice(offset, size); offset = offset + size; uint64 timestamp = raw.toUint64(offset); offset = offset + 8; return (offset, extra, timestamp); } function parseEventAsset(bytes memory raw, uint256 offset) internal pure returns ( uint256, uint128, uint256 ) { uint128 id = raw.toUint128(offset); require(id > 0, "invalid asset"); offset = offset + 16; uint256 size = raw.toUint16(offset); offset = offset + 2; require(size <= 32, "integer out of bounds"); uint256 amount = new bytes(32 - size) .concat(raw.slice(offset, size)) .toUint256(0); offset = offset + size; return (offset, id, amount); } function parseEventUser(bytes memory raw, uint256 offset) internal returns (uint256, address) { uint16 size = raw.toUint16(offset); size = 2 + size * 16 + 2; bytes memory members = raw.slice(offset, size); offset = offset + size; address user = getOrCreateUserContract(members); return (offset, user); } function parseEventInput(uint128 id, bytes memory extra) internal returns (address, bytes memory) { uint256 offset = 0; uint16 size = extra.toUint16(offset); offset = offset + 2; string memory symbol = string(extra.slice(offset, size)); offset = offset + size; size = extra.toUint16(offset); offset = offset + 2; string memory name = string(extra.slice(offset, size)); offset = offset + size; bytes memory input = extra.slice(offset, extra.length - offset); if (input.length == 68 && input.toUint128(0) == PID) { input = Storage(input.toAddress(16)).read(input.toUint256(36)); } address asset = getOrCreateAssetContract(id, symbol, name); return (asset, input); } }
contracts/Storage.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; contract Storage { mapping(uint256 => bytes) internal values; function read(uint256 _key) public view returns (bytes memory) { return values[_key]; } function write(uint256 _key, bytes memory raw) public { uint256 key = uint256(keccak256(raw)); require(key == _key, "invalid key or raw"); values[_key] = raw; } }
contracts/libs/BLS.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; library BLS { // Field order uint256 constant N = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // Negated genarator of G2 uint256 constant nG2x1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634; uint256 constant nG2x0 = 10857046999023057135944570762232829481370756359578518086990519993285655852781; uint256 constant nG2y1 = 17805874995975841540914202342111839520379459829704422454583296818431106115052; uint256 constant nG2y0 = 13392588948715843804641432497768002650278120570034223513918757245338268106653; function verifySingle( uint256[2] memory signature, uint256[4] memory pubkey, uint256[2] memory message ) internal view returns (bool) { uint256[12] memory input = [ signature[0], signature[1], nG2x1, nG2x0, nG2y1, nG2y0, message[0], message[1], pubkey[1], pubkey[0], pubkey[3], pubkey[2] ]; uint256[1] memory out; bool success; // solium-disable-next-line security/no-inline-assembly assembly { success := staticcall(sub(gas(), 2000), 8, input, 384, out, 0x20) // use invalid() to make gas estimation work switch success case 0 { invalid() } } require(success, "BLS: paring check call failed"); return out[0] != 0; } function hashToPoint(bytes memory data) internal view returns (uint256[2] memory p) { return mapToPoint(keccak256(data)); } function mapToPoint(bytes32 _x) internal view returns (uint256[2] memory p) { uint256 x = uint256(_x) % N; uint256 y; bool found = false; while (true) { y = mulmod(x, x, N); y = mulmod(y, x, N); y = addmod(y, 3, N); (y, found) = sqrt(y); if (found) { p[0] = x; p[1] = y; break; } x = addmod(x, 1, N); } } function sqrt(uint256 xx) internal view returns (uint256 x, bool hasRoot) { bool success; // solium-disable-next-line security/no-inline-assembly assembly { let freemem := mload(0x40) mstore(freemem, 0x20) mstore(add(freemem, 0x20), 0x20) mstore(add(freemem, 0x40), 0x20) mstore(add(freemem, 0x60), xx) // (N + 1) / 4 = 0xc19139cb84c680a6e14116da060561765e05aa45a1c72a34f082305b61f3f52 mstore( add(freemem, 0x80), 0xc19139cb84c680a6e14116da060561765e05aa45a1c72a34f082305b61f3f52 ) // N = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 mstore( add(freemem, 0xA0), 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 ) success := staticcall( sub(gas(), 2000), 5, freemem, 0xC0, freemem, 0x20 ) switch success case 0 { invalid() } x := mload(freemem) hasRoot := eq(xx, mulmod(x, x, N)) } require(success, "BLS: sqrt modexp call failed"); } }
contracts/libs/Bytes.sol
// SPDX-License-Identifier: Unlicense /* * @title Solidity Bytes Arrays Utils * @author Gonçalo Sá <goncalo.sa@consensys.net> * * @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity. * The library lets you concatenate, slice and type cast bytes arrays both in memory and storage. */ pragma solidity >=0.8.0 <0.9.0; library Bytes { function concat( bytes memory _preBytes, bytes memory _postBytes ) internal pure returns (bytes memory) { bytes memory tempBytes; assembly { // Get a location of some free memory and store it in tempBytes as // Solidity does for memory variables. tempBytes := mload(0x40) // Store the length of the first bytes array at the beginning of // the memory for tempBytes. let length := mload(_preBytes) mstore(tempBytes, length) // Maintain a memory counter for the current write location in the // temp bytes array by adding the 32 bytes for the array length to // the starting location. let mc := add(tempBytes, 0x20) // Stop copying when the memory counter reaches the length of the // first bytes array. let end := add(mc, length) for { // Initialize a copy counter to the start of the _preBytes data, // 32 bytes into its memory. let cc := add(_preBytes, 0x20) } lt(mc, end) { // Increase both counters by 32 bytes each iteration. mc := add(mc, 0x20) cc := add(cc, 0x20) } { // Write the _preBytes data into the tempBytes memory 32 bytes // at a time. mstore(mc, mload(cc)) } // Add the length of _postBytes to the current length of tempBytes // and store it as the new length in the first 32 bytes of the // tempBytes memory. length := mload(_postBytes) mstore(tempBytes, add(length, mload(tempBytes))) // Move the memory counter back from a multiple of 0x20 to the // actual end of the _preBytes data. mc := end // Stop copying when the memory counter reaches the new combined // length of the arrays. end := add(mc, length) for { let cc := add(_postBytes, 0x20) } lt(mc, end) { mc := add(mc, 0x20) cc := add(cc, 0x20) } { mstore(mc, mload(cc)) } // Update the free-memory pointer by padding our last write location // to 32 bytes: add 31 bytes to the end of tempBytes to move to the // next 32 byte block, then round down to the nearest multiple of // 32. If the sum of the length of the two arrays is zero then add // one before rounding down to leave a blank 32 bytes (the length block with 0). mstore(0x40, and( add(add(end, iszero(add(length, mload(_preBytes)))), 31), not(31) // Round down to the nearest 32 bytes. )) } return tempBytes; } function concatStorage(bytes storage _preBytes, bytes memory _postBytes) internal { assembly { // Read the first 32 bytes of _preBytes storage, which is the length // of the array. (We don't need to use the offset into the slot // because arrays use the entire slot.) let fslot := sload(_preBytes.slot) // Arrays of 31 bytes or less have an even value in their slot, // while longer arrays have an odd value. The actual length is // the slot divided by two for odd values, and the lowest order // byte divided by two for even values. // If the slot is even, bitwise and the slot with 255 and divide by // two to get the length. If the slot is odd, bitwise and the slot // with -1 and divide by two. let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2) let mlength := mload(_postBytes) let newlength := add(slength, mlength) // slength can contain both the length and contents of the array // if length < 32 bytes so let's prepare for that // v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage switch add(lt(slength, 32), lt(newlength, 32)) case 2 { // Since the new array still fits in the slot, we just need to // update the contents of the slot. // uint256(bytes_storage) = uint256(bytes_storage) + uint256(bytes_memory) + new_length sstore( _preBytes.slot, // all the modifications to the slot are inside this // next block add( // we can just add to the slot contents because the // bytes we want to change are the LSBs fslot, add( mul( div( // load the bytes from memory mload(add(_postBytes, 0x20)), // zero all bytes to the right exp(0x100, sub(32, mlength)) ), // and now shift left the number of bytes to // leave space for the length in the slot exp(0x100, sub(32, newlength)) ), // increase length by the double of the memory // bytes length mul(mlength, 2) ) ) ) } case 1 { // The stored value fits in the slot, but the combined value // will exceed it. // get the keccak hash to get the contents of the array mstore(0x0, _preBytes.slot) let sc := add(keccak256(0x0, 0x20), div(slength, 32)) // save new length sstore(_preBytes.slot, add(mul(newlength, 2), 1)) // The contents of the _postBytes array start 32 bytes into // the structure. Our first read should obtain the `submod` // bytes that can fit into the unused space in the last word // of the stored array. To get this, we read 32 bytes starting // from `submod`, so the data we read overlaps with the array // contents by `submod` bytes. Masking the lowest-order // `submod` bytes allows us to add that value directly to the // stored value. let submod := sub(32, slength) let mc := add(_postBytes, submod) let end := add(_postBytes, mlength) let mask := sub(exp(0x100, submod), 1) sstore( sc, add( and( fslot, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00 ), and(mload(mc), mask) ) ) for { mc := add(mc, 0x20) sc := add(sc, 1) } lt(mc, end) { sc := add(sc, 1) mc := add(mc, 0x20) } { sstore(sc, mload(mc)) } mask := exp(0x100, sub(mc, end)) sstore(sc, mul(div(mload(mc), mask), mask)) } default { // get the keccak hash to get the contents of the array mstore(0x0, _preBytes.slot) // Start copying to the last used word of the stored array. let sc := add(keccak256(0x0, 0x20), div(slength, 32)) // save new length sstore(_preBytes.slot, add(mul(newlength, 2), 1)) // Copy over the first `submod` bytes of the new data as in // case 1 above. let slengthmod := mod(slength, 32) let mlengthmod := mod(mlength, 32) let submod := sub(32, slengthmod) let mc := add(_postBytes, submod) let end := add(_postBytes, mlength) let mask := sub(exp(0x100, submod), 1) sstore(sc, add(sload(sc), and(mload(mc), mask))) for { sc := add(sc, 1) mc := add(mc, 0x20) } lt(mc, end) { sc := add(sc, 1) mc := add(mc, 0x20) } { sstore(sc, mload(mc)) } mask := exp(0x100, sub(mc, end)) sstore(sc, mul(div(mload(mc), mask), mask)) } } } function slice( bytes memory _bytes, uint256 _start, uint256 _length ) internal pure returns (bytes memory) { require(_length + 31 >= _length, "slice_overflow"); require(_bytes.length >= _start + _length, "slice_outOfBounds"); bytes memory tempBytes; assembly { switch iszero(_length) case 0 { // Get a location of some free memory and store it in tempBytes as // Solidity does for memory variables. tempBytes := mload(0x40) // The first word of the slice result is potentially a partial // word read from the original array. To read it, we calculate // the length of that partial word and start copying that many // bytes into the array. The first word we copy will start with // data we don't care about, but the last `lengthmod` bytes will // land at the beginning of the contents of the new array. When // we're done copying, we overwrite the full first word with // the actual length of the slice. let lengthmod := and(_length, 31) // The multiplication in the next line is necessary // because when slicing multiples of 32 bytes (lengthmod == 0) // the following copy loop was copying the origin's length // and then ending prematurely not copying everything it should. let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod))) let end := add(mc, _length) for { // The multiplication in the next line has the same exact purpose // as the one above. let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start) } lt(mc, end) { mc := add(mc, 0x20) cc := add(cc, 0x20) } { mstore(mc, mload(cc)) } mstore(tempBytes, _length) //update free-memory pointer //allocating the array padded to 32 bytes like the compiler does now mstore(0x40, and(add(mc, 31), not(31))) } //if we want a zero-length slice let's just return a zero-length array default { tempBytes := mload(0x40) //zero out the 32 bytes slice we are about to return //we need to do it because Solidity does not garbage collect mstore(tempBytes, 0) mstore(0x40, add(tempBytes, 0x20)) } } return tempBytes; } function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) { require(_bytes.length >= _start + 20, "toAddress_outOfBounds"); address tempAddress; assembly { tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000) } return tempAddress; } function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) { require(_bytes.length >= _start + 1 , "toUint8_outOfBounds"); uint8 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x1), _start)) } return tempUint; } function toUint16(bytes memory _bytes, uint256 _start) internal pure returns (uint16) { require(_bytes.length >= _start + 2, "toUint16_outOfBounds"); uint16 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x2), _start)) } return tempUint; } function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) { require(_bytes.length >= _start + 4, "toUint32_outOfBounds"); uint32 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x4), _start)) } return tempUint; } function toUint64(bytes memory _bytes, uint256 _start) internal pure returns (uint64) { require(_bytes.length >= _start + 8, "toUint64_outOfBounds"); uint64 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x8), _start)) } return tempUint; } function toUint96(bytes memory _bytes, uint256 _start) internal pure returns (uint96) { require(_bytes.length >= _start + 12, "toUint96_outOfBounds"); uint96 tempUint; assembly { tempUint := mload(add(add(_bytes, 0xc), _start)) } return tempUint; } function toUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128) { require(_bytes.length >= _start + 16, "toUint128_outOfBounds"); uint128 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x10), _start)) } return tempUint; } function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) { require(_bytes.length >= _start + 32, "toUint256_outOfBounds"); uint256 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x20), _start)) } return tempUint; } function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) { require(_bytes.length >= _start + 32, "toBytes32_outOfBounds"); bytes32 tempBytes32; assembly { tempBytes32 := mload(add(add(_bytes, 0x20), _start)) } return tempBytes32; } function equal(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bool) { bool success = true; assembly { let length := mload(_preBytes) // if lengths don't match the arrays are not equal switch eq(length, mload(_postBytes)) case 1 { // cb is a circuit breaker in the for loop since there's // no said feature for inline assembly loops // cb = 1 - don't breaker // cb = 0 - break let cb := 1 let mc := add(_preBytes, 0x20) let end := add(mc, length) for { let cc := add(_postBytes, 0x20) // the next line is the loop condition: // while(uint256(mc < end) + cb == 2) } eq(add(lt(mc, end), cb), 2) { mc := add(mc, 0x20) cc := add(cc, 0x20) } { // if any of these checks fails then arrays are not equal if iszero(eq(mload(mc), mload(cc))) { // unsuccess: success := 0 cb := 0 } } } default { // unsuccess: success := 0 } } return success; } function equalStorage( bytes storage _preBytes, bytes memory _postBytes ) internal view returns (bool) { bool success = true; assembly { // we know _preBytes_offset is 0 let fslot := sload(_preBytes.slot) // Decode the length of the stored array like in concatStorage(). let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2) let mlength := mload(_postBytes) // if lengths don't match the arrays are not equal switch eq(slength, mlength) case 1 { // slength can contain both the length and contents of the array // if length < 32 bytes so let's prepare for that // v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage if iszero(iszero(slength)) { switch lt(slength, 32) case 1 { // blank the last byte which is the length fslot := mul(div(fslot, 0x100), 0x100) if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) { // unsuccess: success := 0 } } default { // cb is a circuit breaker in the for loop since there's // no said feature for inline assembly loops // cb = 1 - don't breaker // cb = 0 - break let cb := 1 // get the keccak hash to get the contents of the array mstore(0x0, _preBytes.slot) let sc := keccak256(0x0, 0x20) let mc := add(_postBytes, 0x20) let end := add(mc, mlength) // the next line is the loop condition: // while(uint256(mc < end) + cb == 2) for {} eq(add(lt(mc, end), cb), 2) { sc := add(sc, 1) mc := add(mc, 0x20) } { if iszero(eq(sload(sc), mload(mc))) { // unsuccess: success := 0 cb := 0 } } } } } default { // unsuccess: success := 0 } } return success; } }
contracts/libs/Integer.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.0 <0.9.0; import {Bytes} from "./Bytes.sol"; library Integer { using Bytes for bytes; function uint16ToFixedBytes(uint16 x) internal pure returns (bytes memory) { bytes memory c = new bytes(2); bytes2 b = bytes2(x); for (uint256 i = 0; i < 2; i++) { c[i] = b[i]; } return c; } function uint64ToFixedBytes(uint64 x) internal pure returns (bytes memory) { bytes memory c = new bytes(8); bytes8 b = bytes8(x); for (uint256 i = 0; i < 8; i++) { c[i] = b[i]; } return c; } function uint128ToFixedBytes(uint128 x) internal pure returns (bytes memory) { bytes memory c = new bytes(16); bytes16 b = bytes16(x); for (uint256 i = 0; i < 16; i++) { c[i] = b[i]; } return c; } function uint256ToVarBytes(uint256 x) internal pure returns (bytes memory, uint16) { bytes memory c = new bytes(32); bytes32 b = bytes32(x); uint16 offset = 0; for (uint16 i = 0; i < 32; i++) { c[i] = b[i]; if (c[i] > 0 && offset == 0) { offset = i; } } uint16 size = 32 - offset; return (c.slice(offset, 32 - offset), size); } }
Contract ABI
[{"type":"constructor","stateMutability":"nonpayable","inputs":[{"type":"bytes","name":"_members","internalType":"bytes"}]},{"type":"event","name":"ProcessCalled","inputs":[{"type":"address","name":"process","internalType":"address","indexed":true},{"type":"bytes","name":"input","internalType":"bytes","indexed":false},{"type":"bool","name":"result","internalType":"bool","indexed":false},{"type":"bytes","name":"output","internalType":"bytes","indexed":false}],"anonymous":false},{"type":"function","stateMutability":"view","outputs":[{"type":"bytes","name":"","internalType":"bytes"}],"name":"members","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"address"}],"name":"registry","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"run","inputs":[{"type":"address","name":"asset","internalType":"address"},{"type":"uint256","name":"amount","internalType":"uint256"},{"type":"bytes","name":"extra","internalType":"bytes"}]}]
Contract Creation Code
0x60806040523480156200001157600080fd5b5060405162000d2d38038062000d2d83398101604081905262000034916200011f565b600080546001600160a01b0319163317905580516200005b90600190602084019062000063565b505062000238565b8280546200007190620001fb565b90600052602060002090601f016020900481019282620000955760008555620000e0565b82601f10620000b057805160ff1916838001178555620000e0565b82800160010185558215620000e0579182015b82811115620000e0578251825591602001919060010190620000c3565b50620000ee929150620000f2565b5090565b5b80821115620000ee5760008155600101620000f3565b634e487b7160e01b600052604160045260246000fd5b600060208083850312156200013357600080fd5b82516001600160401b03808211156200014b57600080fd5b818501915085601f8301126200016057600080fd5b81518181111562000175576200017562000109565b604051601f8201601f19908116603f01168101908382118183101715620001a057620001a062000109565b816040528281528886848701011115620001b957600080fd5b600093505b82841015620001dd5784840186015181850187015292850192620001be565b82841115620001ef5760008684830101525b98975050505050505050565b600181811c908216806200021057607f821691505b602082108114156200023257634e487b7160e01b600052602260045260246000fd5b50919050565b610ae580620002486000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80637b103999146100465780637ceba01814610076578063bdd4d18d14610099575b600080fd5b600054610059906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b610089610084366004610855565b6100ae565b604051901515815260200161006d565b6100a161035e565b60405161006d919061098a565b600080546001600160a01b031633146100fd5760405162461bcd60e51b815260206004820152600c60248201526b6e6f7420726567697374727960a01b60448201526064015b60405180910390fd5b601c8251101561019757600054604051635569f64b60e11b81526001600160a01b038681166004830152602482018690529091169063aad3ec9690604401602060405180830381600087803b15801561015557600080fd5b505af1158015610169573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061018d919061099d565b5060019050610357565b60006101a383826103ec565b905060018161ffff1610806101bc575060108161ffff16115b1561025257600054604051635569f64b60e11b81526001600160a01b038781166004830152602482018790529091169063aad3ec9690604401602060405180830381600087803b15801561020f57600080fd5b505af1158015610223573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610247919061099d565b506001915050610357565b60025b83518110156102c757600081600214905060006102818384885161027991906109d5565b889190610449565b9050600080610292838b8b87610556565b91509150806102a457505050506102c7565b6102ae82866109ec565b94505050505081806102bf90610a04565b925050610255565b50600054604051635569f64b60e11b81526001600160a01b038781166004830152602482018790529091169063aad3ec9690604401602060405180830381600087803b15801561031657600080fd5b505af1925050508015610346575060408051601f3d908101601f191682019092526103439181019061099d565b60015b61034f57610351565b505b60019150505b9392505050565b6001805461036b90610a22565b80601f016020809104026020016040519081016040528092919081815260200182805461039790610a22565b80156103e45780601f106103b9576101008083540402835291602001916103e4565b820191906000526020600020905b8154815290600101906020018083116103c757829003601f168201915b505050505081565b60006103f98260026109ec565b835110156104405760405162461bcd60e51b8152602060048201526014602482015273746f55696e7431365f6f75744f66426f756e647360601b60448201526064016100f4565b50016002015190565b60608161045781601f6109ec565b10156104965760405162461bcd60e51b815260206004820152600e60248201526d736c6963655f6f766572666c6f7760901b60448201526064016100f4565b6104a082846109ec565b845110156104e45760405162461bcd60e51b8152602060048201526011602482015270736c6963655f6f75744f66426f756e647360781b60448201526064016100f4565b606082158015610503576040519150600082526020820160405261054d565b6040519150601f8416801560200281840101858101878315602002848b0101015b8183101561053c578051835260209283019201610524565b5050858452601f01601f1916604052505b50949350505050565b6000806000865181601461056a91906109ec565b111561057b579150600090506107d1565b600061058788836107da565b90506105948260146109ec565b915084156106a35760405163095ea7b360e01b81526001600160a01b0382811660048301526000602483015288169063095ea7b390604401602060405180830381600087803b1580156105e657600080fd5b505af11580156105fa573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061061e919061099d565b5060405163095ea7b360e01b81526001600160a01b0382811660048301526024820188905288169063095ea7b390604401602060405180830381600087803b15801561066957600080fd5b505af115801561067d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106a1919061099d565b505b87516106b08360026109ec565b11156106c257509150600090506107d1565b60006106ce89846103ec565b61ffff1690506106df8360026109ec565b89519093506106ee82856109ec565b111561070357826000945094505050506107d1565b60006107108a8584610449565b9050600080846001600160a01b03168360405161072d9190610a5d565b6000604051808303816000865af19150503d806000811461076a576040519150601f19603f3d011682016040523d82523d6000602084013e61076f565b606091505b50909250905061077f84876109ec565b9550846001600160a01b03167f374b40ceb7994eb007a897f9cafaa351e1e66f4dba809f52995372acc1f12d308484846040516107be93929190610a79565b60405180910390a2509395509293505050505b94509492505050565b60006107e78260146109ec565b8351101561082f5760405162461bcd60e51b8152602060048201526015602482015274746f416464726573735f6f75744f66426f756e647360581b60448201526064016100f4565b500160200151600160601b900490565b634e487b7160e01b600052604160045260246000fd5b60008060006060848603121561086a57600080fd5b83356001600160a01b038116811461088157600080fd5b925060208401359150604084013567ffffffffffffffff808211156108a557600080fd5b818601915086601f8301126108b957600080fd5b8135818111156108cb576108cb61083f565b604051601f8201601f19908116603f011681019083821181831017156108f3576108f361083f565b8160405282815289602084870101111561090c57600080fd5b8260208601602083013760006020848301015280955050505050509250925092565b60005b83811015610949578181015183820152602001610931565b83811115610958576000848401525b50505050565b6000815180845261097681602086016020860161092e565b601f01601f19169290920160200192915050565b602081526000610357602083018461095e565b6000602082840312156109af57600080fd5b8151801515811461035757600080fd5b634e487b7160e01b600052601160045260246000fd5b6000828210156109e7576109e76109bf565b500390565b600082198211156109ff576109ff6109bf565b500190565b600061ffff821680610a1857610a186109bf565b6000190192915050565b600181811c90821680610a3657607f821691505b60208210811415610a5757634e487b7160e01b600052602260045260246000fd5b50919050565b60008251610a6f81846020870161092e565b9190910192915050565b606081526000610a8c606083018661095e565b84151560208401528281036040840152610aa6818561095e565b969550505050505056fea265627a7a7231582092fbf7a05fe947ca227e879053d3cd676116b315b5dc2c6169168c1a6fea99f664736f6c6343000809003200000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014000140e2648c18843ab1b019102c6cb3ef460001000000000000000000000000
Deployed ByteCode
0x608060405234801561001057600080fd5b50600436106100415760003560e01c80637b103999146100465780637ceba01814610076578063bdd4d18d14610099575b600080fd5b600054610059906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b610089610084366004610855565b6100ae565b604051901515815260200161006d565b6100a161035e565b60405161006d919061098a565b600080546001600160a01b031633146100fd5760405162461bcd60e51b815260206004820152600c60248201526b6e6f7420726567697374727960a01b60448201526064015b60405180910390fd5b601c8251101561019757600054604051635569f64b60e11b81526001600160a01b038681166004830152602482018690529091169063aad3ec9690604401602060405180830381600087803b15801561015557600080fd5b505af1158015610169573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061018d919061099d565b5060019050610357565b60006101a383826103ec565b905060018161ffff1610806101bc575060108161ffff16115b1561025257600054604051635569f64b60e11b81526001600160a01b038781166004830152602482018790529091169063aad3ec9690604401602060405180830381600087803b15801561020f57600080fd5b505af1158015610223573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610247919061099d565b506001915050610357565b60025b83518110156102c757600081600214905060006102818384885161027991906109d5565b889190610449565b9050600080610292838b8b87610556565b91509150806102a457505050506102c7565b6102ae82866109ec565b94505050505081806102bf90610a04565b925050610255565b50600054604051635569f64b60e11b81526001600160a01b038781166004830152602482018790529091169063aad3ec9690604401602060405180830381600087803b15801561031657600080fd5b505af1925050508015610346575060408051601f3d908101601f191682019092526103439181019061099d565b60015b61034f57610351565b505b60019150505b9392505050565b6001805461036b90610a22565b80601f016020809104026020016040519081016040528092919081815260200182805461039790610a22565b80156103e45780601f106103b9576101008083540402835291602001916103e4565b820191906000526020600020905b8154815290600101906020018083116103c757829003601f168201915b505050505081565b60006103f98260026109ec565b835110156104405760405162461bcd60e51b8152602060048201526014602482015273746f55696e7431365f6f75744f66426f756e647360601b60448201526064016100f4565b50016002015190565b60608161045781601f6109ec565b10156104965760405162461bcd60e51b815260206004820152600e60248201526d736c6963655f6f766572666c6f7760901b60448201526064016100f4565b6104a082846109ec565b845110156104e45760405162461bcd60e51b8152602060048201526011602482015270736c6963655f6f75744f66426f756e647360781b60448201526064016100f4565b606082158015610503576040519150600082526020820160405261054d565b6040519150601f8416801560200281840101858101878315602002848b0101015b8183101561053c578051835260209283019201610524565b5050858452601f01601f1916604052505b50949350505050565b6000806000865181601461056a91906109ec565b111561057b579150600090506107d1565b600061058788836107da565b90506105948260146109ec565b915084156106a35760405163095ea7b360e01b81526001600160a01b0382811660048301526000602483015288169063095ea7b390604401602060405180830381600087803b1580156105e657600080fd5b505af11580156105fa573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061061e919061099d565b5060405163095ea7b360e01b81526001600160a01b0382811660048301526024820188905288169063095ea7b390604401602060405180830381600087803b15801561066957600080fd5b505af115801561067d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106a1919061099d565b505b87516106b08360026109ec565b11156106c257509150600090506107d1565b60006106ce89846103ec565b61ffff1690506106df8360026109ec565b89519093506106ee82856109ec565b111561070357826000945094505050506107d1565b60006107108a8584610449565b9050600080846001600160a01b03168360405161072d9190610a5d565b6000604051808303816000865af19150503d806000811461076a576040519150601f19603f3d011682016040523d82523d6000602084013e61076f565b606091505b50909250905061077f84876109ec565b9550846001600160a01b03167f374b40ceb7994eb007a897f9cafaa351e1e66f4dba809f52995372acc1f12d308484846040516107be93929190610a79565b60405180910390a2509395509293505050505b94509492505050565b60006107e78260146109ec565b8351101561082f5760405162461bcd60e51b8152602060048201526015602482015274746f416464726573735f6f75744f66426f756e647360581b60448201526064016100f4565b500160200151600160601b900490565b634e487b7160e01b600052604160045260246000fd5b60008060006060848603121561086a57600080fd5b83356001600160a01b038116811461088157600080fd5b925060208401359150604084013567ffffffffffffffff808211156108a557600080fd5b818601915086601f8301126108b957600080fd5b8135818111156108cb576108cb61083f565b604051601f8201601f19908116603f011681019083821181831017156108f3576108f361083f565b8160405282815289602084870101111561090c57600080fd5b8260208601602083013760006020848301015280955050505050509250925092565b60005b83811015610949578181015183820152602001610931565b83811115610958576000848401525b50505050565b6000815180845261097681602086016020860161092e565b601f01601f19169290920160200192915050565b602081526000610357602083018461095e565b6000602082840312156109af57600080fd5b8151801515811461035757600080fd5b634e487b7160e01b600052601160045260246000fd5b6000828210156109e7576109e76109bf565b500390565b600082198211156109ff576109ff6109bf565b500190565b600061ffff821680610a1857610a186109bf565b6000190192915050565b600181811c90821680610a3657607f821691505b60208210811415610a5757634e487b7160e01b600052602260045260246000fd5b50919050565b60008251610a6f81846020870161092e565b9190910192915050565b606081526000610a8c606083018661095e565b84151560208401528281036040840152610aa6818561095e565b969550505050505056fea265627a7a7231582092fbf7a05fe947ca227e879053d3cd676116b315b5dc2c6169168c1a6fea99f664736f6c63430008090032