Last active
September 5, 2018 11:33
-
-
Save hcaw/36d093d703a713c01e5bd0e9dd7c66e9 to your computer and use it in GitHub Desktop.
Passing memory references to structs to an internal function and then changing the value, and checking whether the value persists after exiting the function.
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
pragma solidity 0.4.24; | |
pragma experimental ABIEncoderV2; | |
contract AnotherContract { | |
event TradeFound(uint tradeID, bool ignoreTrade); | |
event OrderFound(uint tradeID, uint orderID, bool ignoreOrder, uint amountSpent, uint amountReceived); | |
event TradesLength(uint numberOfTrades); | |
event OrdersLength(uint tradeID, uint numberOfOrders); | |
event SettingTradeID(uint tradeId); | |
struct TradeFlag { | |
bool ignoreTrade; | |
uint256 tradeId; | |
bool[] ignoreOrder; | |
OrderFlag[] orderFlags; | |
} | |
struct OrderFlag { | |
uint orderId; | |
uint amountSpent; | |
uint amountReceived; | |
} | |
function performRebalance( | |
) public payable { | |
TradeFlag[] memory tradeFlags = initialiseTradeFlags(); | |
staticChecks(tradeFlags); | |
performTrades(tradeFlags); | |
checksFlags(tradeFlags); | |
} | |
function checksFlags(TradeFlag[] tradeFlags) public { | |
emit TradesLength(tradeFlags.length); | |
for (uint256 i = 0; i < tradeFlags.length; i++) { | |
emit OrdersLength(i, tradeFlags[i].ignoreOrder.length); | |
emit TradeFound(tradeFlags[i].tradeId, tradeFlags[i].ignoreTrade); | |
for (uint256 j = 0; j < tradeFlags[i].orderFlags.length; j++) { | |
OrderFlag memory thisOrderFlag = tradeFlags[i].orderFlags[j]; | |
emit OrderFound(tradeFlags[i].tradeId, thisOrderFlag.orderId, tradeFlags[i].ignoreOrder[j], thisOrderFlag.amountSpent, thisOrderFlag.amountReceived); | |
} | |
} | |
} | |
function performTrades(TradeFlag[] tradeFlags) internal { | |
for (uint256 i = 0; i < tradeFlags.length; i++) { | |
TradeFlag memory thisTradeFlag = tradeFlags[i]; | |
preTradeDynamicChecks(thisTradeFlag, i); | |
for (uint j = 0; j < thisTradeFlag.orderFlags.length; j++) { | |
OrderFlag memory thisOrderFlag = thisTradeFlag.orderFlags[j]; | |
recordAmounts(thisOrderFlag, j); | |
} | |
} | |
} | |
function recordAmounts(OrderFlag orderFlag, uint orderAmount) internal { | |
orderFlag.amountSpent = orderAmount * 2; | |
orderFlag.amountReceived = orderAmount * 4; | |
} | |
function preTradeDynamicChecks(TradeFlag tradeFlag, uint id) internal { | |
tradeFlag.tradeId = id; | |
for (uint256 i = 0; i < tradeFlag.orderFlags.length; i++) { | |
tradeFlag.orderFlags[i].orderId = i; | |
} | |
emit SettingTradeID(tradeFlag.tradeId); | |
} | |
function staticChecks(TradeFlag[] tradeFlags) public { | |
for (uint256 i = 0; i < tradeFlags.length; i++) { | |
TradeFlag memory thisTradeFlag = tradeFlags[i]; | |
if (i % 2 == 0 ) { | |
thisTradeFlag.ignoreTrade = true; | |
} | |
for (uint256 j = 0; j < thisTradeFlag.ignoreOrder.length; j++) { | |
thisTradeFlag.ignoreOrder[j] = true; | |
} | |
} | |
} | |
function initialiseTradeFlags() internal pure returns (TradeFlag[]) { | |
uint len = 5; | |
TradeFlag[] memory tradeFlags = new TradeFlag[](len); | |
for (uint256 i = 0; i < len; i++) { | |
tradeFlags[i].ignoreOrder = new bool[](len); | |
tradeFlags[i].orderFlags = new OrderFlag[](len); | |
} | |
return tradeFlags; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment