forked from Cocos-BCX/Go-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
465 lines (427 loc) · 15.3 KB
/
asset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package CocosSDK
import (
"CocosSDK/rpc"
. "CocosSDK/type"
"errors"
"math"
)
/*吃单 NH 资产买入*/
func FillNhAsset(order_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
order_info := rpc.GetNhAssetOrderInfo(order_id)
tx := &FillNhOrder{
PriceAmount: String(order_info.Price.Amount.String()),
PriceAssetID: ObjectId(order_info.Price.AssetID),
NhAsset: ObjectId(order_info.NhAssetID),
Seller: ObjectId(order_info.Seller),
PriceAssetSymbol: String(order_info.AssetQualifier),
Order: ObjectId(order_id),
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
Extensions: []interface{}{},
}
return Wallet.SignAndSendTX(OP_FILL_NHORDER, tx)
}
/*取消 NH 资产卖出单*/
func CancelNhAssetOrder(order_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
tx := &CancelOrder{
Order: ObjectId(order_id),
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
Extensions: []interface{}{},
}
return Wallet.SignAndSendTX(OP_CANCEL_NH_ORDER, tx)
}
/*NH 资产卖出单*/
func SellNhAsset(otcaccount_name, asset_id, memo, pending_order_fee_asset, price_asset string, pending_order_fee_amount, price_amount uint64) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
otcaccount_info := rpc.GetAccountInfoByName(otcaccount_name)
pending_asset_info := rpc.GetTokenInfo(pending_order_fee_asset)
price_asset_info := rpc.GetTokenInfo(price_asset)
pending_precision := math.Pow10(pending_asset_info.Precision)
price_precision := math.Pow10(price_asset_info.Precision)
tx := &NhOrder{
NhAsset: ObjectId(asset_id),
PendingOrdersFee: Amount{Amount: uint64(float64(pending_order_fee_amount) * pending_precision), AssetID: ObjectId(pending_order_fee_asset)},
Price: Amount{Amount: uint64(float64(price_amount) * price_precision), AssetID: ObjectId(price_asset)},
Seller: ObjectId(Wallet.Default.Info.ID),
Otcaccount: ObjectId(otcaccount_info.ID),
Expiration: GetExpiration(),
Memo: String(memo),
}
return Wallet.SignAndSendTX(OP_SELL_NH_ASSET, tx)
}
/*NH 资产删除*/
func DeleteNhAsset(asset_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
tx := &DelNhAsset{
NhAssetCreator: NhAssetCreator{
FeePayingAccount: ObjectId(Wallet.Default.Info.ID)},
NhAsset: ObjectId(asset_id),
}
return Wallet.SignAndSendTX(OP_DEL_NH_ASSET, tx)
}
/*NH 资产转账*/
func TransferNhAsset(to_name, asset_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
to_info := rpc.GetAccountInfoByName(to_name)
tx := &TransferNh{
To: ObjectId(to_info.ID),
From: ObjectId(Wallet.Default.Info.ID),
NhAsset: ObjectId(asset_id),
}
return Wallet.SignAndSendTX(OP_TRANSFER_NH_ASSET, tx)
}
/*創建NH資產*/
func CreateNhAsset(asset_symbol, world_view, owner_name, base_describe string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
owner_info := rpc.GetAccountInfoByName(owner_name)
nh_asset := &NhAsset{
AssetID: String(asset_symbol),
BaseDescribe: String(base_describe),
Owner: ObjectId(owner_info.ID),
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
WorldView: String(world_view),
}
return Wallet.SignAndSendTX(OP_CREATE_NH_ASSET, nh_asset)
}
/*批准 关联世界观的提议*/
func ApprovalsProposal(proposal_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
approval := &Approvals{
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
Proposal: ObjectId(proposal_id),
ActiveApprovalsToAdd: []Object{ObjectId(Wallet.Default.Info.ID)},
ActiveApprovalsToRemove: []Object{},
OwnerApprovalsToAdd: []Object{},
OwnerApprovalsToRemove: []Object{},
KeyApprovalsToAdd: []Object{},
KeyApprovalsToRemove: []Object{},
Extensions: []interface{}{},
}
return Wallet.SignAndSendTX(OP_APPROVAL, approval)
}
/*提议关联世界观*/
func RelateWorldView(world_view string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
world_view_info := rpc.GetWorldViewInfo(world_view)
creator := rpc.GetWorldViewCreator(world_view_info.WorldViewCreator)
op_data := &ProposedOps{
RelatedAccount: ObjectId(Wallet.Default.Info.ID),
WorldView: String(world_view),
ViewOwner: ObjectId(creator.Creator),
}
ops := OPS{
ID: OP_RELATE_WORLDVIEW,
Ops: *op_data,
}
op := &RelatedWorldView{
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
ExpirationTime: GetExpiration(),
ProposedOps: []OPS{ops},
Extensions: []interface{}{},
}
//fees := rpc.GetRequireFeeData(21, op)
return Wallet.SignAndSendTX(OP_PROPOSAL, op)
}
/*創建世界觀*/
func CreateWorldView(name string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
world_view := &WorldView{
//Fee: EmptyFee(),
FeePayingAccount: ObjectId(Wallet.Default.Info.ID),
WorldView: String(name),
}
return Wallet.SignAndSendTX(OP_CREATE_WORLDVIEW, world_view)
}
const (
Charge_market_fee = 0x01
White_list = 0x02
Override_authority = 0x04
Transfer_restricted = 0x08
Gisable_force_settle = 0x10
Global_settle = 0x20
Disable_issuer = 0x40
Witness_fed_asset = 0x80
Committee_fed_asset = 0x100
Default_Permissions = Charge_market_fee | White_list | Override_authority | Transfer_restricted
)
/*更新 token*/
func UpdateToken(symbol string, max_supply uint64, new_issuer ...string) (string, error) {
update_asset_info := rpc.GetTokenInfoBySymbol(symbol)
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
precision := uint64(math.Pow10(update_asset_info.Precision))
cm_op := CommonOptions{
MaxSupply: max_supply * precision,
MarketFeePercent: 0,
MaxMarketFee: 0,
Flags: 0,
IssuerPermissions: uint64(update_asset_info.Options.IssuerPermissions.Int64()),
Description: String(`{"main":"` + symbol + `","short_name":"","market":""}`),
Extensions: []interface{}{},
}
var newIssuer ObjectId
if len(new_issuer) >= 1 {
new_issuer_info := rpc.GetAccountInfoByName(new_issuer[0])
newIssuer = ObjectId(new_issuer_info.ID)
} else {
newIssuer = EMPTY_ID
}
AssetData := &UpdateAssetData{
Extensions: []interface{}{},
NewIssuer: Optional(newIssuer),
Issuer: ObjectId(Wallet.Default.Info.ID),
AssetToUpdate: ObjectId(update_asset_info.ID),
NewOptionsData: cm_op,
}
return Wallet.SignAndSendTX(OP_UPDATE_TOKEN, AssetData)
}
/*销毁 token*/
func ReserveToken(symbol string, amount float64) (string, error) {
asset_info := rpc.GetTokenInfoBySymbol(symbol)
precision := math.Pow10(asset_info.Precision)
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
AssetData := &ReserveTokenData{
Extensions: []interface{}{},
Payer: ObjectId(Wallet.Default.Info.ID),
AmountToReserve: Amount{Amount: uint64(float64(amount) * precision), AssetID: ObjectId(asset_info.ID)},
}
return Wallet.SignAndSendTX(OP_RESERVE_TOKEN, AssetData)
}
/*创建 token*/
func CreateToken(symbol string, max_supply, precision uint64, issuer_permissions ...int) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
permissions := Default_Permissions
if len(issuer_permissions) > 0 {
permissions = issuer_permissions[0]
}
new_precision := uint64(math.Pow10(int(precision)))
cm_op := CommonOptions{
MaxSupply: max_supply * new_precision,
MarketFeePercent: 0,
MaxMarketFee: 0,
Flags: 0,
IssuerPermissions: uint64(permissions),
Description: String(`{"main":"` + symbol + `","short_name":"","market":""}`),
Extensions: []interface{}{},
}
AssetData := &CreateAssetData{
Extensions: []interface{}{},
Precision: precision,
Issuer: ObjectId(Wallet.Default.Info.ID),
Symbol: String(symbol),
CommonOptionsData: cm_op,
}
return Wallet.SignAndSendTX(OP_CREATE_ASSET_TOKEN, AssetData)
}
/*发行人 可以领取累计的手续费*/
func ClaimFees(symbol string, value float64) (string, error) {
asset_info := rpc.GetTokenInfoBySymbol(symbol)
precision := math.Pow10(asset_info.Precision)
ctf := &ClaimTokenFees{
Extensions: []interface{}{},
Issuer: ObjectId(Wallet.Default.Info.ID),
AmountToClaim: Amount{Amount: uint64(float64(value) * precision), AssetID: ObjectId(asset_info.ID)},
}
return Wallet.SignAndSendTX(OP_CLAIM_FEES, ctf)
}
/*注资手续费池*/
func TokenFundFeePool(symbol string, amount float64) (string, error) {
asset_info := rpc.GetTokenInfoBySymbol(symbol)
precision := math.Pow10(asset_info.Precision)
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
feePool := &TokenFeePoolData{
AssetID: ObjectId(asset_info.ID),
FromAccount: ObjectId(Wallet.Default.Info.ID),
Amount: uint64(float64(amount) * precision),
Extensions: []interface{}{},
}
return Wallet.SignAndSendTX(OP_FUND_FEEPOOL, feePool)
}
func Pledgegas(beneficiary string, collateral float64) (string, error) {
//m_info := rpc.GetAccountInfoByName(mortgager)
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
b_info := rpc.GetAccountInfoByName(beneficiary)
tk_info := rpc.GetTokenInfo(COCOS_ID)
precision := math.Pow10(tk_info.Precision)
p := &PledgeGas{
Mortgager: ObjectId(Wallet.Default.Info.ID),
Beneficiary: ObjectId(b_info.ID),
Collateral: uint64(collateral * precision),
}
return Wallet.SignAndSendTX(OP_PLEDGE_GAS, p)
}
/*
func CreateVestingBalance(symbol string, amount float64) error {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
asset_info := rpc.GetTokenInfoBySymbol(symbol)
precision := math.Pow10(asset_info.Precision)
p := Policy{
ID: 1,
StartClaim: GetExpiration(),
VestingSeconds: 0,
}
v := &VestingBalanceCreate{
Owner: ObjectId(Wallet.Default.Info.ID),
Amount: Amount{Amount: uint64(amount * precision), AssetID: asset_info.ID},
Policy: p,
Creator: ObjectId(Wallet.Default.Info.ID),
}
return Wallet.SignAndSendTX(OP_VESTING_CREATE, v)
}*/
func WithdrawVestingBalance(balance_id string) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
balances := GetVestingBalances(Wallet.Default.Name)
var balance_info rpc.VestingBalances
for _, balance := range balances {
if balance.ID == balance_id {
balance_info = balance
break
}
}
v := &VestingBalanceWithdraw{
VestingBalance: ObjectId(balance_id),
Owner: ObjectId(Wallet.Default.Info.ID),
Amount: Amount{AssetID: ObjectId(balance_info.Balance.AssetID), Amount: balance_info.GetBalanceAmount()},
}
return Wallet.SignAndSendTX(OP_VESTING_WITHDRAW, v)
}
/*发币*/
func IssueToken(symbol, issue_to_account string, amount float64) (string, error) {
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
to_info := rpc.GetAccountInfoByName(issue_to_account)
asset_info := rpc.GetTokenInfoBySymbol(symbol)
precision := math.Pow10(asset_info.Precision)
issue := &IssueAsset{
Extensions: []interface{}{},
Issuer: ObjectId(Wallet.Default.Info.ID),
IssueToAccount: ObjectId(to_info.ID),
AssetToIssue: Amount{Amount: uint64(amount * precision), AssetID: ObjectId(asset_info.ID)},
}
return Wallet.SignAndSendTX(OP_ISSUE_TOKEN, issue)
}
//投票
func Vote(id string, value float64) (tx_hash string, err error) {
tk_info := rpc.GetTokenInfo(COCOS_ID)
precision := math.Pow10(tk_info.Precision)
info := rpc.GetObject(id)
if info == nil {
return tx_hash, errors.New("vote error:get vote_id error!!")
}
vote_id := info.Get("vote_id").String()
if vote_id == "" {
return tx_hash, errors.New("vote error:get vote_id error!!")
}
if Wallet.Default.Info == nil {
Wallet.Default.Info = rpc.GetAccountInfoByName(Wallet.Default.Name)
}
v := &VoteData{
LockWithVote: OPArray{Int32(0), Amount{Amount: uint64(value * precision), AssetID: COCOS_ID}},
Account: ObjectId(Wallet.Default.Info.ID),
NewOptions: NewOptions{
MemoKey: Wallet.Default.GetMemoKey().GetPublicKey().ToBase58String(),
Votes: Array{VoteId(vote_id)},
Extensions: Extensions{}},
Extensions: Extensions{},
}
if value == 0 {
v.NewOptions.Votes = Array{}
}
return Wallet.SignAndSendTX(OP_VOTE, v, Wallet.Default.GetActiveKey(), Wallet.Default.GetOwnerKey())
}
/*查询订单信息*/
func GetNhAssetOrderInfo(id string) *rpc.NhAssetOrderInfo {
return rpc.GetNhAssetOrderInfo(id)
}
/*查询owner订单列表*/
func GetAccountNhAssetOrderList(owner_name string, page, page_size int) *rpc.OrdersList {
return rpc.GetAccountNhAssetOrderList(owner_name, page, page_size)
}
/*查询订单列表*/
func GetNhAssetOrderList(asset_name, world_view string, page, page_size int) *rpc.OrdersList {
return rpc.GetNhAssetOrderList(asset_name, world_view, page, page_size)
}
/*查询NH 资产列表*/
func GetNhAssetList(acc_name string, page, page_size, _type int, world_view ...string) *rpc.AssetsList {
return rpc.GetNhAssetList(acc_name, page, page_size, _type, world_view)
}
/*查询账户Balance*/
func GetAccountBalances(acc_name string) *[]rpc.Balance {
acc_info := rpc.GetAccountInfoByName(acc_name)
if acc_info == nil {
return nil
}
return rpc.GetAccountBalances(acc_info.ID)
}
/*查询链上所有token信息*/
func GetAllTokenInfo() []*rpc.TokenInfo {
return rpc.QueryTokenList()
}
/*查询收到的所有提议*/
func GetAllProposals(acct_id string) *[]rpc.Proposal {
return rpc.GetProposals(acct_id)
}
/*查询 某条提议*/
func GetAllProposal(proposal_id string) *[]rpc.Proposal {
return rpc.GetProposals(proposal_id)
}
/*通过Symbol查询token信息*/
func GetTokenInfoBySymbol(symbol string) *rpc.TokenInfo {
return rpc.GetTokenInfoBySymbol(symbol)
}
/*通过id查询token信息*/
func GetTokenInfoById(id string) *rpc.TokenInfo {
return rpc.GetTokenInfo(id)
}
/*查询账户待提取的奖励*/
func GetVestingBalances(acct_name ...string) []rpc.VestingBalances {
if len(acct_name) < 1 {
return rpc.GetVestingBalancesByName(Wallet.Default.Name)
}
return rpc.GetVestingBalancesByName(acct_name[0])
}
/*查询账户操作记录*/
func GetAccountHistorys(acct_name string) rpc.History {
return rpc.GetAccountHistory(acct_name)
}
/*获取市场限价单交易历史*/
func GetFillOrderHistory(asset_id, _asset_id string, limit uint64) []interface{} {
return rpc.GetFillOrderHistory(asset_id, _asset_id, limit)
}
/*查询某个时间段的交易市场行情。*/
func GetMarketHistory(asset_id, _asset_id, start, end string, limit uint64) []interface{} {
return rpc.GetMarketHistory(asset_id, _asset_id, start, end, limit)
}