独自トークンを売買し EOS の受け渡しを行う
普通に実装しようと思うと、署名を2, 3回に分けて
購入者がトークン販売コントラクトに EOS を預入
購入者がトークン販売コントラクトでトークンと EOS を交換
販売者がトークン販売コントラクトから EOS を引き出し
という手順を踏むことになるが、少し面倒だった。
今回は、それを無理やり1回の署名で行わせることに挑戦した。
環境
eosio.cdt v1.3.2
用意したコントラクトは、 GitHub から確認してください。以下、その動作の様子を雑に説明します。 1. 最初の状態
code:get_balance.sh
cleos get table eosio.token mokemokecore account
code:response.txt
{
"rows": [{
"balance": "755.1245 EOS"
}
],
"more": false
}
トークン販売者である mokemokecore アカウントの残高は 755.1245 EOS。
code:get_balance.sh
cleos get table eosio.token leohioleohio account
code:response.txt
{
"rows": [{
"balance": "394.5525 EOS"
}
],
"more": false
}
トークン購入者である leohioleohio アカウントの残高は 394.5525 EOS。
トークン販売コントラクト toycashiopcs のテーブルを見る。
code:get_token.sh
cleos get table toycashiopcs toycashiopcs token
code:response.txt
{
"rows": [{
"id": 1,
"subkey": "EOS6KEzAbW8EowSEPc1fd5t1mLMmnVDk5rw3PMjNsnZqRN9PPD23S",
"owner": "mokemokecore",
"sym": "PCS",
"active": 0
}
],
"more": false
}
mokemokecore アカウントがトークン ( ID : 1 ) を持っている。
code:get_sell_order.sh
cleos get table toycashiopcs toycashiopcs sellorder
code:response.txt
{
"rows": [],
"more": false
}
トークンの売り注文は出ていない。
2. mokemokecore アカウントはトークン ( ID: 1 ) を売りに出す(準備)。
code:sell_order.sh
code:response.txt
executed transaction: c00fdf9b6a2c627668e6bc8ab9efa06af1c4ed7cdf337e2377bfd2bf9c5230ea 136 bytes 337 us
# toycashiopcs <= toycashiopcs::sellbyid {"owner":"mokemokecore","token_id":1,"price":"0.1000 EOS","memo":"sell token"}
code:get_balance.sh
cleos get table eosio.token mokemokecore account
code:response.txt
{
"rows": [{
"balance": "755.1245 EOS"
}
],
"more": false
}
mokemokecore アカウントの残高は 755.1245 EOS。変化なし。
code:get_token.sh
cleos get table toycashiopcs toycashiopcs token
code:response.txt
{
"rows": [{
"id": 1,
"subkey": "EOS6KEzAbW8EowSEPc1fd5t1mLMmnVDk5rw3PMjNsnZqRN9PPD23S",
"owner": "toycashiopcs",
"sym": "PCS",
"active": 0
}
],
"more": false
}
トークンの所有権は toycashiopcs に移った。
code:get_sell_order.sh
cleos get table toycashiopcs toycashiopcs sellorder
code:response.txt
{
"rows": [{
"id": 1,
"price": "0.1000 EOS",
"owner": "mokemokecore",
"sym": "PCS"
}
],
"more": false
}
売り注文が記録された。
3. leohioleohio アカウントがトークンを購入する(本題)。
code:buy_token.sh
code:response.txt
executed transaction: 71d063918d7668cfd33b979376db4411530f50b78af1e71b4ac72dcb2172d6c9 152 bytes 768 us
# eosio.token <= eosio.token::transfer {"from":"leohioleohio","to":"toycashiopcs","quantity":"0.1000 EOS","memo":"buy token#1 in pcstoycash...
# leohioleohio <= eosio.token::transfer {"from":"leohioleohio","to":"toycashiopcs","quantity":"0.1000 EOS","memo":"buy token#1 in pcstoycash...
# toycashiopcs <= eosio.token::transfer {"from":"leohioleohio","to":"toycashiopcs","quantity":"0.1000 EOS","memo":"buy token#1 in pcstoycash...
# eosio.token <= eosio.token::transfer {"from":"toycashiopcs","to":"mokemokecore","quantity":"0.1000 EOS","memo":"executed as the inline ac...
# toycashiopcs <= eosio.token::transfer {"from":"toycashiopcs","to":"mokemokecore","quantity":"0.1000 EOS","memo":"executed as the inline ac...
# mokemokecore <= eosio.token::transfer {"from":"toycashiopcs","to":"mokemokecore","quantity":"0.1000 EOS","memo":"executed as the inline ac...
code:get_balance.sh
cleos get table eosio.token mokemokecore account
code:response.txt
{
"rows": [{
"balance": "755.2245 EOS"
}
],
"more": false
}
mokemokecore アカウントの残高は 755.2245 EOS。トークンが売れたため 0.1000 EOS 増加。
code:get_balance.sh
cleos get table eosio.token leohioleohio account
code:response.txt
{
"rows": [{
"balance": "394.4525 EOS"
}
],
"more": false
}
leohioleohio アカウントの残高は 394.4525 EOS。トークンを買ったため 0.1000 EOS 減少。
code:get_token.sh
cleos get table toycashiopcs toycashiopcs token
code:response.txt
{
"rows": [{
"id": 1,
"subkey": "EOS6KEzAbW8EowSEPc1fd5t1mLMmnVDk5rw3PMjNsnZqRN9PPD23S",
"owner": "leohioleohio",
"sym": "PCS",
"active": 0
}
],
"more": false
}
トークンの所有権は購入者 leohioleohio に移った。
code:get_sell_order.sh
cleos get table toycashiopcs toycashiopcs sellorder
code:response.txt
{
"rows": [],
"more": false
}
売り注文はなくなった。
結論
1回の署名で、
買い手が EOS を送る
買い手がトークンを購入
売り手が EOS を受け取る
を行うことができた。
後日談
強制的に複数のアクションを同時に行いたい場合に上記の実装は役に立つが、通常は 1 つのトランザクションの中に複数のアクションを含めることで対応が可能な場合が多い。
関連ワード