-
Notifications
You must be signed in to change notification settings - Fork 170
/
wasm.go
94 lines (80 loc) · 2.35 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
package tx
import (
"fmt"
"os"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
appparams "github.com/umee-network/umee/v5/app/params"
"github.com/umee-network/umee/v5/util/coin"
)
func (c *Client) TxSubmitWasmContract(contractPath string) (*sdk.TxResponse, error) {
fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}
msg, err := readWasmCode(contractPath, fromAddr)
if err != nil {
return nil, err
}
return c.BroadcastTx(&msg)
}
func (c *Client) TxWasmInstantiateContract(storeCode uint64, initMsg []byte) (*sdk.TxResponse, error) {
fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}
amount := sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, sdk.NewInt(1)))
msg := types.MsgInstantiateContract{
Sender: fromAddr.String(),
CodeID: storeCode,
Label: "label",
Funds: amount,
Msg: initMsg,
Admin: "",
}
return c.BroadcastTx(&msg)
}
func (c *Client) TxWasmExecuteContractByAccSeq(contractAddr string, execMsg []byte,
accSeq uint64) (*sdk.TxResponse, error) {
fromAddr, err := c.keyringRecord[0].GetAddress()
if err != nil {
return nil, err
}
amount := sdk.NewCoins(coin.Umee1)
msg := types.MsgExecuteContract{
Sender: fromAddr.String(),
Contract: contractAddr,
Funds: amount,
Msg: execMsg,
}
if accSeq != 0 {
return c.WithAccSeq(accSeq).WithAsyncBlock().BroadcastTx(&msg)
}
return c.WithAsyncBlock().BroadcastTx(&msg)
}
func (c *Client) TxWasmExecuteContract(contractAddr string, execMsg []byte) (*sdk.TxResponse, error) {
return c.TxWasmExecuteContractByAccSeq(contractAddr, execMsg, 0)
}
// Prepares MsgStoreCode object from flags with gzipped wasm byte code field
func readWasmCode(file string, sender sdk.AccAddress) (types.MsgStoreCode, error) {
wasm, err := os.ReadFile(file)
if err != nil {
return types.MsgStoreCode{}, err
}
// gzip the wasm file
if ioutils.IsWasm(wasm) {
wasm, err = ioutils.GzipIt(wasm)
if err != nil {
return types.MsgStoreCode{}, err
}
} else if !ioutils.IsGzip(wasm) {
return types.MsgStoreCode{}, fmt.Errorf("invalid input file. Use wasm binary or gzip")
}
msg := types.MsgStoreCode{
Sender: sender.String(),
WASMByteCode: wasm,
InstantiatePermission: &types.AllowEverybody,
}
return msg, nil
}