簡體   English   中英

如何從 TypeScript 中的 Solidity 智能合約中檢索公共狀態變量的值?

[英]How to retrieve a value of a public state variable from Solidity smart contract in TypeScript?

我正在嘗試通過 JavaScript 以 solidity 返回公共狀態變量highestBid的值,將調用中的值保存到變量的可能方法是什么? 目前我得到: undefined

TS:

    async getHighestBid() {
       this.smartAuction.setProvider(this.provider);
       let result = await this.smartAuction.deployed().
                         then(function(contract: { highestBid: { call: () => Promise<any>; }; })  {contract.highestBid.call().
                         then(function(v) {return v})});
       console.log(result);
  }

索爾:

contract SmartAuction {
    address payable public beneficiary;
    uint public auctionEnd;

    address public highestBidder;
    uint public highestBid;

    mapping(address => uint) private pendingReturns;

    bool private ended;
...
}

如果console.log(result)記錄正確,你必須在函數中返回result ,所以當你調用函數時,它會給你return值。

 return result

在javascript中,如果你不在函數中返回任何東西,如果你調用函數,它會執行函數體,但它會返回undefined

暫無
暫無

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

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