github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/ibctesting/wasm.go (about)

     1  package ibctesting
     2  
     3  //import (
     4  //	"bytes"
     5  //	"compress/gzip"
     6  //	"encoding/json"
     7  //	"fmt"
     8  //	"io/ioutil"
     9  //	"strings"
    10  //
    11  //	wasmd "github.com/fibonacci-chain/fbc/app"
    12  //
    13  //	ibctesting "github.com/cosmos/ibc-go/v3/testing"
    14  //
    15  //	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    16  //	"github.com/golang/protobuf/proto" //nolint
    17  //	"github.com/stretchr/testify/require"
    18  //	abci "github.com/tendermint/tendermint/abci/types"
    19  //	"github.com/tendermint/tendermint/libs/rand"
    20  //
    21  //	"github.com/fibonacci-chain/fbc/x/wasm/types"
    22  //)
    23  //
    24  //var wasmIdent = []byte("\x00\x61\x73\x6D")
    25  //
    26  //// SeedNewContractInstance stores some wasm code and instantiates a new contract on this chain.
    27  //// This method can be called to prepare the store with some valid CodeInfo and ContractInfo. The returned
    28  //// Address is the contract address for this instance. Test should make use of this data and/or use NewIBCContractMockWasmer
    29  //// for using a contract mock in Go.
    30  //func (chain *TestChain) SeedNewContractInstance() sdk.AccAddress {
    31  //	pInstResp := chain.StoreCode(append(wasmIdent, rand.Bytes(10)...))
    32  //	codeID := pInstResp.CodeID
    33  //
    34  //	anyAddressStr := chain.SenderAccount.GetAddress().String()
    35  //	initMsg := []byte(fmt.Sprintf(`{"verifier": %q, "beneficiary": %q}`, anyAddressStr, anyAddressStr))
    36  //	return chain.InstantiateContract(codeID, initMsg)
    37  //}
    38  //
    39  //func (chain *TestChain) StoreCodeFile(filename string) types.MsgStoreCodeResponse {
    40  //	wasmCode, err := ioutil.ReadFile(filename)
    41  //	require.NoError(chain.t, err)
    42  //	if strings.HasSuffix(filename, "wasm") { // compress for gas limit
    43  //		var buf bytes.Buffer
    44  //		gz := gzip.NewWriter(&buf)
    45  //		_, err := gz.Write(wasmCode)
    46  //		require.NoError(chain.t, err)
    47  //		err = gz.Close()
    48  //		require.NoError(chain.t, err)
    49  //		wasmCode = buf.Bytes()
    50  //	}
    51  //	return chain.StoreCode(wasmCode)
    52  //}
    53  //
    54  //func (chain *TestChain) StoreCode(byteCode []byte) types.MsgStoreCodeResponse {
    55  //	storeMsg := &types.MsgStoreCode{
    56  //		Sender:       chain.SenderAccount.GetAddress().String(),
    57  //		WASMByteCode: byteCode,
    58  //	}
    59  //	r, err := chain.SendMsgs(storeMsg)
    60  //	require.NoError(chain.t, err)
    61  //	protoResult := chain.parseSDKResultData(r)
    62  //	require.Len(chain.t, protoResult.Data, 1)
    63  //	// unmarshal protobuf response from data
    64  //	var pInstResp types.MsgStoreCodeResponse
    65  //	require.NoError(chain.t, pInstResp.Unmarshal(protoResult.Data[0].Data))
    66  //	require.NotEmpty(chain.t, pInstResp.CodeID)
    67  //	return pInstResp
    68  //}
    69  //
    70  //func (chain *TestChain) InstantiateContract(codeID uint64, initMsg []byte) sdk.AccAddress {
    71  //	instantiateMsg := &types.MsgInstantiateContract{
    72  //		Sender: chain.SenderAccount.GetAddress().String(),
    73  //		Admin:  chain.SenderAccount.GetAddress().String(),
    74  //		CodeID: codeID,
    75  //		Label:  "ibc-test",
    76  //		Msg:    initMsg,
    77  //		Funds:  sdk.Coins{ibctesting.TestCoin},
    78  //	}
    79  //
    80  //	r, err := chain.SendMsgs(instantiateMsg)
    81  //	require.NoError(chain.t, err)
    82  //	protoResult := chain.parseSDKResultData(r)
    83  //	require.Len(chain.t, protoResult.Data, 1)
    84  //
    85  //	var pExecResp types.MsgInstantiateContractResponse
    86  //	require.NoError(chain.t, pExecResp.Unmarshal(protoResult.Data[0].Data))
    87  //	a, err := sdk.AccAddressFromBech32(pExecResp.Address)
    88  //	require.NoError(chain.t, err)
    89  //	return a
    90  //}
    91  //
    92  //// SmartQuery This will serialize the query message and submit it to the contract.
    93  //// The response is parsed into the provided interface.
    94  //// Usage: SmartQuery(addr, QueryMsg{Foo: 1}, &response)
    95  //func (chain *TestChain) SmartQuery(contractAddr string, queryMsg interface{}, response interface{}) error {
    96  //	msg, err := json.Marshal(queryMsg)
    97  //	if err != nil {
    98  //		return err
    99  //	}
   100  //
   101  //	req := types.QuerySmartContractStateRequest{
   102  //		Address:   contractAddr,
   103  //		QueryData: msg,
   104  //	}
   105  //	reqBin, err := proto.Marshal(&req)
   106  //	if err != nil {
   107  //		return err
   108  //	}
   109  //
   110  //	// TODO: what is the query?
   111  //	res := chain.App.Query(abci.RequestQuery{
   112  //		Path: "/cosmwasm.wasm.v1.Query/SmartContractState",
   113  //		Data: reqBin,
   114  //	})
   115  //
   116  //	if res.Code != 0 {
   117  //		return fmt.Errorf("query failed: (%d) %s", res.Code, res.Log)
   118  //	}
   119  //
   120  //	// unpack protobuf
   121  //	var resp types.QuerySmartContractStateResponse
   122  //	err = proto.Unmarshal(res.Value, &resp)
   123  //	if err != nil {
   124  //		return err
   125  //	}
   126  //	// unpack json content
   127  //	return json.Unmarshal(resp.Data, response)
   128  //}
   129  //
   130  //func (chain *TestChain) parseSDKResultData(r *sdk.Result) sdk.TxMsgData {
   131  //	var protoResult sdk.TxMsgData
   132  //	require.NoError(chain.t, proto.Unmarshal(r.Data, &protoResult))
   133  //	return protoResult
   134  //}
   135  //
   136  //// ContractInfo is a helper function to returns the ContractInfo for the given contract address
   137  //func (chain *TestChain) ContractInfo(contractAddr sdk.AccAddress) *types.ContractInfo {
   138  //	type testSupporter interface {
   139  //		TestSupport() *wasmd.TestSupport
   140  //	}
   141  //	return chain.App.(testSupporter).TestSupport().WasmKeeper().GetContractInfo(chain.GetContext(), contractAddr)
   142  //}