-
Notifications
You must be signed in to change notification settings - Fork 8
/
wasm.go
129 lines (112 loc) · 4.12 KB
/
wasm.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
package ibctesting
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"os"
"strings"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
"github.com/terpnetwork/terp-core/x/wasm/types"
)
var wasmIdent = []byte("\x00\x61\x73\x6D")
// SeedNewContractInstance stores some wasm code and instantiates a new contract on this chain.
// This method can be called to prepare the store with some valid CodeInfo and ContractInfo. The returned
// Address is the contract address for this instance. Test should make use of this data and/or use NewIBCContractMockWasmer
// for using a contract mock in Go.
func (chain *TestChain) SeedNewContractInstance() sdk.AccAddress {
pInstResp := chain.StoreCode(append(wasmIdent, rand.Bytes(10)...))
codeID := pInstResp.CodeID
anyAddressStr := chain.SenderAccount.GetAddress().String()
initMsg := []byte(fmt.Sprintf(`{"verifier": %q, "beneficiary": %q}`, anyAddressStr, anyAddressStr))
return chain.InstantiateContract(codeID, initMsg)
}
func (chain *TestChain) StoreCodeFile(filename string) types.MsgStoreCodeResponse {
wasmCode, err := os.ReadFile(filename)
require.NoError(chain.t, err)
if strings.HasSuffix(filename, "wasm") { // compress for gas limit
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
_, err := gz.Write(wasmCode)
require.NoError(chain.t, err)
err = gz.Close()
require.NoError(chain.t, err)
wasmCode = buf.Bytes()
}
return chain.StoreCode(wasmCode)
}
func (chain *TestChain) StoreCode(byteCode []byte) types.MsgStoreCodeResponse {
storeMsg := &types.MsgStoreCode{
Sender: chain.SenderAccount.GetAddress().String(),
WASMByteCode: byteCode,
}
r, err := chain.SendMsgs(storeMsg)
require.NoError(chain.t, err)
// unmarshal protobuf response from data
require.Len(chain.t, r.MsgResponses, 1)
require.NotEmpty(chain.t, r.MsgResponses[0].GetCachedValue())
pInstResp := r.MsgResponses[0].GetCachedValue().(*types.MsgStoreCodeResponse)
require.NotEmpty(chain.t, pInstResp.CodeID)
require.NotEmpty(chain.t, pInstResp.Checksum)
return *pInstResp
}
func (chain *TestChain) InstantiateContract(codeID uint64, initMsg []byte) sdk.AccAddress {
instantiateMsg := &types.MsgInstantiateContract{
Sender: chain.SenderAccount.GetAddress().String(),
Admin: chain.SenderAccount.GetAddress().String(),
CodeID: codeID,
Label: "ibc-test",
Msg: initMsg,
Funds: sdk.Coins{ibctesting.TestCoin},
}
r, err := chain.SendMsgs(instantiateMsg)
require.NoError(chain.t, err)
require.Len(chain.t, r.MsgResponses, 1)
require.NotEmpty(chain.t, r.MsgResponses[0].GetCachedValue())
pExecResp := r.MsgResponses[0].GetCachedValue().(*types.MsgInstantiateContractResponse)
a, err := sdk.AccAddressFromBech32(pExecResp.Address)
require.NoError(chain.t, err)
return a
}
// SmartQuery This will serialize the query message and submit it to the contract.
// The response is parsed into the provided interface.
// Usage: SmartQuery(addr, QueryMsg{Foo: 1}, &response)
func (chain *TestChain) SmartQuery(contractAddr string, queryMsg interface{}, response interface{}) error {
msg, err := json.Marshal(queryMsg)
if err != nil {
return err
}
req := types.QuerySmartContractStateRequest{
Address: contractAddr,
QueryData: msg,
}
reqBin, err := proto.Marshal(&req)
if err != nil {
return err
}
// TODO: what is the query?
res := chain.App.Query(abci.RequestQuery{
Path: "/cosmwasm.wasm.v1.Query/SmartContractState",
Data: reqBin,
})
if res.Code != 0 {
return fmt.Errorf("query failed: (%d) %s", res.Code, res.Log)
}
// unpack protobuf
var resp types.QuerySmartContractStateResponse
err = proto.Unmarshal(res.Value, &resp)
if err != nil {
return err
}
// unpack json content
return json.Unmarshal(resp.Data, response)
}
// ContractInfo is a helper function to returns the ContractInfo for the given contract address
func (chain *TestChain) ContractInfo(contractAddr sdk.AccAddress) *types.ContractInfo {
return chain.App.WasmKeeper.GetContractInfo(chain.GetContext(), contractAddr)
}