|
| 1 | +// Jest Snapshot v1, https://goo.gl/fbAQLP |
| 2 | + |
| 3 | +exports[`SplittableCommodity.sol 1`] = ` |
| 4 | +pragma solidity ^0.4.24; |
| 5 | +
|
| 6 | +import "./MintableCommodity.sol"; |
| 7 | +import "../participant/ISupplier.sol"; |
| 8 | +import "../../node_modules/zeppelin-solidity/contracts//math/SafeMath.sol"; |
| 9 | +
|
| 10 | +contract SplittableCommodity is MintableCommodity { |
| 11 | + using SafeMath for uint256; |
| 12 | +
|
| 13 | + event Split(address indexed to, uint256 amount, uint64 parentId, address indexed operator, bytes operatorData); |
| 14 | +
|
| 15 | + function split(uint _tokenId, address _to, uint256 _amount) public whenNotPaused { |
| 16 | + address supplierProxy = IContractRegistry(contractRegistry).getLatestProxyAddr("Supplier"); |
| 17 | + require( |
| 18 | + msg.sender == IContractRegistry(contractRegistry).getLatestProxyAddr("FifoCrcMarket") || |
| 19 | + ISupplier(supplierProxy).isAllowed(this, "IMintableCommodity"), |
| 20 | + "Splitting can only be done when both the ISplittableCommodity interface is enabled and is called by a supplier or the FifoCrcMarket is used." |
| 21 | + ); |
| 22 | +
|
| 23 | + commodities[_tokenId].value = commodities[_tokenId].value.sub(_amount); |
| 24 | +
|
| 25 | + CommodityLib.Commodity memory _commodity = CommodityLib.Commodity({ |
| 26 | + category: uint64(1), |
| 27 | + timeRegistered: uint64(now), // solium-disable-line |
| 28 | + parentId: _tokenId, |
| 29 | + value: _amount, |
| 30 | + locked: false, |
| 31 | + misc: commodities[_tokenId].misc |
| 32 | + }); |
| 33 | + uint newCRCId = commodities.push(_commodity).sub(1); |
| 34 | + require(newCRCId <= 18446744073709551616, "You can only split a commodity if it is within a valid index range"); |
| 35 | +
|
| 36 | + if(msg.sender == IContractRegistry(contractRegistry).getLatestProxyAddr("FifoCrcMarket")) { |
| 37 | + _transfer(ownerOf(_tokenId), _to, newCRCId); |
| 38 | + } else { |
| 39 | + _transfer(msg.sender, _to, newCRCId); |
| 40 | + } |
| 41 | +
|
| 42 | + callRecipient( |
| 43 | + msg.sender, |
| 44 | + 0x0, |
| 45 | + _to, |
| 46 | + newCRCId, |
| 47 | + "", |
| 48 | + "", |
| 49 | + false |
| 50 | + ); |
| 51 | +
|
| 52 | + emit Split( |
| 53 | + _to, |
| 54 | + _amount, |
| 55 | + uint64(newCRCId), |
| 56 | + msg.sender, |
| 57 | + "" |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 62 | +pragma solidity ^0.4.24; |
| 63 | +
|
| 64 | +import "./MintableCommodity.sol"; |
| 65 | +import "../participant/ISupplier.sol"; |
| 66 | +import "../../node_modules/zeppelin-solidity/contracts//math/SafeMath.sol"; |
| 67 | +
|
| 68 | +contract SplittableCommodity is MintableCommodity { |
| 69 | + using SafeMath for uint256; |
| 70 | +
|
| 71 | + event Split( |
| 72 | + address indexed to, |
| 73 | + uint256 amount, |
| 74 | + uint64 parentId, |
| 75 | + address indexed operator, |
| 76 | + bytes operatorData |
| 77 | + ); |
| 78 | +
|
| 79 | + function split( |
| 80 | + uint _tokenId, |
| 81 | + address _to, |
| 82 | + uint256 _amount |
| 83 | + ) public whenNotPaused { |
| 84 | + address supplierProxy = IContractRegistry( |
| 85 | + contractRegistry |
| 86 | + ).getLatestProxyAddr("Supplier"); |
| 87 | + require( |
| 88 | + msg.sender == IContractRegistry(contractRegistry).getLatestProxyAddr( |
| 89 | + "FifoCrcMarket" |
| 90 | + ) || ISupplier(supplierProxy).isAllowed(this, "IMintableCommodity"), |
| 91 | + "Splitting can only be done when both the ISplittableCommodity interface is enabled and is called by a supplier or the FifoCrcMarket is used." |
| 92 | + ); |
| 93 | +
|
| 94 | + commodities[_tokenId].value = commodities[_tokenId].value.sub(_amount); |
| 95 | +
|
| 96 | + CommodityLib.Commodity memory _commodity = CommodityLib.Commodity({ |
| 97 | + category: uint64(1), |
| 98 | + timeRegistered: uint64(now), // solium-disable-line |
| 99 | + parentId: _tokenId, |
| 100 | + value: _amount, |
| 101 | + locked: false, |
| 102 | + misc: commodities[_tokenId].misc |
| 103 | + }); |
| 104 | + uint newCRCId = commodities.push(_commodity).sub(1); |
| 105 | + require( |
| 106 | + newCRCId <= 18446744073709551616, |
| 107 | + "You can only split a commodity if it is within a valid index range" |
| 108 | + ); |
| 109 | +
|
| 110 | + if (msg.sender == IContractRegistry(contractRegistry).getLatestProxyAddr( |
| 111 | + "FifoCrcMarket" |
| 112 | + )) { |
| 113 | + _transfer(ownerOf(_tokenId), _to, newCRCId); |
| 114 | + } else { |
| 115 | + _transfer(msg.sender, _to, newCRCId); |
| 116 | + } |
| 117 | +
|
| 118 | + callRecipient(msg.sender, 0x0, _to, newCRCId, "", "", false); |
| 119 | +
|
| 120 | + emit Split(_to, _amount, uint64(newCRCId), msg.sender, ""); |
| 121 | + } |
| 122 | +} |
| 123 | +
|
| 124 | +`; |
0 commit comments