We've updated our Terms of Service. By continuing to use our services, you agree to the updated Terms.

Cosmos

Cosmos

#Installation

Magic interacts with the Cosmos blockchain via Magic's extension NPM package @magic-ext/cosmos. The Cosmos extension also lets you interact with the blockchain using methods from cosmjs.

note

You can skip straight to our kitchen sink example directly:

👉 Cosmos Example

NPM
Yarn
01npm install --save @magic-ext/cosmos

#Initialization

Typescript
01import { Magic } from 'magic-sdk';
02import { CosmosExtension } from '@magic-ext/cosmos';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new CosmosExtension({
07      rpcUrl: 'cosmos rpc url',
08    }),
09  ],
10});

#Common Methods

#Sign and Send Transaction

To send or sign a standard Cosmos blockchain transaction, you can call the magic.cosmos.signAndBroadcast method or magic.cosmos.sign method.

Typescript
01import { Magic } from 'magic-sdk';
02import { CosmosExtension } from '@magic-ext/cosmos';
03import { coins } from '@cosmjs/launchpad';
04
05const magic = new Magic('YOUR_API_KEY', {
06  extensions: [
07    new CosmosExtension({
08      rpcUrl: 'cosmos rpc url',
09    }),
10  ],
11});
12
13const handlerSendTransaction = async () => {
14  const metadata = await magic.user.getMetadata();
15
16  const message = [
17    {
18      typeUrl: '/cosmos.bank.v1beta1.MsgSend',
19      value: {
20        fromAddress: metadata.publicAddress,
21        toAddress: destinationAddress,
22        amount: [
23          {
24            amount: String(sendAmount),
25            denom: 'atom',
26          },
27        ],
28      },
29    },
30  ];
31  const fee = {
32    amount: [{ denom: 'uatom', amount: '500' }],
33    gas: '200000',
34  };
35
36  const sendTransactionResult = await magic.cosmos.signAndBroadcast(message, fee);
37  //or
38  const signTransactionResult = await magic.cosmos.sign(message, fee);
39};

#Send Tokens

Using magic.cosmos.sendTokens function to native tokens on Cosmos blockchain.

Typescript
01import { Magic } from 'magic-sdk';
02import { CosmosExtension } from '@magic-ext/cosmos';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new CosmosExtension({
07      rpcUrl: 'cosmos rpc url',
08    }),
09  ],
10});
11
12const result = await magic.cosmos.sendTokens('recipientAddress', 'transferAmount', 'denom', 'memo');

#Change Address

Using magic.cosmos.changeAddress function to change the address prefix.

Typescript
01import { Magic } from 'magic-sdk';
02import { CosmosExtension } from '@magic-ext/cosmos';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new CosmosExtension({
07      rpcUrl: 'cosmos rpc url',
08    }),
09  ],
10});
11
12const result = await magic.cosmos.changeAddress('address prefix');

#Resources