Skip to content

Commit

Permalink
Implementing standalone transaction to dcr translator#31, #37, #39
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-eshghie committed Oct 16, 2023
1 parent 84b4207 commit ae6d191
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ node server/monitor/monitorCLI.js

Use the following options with the Clawk Standalone CLI Monitor:

| Option | Description | Required |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| `--help` | Show help | No |
| `--version` | Show version number | No |
| `--address` | The address of the deployed contract | Yes |
| `--dcrID` | The DCR model identifier from DCRGraphs.net website | Yes |
| `--simID` | The identifier of the specific simulation you want to model this contract against. This identifier is retrievable by going to address [https://repository.dcrgraphs.net/api/graphs/${dcrID}/sims/](https://repository.dcrgraphs.net/api/graphs/${dcrID}/sims/) replacing ${dcrID} with the ID of the model you are trying to monitor against | Yes |
| `--ABIFileName` | The name of the contract ABI file (for example: PiggyBank.json). You should first put this file in the path `server/monitor/contracts/json-interface` | Yes |
| `--contract` | The contract parameter | Yes |
| `--paramap` | Supply parameter mapping for function calls to DCR graphs semantics for any transaction. The same file name as ABIFileName will be looked up in `server/monitor/contracts/paramaps/` directory. Format of the required json file: <br> `{` <br> ` "functionName": {` <br> ` "paramName": {` <br> ` EVMType: "...",` <br> ` DCRType: "...",` <br> ` DCRNodeID: "..."` <br> ` }` <br> ` }` <br> `}` | No |
| Option | Description | Required |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| `--help` | Show help | No |
| `--version` | Show version number | No |
| `--address` | The address of the deployed contract | Yes |
| `--dcrID` | The DCR model identifier from DCRGraphs.net website | Yes |
| `--simID` | The identifier of the specific simulation you want to model this contract against. This identifier is retrievable by going to address [https://repository.dcrgraphs.net/api/graphs/${dcrID}/sims/](https://repository.dcrgraphs.net/api/graphs/${dcrID}/sims/) replacing ${dcrID} with the ID of the model you are trying to monitor against | Yes |
| `--ABIFileName` | The name of the contract ABI file (for example: PiggyBank.json). You should first put this file in the path `server/monitor/contracts/json-interface` | Yes |
| `--contract` | The contract parameter | Yes |
| `--paramap` | Supply parameter mapping for function calls to DCR graphs semantics for any transaction. The same file name as ABIFileName will be looked up in `server/monitor/contracts/paramaps/` directory. Format of the required json file: <br> `{` <br> ` "functionName": {` <br> ` "paramName": {` <br> ` EVMType: "...",` <br> ` DCRType: "...",` <br> ` DCRNodeID: "..."` <br>` }`<br> `"roleSetter":true ` <br> `"roleVariableIdentifier":"owner"` <br> `}`<br> `}` |
| | No |

#### Example command usage:

Expand Down
31 changes: 16 additions & 15 deletions server/monitor/contractWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ let getContractTransactions = (address, contractABI, activities, paramaps) => {

// Get the function signature from the transaction data
const signature = tx.input.slice(0, 10);


// Find the function ABI that matches the signature
const method = JSON.parse(contractABI).find((m) => m.type === 'function' && `0x${web3.utils.keccak256(m.name + '(' + m.inputs.map((i) => i.type).join(',') + ')').slice(2, 10)}` === signature);
const decodedParams = web3.eth.abi.decodeParameters(method.inputs, tx.input.slice(10));
Expand Down Expand Up @@ -146,21 +148,21 @@ let getContractTransactions = (address, contractABI, activities, paramaps) => {

});
} catch {

console.log("TX conversion failed");
}
if (activities.includes(method.name)) {
let tx_ = {
'dcrID': method.name,
'contractABI': contractABI,
'dcrValue': null,
'dcrType': null
};
contract_queue.push(tx_);
}
console.log("I am here at 149")
if (activities.includes(method.name)) {
console.log("I am here 151.")
let tx_ = {
'dcrID': method.name,
'contractABI': contractABI,
'dcrValue': null,
'dcrType': null
};
contract_queue.push(tx_);
}
console.log(`method is: ${method.name}`);



console.log(`method is: ${method.name}`);



}
Expand All @@ -174,5 +176,4 @@ let getContractTransactions = (address, contractABI, activities, paramaps) => {



//module.exports = listen;
module.exports = getContractTransactions;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"EVMUnit": "hours",
"DCRType": "duration",
"DCRNodeID": "_hoursFromNow"
}
},
"roleSetter":true,
"roleVariableIdentifier": "owner"
}
}
82 changes: 82 additions & 0 deletions server/monitor/evm-to-dcr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
let WaitQueue = require('wait-queue');
let Web3 = require("web3");
let fs = require('fs');
let path = require('path');


let getDCRMethodsToExecute = () => {
//console.log(`Transaction object: ${JSON.stringify(tx)}`);

// Get the function signature from the transaction data
const signature = tx.input.slice(0, 10);

// Find the function ABI that matches the signature
const method = JSON.parse(contractABI).find((m) => m.type === 'function' && `0x${web3.utils.keccak256(m.name + '(' + m.inputs.map((i) => i.type).join(',') + ')').slice(2, 10)}` === signature);
const decodedParams = web3.eth.abi.decodeParameters(method.inputs, tx.input.slice(10));

console.log(`Method is: ${method.name}`)
console.log(`Activities are: ${activities}`);

const checkParametersAreInParamaps = () => {
const relevantKeys = Object.keys(decodedParams).filter(key => !key.match(/^(\d+|__length__)$/));
return relevantKeys.filter(key => Object.hasOwnProperty.call(paramaps, key));
};

// Assumption:
// If the event does not exist in the DCR model, just skip it
if (activities.includes(method.name) || checkParametersAreInParamaps()) {
try {
checkParametersAreInParamaps().forEach(parameter => {
// convert tx parameter to value suitable for DCR
if ((paramaps[parameter][method.name]["EVMType"] === "uint256") && (paramaps[parameter][method.name]["DCRType"] === "duration")) {
// Conversion between time parameters in SC to time in DCR
let unit = paramaps[parameter][method.name]["EVMUnit"];
let value = decodedParams[parameter];

const convertToISO8601 = (value, unit) => {
switch (unit) {
case "hours":
return 'PT' + value + 'H';
case "minutes":
return 'PT' + value + 'M';
case "seconds":
return 'PT' + value + 'S';
default:
return "Invalid unit";
}
};

// DCR event types for REST API are: int, duration, ...
let iso8601Duration = convertToISO8601(value, unit);
let tx_ = {
'dcrID': parameter,
'contractABI': contractABI,
'dcrValue': iso8601Duration,
'dcrType': paramaps[parameter][method.name]["DCRType"]
};
contract_queue.push(tx_);
}


});
} catch {
console.log("TX conversion failed");
}
if (activities.includes(method.name)) {
let tx_ = {
'dcrID': method.name,
'contractABI': contractABI,
'dcrValue': null,
'dcrType': null
};
contract_queue.push(tx_);
}


console.log(`method is: ${method.name}`);

}
}


module.exports = getDCRMethodsToExecute;

0 comments on commit ae6d191

Please sign in to comment.