Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasting transaction failed with code 7 #1577

Open
rixhy-cyd opened this issue Apr 4, 2024 · 3 comments
Open

Broadcasting transaction failed with code 7 #1577

rixhy-cyd opened this issue Apr 4, 2024 · 3 comments

Comments

@rixhy-cyd
Copy link

rixhy-cyd commented Apr 4, 2024

BroadcastTxError: Broadcasting transaction failed with code 7 (codespace: sdk). Log: invalid from address: empty address string is not allowed: invalid address

Was trying to make a authz MsgExec (ExecMSG function below) and i get the above error.
I have attached my auth grant and msg exec code snippet below for reference

const { makeCosmoshubPath } = require('@cosmjs/amino');
const { DirectSecp256k1HdWallet } = require('@cosmjs/proto-signing');
const {
  SigningStargateClient,
  StargateClient,
  GasPrice,
  coins,
} = require('@cosmjs/stargate');
const axios = require('axios');
const { SendAuthorization } = require('cosmjs-types/cosmos/bank/v1beta1/authz');
const { MsgRevoke, MsgExec } = require('cosmjs-types/cosmos/authz/v1beta1/tx');
const { MsgSend } = require('cosmjs-types/cosmos/bank/v1beta1/tx');
async function grantAuth() {
  const wallet = await getWallet();
  const accounts = await wallet.getAccounts();
  console.log('wallet', accounts);
  const client = await SigningStargateClient.connectWithSigner(
    nodeUrl,
    wallet
    // defaultSigningClientOptions,
  );

  const grantMsg = {
    typeUrl: '/cosmos.authz.v1beta1.MsgGrant',
    value: {
      granter: accounts[0].address,
      grantee: accounts[1].address,
      grant: {
        authorization: {
          typeUrl: '/cosmos.bank.v1beta1.SendAuthorization',
          value: SendAuthorization.encode(
            SendAuthorization.fromPartial({
              spendLimit: [
                {
                  denom: 'uatom',
                  amount: '12',
                },
              ],
              allowList: [accounts[1].address],
            })
          ).finish(),
        },
      },
    },
  };
  const grantResult = await client.signAndBroadcast(
    accounts[0].address,
    [grantMsg],
    defaultFee,
    'Test grant for Authz'
  );

  console.log(grantResult);
}


async function getWallet() {
  const wallet = await DirectSecp256k1HdWallet.fromMnemonic('seed input', {
    hdPaths: [makeCosmoshubPath(1), makeCosmoshubPath(2)],
  });

  return wallet;
}
async function ExecMSG() {
  const wallet = await getWallet();
  const accounts = await wallet.getAccounts();

  // await checkAuthzGrant(accounts[0].address, accounts[1].address);
  const msgSend = {
    typeUrl: '/cosmos.bank.v1beta1.MsgSend',
    value: {
      fromAddress: accounts[0].address,
      // from_address: 'cosmos1vltcmk4fgtv5xrjzzjezgyx2c2s20753hyww9s',
      toAddress: accounts[1].address,
      // to_address: 'cosmos18srlyf5qn3ljr4h3t7esurvmrfhttr7fyk9utt',
      amount: coins(10000, 'uatom'), // 0.1 ATOM, assuming 1 ATOM = 1,000,000 uatom
    },
  };

  const execMsg = {
    typeUrl: '/cosmos.authz.v1beta1.MsgExec',
    value: MsgExec.fromPartial({
      grantee: accounts[1].address,
      // grantee: 'cosmos18srlyf5qn3ljr4h3t7esurvmrfhttr7fyk9utt',
      msgs: [msgSend],
    }),
  };

  const client = await SigningStargateClient.connectWithSigner(
    nodeUrl,
    wallet,
    defaultSigningClientOptions
  );
  
  const fee = {
    amount: coins(5000, 'uatom'), 
    gas: '200000', 
    granter: accounts[0].address,
  };
  const execMsgResutl = await client.signAndBroadcastSync(
    accounts[1].address,
    [execMsg],
    fee,
    'Test grant exec for Authz'
  );

  console.log(execMsgResutl);
}
@rixhy-cyd
Copy link
Author

@webmaster128 can you help with the payload for MsgExec

@a-novi
Copy link

a-novi commented Apr 18, 2024

I do have a similar issue. This is my code.

import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { SigningStargateClient, GasPrice } from "@cosmjs/stargate";
import { MsgExec } from "cosmjs-types/cosmos/authz/v1beta1/tx";
import { MsgVote } from "cosmjs-types/cosmos/gov/v1/tx";
import { VoteOption } from "cosmjs-types/cosmos/gov/v1/gov";

// Registry for adding custom messages
const registry = new Registry();
registry.register("/cosmos.authz.v1beta1.MsgExec", MsgExec);
registry.register("/cosmos.gov.v1.MsgVote", MsgVote);

async function voteOnProposal(chainConfig: any, mnemonic: string) {
  const { rpcEndpoint, gasPrices, prefix } = chainConfig;

  const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
    prefix
  });

  const client = await SigningStargateClient.connectWithSigner(
    rpcEndpoint,
    wallet,
    {
      registry,
      gasPrice: GasPrice.fromString(gasPrices)
    }
  );

  const [{ address }] = await wallet.getAccounts();

  await client.signAndBroadcast(
    address[
      {
        typeUrl: "/cosmos.authz.v1beta1.MsgExec",
        value: {
          grantee: address,
          msgs: [
            {
              typeUrl: "/cosmos.gov.v1.MsgVote",
              value: {
                option: VoteOption.VOTE_OPTION_YES,
                proposal_id,
                voter
              }
            }
          ]
        }
      }
    ],
    {
      amount: [{ denom: "uatom", amount: "2000" }],
      gas: "200000"
    },
    ""
  );
}

This is my error:

Failed to vote: BroadcastTxError: Broadcasting transaction failed with code 7 (codespace: sdk). Log: invalid voter address: empty address string is not allowed: invalid address
    at SigningStargateClient.broadcastTxSync (/Users/novi/Desktop/tiexo/cosmos-voting/node_modules/@cosmjs/stargate/src/stargateclient.ts:492:9)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SigningStargateClient.broadcastTx (/Users/novi/Desktop/tiexo/cosmos-voting/node_modules/@cosmjs/stargate/src/stargateclient.ts:460:27)
    at async voteOnProposal (/Users/novi/Desktop/tiexo/cosmos-voting/index.ts:69:15) {
  code: 7,
  codespace: 'sdk',
  log: 'invalid voter address: empty address string is not allowed: invalid address'
}

Does anyone have suggestions for resolving this issue?

@Reecepbcups
Copy link
Member

Reecepbcups commented Apr 18, 2024

From Cosmos dev thread, reference https://github.com/eco-stake/restake-ui/blob/9c28425db989689cc63b6625de367f14909cf01f/src/components/VoteForm.js#L9. Most likely missing registry encodings like novi had above

Their solution:

import { MsgVote } from "cosmjs-types/cosmos/gov/v1/tx";

const registry = new Registry();
registry.register("/cosmos.authz.v1beta1.MsgExec", MsgExec);
registry.register("/cosmos.gov.v1.MsgVote", MsgVote);

function buildExecMessage(grantee: string, messages: any) {
  return {
    typeUrl: "/cosmos.authz.v1beta1.MsgExec",
    value: {
      grantee: grantee,
      msgs: messages
    }
  };
}

const message = buildExecMessage(grantee, [
  {
    typeUrl: "/cosmos.gov.v1beta1.MsgVote",
    value: MsgVote.encode(
      MsgVote.fromPartial({
        proposalId: vote.proposalId as any,
        voter: vote.voter,
        option: vote.option
      })
    ).finish()
  }
]);

const response = await client.signAndBroadcast(
  grantee,
  [message],
  {
    amount: [{ denom: config.denom, amount: "2000" }],
    gas: "200000"
  },
  ""
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants