簡體   English   中英

如何將智能合約返回的地址轉換為可讀的字符串?

[英]How do I convert the address returned from my smart contract into a readable string?

我有返回地址的簡單get函數。 在JS的前端,我想將此地址轉換為某種可讀功能,即字符串。

遷移合同后,我使用web3使用該函數返回地址。 但是,我在閱讀它時遇到了麻煩。 我希望避免將其轉換為.sol文件中的字符串,以避免不必要的耗油量。

這是智能合約中的功能

function getBookAccounts() public returns(address){
   return bookAccount;
}

這是試圖控制台記錄地址的JS文件

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

不幸的是,我無法打印此地址。 我猜想我需要將其轉換為字符串,而不是像在實體中使用的UTF8。

確保將Solidity功能標記為view 否則,web3.js的默認行為是發送事務,並且您可能會找回事務哈希。 (交易沒有返回值。)

function getBookAccounts() public view returns (address) {

如果將函數更改為視圖,則web3.js應該發出調用而不是發送事務。 這樣速度更快,不需要耗油,並且可以返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM