-
Notifications
You must be signed in to change notification settings - Fork 168
/
wasm.go
82 lines (70 loc) · 2.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
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/v6/app/params"
"github.com/umee-network/umee/v6/util/coin"
)
func (c *Client) WasmDeployContract(contractPath string) (*sdk.TxResponse, error) {
fromIdx := 0
msg, err := readWasmCode(contractPath, c.KeyringAddress(fromIdx))
if err != nil {
return nil, err
}
return c.BroadcastTx(fromIdx, &msg)
}
func (c *Client) WasmInitContract(storeCode uint64, initMsg []byte) (*sdk.TxResponse, error) {
fromIdx := 0
amount := sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, sdk.NewInt(1)))
msg := types.MsgInstantiateContract{
Sender: c.KeyringAddress(fromIdx).String(),
CodeID: storeCode,
Label: "label",
Funds: amount,
Msg: initMsg,
Admin: "",
}
return c.BroadcastTx(fromIdx, &msg)
}
func (c *Client) WasmExecContractWithAccSeq(contractAddr string, execMsg []byte, accSeq uint64,
) (*sdk.TxResponse, error) {
fromIdx := 0
amount := sdk.NewCoins(coin.Umee1)
msg := types.MsgExecuteContract{
Sender: c.KeyringAddress(fromIdx).String(),
Contract: contractAddr,
Funds: amount,
Msg: execMsg,
}
if accSeq != 0 {
c.SetAccSeq(accSeq)
}
return c.BroadcastTx(fromIdx, &msg)
}
func (c *Client) WasmExecuteContract(contractAddr string, execMsg []byte) (*sdk.TxResponse, error) {
return c.WasmExecContractWithAccSeq(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
}
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. Wasm file must be a binary or gzip of a binary")
}
return types.MsgStoreCode{
Sender: sender.String(),
WASMByteCode: wasm,
InstantiatePermission: &types.AllowEverybody,
}, nil
}