You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过web3.py及区块浏览器传递sendOFT函数的元组与结构体参数

zkSync合约sendOFT函数调用参数示例

目标合约基础信息

  • 合约地址:0xDAc7479e5F7c01CC59bbF7c1C4EDF5604ADA1FF2
  • 目标函数:sendOFT

函数参数与FeeObj结构体定义

FeeObj结构体(Solidity格式)

struct FeeObj {
    uint256 amount;
    address recipient;
}

sendOFT函数签名(Solidity格式)

function sendOFT(
    uint64 _dstChainId,
    bytes32 _toAddress,
    uint256 _amount,
    FeeObj calldata _fee,
    bytes calldata _params
) external payable;

1. web3.py调用示例

from web3 import Web3

# 初始化zkSync主网RPC连接
w3 = Web3(Web3.HTTPProvider("https://mainnet.era.zksync.io"))

# 合约ABI片段(仅包含sendOFT相关定义)
contract_abi = [
    {
        "inputs": [
            {"internalType": "uint64", "name": "_dstChainId", "type": "uint64"},
            {"internalType": "bytes32", "name": "_toAddress", "type": "bytes32"},
            {"internalType": "uint256", "name": "_amount", "type": "uint256"},
            {"components": [
                {"internalType": "uint256", "name": "amount", "type": "uint256"},
                {"internalType": "address", "name": "recipient", "type": "address"}
            ], "internalType": "struct FeeObj", "name": "_fee", "type": "tuple"},
            {"internalType": "bytes", "name": "_params", "type": "bytes"}
        ],
        "name": "sendOFT",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    }
]

# 加载合约实例
contract = w3.eth.contract(address="0xDAc7479e5F7c01CC59bbF7c1C4EDF5604ADA1FF2", abi=contract_abi)

# 构造调用参数(参考示例交易解码值,按需修改)
dst_chain_id = 1
# 以太坊地址转bytes32:补前导零至32字节长度
to_address = Web3.to_bytes(hexstr="0x000000000000000000000000YourTargetAddressHere")
amount = Web3.to_wei(1, "ether")
fee_obj = {
    "amount": Web3.to_wei(0.001, "ether"),
    "recipient": "0xFeeRecipientAddressHere"
}
params = b""  # 无额外参数时传空字节

# 构建交易(需替换为你的账户私钥)
signer_account = w3.eth.account.from_key("YourPrivateKeyHere")
nonce = w3.eth.get_transaction_count(signer_account.address)
gas_price = w3.eth.gas_price

tx = contract.functions.sendOFT(
    dst_chain_id,
    to_address,
    amount,
    fee_obj,
    params
).build_transaction({
    "from": signer_account.address,
    "nonce": nonce,
    "gasPrice": gas_price,
    "value": 0  # 函数为payable,如需附带ETH需填写对应wei值
})

# 签名并发送交易
signed_tx = signer_account.sign_transaction(tx)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(f"交易哈希: {tx_hash.hex()}")

2. web3.js调用示例

const { Web3 } = require('web3');

// 初始化zkSync主网RPC连接
const web3 = new Web3('https://mainnet.era.zksync.io');

// 合约ABI片段
const contractAbi = [
    {
        "inputs": [
            {"internalType": "uint64", "name": "_dstChainId", "type": "uint64"},
            {"internalType": "bytes32", "name": "_toAddress", "type": "bytes32"},
            {"internalType": "uint256", "name": "_amount", "type": "uint256"},
            {"components": [
                {"internalType": "uint256", "name": "amount", "type": "uint256"},
                {"internalType": "address", "name": "recipient", "type": "address"}
            ], "internalType": "struct FeeObj", "name": "_fee", "type": "tuple"},
            {"internalType": "bytes", "name": "_params", "type": "bytes"}
        ],
        "name": "sendOFT",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    }
];

// 加载合约实例
const contractAddress = '0xDAc7479e5F7c01CC59bbF7c1C4EDF5604ADA1FF2';
const contract = new web3.eth.Contract(contractAbi, contractAddress);

// 构造调用参数(按需修改)
const dstChainId = 1;
// 以太坊地址转bytes32:补前导零至32字节长度
const toAddress = '0x000000000000000000000000YourTargetAddressHere';
const amount = web3.utils.toWei('1', 'ether');
const feeObj = {
    amount: web3.utils.toWei('0.001', 'ether'),
    recipient: '0xFeeRecipientAddressHere'
};
const params = '0x';  // 无额外参数时传空hex字符串

// 发送交易(需替换为你的私钥)
const privateKey = 'YourPrivateKeyHere';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);

async function executeSendOFT() {
    const nonce = await web3.eth.getTransactionCount(account.address);
    const gasPrice = await web3.eth.getGasPrice();

    const txData = contract.methods.sendOFT(
        dstChainId,
        toAddress,
        amount,
        feeObj,
        params
    ).encodeABI();

    const tx = {
        from: account.address,
        to: contractAddress,
        nonce: nonce,
        gasPrice: gasPrice,
        data: txData,
        value: '0'  // 如需附带ETH,填写对应wei值
    };

    // 估算gas(可选)
    tx.gas = await web3.eth.estimateGas(tx);

    // 签名并发送
    const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
    const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
    console.log('交易哈希:', receipt.transactionHash);
}

executeSendOFT();

3. zkSync区块浏览器参数输入示例

在区块浏览器的合约页面进入「Write Contract」标签,选择sendOFT函数后,按以下格式填写参数:

  • _dstChainId: 输入数字类型的目标链ID,例如 1
  • _toAddress: 输入32字节长度的hex格式地址(以太坊地址补前导零),例如 0x000000000000000000000000abc123abc123abc123abc123abc123abc123abc
  • _amount: 输入代币最小单位的数值,例如转账1枚18位小数的代币,填写 1000000000000000000
  • _fee (tuple):
    • amount: 输入手续费的最小单位数值,例如 1000000000000000
    • recipient: 输入手续费接收地址,例如 0x1234567890123456789012345678901234567890
  • _params: 无额外参数时填写 0x,有参数则输入对应hex字符串

连接钱包确认交易细节后,即可发起交易。


内容的提问来源于stack exchange,提问作者Mazyar Daryaee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:51:06