-
Notifications
You must be signed in to change notification settings - Fork 2
/
contract.go
92 lines (79 loc) · 2.54 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
package service
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/frostornge/terra-go/httpclient"
"github.com/cosmos/cosmos-sdk/codec"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
terrawasm "github.com/terra-project/core/x/wasm"
)
//go:generate mockgen -destination ../../../test/mocks/terra/service/service_contract.go . ContractService
type ContractService interface {
GetCodeID(ctx context.Context, codeId uint64) (terrawasm.CodeInfo, error)
GetContractInfo(ctx context.Context, addr cosmostypes.AccAddress) (terrawasm.ContractInfo, error)
QueryContractStore(ctx context.Context, addr cosmostypes.AccAddress, query interface{}, resp interface{}) error
}
type contractService struct {
codec *codec.Codec
client httpclient.Client
}
func NewContractService(client httpclient.Client) ContractService {
return contractService{codec: client.Codec(), client: client}
}
func (svc contractService) GetCodeID(ctx context.Context, codeId uint64) (terrawasm.CodeInfo, error) {
var payload = httpclient.RequestPayload{
Context: ctx,
Method: http.MethodGet,
Path: fmt.Sprintf("/wasm/codes/%d", codeId),
}
var body struct {
Height string `json:"height"`
Result terrawasm.CodeInfo `json:"result"`
}
if err := svc.client.RequestJSON(payload, &body); err != nil {
return terrawasm.CodeInfo{}, errors.Wrap(err, "request json")
}
return body.Result, nil
}
func (svc contractService) GetContractInfo(
ctx context.Context,
addr cosmostypes.AccAddress,
) (terrawasm.ContractInfo, error) {
var payload = httpclient.RequestPayload{
Context: ctx,
Method: http.MethodGet,
Path: fmt.Sprintf("/wasm/contracts/%s", addr.String()),
}
var body struct {
Height string `json:"height"`
Result terrawasm.ContractInfo `json:"result"`
}
if err := svc.client.RequestJSON(payload, &body); err != nil {
return terrawasm.ContractInfo{}, errors.Wrap(err, "request json")
}
return body.Result, nil
}
func (svc contractService) QueryContractStore(
ctx context.Context,
addr cosmostypes.AccAddress,
query interface{},
resp interface{},
) error {
jsonQuery, err := json.Marshal(query)
if err != nil {
return errors.Wrap(err, "marshal query message")
}
var payload = httpclient.RequestPayload{
Context: ctx,
Method: http.MethodGet,
Path: fmt.Sprintf("/wasm/contracts/%s/store", addr.String()),
Query: map[string]string{"query_msg": string(jsonQuery)},
}
if err := svc.client.RequestJSON(payload, resp); err != nil {
return errors.Wrap(err, "request json")
}
return nil
}