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
01npm install --save @magic-ext/terra
#Yarn
01yarn add @magic-ext/terra
#Initializing Extension
#ES Modules/TypeScript
01import { Magic } from 'magic-sdk';
02import { TerraExtension } from "@magic-ext/terra";
03
04const magic = new Magic('YOUR_API_KEY', {
05 extensions: [
06 new TerraExtension({
07 rpcUrl: 'TERRA_RPC_NODE_URL',
08 }),
09 ],
10});
#CDN
01<script src="https://auth.magic.link/sdk"></script>
02<script type="text/javascript" src="https://auth.magic.link/sdk/extension/terra"></script>
03
04<script>
05const magic = new Magic('YOUR_API_KEY', {
06 extensions: [
07 new TerraExtension({
08 rpcUrl: 'TERRA_RPC_NODE_URL',
09 }),
10 ],
11});
12</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
01import { Magic } from "magic-sdk";
02import { TerraExtension } from "@magic-ext/terra";
03import {LCDClient, MsgSend, Key, SimplePublicKey} from '@terra-money/terra.js';
04
05const rpcUrl = 'TERRA_RPC_NODE_URL'
06
07const magic = new Magic("YOUR_API_KEY", {
08 extensions: {
09 terra: new TerraExtension({
10 rpcUrl
11 })
12 }
13});
14
15export class MagicRawKey extends Key {
16 constructor(publicKey) {
17 super(new SimplePublicKey(publicKey));
18 }
19
20 async sign(payload){
21 return magic.terra.sign(payload)
22 }
23}
24
25const publicKey = await magic.terra.getPublicKey();
26const mk = new MagicRawKey(publicKey);
27
28const terra = new LCDClient({
29 URL: rpcUrl,
30 chainID: 'bombay-12',
31});
32
33const wallet = terra.wallet(mk);
34
35const send = new MsgSend(
36 publicAddress,
37 destinationAddress,
38 { uluna: 10000 }
39);
40
41const tx = await wallet.createAndSignTx({
42 msgs: [send],
43 memo: 'test from terra.js!',
44})
45
46console.log('signed transaction', tx);