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

Algorand

Algorand

#Installation

Magic interacts with the Algorand blockchain via Magic's extension NPM package @magic-ext/algorand. The Algorand extension also lets you interact with the blockchain using methods from Algorand's Javascript SDK.

note

You can skip straight to our kitchen sink example directly:

👉 Algorand Example

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

#Initialization

Typescript
01import { Magic } from 'magic-sdk';
02import { AlgorandExtension } from '@magic-ext/algorand';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: {
06    algorand: new AlgorandExtension({
07      rpcUrl: '', // should remain empty
08    }),
09  },
10});

#Common Methods

#Get Test Algorand

Before you can send transaction on the Algorand blockchain, you'll need to acquire some test Algorand.

  1. Go to our Algorand Example application

  2. Login with your email address

  3. Copy your Algorand public address

  4. Go to the Algorand Faucet

  5. Paste your copied Algorand public address in the text input

  6. Now you can use your test Algorand in our example app

#Get Wallet

Use the getWallet function to get the Algorand public address for the current user.

Typescript
01import { Magic } from 'magic-sdk';
02import { AlgorandExtension } from '@magic-ext/algorand';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: {
06    algorand: new AlgorandExtension({
07      rpcUrl: '',
08    }),
09  },
10});
11
12// Get user's Algorand public address
13const publicAddress = await magic.algorand.getWallet();
14console.log('algorand public address', publicAddress);

#Sign Transaction

Note that the Magic Algorand extension follows the method names and conventions by Algorand's Javascript SDK. To sign a standard Algorand blockchain transaction, you can call the magic.algorand.signTransaction() method.

Typescript
01import { Magic } from 'magic-sdk';
02import { AlgorandExtension } from '@magic-ext/algorand';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: {
06    algorand: new AlgorandExtension({
07      rpcUrl: '',
08    }),
09  },
10});
11
12const sendPaymentTransaction = async () => {
13  // Construct Payload
14  let params = await algodClient.getTransactionParams().do();
15  let note = new TextEncoder().encode("Hello World");
16  let txn = algosdk.makePaymentTxnWithSuggestedParams(
17    from,
18    to,
19    amount,
20    closeRemainderTo,
21    note,
22    params
23  );
24  console.log("txn", txn);
25
26  // Sign Payload
27  let encodedTxn = algosdk.encodeObj(txn.get_obj_for_encoding());
28  const signedTxn = await magic.algorand.signTransaction(encodedTxn);
29  // Broadcast Tx
30  const hash = await algodClient.sendRawTransaction(signedTxn.blob).do();
31  console.log("hash", hash);
32  // Wait for confirmation
33  const receipt = await waitForConfirmation(algodClient, hash.txId, 4);
34  console.log("receipt", receipt);
35};
36
37const sendAssetConfigTransaction = async () => {
38  // Construct Payload
39  let params = await algodClient.getTransactionParams().do();
40  let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(
41    from,
42    note,
43    totalSupply,
44    decimals,
45    defaultFrozen,
46    manager,
47    reserve,
48    freeze,
49    clawback,
50    unitName,
51    assetName,
52    assetURL,
53    assetMetadataHash
54    params
55  );
56  console.log("txn", txn);
57
58  // Sign Payload
59  let encodedTxn = algosdk.encodeObj(txn.get_obj_for_encoding());
60  const signedTxn = await magic.algorand.signTransaction(encodedTxn);
61  // Broadcast Tx
62  const hash = await algodClient.sendRawTransaction(signedTxn.blob).do();
63  console.log("hash", hash);
64  // Wait for confirmation
65  const receipt = await waitForConfirmation(algodClient, hash.txId, 4);
66  console.log("receipt", receipt);
67};
68
69const sendAssetTransferTransaction = async () => {
70  // Construct Payload
71  let params = await algosdk.getTransactionParams().do();
72  let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(
73    from,
74    to,
75    closeRemainderTo,
76    revocationTarget,
77    amount,
78    note,
79    assetIndex, // can get from from acfg tx receipt
80    params
81  );
82
83  // Sign Payload
84  let encodedTxn = algosdk.encodeObj(txn.get_obj_for_encoding());
85  const signedTxn = await magic.algorand.signTransaction(encodedTxn);
86  // Broadcast Tx
87  const hash = await algosdk.sendRawTransaction(signedTxn.blob).do();
88  console.log("hash", hash);
89  // Wait for confirmation
90  const receipt = await waitForConfirmation(algosdk, hash.txId, 4);
91  console.log("receipt", receipt);
92};

#Sign Bid

Note that the Magic Algorand extension follows the method names and conventions by Algorand's Javascript SDK. To sign a standard Algorand blockchain Bid, you can call the magic.algorand.signBid() method.

Typescript
01import { Magic } from 'magic-sdk';
02import { AlgorandExtension } from '@magic-ext/algorand';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: {
06    algorand: new AlgorandExtension({
07      rpcUrl: '',
08    }),
09  },
10});
11
12const bid = {
13  bidderKey: 'IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U',
14  auctionKey: '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q',
15  bidAmount: 1000,
16  maxPrice: 10,
17  bidID: 2,
18  auctionID: 56,
19};
20
21const tx = await magic.algorand.signBid(bid);
22
23console.log('signed bid', tx);

#Sign Group Transaction

To sign a standard Algorand blockchain Group transaction with magic user, you can call the magic.algorand.signGroupTransaction() method.

Typescript
01import { Magic } from "magic-sdk";
02import { AlgorandExtension } from "@magic-ext/algorand";
03const algosdk = require('algosdk');
04
05const magic = new Magic('YOUR_API_KEY', {
06    extensions: {
07        algorand: new AlgorandExtension({
08            rpcUrl: ''
09        })
10    }
11});
12
13let client = null;
14async function setupClient() {
15    if( client == null){
16      const token = {
17        "x-api-key": "x api key"
18      };
19      const server = "algorand rpc url";
20      const port = '';
21      let algodClient = new algosdk.Algodv2(token, server, port);
22      client = algodClient;
23    } else {
24      return client;
25    }
26    return client;
27}
28
29
30let algodClient = await setupClient();
31
32let params = await algodClient.getTransactionParams().do();
33
34const txns = [{
35  from: 'magic user public address',
36  to: 'OFHW3Z3T2RML7J2S6KYGHPAMO6IQH76PE2HSCAIN5U5NBGXAIPBOY7DCHI',
37  amount: 1000000,
38  closeRemainderTo: undefined,
39  note: undefined,
40  suggestedParams: params,
41},
42  {
43    from: 'magic user public address',
44    to: 'XRKQBEV7FINQ66SYAFY33UYHOC4GRAICWI3V6V2TXLCQMPJBGGRHLG2E74',
45    amount: 1000000,
46    closeRemainderTo: undefined,
47    note: undefined,
48    suggestedParams: params,
49  }
50]
51
52const signedTX = await magic.algorand.signGroupTransaction(txns);
53
54console.log("signedTX", signedTX);

#Sign Group Transaction V2

To sign a standard Algorand blockchain Group transaction with magic user, you can call the magic.algorand.signGroupTransactionV2() method.

Typescript
01import { Magic } from "magic-sdk";
02import { AlgorandExtension } from "@magic-ext/algorand";
03const algosdk = require('algosdk');
04
05const magic = new Magic('YOUR_API_KEY', {
06    extensions: {
07        algorand: new AlgorandExtension({
08            rpcUrl: 'algorand rpc url'
09        })
10    }
11});
12
13
14  let algodClient = await setupClient();
15
16  let suggestedParams = await algodClient.getTransactionParams().do();
17
18  const txn1 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
19    from: publicAddress,
20    to: 'OFHW3Z3T2RML7J2S6KYGHPAMO6IQH76PE2HSCAIN5U5NBGXAIPBOY7DCHI',
21    amount: 1000,
22    suggestedParams,
23  });
24
25  const txn2 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
26    from: publicAddress,
27    to: 'XRKQBEV7FINQ66SYAFY33UYHOC4GRAICWI3V6V2TXLCQMPJBGGRHLG2E74',
28    amount: 2000,
29    suggestedParams,
30  });
31
32  const txs = [txn1, txn2];
33  algosdk.assignGroupID(txs);
34
35  const txn1B64 = Buffer.from(txn1.toByte()).toString('base64');
36  const txn2B64 = Buffer.from(txn2.toByte()).toString('base64');
37
38  const txn = [
39    {txn: txn1B64},
40    {txn: txn2B64},
41  ];
42
43  const signedTX = await magic.algorand.signGroupTransactionV2(txn);
44
45  console.log('sign group transaction v2', signedTX);

#Resources