使用 Ethereum::ABI 编码元数据并部署 ERC-721
解读
国内区块链岗位面试常把“链上部署”拆成三步:ABI 编码、构造创世交易、本地签名广播。Perl 虽非主流,但在联盟链网关、交易所批量归集、NFT 平台空投脚本里仍被大量使用。面试官问“用 Ethereum::ABI 编码元据并部署 ERC-721”,核心想看三点:
- 是否理解 ERC-721 元数据(name、symbol、baseURI)在构造函数里的排列顺序与类型;
- 是否会用 Perl 生态里唯一成熟的 Ethereum::ABI 完成 tuple 打包;
- 是否能把编码后的 data 拼成可广播的 0x 交易,并用本地私钥签名,而不是靠 geth 控制台。
现场往往只给 15 min 手写,必须一次编译通过,否则直接挂。
知识点
- Ethereum::ABI 0.12 之后只支持 encode_function_call,不支持 decode,需手动拼 selector。
- ERC-721 构造函数原型:
constructor(string memory name, string memory symbol, string memory baseURI)
selector = keccak256("constructor(string,string,string)")[:4] 但部署时无函数名,直接把编码参数跟在 bytecode 后。 - 部署交易结构:
{ to: "", value: 0, data: bytecode + abi_encoded_params, gasPrice, gasLimit, nonce, chainId } - 国内主流链(BSC、Heco、Polygon 国企侧链)chainId 已固定,必须写死,否则 replay 攻击。
- Perl 里 Keccak-256 需靠 Digest::Keccak 或 Crypt::Perl::Keccak,无 XS 版本,面试环境常禁用编译模块,须提前背出纯 Perl 实现或直接用 openssl dgst 离线算好。
- 私钥签名用 Crypt::Perl::ECDSA::PrivateKey,r,s,v 拼成 65 字节,再 RLP 编码,Ethereum::RLP 0.06 有 bug,对空 list 编码错误,需手写 RLP。
- 元数据里的 baseURI 必须带末尾斜杠,否则 OpenSea 中文节点缓存 404,国内面试会抠这个细节。
- 部署后需立刻验证:
- eth_call 读 name() 返回 UTF-8 中文;
- 用 tokenURI(1) 拼出的 URL 可 curl 通;
- 事件 Transfer(address,address,uint256) 日志 topic0 必须等于 keccak 哈希,否则浏览器插件识别失败。
答案
#!/usr/bin/env perl
use strict;
use warnings;
use Ethereum::ABI qw(encode_function_call);
use Digest::Keccak qw(keccak_256);
use Crypt::Perl::ECDSA::PrivateKey;
use Ethereum::RLP;
use Net::Ethereum::RPC;
# 1. 读入编译后的字节码(solc 0.8.19 优化 200 轮)
my $bytecode = '0x60806040523480156200001157600080fd5...<省略>';
# 2. 构造参数
my $name = 'ChineseArtNFT';
my $symbol = 'CAN';
my $baseURI = 'https://nft.example.com/meta/';
# 3. ABI 编码
my $abi = Ethereum::ABI->new;
my $encoded = $abi->encode(
[ 'string', 'string', 'string' ],
[ $name, $symbol, $baseURI ]
);
my $deploy_data = substr($bytecode,2) . substr($encoded,2); # 去掉 0x
# 4. 构造交易
my $chainId = 56; # BSC 主网
my $gasPrice = '0x05d21dba00'; # 25 gwei
my $gasLimit = '0x3d0900'; # 4 M
my $nonce = '0x1c'; # 提前 eth_getTransactionCount 拿到
my $tx = {
nonce => pack('H*', substr($nonce,2)),
gasPrice => pack('H*', substr($gasPrice,2)),
gasLimit => pack('H*', substr($gasLimit,2)),
to => '',
value => pack('H*', '00'),
data => pack('H*', $deploy_data),
chainId => $chainId
};
# 5. RLP 编码
my $rlp = Ethereum::RLP->new;
my $raw = $rlp->encode([
$tx->{nonce},
$tx->{gasPrice},
$tx->{gasLimit},
$tx->{to},
$tx->{value},
$tx->{data},
$tx->{chainId}, 0, 0
]);
# 6. 哈希并签名
my $hash = keccak_256($raw);
my $pvk = Crypt::Perl::ECDSA::PrivateKey->new(
pack('H*', '4f3abcf1b9c....<64 字节私钥>')
);
my ($r, $s) = $pvk->sign_digest($hash);
my $v = 37 + ($chainId * 2); # EIP-155
# 7. 拼最终交易
my $signed = $rlp->encode([
$tx->{nonce},
$tx->{gasPrice},
$tx->{gasLimit},
$tx->{to},
$tx->{value},
$tx->{data},
pack('C', $v),
$r->to_big_int->to_bytes,
$s->to_big_int->to_bytes
]);
my $txHex = '0x' . unpack('H*', $signed);
# 8. 广播
my $rpc = Net::Ethereum::RPC->new('https://bsc-dataseed.binance.org');
my $txHash = $rpc->eth_sendRawTransaction($txHex);
print "Contract deployed at tx: $txHash\n";
现场手写时,把 1-8 步按顺序写清,每步注释中文,保证即使模块名拼错也能让面试官看到思路完整。编译报错只扣 10%,思路断直接 0 分。
拓展思考
- 如果元数据需要可升级,把 baseURI 做成 Ownable 的 setBaseURI,构造函数里只传空串,部署后立刻多签调用,脚本如何改?
提示:再发一笔调用交易,data = selector + abi_encode(newURI),selector=keccak256("setBaseURI(string)")[:4]。 - 国内监管要求 NFT 平台“可撤销”,需在合约层加 freeze 函数,只有工信部备案地址可调用,如何用 Perl 每天定时巡检冻结列表并自动发多签交易?
思路:用 Mojo::UserAgent 轮询备案 API,比对本地 SQLite,发现新增冻结地址后,构造 0x 多签交易,本地签名后丢到 BSC 冗余 RPC,失败则重试 3 次并企业微信告警。 - 当 BSC 出现 18 秒分叉,txHash 回滚,Perl 脚本如何快速判断并重发?
答案:缓存 (nonce, chainId) 维度,eth_getTransactionReceipt 返回 null 超过 120 秒即认为回滚,重新 eth_getTransactionCount 拿最新 nonce,重新签名,但 gasPrice 需上浮 10% 防止再被回滚。