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

fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); #28

Closed LIUYX52 closed 1 year ago

LIUYX52 commented 1 year ago

i want to make a swap by using osmojs

const {
    swapExactAmountIn,
} = osmosis.gamm.v1beta1.MessageComposer.withTypeUrl;

const senderMnemonic = ""

  const chainFind = chains.find(({ chain_name }) => chain_name == 'osmosis');
  const signer = await getOfflineSigner({
    mnemonic:senderMnemonic,
    chain:chainFind
  });

const client = await getSigningOsmosisClient({
    rpcEndpoint:"https://rpc.cosmos.directory/osmosis",
    signer:signer 
});
const fee = {
    amount: coins(0, "uosmo"),
    gas: "500000",
};
const routes = [{
    poolId: "497",
    tokenOutDenom:"ibc/46B44899322F3CD854D2D46DEEF881958467CDD4B3B10086DA49296BBED94BED"
}];

const msg = swapExactAmountIn({
    sender:"osmo1......",
    routes:routes,
    tokenIn: coins("2400", "uosmo"),
    tokenOutMinAmount:"1558"
  });
let result = await client.signAndBroadcast("osmo1......", [msg], fee, '');
console.log(result);

but it failed , the error msg like this :

C:\\node_modules\osmojs\node_modules\long\umd\index.js:318
    return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
                        ^

TypeError: Cannot read properties of undefined (reading 'low')
    at Function.fromValue (C:\code\cosmosDemoDell\node_modules\osmojs\node_modules\long\umd\index.js:318:25)
    at Object.toAmino (C:\code\cosmosDemoDell\node_modules\osmojs\main\codegen\osmosis\gamm\v1beta1\tx.amino.js:103:33)
    at AminoTypes.toAmino (C:\code\cosmosDemoDell\node_modules\osmojs\node_modules\@cosmjs\stargate\build\aminotypes.js:27:30)
    at C:\code\cosmosDemoDell\node_modules\osmojs\node_modules\@cosmjs\stargate\build\signingstargateclient.js:207:60
    at Array.map (<anonymous>)
    at SigningStargateClient.signAmino (C:\code\cosmosDemoDell\node_modules\osmojs\node_modules\@cosmjs\stargate\build\signingstargateclient.js:207:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SigningStargateClient.signAndBroadcast (C:\code\cosmosDemoDell\node_modules\osmojs\node_modules\@cosmjs\stargate\build\signingstargateclient.js:167:23)
    at async file:///C:/code/cosmosDemoDell/03ibc/03_swapByOsmosis.js:41:14
LIUYX52 commented 1 year ago

Dear, i have upload my code to github. you can check my code.

https://github.com/LIUYX52/osmojsTest

pyramation commented 1 year ago

you need to use the Long type. I believe poolId is a long. Please look at the tests

https://github.com/osmosis-labs/osmojs/blob/20f800edf85137a3dba21b19a430335232e52b25/packages/osmojs/starship/__tests__/pools.test.js#L4

https://github.com/osmosis-labs/osmojs/blob/20f800edf85137a3dba21b19a430335232e52b25/packages/osmojs/starship/__tests__/pools.test.js#L248

you can also checkout the create cosmos app examples

https://github.com/cosmology-tech/create-cosmos-app/blob/5d2797928bc56a55867d6fc3dbb587930bc01e62/examples/swap-tokens/components/react/swap-tokens.tsx#L476-L481