Solidity

Web3 - 從沒有 ABI 的已部署契約中獲取變數值

  • August 23, 2019

我正在嘗試使用 Web3 1.0.0-beta.34 建構一個能夠管理使用者添加的 ERC20 代幣的錢包。在使用其地址將某個代幣添加到錢包後,我能夠獲取某個錢包中的硬幣數量。

由於我希望使用者添加任何 ERC20 代幣,因此我不知道代幣合約的 ABI。Web3 需要 ABI 來實例化合約,我解決了這個問題,獲取單個balanceOf函式的 ABI,然後使用以下方法實例化合約:

var contract = new web3.eth.Contract(balanceOfABI, contractAddress);

現在我需要獲取令牌的小數點,所以我基本上需要一個decimals變數的吸氣劑,但這是不可能的,因為這不是一個函式,我不能使用上面的技巧。

**有沒有辦法為常見的 getter 方法創建 ABI?**JavaScript 程式碼最後應該是這樣的:

var contract = new web3.eth.Contract(getterABI, contractAddress);
contract.methods.decimals().call(function (error, result) {
   if(!error) 
       console.log(result);
});

我希望我能表達我的觀點,非常感謝。

合約的 ABI(應用程序二進制介面)定義了該合約可以採取的行動。儘管 ERC20 代幣之間的合約邏輯/程式碼可能有所不同,但 ERC20 標準本身就是函式名稱列表。因此,只需採取 ERC20 定義的基本操作,您就可以將它們應用於任何 ERC20 合約,儘管合約使用您的錢包的任何特殊/附加功能都不知道。

[
 {
   "constant": true,
   "inputs": [],
   "name": "name",
   "outputs": [
     {
       "name": "",
       "type": "string"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "constant": false,
   "inputs": [
     {
       "name": "_spender",
       "type": "address"
     },
     {
       "name": "_value",
       "type": "uint256"
     }
   ],
   "name": "approve",
   "outputs": [],
   "payable": false,
   "type": "function"
 },
 {
   "constant": true,
   "inputs": [],
   "name": "totalSupply",
   "outputs": [
     {
       "name": "",
       "type": "uint256"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "constant": false,
   "inputs": [
     {
       "name": "_from",
       "type": "address"
     },
     {
       "name": "_to",
       "type": "address"
     },
     {
       "name": "_value",
       "type": "uint256"
     }
   ],
   "name": "transferFrom",
   "outputs": [],
   "payable": false,
   "type": "function"
 },
 {
   "constant": true,
   "inputs": [],
   "name": "decimals",
   "outputs": [
     {
       "name": "",
       "type": "uint256"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "constant": true,
   "inputs": [
     {
       "name": "_who",
       "type": "address"
     }
   ],
   "name": "balanceOf",
   "outputs": [
     {
       "name": "balance",
       "type": "uint256"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "constant": true,
   "inputs": [],
   "name": "symbol",
   "outputs": [
     {
       "name": "",
       "type": "string"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "constant": false,
   "inputs": [
     {
       "name": "_to",
       "type": "address"
     },
     {
       "name": "_value",
       "type": "uint256"
     }
   ],
   "name": "transfer",
   "outputs": [],
   "payable": false,
   "type": "function"
 },
 {
   "constant": true,
   "inputs": [
     {
       "name": "_owner",
       "type": "address"
     },
     {
       "name": "_spender",
       "type": "address"
     }
   ],
   "name": "allowance",
   "outputs": [
     {
       "name": "remaining",
       "type": "uint256"
     }
   ],
   "payable": false,
   "type": "function"
 },
 {
   "anonymous": false,
   "inputs": [
     {
       "indexed": true,
       "name": "from",
       "type": "address"
     },
     {
       "indexed": true,
       "name": "to",
       "type": "address"
     },
     {
       "indexed": false,
       "name": "value",
       "type": "uint256"
     }
   ],
   "name": "Transfer",
   "type": "event"
 },
 {
   "anonymous": false,
   "inputs": [
     {
       "indexed": true,
       "name": "owner",
       "type": "address"
     },
     {
       "indexed": true,
       "name": "spender",
       "type": "address"
     },
     {
       "indexed": false,
       "name": "value",
       "type": "uint256"
     }
   ],
   "name": "Approval",
   "type": "event"
 }
]

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