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 f41b9b1 commit a5682ad
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1,096 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 @@ -290,12 +290,12 @@ For more details, please refer to [DARC Instruction Set Opcode Table(opcode.md)]
| 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_UINT256_1_LESS_THAN_2 | string oracle, string method, uint256[] args || NOT READY, DO NOT USE
| 186 | ORACLE_CALL_UINT256_1_GREATER_THAN_2 | string oracle, string method, uint256[] args || NOT READY, DO NOT USE
| 187 | ORACLE_CALL_UINT256_1_EQUALS_2 | string oracle, string method, uint256[] args || NOT READY, DO NOT USE
| 188 | ORACLE_CALL_STRING_RESULT_EQUALS | string oracle, string method, string[] args, string expectedValue || NOT READY, DO NOT USE
| 189 | ORACLE_CALL_STRING_1_EQUALS_2 | string oracle, string method, string[] args || NOT READY, DO NOT USE
| 190 | ORACLE_CALL_STRING_RESULT_IN_LIST | string oracle, string method, string[] args, string[] expectedValues || 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
| 187 | Placeholder187 | | |
| 188 | Placeholder188 | | |
| 189 | Placeholder189 | | |
| 190 | Placeholder190 | | |
| 191 | Placeholder191 | | |
| 192 | Placeholder192 | | |
| 193 | Placeholder193 | | |
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 | Placeholder465 | | |
| 466 | Placeholder466 | | |
| 467 | Placeholder467 | | |
| 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".
| 468 | Placeholder468 | | |
| 469 | Placeholder469 | | |
| 470 | Placeholder470 | | |
Expand Down
59 changes: 59 additions & 0 deletions darc-protocol/contracts/protocol/Plugin/Condition_TokenAndCash.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "../MachineStateManager.sol";
import "../Utilities/StringUtils.sol";
import "../Utilities/OpcodeMap.sol";
import "../Plugin.sol";
import "../Utilities/ExternalValueReader.sol";

contract Condition_TokenAndCash is MachineStateManager {
/**
Expand Down Expand Up @@ -76,6 +77,64 @@ 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) {
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;


(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);

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; }
}
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) {
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);

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; }
}
return false;
}

function ID_467_TOKEN_X_OP_ANY_PRICE_EQUALS_ORACLE_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);

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; }
}
return false;
}



// -------------------------------- below are helper functions ----------------------------
Expand Down
61 changes: 61 additions & 0 deletions darc-protocol/contracts/protocol/Utilities/ExternalValueReader.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;

library ExternalValueReader {

function tryReadUINT256(address _contract, bytes memory _data) internal view returns (bool, uint256) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, 0);
}
return (true, abi.decode(returnData, (uint256)));
}

function tryReadAddress(address _contract, bytes memory _data) internal view returns (bool, address) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, address(0));
}
return (true, abi.decode(returnData, (address)));
}

function tryReadBytes32(address _contract, bytes memory _data) internal view returns (bool, bytes32) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, bytes32(0));
}
return (true, abi.decode(returnData, (bytes32)));
}

function tryReadBytes(address _contract, bytes memory _data) internal view returns (bool, bytes memory) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, new bytes(0));
}
return (true, returnData);
}

function tryReadString(address _contract, bytes memory _data) internal view returns (bool, string memory) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, "");
}
return (true, abi.decode(returnData, (string)));
}

function tryReadBool(address _contract, bytes memory _data) internal view returns (bool, bool) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, false);
}
return (true, abi.decode(returnData, (bool)));
}

function tryReadInt(address _contract, bytes memory _data) internal view returns (bool, int) {
(bool success, bytes memory returnData) = _contract.staticcall(_data);
if (!success) {
return (false, 0);
}
return (true, abi.decode(returnData, (int)));
}
}
Loading

0 comments on commit a5682ad

Please sign in to comment.