This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 361
/
template.go
601 lines (505 loc) · 24.2 KB
/
template.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package commands
import (
"fmt"
"github.com/bytom/errors"
)
const (
// contract TradeOffer's clause
clauseTrade = "00000000"
clauseCancel = "13000000"
tradeOfferEnding = "1a000000"
// contract Escrow's clause
clauseApprove = "00000000"
clauseReject = "1a000000"
escrowEnding = "28000000"
// contract LoanCollateral's clause
clauseRepay = "00000000"
clauseDefault = "1b000000"
loanCollateralEnding = "26000000"
// contract CallOption's clause
clauseExercise string = "00000000"
clauseExpire string = "20000000"
callOptionEnding string = "2c000000"
)
var (
errBadContractArguments = errors.New("bad contract arguments")
)
// common receiver template, the clause takes only one parameter without Signature in the contract that contains only one clause,
// or the clause takes no parameters in the contract with multiple clauses. In addition, clause's statement like "lock payment with program" or "unlock value"
var buildCommonRecvReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": %s, "account_id": "%s"}
]}`
var buildCommonRecvReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is LockWithPublicKey
var buildLockWithPublicKeyReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": %s, "account_id": "%s"}
]}`
var buildLockWithPublicKeyReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is LockWithMultiSig
var buildLockWithMultiSigReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": %s, "account_id": "%s"}
]}`
var buildLockWithMultiSigReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is LockWithPublicKeyHash
var buildLockWithPublicKeyHashReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}},
{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": %s, "account_id": "%s"}
]}`
var buildLockWithPublicKeyHashReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}},
{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is TradeOffer's clause trade, the code of clause contains only two statement with "lock payment with program" and "unlock value"
var buildTradeOfferClauseTradeReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "%s", "amount": %s, "account_id": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"}
]}`
var buildTradeOfferClauseTradeReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "%s", "amount": %s, "account_alias": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"}
]}`
// contract is TradeOffer's clause cancel
var buildTradeOfferClauseCancelReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": %s, "account_id": "%s"}
]}`
var buildTradeOfferClauseCancelReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is Escrow
var buildEscrowReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id": "%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"}
]}`
var buildEscrowReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is LoanCollateral's clause repay, the code of clause contains only two statement with "lock payment with program" and "lock value with control_program"
var buildLoanCollateralClauseRepayReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "%s", "amount": %s, "account_id": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"}
]}`
var buildLoanCollateralClauseRepayReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "%s", "amount": %s, "account_alias": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"}
]}`
// contract is CallOption's clause exercise, the code of clause contains only two statement with "lock payment with program" and "unlock value"
var buildCallOptionClauseExerciseReqFmt = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_id": "%s", "amount": %s, "account_id": "%s"},
{"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":%s, "account_id": "%s"},
{"type": "control_program", "asset_id": "%s", "amount": %s, "control_program": "%s"}
]}`
var buildCallOptionClauseExerciseReqFmtByAlias = `
{"actions": [
{"type": "spend_account_unspent_output", "output_id":"%s", "arguments": [{"type": "raw_tx_signature", "raw_data": {"xpub": "%s", "derivation_path": ["%s", "%s"]}},
{"type": "data", "raw_data": {"value": "%s"}}]},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"},
{"type": "spend_account", "asset_alias": "%s", "amount": %s, "account_alias": "%s"},
{"type": "spend_account", "asset_alias": "BTM", "amount": %s, "account_alias": "%s"},
{"type": "control_program", "asset_alias": "%s", "amount": %s, "control_program": "%s"}
]}`
// contract arguments
type baseContractArg struct {
accountInfo string
assetInfo string
amount string
alias bool
program string
btmGas string
outputID string
}
type basePubInfo struct {
rootPub string
path1 string
path2 string
}
type innerContractArg struct {
innerAccountInfo string
innerAssetInfo string
innerAmount string
innerProgram string
}
// addContractArgs add arguments for template contracts
func addContractArgs(contractName string, baseArg baseContractArg, specArgs []string, usage string) (buildReqStr string, err error) {
switch contractName {
case "LockWithPublicKey":
if len(specArgs) != 3 {
err = errors.WithDetailf(errBadContractArguments, "%s <rootPub> <path1> <path2> [flags]\n", usage)
return
}
pubInfo := basePubInfo{
rootPub: specArgs[0],
path1: specArgs[1],
path2: specArgs[2],
}
buildReqStr = addLockWithPublicKeyArg(baseArg, pubInfo)
case "LockWithMultiSig":
if len(specArgs) != 6 {
err = errors.WithDetailf(errBadContractArguments, "%s <rootPub1> <path11> <path12> <rootPub2> <path21> <path22> [flags]\n", usage)
return
}
pubInfos := [2]basePubInfo{
{
rootPub: specArgs[0],
path1: specArgs[1],
path2: specArgs[2],
},
{
rootPub: specArgs[3],
path1: specArgs[4],
path2: specArgs[5],
},
}
buildReqStr = addLockWithMultiSigArg(baseArg, pubInfos)
case "LockWithPublicKeyHash":
if len(specArgs) != 4 {
err = errors.WithDetailf(errBadContractArguments, "%s <pubKey> <rootPub> <path1> <path2> [flags]\n", usage)
return
}
pubkey := specArgs[0]
pubInfo := basePubInfo{
rootPub: specArgs[1],
path1: specArgs[2],
path2: specArgs[3],
}
buildReqStr = addLockWithPublicKeyHashArg(baseArg, pubInfo, pubkey)
case "RevealPreimage":
if len(specArgs) != 1 {
err = errors.WithDetailf(errBadContractArguments, "%s <value> [flags]\n", usage)
return
}
value := specArgs[0]
buildReqStr = addRevealPreimageArg(baseArg, value)
case "TradeOffer":
switch {
case len(specArgs) <= 0:
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> (<innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram>) | (<rootPub> <path1> <path2>) [flags]\n", usage)
case specArgs[0] == clauseTrade:
if len(specArgs) != 5 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram> [flags]\n", usage)
return
}
inner := &innerContractArg{
innerAccountInfo: specArgs[1],
innerAssetInfo: specArgs[2],
innerAmount: specArgs[3],
innerProgram: specArgs[4],
}
buildReqStr, err = addTradeOfferArg(baseArg, clauseTrade, inner, nil)
case specArgs[0] == clauseCancel:
if len(specArgs) != 4 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <rootPub> <path1> <path2> [flags]\n", usage)
return
}
pubInfo := &basePubInfo{
rootPub: specArgs[1],
path1: specArgs[2],
path2: specArgs[3],
}
buildReqStr, err = addTradeOfferArg(baseArg, clauseCancel, nil, pubInfo)
case specArgs[0] == tradeOfferEnding:
err = errors.WithDetailf(errBadContractArguments, "Clause ending was selected in contract %s, ending exit\n", contractName)
default:
err = errors.WithDetailf(errBadContractArguments, "selected clause [%s] error, contract %s's clause must in set [%s, %s, %s]\n",
specArgs[0], contractName, clauseTrade, clauseCancel, tradeOfferEnding)
}
case "Escrow":
switch {
case len(specArgs) <= 0:
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <rootPub> <path1> <path2> <controlProgram> [flags]\n", usage)
case specArgs[0] == clauseApprove || specArgs[0] == clauseReject:
if len(specArgs) != 5 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <rootPub> <path1> <path2> <controlProgram> [flags]\n", usage)
return
}
pubInfo := &basePubInfo{
rootPub: specArgs[1],
path1: specArgs[2],
path2: specArgs[3],
}
controlProgram := specArgs[4]
buildReqStr, err = addEscrowArg(baseArg, specArgs[0], pubInfo, controlProgram)
case specArgs[0] == escrowEnding:
err = errors.WithDetailf(errBadContractArguments, "Clause ending was selected in contract %s, ending exit\n", contractName)
default:
err = errors.WithDetailf(errBadContractArguments, "selected clause [%s] error, contract %s's clause must in set [%s, %s, %s]\n",
specArgs[0], contractName, clauseApprove, clauseReject, escrowEnding)
}
case "LoanCollateral":
switch {
case len(specArgs) <= 0:
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> (<innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram> <controlProgram>) | (<controlProgram>) [flags]\n", usage)
case specArgs[0] == clauseRepay:
if len(specArgs) != 6 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram> <controlProgram> [flags]\n", usage)
return
}
inner := &innerContractArg{
innerAccountInfo: specArgs[1],
innerAssetInfo: specArgs[2],
innerAmount: specArgs[3],
innerProgram: specArgs[4],
}
controlProgram := specArgs[5]
buildReqStr, err = addLoanCollateralArg(baseArg, specArgs[0], inner, controlProgram)
case specArgs[0] == clauseDefault:
if len(specArgs) != 2 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <controlProgram> [flags]\n", usage)
return
}
controlProgram := specArgs[1]
buildReqStr, err = addLoanCollateralArg(baseArg, specArgs[0], nil, controlProgram)
case specArgs[0] == loanCollateralEnding:
err = errors.WithDetailf(errBadContractArguments, "Clause ending was selected in contract %s, ending exit\n", contractName)
default:
err = errors.WithDetailf(errBadContractArguments, "selected clause [%s] error, contract %s's clause must in set:[%s, %s, %s]\n",
specArgs[0], contractName, clauseRepay, clauseDefault, loanCollateralEnding)
}
case "CallOption":
switch {
case len(specArgs) <= 0:
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> (<innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram> <rootPub> <path1> <path2>) | (<controlProgram>) [flags]\n", usage)
case specArgs[0] == clauseExercise:
if len(specArgs) != 8 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <innerAccountID|alias> <innerAssetID|alias> <innerAmount> <innerProgram> <rootPub> <path1> <path2> [flags]\n", usage)
return
}
inner := &innerContractArg{
innerAccountInfo: specArgs[1],
innerAssetInfo: specArgs[2],
innerAmount: specArgs[3],
innerProgram: specArgs[4],
}
pubInfo := &basePubInfo{
rootPub: specArgs[5],
path1: specArgs[6],
path2: specArgs[7],
}
buildReqStr, err = addCallOptionArg(baseArg, specArgs[0], inner, pubInfo, "")
case specArgs[0] == clauseExpire:
if len(specArgs) != 2 {
err = errors.WithDetailf(errBadContractArguments, "%s <clauseSelector> <controlProgram> [flags]\n", usage)
return
}
controlProgram := specArgs[1]
buildReqStr, err = addCallOptionArg(baseArg, specArgs[0], nil, nil, controlProgram)
case specArgs[0] == callOptionEnding:
err = errors.WithDetailf(errBadContractArguments, "Clause ending was selected in contract %s, ending exit\n", contractName)
default:
err = errors.WithDetailf(errBadContractArguments, "selected clause [%s] error, contract %s's clause must in set:[%s, %s, %s]\n",
specArgs[0], contractName, clauseExercise, clauseExpire, callOptionEnding)
}
default:
err = errors.WithDetailf(errBadContractArguments, "Invalid contract template name [%s]", contractName)
}
return
}
func addLockWithPublicKeyArg(baseArg baseContractArg, pubInfo basePubInfo) (buildReqStr string) {
buildReqFmt := buildLockWithPublicKeyReqFmt
if baseArg.alias {
buildReqFmt = buildLockWithPublicKeyReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubInfo.rootPub, pubInfo.path1, pubInfo.path2,
baseArg.assetInfo, baseArg.amount, baseArg.program, baseArg.btmGas, baseArg.accountInfo)
return
}
func addLockWithMultiSigArg(baseArg baseContractArg, pubInfos [2]basePubInfo) (buildReqStr string) {
buildReqFmt := buildLockWithMultiSigReqFmt
if baseArg.alias {
buildReqFmt = buildLockWithMultiSigReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubInfos[0].rootPub, pubInfos[0].path1, pubInfos[0].path2,
pubInfos[1].rootPub, pubInfos[1].path1, pubInfos[1].path2,
baseArg.assetInfo, baseArg.amount, baseArg.program, baseArg.btmGas, baseArg.accountInfo)
return
}
func addLockWithPublicKeyHashArg(baseArg baseContractArg, pubInfo basePubInfo, pubkey string) (buildReqStr string) {
buildReqFmt := buildLockWithPublicKeyHashReqFmt
if baseArg.alias {
buildReqFmt = buildLockWithPublicKeyHashReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubkey, pubInfo.rootPub, pubInfo.path1, pubInfo.path2,
baseArg.assetInfo, baseArg.amount, baseArg.program, baseArg.btmGas, baseArg.accountInfo)
return
}
func addRevealPreimageArg(baseArg baseContractArg, value string) (buildReqStr string) {
buildReqFmt := buildCommonRecvReqFmt
if baseArg.alias {
buildReqFmt = buildCommonRecvReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, value,
baseArg.assetInfo, baseArg.amount, baseArg.program, baseArg.btmGas, baseArg.accountInfo)
return
}
func addTradeOfferArg(baseArg baseContractArg, selector string, innerArg *innerContractArg, pubInfo *basePubInfo) (buildReqStr string, err error) {
switch selector {
case clauseTrade:
if innerArg == nil {
err = errors.New("Contract TradeOffer's clause trade argument is nil")
return
}
buildReqFmt := buildTradeOfferClauseTradeReqFmt
if baseArg.alias {
buildReqFmt = buildTradeOfferClauseTradeReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, selector,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerProgram,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerAccountInfo,
baseArg.btmGas, baseArg.accountInfo,
baseArg.assetInfo, baseArg.amount, baseArg.program)
case clauseCancel:
if pubInfo == nil {
err = errors.New("Contract TradeOffer's clause cancel argument is nil")
return
}
buildReqFmt := buildTradeOfferClauseCancelReqFmt
if baseArg.alias {
buildReqFmt = buildTradeOfferClauseCancelReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubInfo.rootPub, pubInfo.path1, pubInfo.path2, selector,
baseArg.assetInfo, baseArg.amount, baseArg.program, baseArg.btmGas, baseArg.accountInfo)
default:
err = errors.New("Invalid contract clause selector")
}
return
}
func addEscrowArg(baseArg baseContractArg, selector string, pubInfo *basePubInfo, controlProgram string) (buildReqStr string, err error) {
switch selector {
case clauseApprove, clauseReject:
if pubInfo == nil {
err = errors.New("Contract Escrow's clause argument is nil")
return
}
buildReqFmt := buildEscrowReqFmt
if baseArg.alias {
buildReqFmt = buildEscrowReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubInfo.rootPub, pubInfo.path1, pubInfo.path2, selector,
baseArg.assetInfo, baseArg.amount, controlProgram, baseArg.btmGas, baseArg.accountInfo)
default:
err = errors.New("Invalid contract clause selector")
}
return
}
func addLoanCollateralArg(baseArg baseContractArg, selector string, innerArg *innerContractArg, controlProgram string) (buildReqStr string, err error) {
switch selector {
case clauseRepay:
if innerArg == nil {
err = errors.New("Contract LoanCollateral's clause repay argument is nil")
return
}
buildReqFmt := buildLoanCollateralClauseRepayReqFmt
if baseArg.alias {
buildReqFmt = buildLoanCollateralClauseRepayReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, selector,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerProgram,
baseArg.assetInfo, baseArg.amount, controlProgram,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerAccountInfo,
baseArg.btmGas, baseArg.accountInfo)
case clauseDefault:
buildReqFmt := buildCommonRecvReqFmt
if baseArg.alias {
buildReqFmt = buildCommonRecvReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, selector,
baseArg.assetInfo, baseArg.amount, controlProgram, baseArg.btmGas, baseArg.accountInfo)
default:
err = errors.New("Invalid contract clause selector")
}
return
}
func addCallOptionArg(baseArg baseContractArg, selector string, innerArg *innerContractArg, pubInfo *basePubInfo, controlProgram string) (buildReqStr string, err error) {
switch selector {
case clauseExercise:
if innerArg == nil || pubInfo == nil {
err = errors.New("Contract CallOption's clause exercise argument is nil")
return
}
buildReqFmt := buildCallOptionClauseExerciseReqFmt
if baseArg.alias {
buildReqFmt = buildCallOptionClauseExerciseReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, pubInfo.rootPub, pubInfo.path1, pubInfo.path2, selector,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerProgram,
innerArg.innerAssetInfo, innerArg.innerAmount, innerArg.innerAccountInfo,
baseArg.btmGas, baseArg.accountInfo,
baseArg.assetInfo, baseArg.amount, baseArg.program)
case clauseExpire:
buildReqFmt := buildCommonRecvReqFmt
if baseArg.alias {
buildReqFmt = buildCommonRecvReqFmtByAlias
}
buildReqStr = fmt.Sprintf(buildReqFmt, baseArg.outputID, selector,
baseArg.assetInfo, baseArg.amount, controlProgram, baseArg.btmGas, baseArg.accountInfo)
default:
err = errors.New("Invalid contract clause selector")
}
return
}