|
| 1 | +pragma solidity ^0.6.4; |
| 2 | +pragma experimental "ABIEncoderV2"; |
| 3 | + |
| 4 | +import "@connext/contracts/src.sol/shared/libs/LibChannelCrypto.sol"; |
| 5 | +import "@connext/contracts/src.sol/shared/libs/LibCommitment.sol"; |
| 6 | +import "@connext/contracts/src.sol/adjudicator/ChallengeRegistry.sol"; |
| 7 | +import "@connext/contracts/src.sol/funding/libs/LibOutcome.sol"; |
| 8 | + |
| 9 | +import "./MultisigData.sol"; |
| 10 | +import "./MinimumViableMultisig.sol"; |
| 11 | + |
| 12 | + |
| 13 | +contract IndexerCtdt is MultisigData { |
| 14 | + uint256 constant MAX_UINT256 = 2**256 - 1; |
| 15 | + |
| 16 | + struct FreeBalanceAppState { |
| 17 | + address[] tokenAddresses; |
| 18 | + // The inner array contains the list of CoinTransfers for a single asset type |
| 19 | + // The outer array contains the list of asset balances for respecitve assets |
| 20 | + // according to the indexing used in the `tokens` array above |
| 21 | + LibOutcome.CoinTransfer[][] balances; |
| 22 | + bytes32[] activeApps; |
| 23 | + } |
| 24 | + |
| 25 | + struct MultiAssetMultiPartyCoinTransferInterpreterParams { |
| 26 | + uint256[] limit; |
| 27 | + address[] tokenAddresses; |
| 28 | + } |
| 29 | + |
| 30 | + function executeWithdraw( |
| 31 | + address, /* interpreterAddress */ |
| 32 | + bytes32, /* nonce */ |
| 33 | + bytes memory encodedOutput, |
| 34 | + bytes memory encodedParams |
| 35 | + ) public { |
| 36 | + address withdrawInterpreter = MinimumViableMultisig(masterCopy) |
| 37 | + .INDEXER_WITHDRAW_INTERPRETER_ADDRESS(); |
| 38 | + (bool success, ) = withdrawInterpreter.delegatecall( |
| 39 | + abi.encodeWithSignature( |
| 40 | + "interpretOutcomeAndExecuteEffect(bytes,bytes)", |
| 41 | + encodedOutput, |
| 42 | + encodedParams |
| 43 | + ) |
| 44 | + ); |
| 45 | + |
| 46 | + require(success, "Execution of executeWithdraw failed"); |
| 47 | + } |
| 48 | + |
| 49 | + function executeEffectOfFreeBalance( |
| 50 | + ChallengeRegistry challengeRegistry, |
| 51 | + bytes32 freeBalanceAppIdentityHash, |
| 52 | + address /* multiAssetMultiPartyCoinTransferInterpreterAddress */ |
| 53 | + ) public { |
| 54 | + FreeBalanceAppState memory freeBalanceAppState = abi.decode( |
| 55 | + challengeRegistry.getOutcome(freeBalanceAppIdentityHash), |
| 56 | + (FreeBalanceAppState) |
| 57 | + ); |
| 58 | + |
| 59 | + uint256[] memory limits = new uint256[](freeBalanceAppState.tokenAddresses.length); |
| 60 | + |
| 61 | + address multiAssetInterpreter = MinimumViableMultisig(masterCopy) |
| 62 | + .INDEXER_MULTI_ASSET_INTERPRETER_ADDRESS(); |
| 63 | + |
| 64 | + for (uint256 i = 0; i < freeBalanceAppState.tokenAddresses.length; i++) { |
| 65 | + // The transaction's interpreter parameters are determined at the time |
| 66 | + // of creation of the free balance; hence we cannot know how much will be |
| 67 | + // deposited into it all-time. Relying on the app state is unsafe so |
| 68 | + // we just give it full permissions by setting the limit to the max here. |
| 69 | + limits[i] = MAX_UINT256; |
| 70 | + } |
| 71 | + |
| 72 | + (bool success, ) = multiAssetInterpreter.delegatecall( |
| 73 | + abi.encodeWithSignature( |
| 74 | + "interpretOutcomeAndExecuteEffect(bytes,bytes)", |
| 75 | + abi.encode(freeBalanceAppState.balances), |
| 76 | + abi.encode( |
| 77 | + MultiAssetMultiPartyCoinTransferInterpreterParams( |
| 78 | + limits, |
| 79 | + freeBalanceAppState.tokenAddresses |
| 80 | + ) |
| 81 | + ) |
| 82 | + ) |
| 83 | + ); |
| 84 | + |
| 85 | + require(success, "Execution of executeEffectOfFreeBalance failed"); |
| 86 | + } |
| 87 | + |
| 88 | + /// @notice Execute a fund transfer for a state channel app in a finalized state |
| 89 | + /// @param appIdentityHash AppIdentityHash to be resolved |
| 90 | + function executeEffectOfInterpretedAppOutcome( |
| 91 | + ChallengeRegistry challengeRegistry, |
| 92 | + bytes32 freeBalanceAppIdentityHash, |
| 93 | + bytes32 appIdentityHash, |
| 94 | + address, /* interpreterAddress */ |
| 95 | + bytes memory interpreterParams |
| 96 | + ) public { |
| 97 | + bytes32[] memory activeApps = abi |
| 98 | + .decode(challengeRegistry.getOutcome(freeBalanceAppIdentityHash), (FreeBalanceAppState)) |
| 99 | + .activeApps; |
| 100 | + |
| 101 | + bool appIsFunded = false; |
| 102 | + |
| 103 | + address singleAssetInterpreter = MinimumViableMultisig(masterCopy) |
| 104 | + .INDEXER_SINGLE_ASSET_INTERPRETER_ADDRESS(); |
| 105 | + |
| 106 | + for (uint256 i = 0; i < activeApps.length; i++) { |
| 107 | + if (activeApps[i] == appIdentityHash) { |
| 108 | + appIsFunded = true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + require(appIsFunded, "Referenced AppInstance is not funded"); |
| 113 | + |
| 114 | + bytes memory outcome = challengeRegistry.getOutcome(appIdentityHash); |
| 115 | + |
| 116 | + (bool success, ) = singleAssetInterpreter.delegatecall( |
| 117 | + abi.encodeWithSignature( |
| 118 | + "interpretOutcomeAndExecuteEffect(bytes,bytes)", |
| 119 | + outcome, |
| 120 | + interpreterParams |
| 121 | + ) |
| 122 | + ); |
| 123 | + |
| 124 | + require(success, "Execution of executeEffectOfInterpretedAppOutcome failed"); |
| 125 | + } |
| 126 | +} |
0 commit comments