0

Using Ethers JS library in javascript code. When calling a solididy function that returns an Uint type, its returning an object with the uint in hex format.

SOLIDITY function

function totalRecords() public view returns (uint256){
    return Records.length;
}

ETHERS JS call

CONTRACT.totalRecords().then( _num => { console.log("TOTAL_RECORDS " + _num); });

REPONSE

Object { _hex: "0x0a", _isBigNumber: true }
​
_hex: "0x0a"
​
_isBigNumber: true

2 Answers 2

1

I get the uint of the returned object as hex with ethers.utils.arrayify() function

CONTRACT.totalRecords().then( _num => { 
   console.log("TOTAL_RECORDS ");  
   console.log( ethers.utils.arrayify( _num._hex )[0] ); 
});
0

The _hex is an internal value, and toString() should be available for you.

-CONTRACT.totalRecords().then( _num => { console.log("TOTAL_RECORDS " + _num); });
+CONTRACT.totalRecords().then( _num => { console.log("TOTAL_RECORDS " + _num.toString()); });

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.