-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
157 lines (130 loc) · 3.88 KB
/
contract.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
package api
import (
"context"
"strings"
"github.com/bytom/bystack/contract"
"github.com/bytom/bystack/crypto/sha3pool"
chainjson "github.com/bytom/bystack/encoding/json"
"github.com/bytom/bystack/errors"
"github.com/bytom/bystack/protocol/bc"
"github.com/bytom/bystack/protocol/vm/vmutil"
)
// pre-define errors for supporting bytom errorFormatter
var (
ErrNullContract = errors.New("contract is empty")
ErrNullContractID = errors.New("contract id is empty")
ErrNullContractAlias = errors.New("contract alias is empty")
)
// POST /create-asset
func (a *API) createContract(_ context.Context, ins struct {
Alias string `json:"alias"`
Contract chainjson.HexBytes `json:"contract"`
}) Response {
ins.Alias = strings.TrimSpace(ins.Alias)
if ins.Alias == "" {
return NewErrorResponse(ErrNullContractAlias)
}
if ins.Contract == nil {
return NewErrorResponse(ErrNullContract)
}
var hash [32]byte
sha3pool.Sum256(hash[:], ins.Contract)
registerProgram, err := vmutil.RegisterProgram(ins.Contract)
if err != nil {
return NewErrorResponse(err)
}
callProgram, err := vmutil.CallContractProgram(hash[:])
if err != nil {
return NewErrorResponse(err)
}
c := &contract.Contract{
Hash: hash[:],
Alias: ins.Alias,
Contract: ins.Contract,
CallProgram: callProgram,
RegisterProgram: registerProgram,
}
if err := a.wallet.ContractReg.SaveContract(c); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(c)
}
// POST /update-contract-alias
func (a *API) updateContractAlias(_ context.Context, ins struct {
ID chainjson.HexBytes `json:"id"`
Alias string `json:"alias"`
}) Response {
if ins.ID == nil {
return NewErrorResponse(ErrNullContractID)
}
ins.Alias = strings.TrimSpace(ins.Alias)
if ins.Alias == "" {
return NewErrorResponse(ErrNullContractAlias)
}
if err := a.wallet.ContractReg.UpdateContract(ins.ID, ins.Alias); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
}
// POST /get-contract
func (a *API) getContract(_ context.Context, ins struct {
ID chainjson.HexBytes `json:"id"`
}) Response {
if ins.ID == nil {
return NewErrorResponse(ErrNullContractID)
}
c, err := a.wallet.ContractReg.GetContract(ins.ID)
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(c)
}
// POST /list-contracts
func (a *API) listContracts(_ context.Context) Response {
cs, err := a.wallet.ContractReg.ListContracts()
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(cs)
}
type ContractInstance struct {
UTXOs []*contract.UTXO `json:"utxos"`
TxHash *bc.Hash `json:"tx_hash"`
Status contract.Status `json:"status"`
Unconfirmed []*contract.TreeNode `json:"unconfirmed"`
}
func (a *API) getContractInstance(_ context.Context, ins struct {
TraceID string `json:"trace_id"`
}) Response {
instance, err := a.contractTracer.GetInstance(ins.TraceID)
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(&ContractInstance{
UTXOs: instance.UTXOs,
TxHash: instance.TxHash,
Status: instance.Status,
Unconfirmed: instance.Unconfirmed,
})
}
func (a *API) createContractInstance(_ context.Context, ins struct {
TxHash chainjson.HexBytes `json:"tx_hash"`
BlockHash chainjson.HexBytes `json:"block_hash"`
}) Response {
var txHash, blockHash [32]byte
copy(txHash[:], ins.TxHash)
copy(blockHash[:], ins.BlockHash)
traceIDs, err := a.contractTracer.CreateInstance(bc.NewHash(txHash), bc.NewHash(blockHash))
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(traceIDs)
}
func (a *API) removeContractInstance(_ context.Context, ins struct {
TraceID string `json:"trace_id"`
}) Response {
if err := a.contractTracer.RemoveInstance(ins.TraceID); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
}