看到恒星的blog说也可以做1CO,想对比一下和ETH的区别,毕竟恒星STR/XLM近期可以说非常强势,不知不觉市值到了第6。
先了解了一下开发文档https://www.stellar.org/developers/,简要总结以下几点
- 同以太坊一样,需要一个RPC服务器,以太坊节点可以提供rpc服务,STR是用一个叫Horizon API Server的东西,可以自建,也可以用官方或第三方公开的
- STR地址也是免费生成的,但是激活需要5个恒星币,每次上链操作需要1/100000(10万分之一)个恒星币
- 自定义的资产设置好信任线之后,是可以自由交换的,比如我发了资产A,其他人发了资产B,可以在恒星内部自由交换
废话不说,上代码操作(环境py3.6)
生成2个地址,
一个用于资产发布,一个用户资产接收,辅助函数如下
from stellar_base.keypair import Keypair
import requests
from stellar_base.asset import Asset
from stellar_base.horizon import horizon_testnet
from stellar_base.operation import ChangeTrust,Payment
from stellar_base.memo import TextMemo
from stellar_base.transaction import Transaction
from stellar_base.transaction_envelope import TransactionEnvelope as Te
horizon = horizon_testnet()
def create_account():
'''
' 账号也就是随机生成的地址和私钥
'''
kp = Keypair.random()
publickey = kp.address().decode()
seed = kp.seed().decode()
return publickey, seed
def active_account(address):
'''
' 测试网络中,可以找机器人要测试币,激活账号
'''
requests.get('https://horizon-testnet.stellar.org/friendbot?addr=' + address)
def test_accounts(address):
resp=horizon.account(address)
if 'balances' in resp:
print(json.dumps(resp['balances'],indent=1)) #账号状态正常
else:
print(resp['detail']) #账号状态异常
测试结果如下,每个账号发了10000测试币
>>> address, password = create_account()
>>> active_account(address)
test_accounts(address)
>>> test_accounts(address)
[{'balance': '10000.0000000', 'buying_liabilities': '0.0000000', 'selling_liabilities': '0.0000000', 'asset_type': 'native'}]
>>> customer_address, customer_password = create_account()
>>> active_account(customer_address)
test_accounts(customer_address)
>>> test_accounts(customer_address)
[{'balance': '10000.0000000', 'buying_liabilities': '0.0000000', 'selling_liabilities': '0.0000000', 'asset_type': 'native'}]
账号1作为发行资产方,账号2作为接收方
辅助函数如下
def create_asset(code,issuer_address):
return Asset(code, issuer_address)
def change_trust(asset,receiver_address):
op = ChangeTrust({
'source': receiver_address,
'asset': asset,
'limit': '5000'
})
sequence = horizon.account(customer_address).get('sequence')
msg = TextMemo('Change Trust Operation')
tx = Transaction(
source=customer_address,
opts={
'sequence': sequence,
'memo': msg,
'operations': [
op,
],
},
)
receiving_account = Keypair.from_seed(customer_password)
envelope = Te(tx=tx, opts={"network_id": "TESTNET"})
envelope.sign(receiving_account)
xdr_envelope = envelope.xdr()
response = horizon.submit(xdr_envelope)
if 'result_xdr' in response:
print('Successful')
else:
print('Things go Fishy')
测试结果
>>> asset = create_asset("TEST1",address)
>>> change_trust(asset,customer_address)
Successful
>>> test_accounts(customer_address)
[
{
"balance": "0.0000000",
"limit": "5000.0000000",
"buying_liabilities": "0.0000000",
"selling_liabilities": "0.0000000",
"asset_type": "credit_alphanum12",
"asset_code": "TEST1",
"asset_issuer": "GC4AO3UUFPPCJTEKUKEZEUVIB6QBUXSGDRBZJJIDSD3QFN2MJJ7V4ACS"
},
{
"balance": "9999.9999900",
"buying_liabilities": "0.0000000",
"selling_liabilities": "0.0000000",
"asset_type": "native"
}
]
可以看到,customer_address添加了一条对TEST1的信任线,上限5000,但是还没有实际的余额
将TEST1资产转移到custoner_address
辅助函数如下
def transfer_fund(amount,asset,customer_address,issue_address,issuer_seed):
print('Transferring fund to {}'.format(customer_address))
op = Payment({
'source': issue_address,
'destination': customer_address,
'asset': asset,
'amount': str(amount)
})
msg = TextMemo('Test Payment')
sequence = horizon.account(issue_address).get('sequence')
tx = Transaction(
source=issue_address,
opts={
'sequence': sequence,
'memo': msg,
'operations': [
op,
],
},
)
issuer_account = Keypair.from_seed(issuer_seed)
envelope = Te(tx=tx, opts={"network_id": "TESTNET"})
envelope.sign(issuer_account)
xdr_envelope = envelope.xdr()
response = horizon.submit(xdr_envelope)
if 'result_xdr' in response:
print('Successful Transfer')
else:
print('Things go Fishy')
运行结果如下
Transferring fund to GDJZY3YQB4E3KXHTHT5KVS2PZPJVGGVUJ6KJQ6F7NEE7INQLL4EMX5QF
Successful Transfer
>>> test_accounts(customer_address)
[
{
"balance": "1000.0000000",
"limit": "5000.0000000",
"buying_liabilities": "0.0000000",
"selling_liabilities": "0.0000000",
"asset_type": "credit_alphanum12",
"asset_code": "TEST1",
"asset_issuer": "GC4AO3UUFPPCJTEKUKEZEUVIB6QBUXSGDRBZJJIDSD3QFN2MJJ7V4ACS"
},
{
"balance": "9999.9999900",
"buying_liabilities": "0.0000000",
"selling_liabilities": "0.0000000",
"asset_type": "native"
}
]
可以看到,customer现在有1000个TEST1资产了,limit是最大持有量,如果超过了5000个,是会不成功的
后面再来个小结吧
- 转账非常快,几乎可以在s级完成
- 这个资产和ETH的ERC20还有点不太一样,有点像借贷关系,示例中是没有总量限定的,也就是可以给任意多个customer_address发送资产,只要不操过信任基线中设置的limit就行,限定总量类型的还要再研究一下资产的文档