Javascript

encode_abi 編碼函式呼叫

  • January 7, 2022

我正在嘗試將 Javascript 程式碼轉換為 Python,並相信 JS 中用於在 Python 中編碼 ABI 的等效“web3.eth.abi.encodeFunctionSignature”函式與“eth-abi”庫中的“encode_abi”函式(https:// eth-abi.readthedocs.io/en/latest/encoding.html)。

但是,我完全堅持使用“encode_abi”對下面的“_maxTxAmount”函式呼叫進行編碼的正確語法。任何幫助/解釋將不勝感激。

   let sig = web3.eth.abi.encodeFunctionSignature({ name: '_maxTxAmount', type: 'function', inputs: [] });
d = {
   to: address,
   from: '0x8894e0a0c962cb723c1976a4421c95949be2d4e3',
   value: 0,
   gas: 15000000,
   data: sig,
};
try {
   let val = await web3.eth.call(d);
   maxTXAmount = web3.utils.toBN(val);

上面的摘錄來自我在 Github 上找到的一個反honeypot腳本,我想使用這個函式呼叫來查詢上面的函式以及未經驗證的合約,例如我不能進行“正常”合約呼叫。

為此,了解函式呼叫的編碼方式很重要。

第一部分是計算函式選擇器。這是啟動 calldata 的方法的 4 字節標識符。(參見https://docs.soliditylang.org/en/v0.8.11/abi-spec.html?highlight=Function%20id#function-selector)。例如,函式選擇器將是_maxTxAmount.

然後附加該函式的編碼參數。為此,您可以使用您提到的庫。對於您的範例,您不需要它,因為該函式沒有任何參數。

使用 python 與合約互動的最簡單方法是 web3py。這是呼叫合約的範例:https ://web3py.readthedocs.io/en/stable/contracts.html

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