Solidity

松露:如何獲得任何地址或合約地址的餘額

  • November 1, 2021

例如我有一個這樣的測試塊:

contract('CreeptomaPresale', function(accounts) {
   describe("adopted over allow quantity", function () {
       it("test get balance", async function () {
           let instance = await CreeptomaPresale.deployed();
           console.log("deployed address:" +  address)
       });
   });
});

我可以獲得部署地址。但是現在,我不知道如何獲得這個地址或任何其他地址的餘額。

謝謝

嘗試

contract('CreeptomaPresale', function(accounts) {
   describe("adopted over allow quantity", function () {
       it("test get balance", async function () {
           let instance = await CreeptomaPresale.deployed();
           console.log("deployed address:" +  instance.address);
           let balance = await web3.eth.getBalance(instance.address)
       });
   });
});

對於較新版本的 Truffle,根據範例,它實際上應該是:

let balance = await web3.eth.getBalance(instance.address);

否則,您將收到以下錯誤:

ReferenceError:地址未定義

引用自:https://ethereum.stackexchange.com/questions/45952