0

I have written a program for Truffle console. My code accesses the string and should return statements related to Solidity send and transfer. When I am testing each line of Solidity SC as shown below, I am getting correct result but when I am testing using the whole code stored in the "string" variable, the false condition is not working or there is some other problem due to which I am getting all the lines of Solidity code instead of just send and transfer statements. For example:

Str[ctr] = "bool sent = _to.send(msg.value);".includes(keyWords[keyWordCtr])//true 

My complete code is :

var assert = require('assert');
const path = require("path");

module.exports = async function(callback) 
{
   try { 

string = `contract SendEther {
    function sendViaTransfer(address payable _to) public payable {
        _to.transfer(msg.value);}

    function sendViaSend(address payable _to) public payable {
        bool sent = _to.send(msg.value);
        require(sent, "Failed to send Ether");}

    function sendViaCall(address payable _to) public payable {
        (bool sent, bytes memory data) = _to.call{value: msg.value}("");
        require(sent, "Failed to send Ether");}}`
const Str=[];
let ctr=0;
lines = string.split(/\r\n|\n/);
const keyWords = new Array( ".send(", "transfer(");
for (let keyWordCtr = 0; keyWordCtr <1; ++keyWordCtr){
   for(let line = 0; line < lines.length-1; line++){
      if (lines[line].includes(keyWords[keyWordCtr]) == false)//Note replacing search by includes not working
         continue;
      
      Str[ctr] = (lines[line].includes(keyWords[keyWordCtr])? lines.toString() : false);
      ctr++;
   }
}
for(let i = 0; i< Str.length; i++)
    console.log(Str[i])
}//try
       catch (error) {
          console.log(error)
       }
   callback();
}

When I am executing the program, I am getting the whole code, otherwise, I found that only two following statements are returning true values:

_to.transfer(msg.value);
bool sent = _to.send(msg.value);

Somebody please guide me how to fix my code so that it returns only the above two statements of Solidity program. Right now it is returning the entire Solidity code.

Zulfi.

1 Answer 1

1

I have solved this problem. I had a problem with the string:

Str[ctr] = (lines[line].includes(keyWords[keyWordCtr])? lines.toString() : false);

When I changed it to:

Str[ctr] = (lines[line].includes(keyWords[keyWordCtr])? lines[line].toString() : false);

My program is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.