日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

EOS 智能合约源代码解读 (10)token合约“几种关键操作”

發(fā)布時(shí)間:2025/3/21 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EOS 智能合约源代码解读 (10)token合约“几种关键操作” 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. create:負(fù)責(zé)創(chuàng)建資產(chǎn)

void token::create( const text_name& issuer, const asset& maximum_supply) {require_auth( get_self() );name issuer_id(get_account_id(issuer.c_str(), issuer.size()));auto sym = maximum_supply.symbol;check( sym.is_valid(), "invalid symbol name" );check( maximum_supply.is_valid(), "invalid supply");check( maximum_supply.amount > 0, "max-supply must be positive");stats statstable( get_self(), sym.code().raw() );auto existing = statstable.find( sym.code().raw() );check( existing == statstable.end(), "token with symbol already exists" );statstable.emplace( get_self(), [&]( auto& s ) {s.supply.symbol = maximum_supply.symbol;s.max_supply = maximum_supply;s.issuer = issuer_id;}); }
  • 獲取eosio.token合約部署賬戶的授權(quán)
  • 判斷幣種符號(hào) (sysmol name) 是否合法,幣種符號(hào)必須是大寫字母,長(zhǎng)度小于8位
  • 判斷發(fā)行量是否越界且是否為正數(shù),支持的最大發(fā)行量 amount <= 2^62-1
  • 查詢statstable表,判斷代幣符號(hào)是否存在,eosio.token支持發(fā)行多資產(chǎn)
  • 建表,將代幣符號(hào)、發(fā)行總量、發(fā)行方存入數(shù)據(jù)庫(kù)

1.1 調(diào)用實(shí)例

cleos push action eosio.token create ‘[ “eosio”, “1000000000.0000 EOS”]’ -p eosio.token

  • eosio.token合約部署在eosio.token賬戶
  • 資產(chǎn)發(fā)行賬戶為eosio
  • 最大發(fā)行量10億
  • 幣種精度4
  • 幣種符號(hào)EOS

2. issue:負(fù)責(zé)發(fā)行資產(chǎn)

issue方法的實(shí)現(xiàn)在eosio.token.cpp中,需要傳入兩個(gè)參數(shù)完成幣種的發(fā)行:

  • to:發(fā)行金額打入的賬戶
  • quantity:發(fā)行金額
  • memo:交易備注
void token::issue(const text_name& to, const asset& quantity, const string& memo ) {name to_id(get_account_id(to.c_str(), to.size()));auto sym = quantity.symbol;check( sym.is_valid(), "invalid symbol name" );check( memo.size() <= 256, "memo has more than 256 bytes" );stats statstable( get_self(), sym.code().raw() );auto existing = statstable.find( sym.code().raw() );check( existing != statstable.end(), "token with symbol does not exist, create token before issue" );const auto& st = *existing;check( to_id == st.issuer, "tokens can only be issued to issuer account" );require_auth( st.issuer );check( quantity.is_valid(), "invalid quantity" );check( quantity.amount > 0, "must issue positive quantity" );check( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );check( quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply");statstable.modify( st, same_payer, [&]( auto& s ) {s.supply += quantity;});add_balance( st.issuer, quantity, st.issuer ); }
  • 前置檢查:查表判斷幣種是否存在、memo最大長(zhǎng)度不能超過(guò)256字節(jié)
  • 查表獲取幣種信息:發(fā)行賬戶、最大發(fā)行量、幣種精度、幣種符號(hào)
  • 獲取發(fā)行賬戶的授權(quán)
  • 前置檢查:發(fā)行金額為正數(shù)且不超過(guò)最大發(fā)行量、幣種精度校驗(yàn)
  • 更新表,增加發(fā)行資產(chǎn)的余額
  • 如果to賬戶和發(fā)行賬戶不一致,調(diào)用內(nèi)聯(lián)合約轉(zhuǎn)賬

調(diào)用實(shí)例: 資產(chǎn)發(fā)行賬戶eosio授權(quán),將10億EOS打入eosio賬戶中,備注是test issue

cleos push action eosio.token issue '[ "eosio", "1000000000.0000 EOS", "test issue"]' -p eosio

3. transfer:負(fù)責(zé)資產(chǎn)轉(zhuǎn)賬

transfer方法的實(shí)現(xiàn)在eosio.token.cpp中,需要傳入四個(gè)參數(shù)完成幣種的轉(zhuǎn)賬:

  • from:出幣賬戶
  • to:入金賬戶
  • quantity:轉(zhuǎn)賬金額
  • memo:交易備注
void token::transfer( const text_name& from,const text_name& to,const asset& quantity,const string& memo ) {name from_id(get_account_id(from.c_str(), from.size()));name to_id(get_account_id(to.c_str(), to.size()));check( from != to, "cannot transfer to self" );require_auth( from_id );check( is_account( to ), "to account does not exist");auto sym = quantity.symbol.code();stats statstable( get_self(), sym.raw() );const auto& st = statstable.get( sym.raw() );require_recipient( from );require_recipient( to );check( quantity.is_valid(), "invalid quantity" );check( quantity.amount > 0, "must transfer positive quantity" );check( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );check( memo.size() <= 256, "memo has more than 256 bytes" );auto payer = has_auth( to ) ? to_id : from_id;sub_balance( from_id, quantity );add_balance( to_id, quantity, payer ); }

資產(chǎn)的轉(zhuǎn)賬:

  • 前置檢查:判斷是否轉(zhuǎn)賬給自己、判斷to賬戶是否存在
  • 獲得from賬戶的授權(quán)
  • 獲取幣種信息:發(fā)行賬戶、最大發(fā)行量、幣種精度、幣種符號(hào)
  • 分別通知from、to賬戶合約調(diào)用結(jié)果
  • 前置檢查:轉(zhuǎn)賬金額為正且不超過(guò)最大發(fā)行量、幣種精度正確、memo長(zhǎng)度不大于256
  • 加減from和to地址賬戶余額

總結(jié)

以上是生活随笔為你收集整理的EOS 智能合约源代码解读 (10)token合约“几种关键操作”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。