区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载本指南以 EOSIO 开源智能合约平台本仓库 eo/eos中cleos客户端的transfer子命令为核心完整演示如何将eosio.token合约发行的代币如SYS从一个账户转移到另一个账户并深入解析cleos transfer在 programs/cleos/main.cpp 中的底层实现原理、全部命令行参数以及相关测试用例。阅读本文后你将掌握转账命令的标准写法、参数语义、权限模型与常见错误规避方法。前置条件在执行转账操作之前需要确认以下环境与知识均已就绪已安装当前仓库支持的cleos客户端。cleos随 EOSIO 软件一同构建安装构建与安装方式可参考 docs/00_install/index.md。目标网络你所连接的链上已部署eosio.token合约。只有合约部署后代币的transferaction 才可被调用。发送方账户下文示例中的alice持有足够数量的待转账代币且已解锁钱包并导入对应私钥cleos wallet unlock/cleos wallet import。理解以下概念交易transaction一次提交到链上的原子操作集合转账动作被封装在交易中执行。转账不可逆代币转账一旦被区块确认并达到不可逆状态就无法回滚或撤销发送前务必核对收款账户与金额。核心命令cleos transfercleos transfer是cleos提供的标准代币转账子命令其官方功能描述为 Transfer tokens from account to account从账户向账户转移代币详见 docs/02_cleos/03_command-reference/transfer.md。基本语法cleos transfer sender recipient amount SYMBOL memo -p senderactive其中四个位置参数依次为参数说明sender转出代币的账户名recipient接收代币的账户名amount转账数量格式为数量 符号如0.0001 SYS必须用引号包裹memo随交易附带的消息/备注可选但推荐保留便于对账标准操作示例以下命令演示从账户alice向账户bob转账0.0001 SYS并附带备注Hodl!cleos transfer alice bob 0.0001 SYS Hodl! -p aliceactive其中alice 转出代币的账户bob 接收代币的账户0.0001 SYS 发送的SYS代币数量Hodl! 随交易附带的备注memo-p aliceactive 以alice账户的active权限为交易授权签名默认为senderactive此处显式指定更清晰。示例输出命令执行成功后的典型输出如下不同网络与版本可能略有差异此处的输出为文档原始示意其中 quantity 展示为25.0000 SYSexecuted transaction: 800835f28659d405748f4ac0ec9e327335eae579a0d8e8ef6330e78c9ee1b67c 128 bytes 1073 us # eosio.token eosio.token::transfer {from:alice,to:bob,quantity:25.0000 SYS,memo:m} # alice eosio.token::transfer {from:alice,to:bob,quantity:25.0000 SYS,memo:m} # bob eosio.token::transfer {from:alice,to:bob,quantity:25.0000 SYS,memo:m}输出解读executed transaction: 交易ID交易已在链上执行成功并给出交易哈希随后的每一行对应链上执行时打印的 action 轨迹第一行是eosio.token合约自身执行的transferaction接下来是发送方alice与接收方bob因收到通知notify而打印的相同 action 数据。全部命令行参数详解除了位置参数cleos transfer还支持以下选项与 docs/02_cleos/03_command-reference/transfer.md 一致它们在 programs/cleos/main.cpp 中以 CLI11 定义选项含义与默认值-c, --contract控制该代币的合约账户名默认eosio.token。可指定其他实现了相同transfer接口的合约--pay-ram-to-open由发送方支付 RAM为接收方打开代币余额行当接收方尚无该代币的余额记录时使用-x, --expiration交易过期时间秒默认 30 秒-f, --force-unique强制交易唯一。会消耗额外带宽防止误发相同交易当 memo 为空时自动在 memo 中注入随机 nonce见下文源码解析-s, --skip-sign跳过签名不使用解锁钱包中的密钥签名-j, --json以 JSON 格式打印结果--json-file将结果以 JSON 格式保存到文件-d, --dont-broadcast不广播交易到网络仅打印到 stdout--return-packed与--dont-broadcast配合获取打包后的交易-r, --ref-block指定用于 TAPOSTransaction as Proof-of-Stake的参考区块号或区块 ID--use-old-rpc使用旧版 RPCpush_transaction而非新版send_transaction-p, --permission授权交易使用的账户权限级别默认senderactive--max-cpu-usage-ms交易执行的 CPU 用量上限毫秒默认 0 表示不限--max-net-usage交易的网络带宽用量上限字节默认 0 表示不限--delay-sec延迟交易秒数默认 0 秒简化数量参数当省略符号时cleos会按如下规则处理源码to_asset逻辑见下文cleos transfer inita tester 1000即省略符号直接写数量。此时cleos会先向链上查询eosio.token合约中该符号的精度信息再对数量做精度对齐不足精度的自动补齐小数位。源码级解析cleos transfer 是如何工作的1. 子命令与参数定义在 programs/cleos/main.cpp 中transfer子命令被定义为string con eosio.token; // 默认代币合约 ... auto transfer app.add_subcommand(transfer, localized(Transfer tokens from account to account)); transfer-add_option(sender, sender, localized(The account sending tokens))-required(); transfer-add_option(recipient, recipient, localized(The account receiving tokens))-required(); transfer-add_option(amount, amount, localized(The amount of tokens to send))-required(); transfer-add_option(memo, memo, localized(The memo for the transfer)); transfer-add_option(--contract,-c, con, localized(The contract that controls the token)); transfer-add_flag(--pay-ram-to-open, pay_ram, localized(Pay RAM to open recipients token balance row)); add_standard_transaction_options_plus_signing(transfer, senderactive);从源码结构可以确认sender、recipient、amount为必填参数memo可选默认合约名为eosio.token默认授权为senderactive。2. 数量解析与精度对齐to_asset执行回调中先调用to_asset将金额字符串解析为资产对象见 programs/cleos/main.cppasset to_asset( account_name code, const string s ) { static map pairaccount_name, eosio::chain::symbol_code, eosio::chain::symbol cache; auto a asset::from_string( s ); eosio::chain::symbol_code sym a.get_symbol().to_symbol_code(); auto it cache.find( make_pair(code, sym) ); ... auto json call(get_currency_stats_func, ...); // 查询合约的货币统计get_currency_stats ... if ( a.decimals() expected_symbol.decimals() ) { auto factor expected_symbol.precision() / a.precision(); a asset( a.get_amount() * factor, expected_symbol ); } else if ( a.decimals() expected_symbol.decimals() ) { EOS_THROW(symbol_type_exception, Too many decimal digits in ${a}, only ${d} supported, ...); } return a; }要点cleos会通过 RPC 调用合约的get_currency_stats接口查询符号对应的最大供应量从而获得该符号的官方精度用户给出的金额小数位数少于官方精度时自动补齐如1000→1000.0000 SYS小数位数超过官方精度时直接抛出symbol_type_exception错误拒绝执行。这正是转账时金额精度必须合法的底层保障。3. 构造 transfer actioncreate_transfer随后调用create_transfer构造链上 action见 programs/cleos/main.cppchain::action create_transfer(const string contract, const name sender, const name recipient, asset amount, const string memo ) { auto transfer fc::mutable_variant_object (from, sender) (to, recipient) (quantity, amount) (memo, memo); return action { get_account_permissions(tx_permission, {sender, config::active_name}), name(contract), transfer_n, variant_to_bin( name(contract), transfer_n, transfer ) }; }即向目标合约默认eosio.token发出transferaction数据结构为{from, to, quantity, memo}授权级别取自senderactive。4. --pay-ram-to-open 的底层行为在回调中programs/cleos/main.cpp如果指定了--pay-ram-to-opencleos会先构造并发送一个openaction再发送transferif (!pay_ram) { send_actions( { transfer }, signing_keys_opt.get_keys()); } else { auto open_ create_open(con, name(recipient), transfer_amount.get_symbol(), name(sender)); send_actions( { open_, transfer }, signing_keys_opt.get_keys()); }create_openprograms/cleos/main.cpp构造{owner, symbol, ram_payer}结构ram_payer为发送方sender。这意味着当接收方账户从未持有过该代币其余额行尚未创建时发送方可以主动承担 RAM 费用帮对方开户从而避免转账因接收方余额表不存在而失败。5. 强制唯一--force-unique的实现当指定-f/--force-unique且 memo 为空时cleos会自动生成随机字符串注入 memo作为交易的 nonceif (tx_force_unique memo.size() 0) { memo generate_nonce_string(); tx_force_unique false; }这保证了两次相同的转账也会因 memo 不同而生成不同交易 ID防止重复广播同一笔交易代价是额外带宽消耗。结合测试用例理解转账语义仓库的单元测试 unittests/eosio.token_tests.cpp 覆盖了eosio.token合约的转账语义可帮助理解真实链上的行为正常转账transfer( alice_n, bob_n, asset::from_string(300 CERO), hola )验证账户间成功转出、余额更新超额转账向余额不足的账户发起701 CERO转账会被拒绝非法金额转账-1000 CERO负数同样会被合约拒绝。这些用例印证了虽然cleos transfer负责构造并广播交易但金额校验余额充足、数量为正、精度正确最终由eosio.token合约在链上强制执行cleos端只是做了前置的精度对齐。常见问题与注意事项金额必须带符号并用引号包裹0.0001 SYS中的符号是必须的且cleos会校验小数精度省略符号时按默认精度补齐建议显式书写符号避免歧义。精度超限会直接报错若金额小数位超过合约定义精度cleos抛出Too many decimal digits ... only d supported异常。转账不可逆区块确认后无法撤销务必核对recipient与金额memo建议保留原始信息便于审计对账。接收方无余额记录时可先由发送方用--pay-ram-to-open帮助对方开户或提示接收方先通过其他途径持有该代币。授权级别默认使用senderactive签名若使用自定义权限或合约账户发送务必用-p accountpermission显式指定。代币不一定是 SYS-c/--contract可指定控制该代币的其他合约只要是实现了标准transfer接口的合约均可按本文流程转账。总结通过cleos transfer用户可以便捷地将eosio.token合约发行的代币在账户间转移。本文从命令语法、参数选项、示例输出入手并深入到 programs/cleos/main.cpp 的to_asset、create_transfer、create_open等实现解释了金额精度对齐、默认授权、--pay-ram-to-open开户与--force-unique防重等底层机制同时结合 unittests/eosio.token_tests.cpp 说明了链上校验语义。掌握上述内容后即可在实际网络上安全、正确地完成代币转账操作。赞分享区块链【免费下载链接】eosAn open source smart contract platform项目地址https://gitcode.com/gh_mirrors/eo/eos点击查看免费下载相关推荐Archon archon-post-review-to-pr 详解将代码审查结果自动发布为 GitHub PR 评论的确定性 Agent 命令模板Archon archon post review to pr 详解将代码审查结果自动发布为 GitHub PR 评论的确定性 Agent 命令模板 本文以区块链EOSIO cleos get currency balance 命令详解查询账户代币余额的完整指南EOSIO cleos get currency balance 命令详解查询账户代币余额的完整指南 导读 cleos get currency balanc区块链WeChatMsg 完整指南3 步免费导出微信聊天记录HTML、Word、CSV 永久保存WeChatMsg 完整指南3 步免费导出微信聊天记录HTML、Word、CSV 永久保存 刚重装完系统想翻去年和某个人的聊天新微信里只剩最近几条记录。区块链创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
