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.
NOTEYou can skip straight to our kitchen sink example directly:Cosmos Example
npm install --save @magic-ext/cosmos

Initialization

JavaScript
import { Magic } from 'magic-sdk';
import { CosmosExtension } from '@magic-ext/cosmos';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new CosmosExtension({
      rpcUrl: 'cosmos rpc url',
    }),
  ],
});

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.
JavaScript
import { Magic } from 'magic-sdk';
import { CosmosExtension } from '@magic-ext/cosmos';
import { coins } from '@cosmjs/launchpad';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new CosmosExtension({
      rpcUrl: 'cosmos rpc url',
    }),
  ],
});

const handlerSendTransaction = async () => {
  const metadata = await magic.user.getMetadata();

  const message = [
    {
      typeUrl: '/cosmos.bank.v1beta1.MsgSend',
      value: {
        fromAddress: metadata.publicAddress,
        toAddress: destinationAddress,
        amount: [
          {
            amount: String(sendAmount),
            denom: 'atom',
          },
        ],
      },
    },
  ];
  const fee = {
    amount: [{ denom: 'uatom', amount: '500' }],
    gas: '200000',
  };

  const sendTransactionResult = await magic.cosmos.signAndBroadcast(message, fee);
  //or
  const signTransactionResult = await magic.cosmos.sign(message, fee);
};

Send Tokens

Using magic.cosmos.sendTokens function to native tokens on Cosmos blockchain.
JavaScript
import { Magic } from 'magic-sdk';
import { CosmosExtension } from '@magic-ext/cosmos';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new CosmosExtension({
      rpcUrl: 'cosmos rpc url',
    }),
  ],
});

const result = await magic.cosmos.sendTokens('recipientAddress', 'transferAmount', 'denom', 'memo');

Change Address

Using magic.cosmos.changeAddress function to change the address prefix.
JavaScript
import { Magic } from 'magic-sdk';
import { CosmosExtension } from '@magic-ext/cosmos';

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new CosmosExtension({
      rpcUrl: 'cosmos rpc url',
    }),
  ],
});

const result = await magic.cosmos.changeAddress('address prefix');

Resources