欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

EOS 智能合约源代码解读 (3)asset.hpp

发布时间:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 EOS 智能合约源代码解读 (3)asset.hpp 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 合约中关于资产的数据结构的定义

输入字符串: “10.0000 CUR”
输出:amount = 10, symbol(4,“CUR”)

/**asset includes amount and currency symbol*/ struct asset : fc::reflect_init { // 通过给定的符号名称以及资产数量构建一个新的资产对象。explicit asset(share_type a = 0, symbol id = symbol(CORE_SYMBOL)) :amount(a), sym(id) {eosio_assert( is_amount_within_range(), asset_type_exception, "magnitude of asset amount must be less than 2^62" );eosio_assert( sym.valid(), asset_type_exception, "invalid symbol" );share_type amount; // 资产数量symbol_type symbol; // 资产符号名称,详见以下symbol_type源码分析。static constexpr int64_t max_amount = (1LL << 62) - 1; //资产数量最大值,取决于int64_t类型的取值范围。// 检查资产数量是否在范围以内,是否超过了最大限额。 bool is_amount_within_range() const { return -max_amount <= amount && amount <= max_amount; }// 检查资产对象是否有效,有效资产的数量应该小于等于最大限额同时它的符号名称也是有效的。 bool is_valid() const { return is_amount_within_range() && symbol.is_valid(); }// 设置资产的数量void set_amount(int64_t a) {amount = a;eosio_assert(is_amount_within_range(), "magnitude of asset amount must be less than 2^62");}//资产对象的运算符重载 ...// 打印资产void print() const{int64_t p = (int64_t)symbol.precision();int64_t p10 = 1;while (p > 0){p10 *= 10;--p;}p = (int64_t)symbol.precision();char fraction[p + 1];fraction[p] = '\0';auto change = amount % p10;for (int64_t i = p - 1; i >= 0; --i){fraction[i] = (change % 10) + '0';change /= 10;}printi(amount / p10);prints(".");prints_l(fraction, uint32_t(p));prints(" ");symbol.print(false);} EOSLIB_SERIALIZE(asset, (amount)(symbol))}void reflector_init()const {eosio_assert( is_amount_within_range(), asset_type_exception, "magnitude of asset amount must be less than 2^62" );eosio_assert( sym.valid(), asset_type_exception, "invalid symbol" );}};//using share_type = int64_t;struct extended_asset { // 默认构造器,构造一个扩展资产对象extended_asset(){}// 通过给定的数量和扩展符号构造一个扩展资产对象。extended_asset( asset a, text_name n ):quantity(a),contract(n){}asset quantity;text_name contract;// 资产拥有者 };

总结

以上是生活随笔为你收集整理的EOS 智能合约源代码解读 (3)asset.hpp的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。