Documentation Index
Fetch the complete documentation index at: https://docs.magic.link/llms.txt
Use this file to discover all available pages before exploring further.
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
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.
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.
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.
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