Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed Feb 19, 2024
1 parent a5682ad commit acd063e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 23 deletions.
18 changes: 9 additions & 9 deletions darc-docs/docs/DARC Protocol/Condition Nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ For more details, please refer to [DARC Instruction Set Opcode Table(opcode.md)]
| 178 | Placeholder178 | | |
| 179 | Placeholder179 | | |
| 180 | Placeholder180 | | |
| 181 | ORACLE_CALL_UINT256_RESULT_EQUALS | string oracle, string method, uint256[] args, uint256 expectedValue || NOT READY, DO NOT USE
| 182 | ORACLE_CALL_UINT256_RESULT_GREATER_THAN | string oracle, string method, uint256[] args, uint256 minValue || NOT READY, DO NOT USE
| 183 | ORACLE_CALL_UINT256_RESULT_LESS_THAN | string oracle, string method, uint256[] args, uint256 maxValue || NOT READY, DO NOT USE
| 184 | ORACLE_CALL_UINT256_RESULT_IN_RANGE | string oracle, string method, uint256[] args, uint256 minValue, uint256 maxValue || NOT READY, DO NOT USE
| 185 | ORACLE_CALL_STRING_RESULT_EQUALS | string oracle, string method, string[] args, string expectedValue || NOT READY, DO NOT USE
| 186 | ORACLE_CALL_STRING_RESULT_IN_LIST | string oracle, string method, string[] args, string[] expectedValues || NOT READY, DO NOT USE
| 181 | EXTERNAL_CALL_UINT256_RESULT_EQUALS | ADDRESS_2DARRAY[0][0] externalAddress, BYTES encodedParameters, UINT256_2DARRAY[0][0] value || NOT READY, DO NOT USE
| 182 | EXTERNAL_CALL_UINT256_RESULT_GREATER_THAN | sADDRESS_2DARRAY[0][0] externalAddress, BYTES encodedParameters, UINT256_2DARRAY[0][0] value || NOT READY, DO NOT USE
| 183 | EXTERNAL_CALL_UINT256_RESULT_LESS_THAN | ADDRESS_2DARRAY[0][0] externalAddress, BYTES encodedParameters, UINT256_2DARRAY[0][0] value || NOT READY, DO NOT USE
| 184 | EXTERNAL_CALL_UINT256_RESULT_IN_RANGE | ADDRESS_2DARRAY[0][0] externalAddress, BYTES encodedParameters, UINT256_2DARRAY[0][0] minValue, UINT256_2DARRAY[0][1] maxValue || NOT READY, DO NOT USE
| 185 | EXTERNAL_CALL_STRING_RESULT_EQUALS | string external, string method, string[] args, string expectedValue || NOT READY, DO NOT USE
| 186 | Placeholder186 | | |
| 187 | Placeholder187 | | |
| 188 | Placeholder188 | | |
| 189 | Placeholder189 | | |
Expand Down Expand Up @@ -578,9 +578,9 @@ For more details, please refer to [DARC Instruction Set Opcode Table(opcode.md)]
| 462 | TOKEN_X_OP_ANY_PRICE_LESS_THAN | UINT256_2DARRAY[0][0] tokenClass, UINT256_2DARRAY[0][1] price | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 463 | TOKEN_X_OP_ANY_PRICE_IN_RANGE | UINT256_2DARRAY[0][0] tokenClass, UINT256_2DARRAY[0][1] startPrice, UINT256_2DARRAY[0][2] endPrice | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 464 | TOKEN_X_OP_ANY_PRICE_EQUALS | UINT256_2DARRAY[0][0] tokenClass, UINT256_2DARRAY[0][1] price | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 465 | TOKEN_X_OP_ANY_PRICE_GREATER_THAN_ORACLE_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] oracleContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 466 | TOKEN_X_OP_ANY_PRICE_LESS_THAN_ORACLE_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] oracleContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 467 | TOKEN_X_OP_ANY_PRICE_EQUALS_ORACLE_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] oracleContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 465 | TOKEN_X_OP_ANY_PRICE_GREATER_THAN_EXTERNAL_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] externalContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 466 | TOKEN_X_OP_ANY_PRICE_LESS_THAN_EXTERNAL_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] externalContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 467 | TOKEN_X_OP_ANY_PRICE_EQUALS_EXTERNAL_VALUE_UINT256 | UINT256_2DARRAY[0][0] tokenClass, ADDRESS_2DARRAY[0][0] externalContractAddress, BYTES abiEncodedParameters | For three operations: "BATCH_PAY_TO_MINT_TOKENS", "BATCH_PAY_TO_TRANSFER_TOKENS" and "BATCH_BURN_TOKENS_AND_REFUND".
| 468 | Placeholder468 | | |
| 469 | Placeholder469 | | |
| 470 | Placeholder470 | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "./Condition_TokenAndCash.sol";
import "./Condition_CreateTokenClass.sol";
import "./Condition_Program.sol";
import "./Condition_OperationLog.sol";
import "./Condition_External.sol";


/**
Expand All @@ -26,7 +27,8 @@ import "./Condition_OperationLog.sol";
contract ConditionExpressionFactory is
Condition_Operator, Condition_MachineState, Condition_Operation, Condition_BatchOp, Condition_PluginAndVoting,
Condition_MembershipOp, Condition_Withdrawable, Condition_TokenAndCash,
Condition_CreateTokenClass, Condition_Program, Condition_OperationLog
Condition_CreateTokenClass, Condition_Program, Condition_OperationLog,
Condition_External
{

/**
Expand Down Expand Up @@ -55,6 +57,8 @@ contract ConditionExpressionFactory is

if (exp >=151 && exp <= 180) { return operationExpressionCheck( operation, param, exp); }

if (exp >= 181 && exp <= 210) { return externalExpressionCheck(param, exp); }

if (exp >= 211 && exp <= 300) { return batchOpExpressionCheck(bIsBeforeOperation, operation, param, exp); }

if (exp >= 301 && exp < 400 ) { return pluginAndVotingOpExpressionCheck(bIsBeforeOperation, operation, param, exp); }
Expand Down
85 changes: 85 additions & 0 deletions darc-protocol/contracts/protocol/Plugin/Condition_External.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;
/**
* @title Condition of Oracle values
* @author DARC Team
* @notice All the condition expression functions related to Operator
*/


import "../MachineState.sol";
import "../MachineStateManager.sol";
import "../Utilities/StringUtils.sol";
import "../Utilities/OpcodeMap.sol";
import "../Plugin.sol";
import "../Utilities/ExternalValueReader.sol";

contract Condition_External is MachineStateManager {
/**
* The function to check the external(oracle) related condition expression
* @param param The parameter list of the condition expression
* @param id The id of the condition expression
*/
function externalExpressionCheck(NodeParam memory param, uint256 id) internal view returns (bool) {
if (id== 181) return ID_181_EXTERNAL_CALL_UINT256_RESULT_EQUALS(param);
if (id== 182) return ID_182_EXTERNAL_CALL_UINT256_RESULT_GREATER_THAN(param);
if (id== 183) return ID_183_EXTERNAL_CALL_UINT256_RESULT_LESS_THAN(param);
if (id== 184) return ID_184_EXTERNAL_CALL_UINT256_RESULT_IN_RANGE(param);
if (id== 185) return ID_185_EXTERNAL_CALL_STRING_RESULT_EQUALS(param);
return false;
}

function ID_181_EXTERNAL_CALL_UINT256_RESULT_EQUALS(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_181: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_181: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_181: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_181: The BYTES length is not 0");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value == param.UINT256_2DARRAY[0][0]) { return true; }
return false;
}

function ID_182_EXTERNAL_CALL_UINT256_RESULT_GREATER_THAN(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_182: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_182: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_182: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_182: The BYTES length is not 0");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value > param.UINT256_2DARRAY[0][0]) { return true; }
return false;
}

function ID_183_EXTERNAL_CALL_UINT256_RESULT_LESS_THAN(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_183: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_183: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_183: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_183: The BYTES length is not 0");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value < param.UINT256_2DARRAY[0][0]) { return true; }
return false;
}

function ID_184_EXTERNAL_CALL_UINT256_RESULT_IN_RANGE(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_184: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_184: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 1, "CE ID_184: The BYTES length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_184: The BYTES[0] length is not 2");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value >= param.UINT256_2DARRAY[0][0] && value <= param.UINT256_2DARRAY[0][1]) { return true; }
return false;
}

function ID_185_EXTERNAL_CALL_STRING_RESULT_EQUALS(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_185: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_185: The ADDRESS_2DARRAY[0] length is not 1");
require(param.STRING_ARRAY.length == 1, "CE ID_185: The STRING_2DARRAY length is not 1");
(bool success, string memory value) = ExternalValueReader.tryReadString(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (keccak256(abi.encodePacked(value)) == keccak256(abi.encodePacked(param.STRING_ARRAY[0]))) { return true; }
return false;
}
}
30 changes: 17 additions & 13 deletions darc-protocol/contracts/protocol/Plugin/Condition_TokenAndCash.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ contract Condition_TokenAndCash is MachineStateManager {
* @param param The parameter list of the condition expression
* @param id The id of the condition expression
*/
function tokenAndCashExpressionCheck(Operation memory op, NodeParam memory param, uint256 id) internal pure returns (bool) {
function tokenAndCashExpressionCheck(Operation memory op, NodeParam memory param, uint256 id) internal view returns (bool) {
if (id== 461) return ID_461_TOKEN_X_OP_ANY_PRICE_GREATER_THAN(op, param);
if (id== 462) return ID_462_TOKEN_X_OP_ANY_PRICE_LESS_THAN(op, param);
if (id== 463) return ID_463_TOKEN_X_OP_ANY_PRICE_IN_RANGE(op, param);
if (id== 464) return ID_464_TOKEN_X_OP_ANY_PRICE_EQUALS(op, param);
if (id== 465) return ID_465_TOKEN_X_OP_ANY_PRICE_GREATER_THAN_EXTERNAL_VALUE_UINT256(op, param);
if (id== 466) return ID_466_TOKEN_X_OP_ANY_PRICE_LESS_THAN_EXTERNAL_VALUE_UINT256(op, param);
if (id== 467) return ID_467_TOKEN_X_OP_ANY_PRICE_EQUALS_EXTERNAL_VALUE_UINT256(op, param);

return false;
}

Expand Down Expand Up @@ -77,7 +81,7 @@ contract Condition_TokenAndCash is MachineStateManager {
return false;
}

function ID_465_TOKEN_X_OP_ANY_PRICE_GREATER_THAN_ORACLE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_465_TOKEN_X_OP_ANY_PRICE_GREATER_THAN_EXTERNAL_VALUE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_465: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_465: The UINT256_2DARRAY[0] length is not 1");
if (bIsTokenOperationWithCash(op) == false) return false;
Expand All @@ -86,51 +90,51 @@ contract Condition_TokenAndCash is MachineStateManager {
(uint256[] memory tokenClassList, , uint256[] memory priceList) = getTokenClassAmountPriceList(op);
require(tokenClassList.length == priceList.length, "CE ID_465: The token class list length is not equal to price list length");

// try to get oracle value
(bool success, uint256 oracleValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);
// try to get external value
(bool success, uint256 externalValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);

if (success == false) return false;

for (uint256 i = 0; i < tokenClassList.length; i++) {
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] > oracleValue) { return true; }
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] > externalValue) { return true; }
}
return false;
}

function ID_466_TOKEN_X_OP_ANY_PRICE_LESS_THAN_ORACLE_VALUE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_466_TOKEN_X_OP_ANY_PRICE_LESS_THAN_EXTERNAL_VALUE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_466: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_466: The UINT256_2DARRAY[0] length is not 1");
if (bIsTokenOperationWithCash(op) == false) return false;

(uint256[] memory tokenClassList, , uint256[] memory priceList) = getTokenClassAmountPriceList(op);
require(tokenClassList.length == priceList.length, "CE ID_466: The token class list length is not equal to price list length");

// try to get oracle value
(bool success, uint256 oracleValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);
// try to get external value
(bool success, uint256 externalValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);

if (success == false) return false;

for (uint256 i = 0; i < tokenClassList.length; i++) {
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] < oracleValue) { return true; }
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] < externalValue) { return true; }
}
return false;
}

function ID_467_TOKEN_X_OP_ANY_PRICE_EQUALS_ORACLE_VALUE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_467_TOKEN_X_OP_ANY_PRICE_EQUALS_EXTERNAL_VALUE_UINT256(Operation memory op, NodeParam memory param) private view returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_467: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_467: The UINT256_2DARRAY[0] length is not 1");
if (bIsTokenOperationWithCash(op) == false) return false;

(uint256[] memory tokenClassList, , uint256[] memory priceList) = getTokenClassAmountPriceList(op);
require(tokenClassList.length == priceList.length, "CE ID_467: The token class list length is not equal to price list length");

// try to get oracle value
(bool success, uint256 oracleValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);
// try to get external value
(bool success, uint256 externalValue) = ExternalValueReader.tryReadUINT256(op.param.ADDRESS_2DARRAY[0][0], op.param.BYTES);

if (success == false) return false;

for (uint256 i = 0; i < tokenClassList.length; i++) {
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] == oracleValue) { return true; }
if (tokenClassList[i] == param.UINT256_2DARRAY[0][0] && priceList[i] == externalValue) { return true; }
}
return false;
}
Expand Down

0 comments on commit acd063e

Please sign in to comment.