Solidity

如何在 brownie 中訪問 Struct 值

  • April 20, 2022
//transaction struct of what a tx should have
struct Transaction{
   address to;
   uint value;
   bool executed;
   uint numConfirmations;
   mapping(address => bool) isConfirmed;
   mapping(address => bool) isRevoked;
}

//Array of transactions to keep track of each tx
Transaction[] public transactions;    

一個 Transaction 結構體和一個 Transaction 數組類型。我想訪問數組中的任何 Transaction 值並將其用於測試巧克力蛋糕。我怎樣才能做到這一點?

將感謝您的回复!

你可以這樣做:

# Get accoun
account = get_account()

# Deploy Contract
contract = ContractName.deploy(
   {"from": account}
)
# Retrieve the latest contract
contract = ContractName[-1]

print(contract.array_name(0))

由於transactions數組是公共的,solidity 會自動創建一個getter函式,該函式與您的數組同名。通過呼叫這個getter,你會得到一個元組。例如:contract.array_name(0)[0]將列印結構的第一個欄位。

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