Unable to resolve type URL /cosmwasm.wasm.v1.MsgExecuteContract: tx parse error"

I am facing following error . But can’t understand why? I was trying to execute my own smart contract.
unable to resolve type URL /cosmwasm.wasm.v1.MsgExecuteContract: tx parse error"

full response:
{“tx_response”: {“code”: 2, “codespace”: “sdk”, “data”: “”, “events”: , “gas_used”: “0”, “gas_wanted”: “0”, “height”: “0”, “info”: “”, “logs”: , “raw_log”: “unable to resolve type URL /cosmwasm.wasm.v1.MsgExecuteContract: tx parse error”, “timestamp”: “”, “tx”: null, “txhash”: “D88F0E00460E352E0BEE780F221552F42D29992E5A7973488567D0E75AE8AAC7”}}

full Code snippet:
async function t() {
const mnemonic =
‘nice same resist skirt cross tone object grass remember then pluck fall’;
const chainId = ‘pulsar-2’;
const cosmos = new Cosmos(lcdUrl, chainId);

cosmos.setPath(“m/44’/529’/0’/0/0”);
cosmos.setBech32MainPrefix(‘secret’);
const address = cosmos.getAddress(mnemonic);
console.log('address: ', address);

const privKey = cosmos.getECPairPriv(mnemonic);
const pubKeyAny = cosmos.getPubKeyAny(privKey);
console.log(‘priv key:’, privKey);
console.log('public key: ', pubKeyAny);

await cosmos.getAccounts(address).then(data => {
console.log('data: ', data);

const CONTRACT_ADDRESS = 'secret14snjfvwxdc4xyl3t4kd5a8u56y5eu9rpzr9hwt';

const CONTRACT_CODE_HASH =
  'e31293e76ff207af810dbe93dab6f9428864d392c5341acbc50a5c0e2cbe26b2';

const VIEWING_KEY = 'my viewing key';
const v_key_msg = {
  set_viewing_key: {
    key: VIEWING_KEY,
  },
};
let cw20Contract =
  'juno10rktvmllvgctcmhl5vv8kl3mdksukyqf2tdveh8drpn0sppugwwqjzz30z';
//let transferBytes = new Buffer(v_key_msg, 'utf8');
const msgExecuteContract = new message.cosmwasm.wasm.v1.MsgExecuteContract({
  sender: address,
  contract: CONTRACT_ADDRESS,
  msg: v_key_msg,
  funds: [{denom: 'uscrt', amount: String(3000000)}],
});

const msgExecuteContractAny = new message.google.protobuf.Any({
  type_url: '/cosmwasm.wasm.v1.MsgExecuteContract',
  value:
    message.cosmwasm.wasm.v1.MsgExecuteContract.encode(
      msgExecuteContract,
    ).finish(),
});

const txBody = new message.cosmos.tx.v1beta1.TxBody({
  messages: [msgExecuteContractAny],
  memo: '',
});
console.log('txBody: ', txBody);

const signerInfo = new message.cosmos.tx.v1beta1.SignerInfo({
  public_key: pubKeyAny,
  mode_info: {
    single: {
      mode: message.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
    },
  },
  sequence: data.account.sequence,
});

const feeValue = new message.cosmos.tx.v1beta1.Fee({
  amount: [{denom: 'uscrt', amount: String(25000)}],
  gas_limit: 200000,
});

const authInfo = new message.cosmos.tx.v1beta1.AuthInfo({
  signer_infos: [signerInfo],
  fee: feeValue,
});

const signedTxBytes = cosmos.sign(
  txBody,
  authInfo,
  data.account.account_number,
  privKey,
);
cosmos.broadcast(signedTxBytes).then(response => console.log(response));

});
}

Hi, it seems to me that you’re trying to deploy a CosmWasm v1 contract on pulsar-2, which doesn’t support CosmWasm v1 yet (wait a few weeks :pray:). In the meantime you can deploy CosmWasm v0.10 contracts.

Also, your Javascript code seem to use protobuf definitions that don’t exist in Secret, Instead of /cosmwasm.wasm.v1.MsgExecuteContract we have /secret.compute.v1beta1.MsgExecuteContract which is defined here. But when working with Secret I’d suggest using secret.js (docs), which will make your code look like that:

async function t() {
  const mnemonic = "nice same resist skirt cross tone object grass remember then pluck fall";
  const chainId = "pulsar-2";

  const wallet = new Wallet(mnemonic);
  const secretjs = await SecretNetworkClient.create({
    chainId,
    grpcWebUrl: "https://grpc.pulsar.scrttestnet.com", // from https://github.com/scrtlabs/api-registry#api-endpoints-1
    wallet,
    walletAddress: wallet.address,
  });

  const CONTRACT_ADDRESS = "secret14snjfvwxdc4xyl3t4kd5a8u56y5eu9rpzr9hwt";
  const CONTRACT_CODE_HASH = "e31293e76ff207af810dbe93dab6f9428864d392c5341acbc50a5c0e2cbe26b2";

  const VIEWING_KEY = "my viewing key";
  const v_key_msg = {
    set_viewing_key: {
      key: VIEWING_KEY,
    },
  };

  const tx = await secretjs.tx.broadcast([
    new MsgExecuteContract({
      sender: wallet.address,
      contractAddress: CONTRACT_ADDRESS,
      codeHash: CONTRACT_CODE_HASH,
      msg: v_key_msg,
      sentFunds: [{ denom: "uscrt", amount: String(3000000) }], // why?
    }),
  ]);

  console.log(tx);
}

Yeah, I was trying to use secretjs before trying cosmjs or cosmostation.
But problem is as I was trying it in react-native (android) env, I was getting following error:
Error: This environment’s XHR implementation cannot support binary transfer.

Can you try secretjs@1.3.0-beta.22?

Yeah, sure.
Okay, let me try.

Yeah, I have tried secretjs@1.3.0-beta.22.
when I tried to create wallet using
const wallet = new Wallet();
I got an error called ‘Invalid integer’.

Then I generated wallet in following method:
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: ‘secret’,
});

const [firstAccount] = await wallet.getAccounts();

then wrote other lines to execute contract,
const secretjs = await SecretNetworkClient.create({
chainId,
grpcWebUrl: ‘https://grpc.pulsar.scrttestnet.com’,
wallet,
walletAddress: firstAccount.address,
});
console.log(’ secretjs: ', JSON.stringify(secretjs));

const CONTRACT_ADDRESS = ‘secret14snjfvwxdc4xyl3t4kd5a8u56y5eu9rpzr9hwt’;
const CONTRACT_CODE_HASH =
‘e31293e76ff207af810dbe93dab6f9428864d392c5341acbc50a5c0e2cbe26b2’;

const VIEWING_KEY = ‘my viewing key’;
const v_key_msg = {
set_viewing_key: {
key: VIEWING_KEY,
},
};

const tx = await secretjs.tx.broadcast([
new MsgExecuteContract({
sender: firstAccount.address,
contractAddress: CONTRACT_ADDRESS,
codeHash: CONTRACT_CODE_HASH,
msg: v_key_msg,
sentFunds: [{denom: ‘uscrt’, amount: String(3000000)}], // why?
}),
]);

console.log(tx);

But it shows error:
TypeError: Object is not a constructor (evaluating ‘new (_$$_REQUIRE(_dependencyMap[10], “cosmjs-types/cosmwasm/wasm/v1/tx”).MsgExecuteContract)’)

I just put all your code above into this Sandbox and it works, maybe it helps to play around with it.

Yeah, It also works for me using nextJS (web application).
But it doesn’t work when using in react-native (mobile application).

Btw thank you so much for your reply.

1 Like

You don’t need cosmjs, everything in the code I shared is imported from secretjs

Yeah, I understood your point.
Actually I tried to tell that when tried to generate wallet using - wallet = new Wallet() constructor, I got an error called ‘Invalid integer’.
That’s why I used only wallet generating part from cosmjs.
And then Other parts (i.e client creation etc) from secretjs.

Now I have one question - Can’t I communicate with secret network (especially secret contract that is developed in rust lang) using cosmjs?
Actually I was able to send scrt tokens between two secret network address using cosmjs, but I faced problems when tried to communicate with contract.

You can use cosmjs for everything except call secret contracts, because that requires encryption and cosmjs doesn’t know how to do that

Oh! I see.
That means I was attempting a wrong thing (calling secret contract using cosmjs).

Btw Thank you so much for all of your responses so far.

Getting error when query balance.
TypeError: undefined is not an object (evaluating ‘r.g.location.protocol’)

**And I got another error when trying to execute secret contract .
TypeError: Object is not a constructor (evaluating ‘new (_$$_REQUIRE(_dependencyMap[10], “cosmjs-types/cosmwasm/wasm/v1/tx”).MsgExecuteContract)’)

My question what are the possible reason for these two error.

///////////////////////////////////////////////////////////////////////////////////////////////
Code Snippet:
const wallet1 = new AminoWallet(mnemonic);

const wallet = await wallet1.init(mnemonic);
console.log('wallet: ', wallet);

//const wallet = new Wallet(mnemonic);
const secretjs = await SecretNetworkClient.create({
chainId,
grpcWebUrl: ‘https://grpc.pulsar.scrttestnet.com’, // from GitHub - scrtlabs/api-registry: A registry of API endpoints for mainnet and testnet
wallet,
walletAddress: wallet.address,
});

const {
balance: {amount},
} = await secretjs.query.bank.balance({
address: ‘secret1w076ykc6fne0tvgp9wcppg5xw8wx6f8sy93e37’,
denom: ‘uscrt’,
});
console.log('balance: ', amount);

Here in AminoWallet class:
class AminoWallet {
/**
* @param {String} mnemonic Import mnemonic or generate random if empty
* @param {Number} [options.hdAccountIndex] The account index in the HD derivation path. Defaults to 0.
* @param {Number} [options.coinType] The coin type in the HD derivation path. Defaults to Secret’s 529.
* @param {String} [options.bech32Prefix] The bech32 prefix for the account’s address. Defaults tp "secret"
*/

        constructor(mnemonic) {

                        }

  async init(mnemonic = "", options = {}) {
      

        console.log("Came here at constructor: ");
        var _a, _b, _c;
        if (mnemonic === "") {
           mnemonic = bip39.generateMnemonic(256 /* 24 words */);
        }
        
        this.mnemonic = mnemonic;
        console.log("mnemonic: ", mnemonic);
        this.hdAccountIndex = (_a = options.hdAccountIndex) !== null && _a !== void 0 ? _a : 0;
        this.coinType = (_b = options.coinType) !== null && _b !== void 0 ? _b : exports.SECRET_COIN_TYPE;
        this.bech32Prefix = (_c = options.bech32Prefix) !== null && _c !== void 0 ? _c : exports.SECRET_BECH32_PREFIX;
        const seed = bip39.mnemonicToSeedSync(this.mnemonic);
        const node = bip32.fromSeed(seed);
        const secretHD = node.derivePath(`m/44'/${this.coinType}'/0'/0/${this.hdAccountIndex}`);
        const privateKey = secretHD.privateKey;
        if (!privateKey) {
            throw new Error("Failed to derive key pair");
        }
        this.privateKey = new Uint8Array(privateKey);
        console.log("private key: ", privateKey);
       
        const { pubkey } = await crypto_1.Secp256k1.makeKeypair(privateKey);
        console.log("pub key: ", pubkey);
        const publicKey1 = await crypto_1.Secp256k1.compressPubkey(pubkey);
        console.log("public key1: ",publicKey1);
        this.publicKey = publicKey1;
        this.address = pubkeyToAddress(this.publicKey, this.bech32Prefix);
        console.log("this: ", this);
        return this;
      };