I have simple get function that returns an address. On the front end in JS, I want to convert this address into some sort of readable function, namely a string.
After migrating my contract, I use web3 to use the function to return an address. However, I'm having troubles reading it. I'm hoping to avoid converting it into a string in the .sol file as to avoid unnecessary gas usage.
This is the function in the smart contract
function getBookAccounts() public returns(address){
return bookAccount;
}
Here is the JS file trying to console log the address
async showAccounts() {
const contract = require('truffle-contract')
const simpleStorage = contract(SimpleStorageContract)
simpleStorage.setProvider(this.state.web3.currentProvider)
var currAccount = await this.simpleStorageInstance.getBookAccounts();
console.log('The address is ', currAccount)
}
Unfortunately, I can't print this address. I'm guessing I need to convert it into a string rather than a UTF8 as is used in solidity.
Make sure your Solidity function is marked as a view
. Otherwise the default behavior of web3.js is to send a transaction, and you're probably getting back a transaction hash. (Transactions don't have return values.)
function getBookAccounts() public view returns (address) {
If you change the function to a view, web3.js should make a call instead of sending a transaction. This is faster, requires no gas, and can return a value.