看到恒星的blog說也可以做1CO,想對比一下和ETH的區(qū)別,畢竟恒星STR/XLM近期可以說非常強勢,不知不覺市值到了第6。
先了解了一下開發(fā)文檔https://www.stellar.org/developers/,簡要總結(jié)以下幾點
- 同以太坊一樣,需要一個RPC服務器,以太坊節(jié)點可以提供rpc服務,STR是用一個叫Horizon API Server的東西,可以自建,也可以用官方或第三方公開的
- STR地址也是免費生成的,但是激活需要5個恒星幣,每次上鏈操作需要1/100000(10萬分之一)個恒星幣
- 自定義的資產(chǎn)設置好信任線之后,是可以自由交換的,比如我發(fā)了資產(chǎn)A,其他人發(fā)了資產(chǎn)B,可以在恒星內(nèi)部自由交換
廢話不說,上代碼操作(環(huán)境py3.6)
生成2個地址,
一個用于資產(chǎn)發(fā)布,一個用戶資產(chǎn)接收,輔助函數(shù)如下
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):
'''
' 測試網(wǎng)絡中,可以找機器人要測試幣,激活賬號
'''
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)) #賬號狀態(tài)正常
else:
print(resp['detail']) #賬號狀態(tài)異常
測試結(jié)果如下,每個賬號發(fā)了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作為發(fā)行資產(chǎn)方,賬號2作為接收方
輔助函數(shù)如下
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')
測試結(jié)果
>>> 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資產(chǎn)轉(zhuǎn)移到custoner_address
輔助函數(shù)如下
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')
運行結(jié)果如下
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現(xiàn)在有1000個TEST1資產(chǎn)了,limit是最大持有量,如果超過了5000個,是會不成功的
后面再來個小結(jié)吧
- 轉(zhuǎn)賬非???,幾乎可以在s級完成
- 這個資產(chǎn)和ETH的ERC20還有點不太一樣,有點像借貸關系,示例中是沒有總量限定的,也就是可以給任意多個customer_address發(fā)送資產(chǎn),只要不操過信任基線中設置的limit就行,限定總量類型的還要再研究一下資產(chǎn)的文檔