How to Integrate with the Terra Blockchain with Magic
How to Integrate with the Terra Blockchain with Magic
#Installation
Magic interacts with the Terra blockchain via Magic's extension NPM package @magic-ext/terra
. The Terra extension also lets you interact with the blockchain using methods from Terra's Javascript SDK.
You can skip straight to our kitchen sink example directly:
👉 Terra Example
#NPM
npm install --save @magic-ext/terra
#Yarn
yarn add @magic-ext/terra
#Initializing Extension
#ES Modules/TypeScript
import { Magic } from 'magic-sdk';
import { TerraExtension } from "@magic-ext/terra";
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TerraExtension({
rpcUrl: 'TERRA_RPC_NODE_URL',
}),
],
});
#CDN
<script src="https://auth.magic.link/sdk"></script>
<script type="text/javascript" src="https://auth.magic.link/sdk/extension/terra"></script>
<script>
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new TerraExtension({
rpcUrl: 'TERRA_RPC_NODE_URL',
}),
],
});
</script>
#Sign Transaction
Note that the Magic Terra extension follows the method names and conventions by Terra's Javascript SDK. To sign a standard Terra blockchain transaction, you can call the follow the doc.
#ES Modules/TypeScript
import { Magic } from "magic-sdk";
import { TerraExtension } from "@magic-ext/terra";
import {LCDClient, MsgSend, Key, SimplePublicKey} from '@terra-money/terra.js';
const rpcUrl = 'TERRA_RPC_NODE_URL'
const magic = new Magic("YOUR_API_KEY", {
extensions: {
terra: new TerraExtension({
rpcUrl
})
}
});
export class MagicRawKey extends Key {
constructor(publicKey) {
super(new SimplePublicKey(publicKey));
}
async sign(payload){
return magic.terra.sign(payload)
}
}
const publicKey = await magic.terra.getPublicKey();
const mk = new MagicRawKey(publicKey);
const terra = new LCDClient({
URL: rpcUrl,
chainID: 'bombay-12',
});
const wallet = terra.wallet(mk);
const send = new MsgSend(
publicAddress,
destinationAddress,
{ uluna: 10000 }
);
const tx = await wallet.createAndSignTx({
msgs: [send],
memo: 'test from terra.js!',
})
console.log('signed transaction', tx);