osmosis-labs / osmojs

OsmosJS makes it easy to compose and broadcast Osmosis and Cosmos messages
https://cosmology.zone/products/osmojs
Apache License 2.0
63 stars 32 forks source link

multiSend example #2

Closed pyramation closed 1 year ago

pyramation commented 2 years ago

we may also need to do something special with the signingCosmosClient to make this easier:

an example from the community:

const {
  AminoTypes,
  SigningStargateClient
} = require('@cosmjs/stargate');
const { Registry } = require('@cosmjs/proto-signing');
const { defaultRegistryTypes } = require('@cosmjs/stargate');
const { OfflineSigner } = require('@cosmjs/proto-signing');
const { cosmos } = require('osmojs');

const getSigningCosmosClient = async ({ rpcEndpoint, signer }) => {
  // registry
  const registry = new Registry(defaultRegistryTypes);

  // aminotypes
  const aminoTypes = new AminoTypes({
    ...cosmos.bank.v1beta1.AminoConverter
  });

  cosmos.bank.v1beta1.load(registry);

  const client = await SigningStargateClient.connectWithSigner(
    rpcEndpoint,
    signer,
    { registry, aminoTypes }
  );

  return client;
};

module.exports = { getSigningCosmosClient: getSigningCosmosClient };

// #######################################

// ... and then in my index.js:

const {
  osmosis,
  ibc,
  cosmos,
  getSigningOsmosisClient,
  //getSigningCosmosClient,
  signAndBroadcast,
  FEE_VALUES
} = require('osmojs');

const { getSigningCosmosClient } = require('./cosmosclient.js');

/* ... */
pyramation commented 2 years ago

other examples that have simple flow https://gist.github.com/fadeev/a4981eff1cf3a805ef10e25313d5f2b7

pyramation commented 2 years ago

Here is the original index that impelled us to customize the signing client:


const { config } = require('./config/config.js');
const { generateSignerWallet } = require('./signer.js');

const {
  osmosis,
  ibc,
  cosmos,
  getSigningOsmosisClient,
  signAndBroadcast,
  FEE_VALUES
} = require('osmojs');

const { coin, coins } = require('@cosmjs/amino');

const {
  multiSend,
  send
} = cosmos.bank.v1beta1.MessageComposer.fromPartial;

/* TEST */

const amount = coins(1, 'uosmo');

const fee = FEE_VALUES.osmosis.swapExactAmountIn;

(async () => {
  const signer = await generateSignerWallet("osmosis", config.mnemonic).then((signer) => { return signer; });
  const address = await signer.getAccounts().then((accounts) => { return accounts[0].address; });

  const rpcEndpoint = config.rpcEndpoint;
  const client = await getSigningOsmosisClient({ rpcEndpoint, signer });

  const msg = send({
    fromAddress: address,
    toAddress: "osmo1REDACTED",
    amount: amount
  });

  console.dir(msg, { depth: null });
  /*
  prints the following:

  {
    typeUrl: '/cosmos.bank.v1beta1.MsgSend',
    value: {
      fromAddress: 'osmo1REDACTED',
      toAddress: 'osmo1osmo1REDACTED',
      amount: [ { denom: 'uosmo', amount: '1' } ]
    }
  }  
  */

  const res = await signAndBroadcast({
    client,
    chainId: 'osmosis-1',
    address: address,
    msgs: [msg],
    fee,
    memo: config.default_memo
  });

  /*
  nodejs crashes with error:
    C:\dev\user\test\node_modules\@cosmjs\stargate\build\aminotypes.js:21
                throw new Error(`Type URL '${typeUrl}' does not exist in the Amino message type register. ` +
                      ^
    Error: Type URL '/cosmos.bank.v1beta1.MsgSend' does not exist in the Amino message type register. If you need support for this message type, you can pass in additional entries to the AminoTypes constructor. If you think this message type should be included by default, please open an issue at https://github.com/cosmos/cosmjs/issues.
        at AminoTypes.toAmino (C:\dev\user\test\node_modules\@cosmjs\stargate\build\aminotypes.js:21:19)
        at C:\dev\user\test\node_modules\@cosmjs\stargate\build\signingstargateclient.js:207:60
        at Array.map (<anonymous>)
        at SigningStargateClient.signAmino (C:\dev\user\test\node_modules\@cosmjs\stargate\build\signingstargateclient.js:207:31)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
  */

  console.log(res);
})();
ValarDragon commented 2 years ago

I'm actually proposing we make multi-send only allow one sender haha: https://github.com/cosmos/cosmos-sdk/issues/12601