How to use algosdk - 10 common examples

To help you get started, we’ve selected a few algosdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PureStake / api-examples / javascript-examples / v2 / indexer_asset_search.js View on Github external
// Algorand Indexer (v2) example 
// Demonstrate an Indexer Asset Search with pagination on TestNet

const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/idx2";
const port = "";

const token = {
    'X-API-key' : 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}

let indexerClient = new algosdk.Indexer(token, baseServer, port);

(async()=> {

    let i =5;

    let assetInfo = await indexerClient.searchForAssets().limit(1).name('test').do()
    console.log(assetInfo)

    while(i && ('next-token' in assetInfo)) {
        assetInfo = await indexerClient.searchForAssets().limit(1).name('test').nextToken(assetInfo['next-token']).do();
        i--;
        console.log(assetInfo) // Array of assets object
        console.log(assetInfo.assets[0]) // An asset returned
    }

})().catch(e => {
github PureStake / api-examples / javascript-examples / get_versions.js View on Github external
// 
// Code borrowed and extended from https://developer.algorand.org/docs/javascript-sdk
//
// Example using PureStake token object, replacing the token as a string
//

// DEPRECATED 12/30/20 with Algod v1 API Shutdown

const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps1"
const port = "";
const token = {
    'X-API-Key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab'
}

const algodclient = new algosdk.Algod(token, baseServer, port);

(async () => {
    console.log(await algodclient.versions());
})().catch(e => {
    console.log(e);
});
github PureStake / api-examples / javascript-examples / submit_tx.js View on Github external
// Both algodclient objects are necessary, as the params call is application/json and the SDK does not allow by request header assignment
// The TestNet account in this example is U2VHSZL3LNGATL3IBCXFCPBTYSXYZBW2J4OGMPLTA4NA2CB4PR7AW7C77E - PureStake cannot guarantee any funds will exist to execute this example
// To add Algo's - https://bank.testnet.algorand.network

// DEPRECATED 12/30/20 with Algod v1 API Shutdown


const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps1"
const port = "";

const token = {
    'X-API-key' : 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}

const algodclient = new algosdk.Algod(token, baseServer, port); 

var mnemonic = "code thrive mouse code badge example pride stereo sell viable adjust planet text close erupt embrace nature upon february weekend humble surprise shrug absorb faint"; 
var recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic); 
console.log(recoveredAccount.addr);


(async() => {

    let params = await algodclient.getTransactionParams();
    let endRound = params.lastRound + parseInt(1000);

    let txn = {
        "from": recoveredAccount.addr,
        "to": "AEC4WDHXCDF4B5LBNXXRTB3IJTVJSWUZ4VJ4THPU2QGRJGTA3MIDFN3CQA",
        "fee": 10,
        "amount": 2,
github PureStake / api-examples / javascript-examples / get_tx.js View on Github external
// 
// Code borrowed and extended from https://developer.algorand.org/docs/javascript-sdk
//
// Example using PureStake token object 
//

// DEPRECATED 12/30/20 with Algod v1 API Shutdown

const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps1"
const port = "";
const token = {
    'X-API-Key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab'
}

const algodclient = new algosdk.Algod(token, baseServer, port);

(async () => {
    console.log(await algodclient.transactionById('3J7Z46VKGJ6VATCHOE3I2JZXVDXKAWOKO3JJCR6R7EEX5VBKEATQ'));
})().catch(e => {
    console.log(e);
});
github PureStake / api-examples / javascript-examples / submit_tx.js View on Github external
// DEPRECATED 12/30/20 with Algod v1 API Shutdown


const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps1"
const port = "";

const token = {
    'X-API-key' : 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}

const algodclient = new algosdk.Algod(token, baseServer, port); 

var mnemonic = "code thrive mouse code badge example pride stereo sell viable adjust planet text close erupt embrace nature upon february weekend humble surprise shrug absorb faint"; 
var recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic); 
console.log(recoveredAccount.addr);


(async() => {

    let params = await algodclient.getTransactionParams();
    let endRound = params.lastRound + parseInt(1000);

    let txn = {
        "from": recoveredAccount.addr,
        "to": "AEC4WDHXCDF4B5LBNXXRTB3IJTVJSWUZ4VJ4THPU2QGRJGTA3MIDFN3CQA",
        "fee": 10,
        "amount": 2,
        "firstRound": params.lastRound,
        "lastRound": endRound,
        "genesisID": params.genesisID,
github PureStake / api-examples / javascript-examples / logicSig.js View on Github external
// Updated for v2 1/20/21 


const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps2"
const port = "";
const token = {
    'X-API-key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}


const algodclient = new algosdk.Algodv2(token, baseServer, port);

var mnemonic = "code thrive mouse code badge example pride stereo sell viable adjust planet text close erupt embrace nature upon february weekend humble surprise shrug absorb faint";
var recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic);
console.log(recoveredAccount.addr);

(async () => {
    //Get the relevant params from the algod
    let params = await algodclient.getTransactionParams().do();

    // move the TEAL  program into Uint8Array
    let program = new Uint8Array(Buffer.from("ASABASI=", "base64"));
    let lsig = algosdk.makeLogicSig(program);
    lsig.sign(recoveredAccount.sk);


    //create a transaction
    let txn = {
        "from": recoveredAccount.addr,
        "to": "SOEI4UA72A7ZL5P25GNISSVWW724YABSGZ7GHW5ERV4QKK2XSXLXGXPG5Y",
github PureStake / api-examples / javascript-examples / logicSig.js View on Github external
// Both algodclient objects are necessary, as the params call is application/json and the SDK does not allow by request header assignment
// The TestNet account in this example is U2VHSZL3LNGATL3IBCXFCPBTYSXYZBW2J4OGMPLTA4NA2CB4PR7AW7C77E - PureStake cannot guarantee any funds will exist to execute this example
// To add Algo's - https://bank.testnet.algorand.network

// Updated for v2 1/20/21 


const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/ps2"
const port = "";
const token = {
    'X-API-key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}


const algodclient = new algosdk.Algodv2(token, baseServer, port);

var mnemonic = "code thrive mouse code badge example pride stereo sell viable adjust planet text close erupt embrace nature upon february weekend humble surprise shrug absorb faint";
var recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic);
console.log(recoveredAccount.addr);

(async () => {
    //Get the relevant params from the algod
    let params = await algodclient.getTransactionParams().do();

    // move the TEAL  program into Uint8Array
    let program = new Uint8Array(Buffer.from("ASABASI=", "base64"));
    let lsig = algosdk.makeLogicSig(program);
    lsig.sign(recoveredAccount.sk);


    //create a transaction
github PureStake / api-examples / javascript-examples / v2 / indexer_block.js View on Github external
// Algorand Indexer (v2) example 
// Retrieve block - TestNet

const algosdk = require('algosdk');
const baseServer = "https://testnet-algorand.api.purestake.io/idx2";
const port = "";

const token = {
    'X-API-key' : 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab',
}

let indexerClient = new algosdk.Indexer(token, baseServer, port);

(async()=> {
    let blockInfo = await indexerClient.lookupBlock(5).do()
    console.log(blockInfo)
})().catch(e => {
    console.log(e);
});
github BitGo / BitGoJS / modules / core / src / v2 / coins / algo.ts View on Github external
signMessage(key: KeyPair, message: string | Buffer): Buffer {
    // key.prv actually holds the encoded seed, but we use the prv name to avoid breaking the keypair schema.
    // See jsdoc comment in isValidPrv
    let seed = key.prv;
    if (!this.isValidPrv(seed)) {
      throw new Error(`invalid seed: ${seed}`);
    }
    if (typeof seed === 'string') {
      try {
        seed = Seed.decode(seed).seed;
      } catch (e) {
        throw new Error(`could not decode seed: ${seed}`);
      }
    }
    const keyPair = generateAccountFromSeed(seed);

    if (!Buffer.isBuffer(message)) {
      message = Buffer.from(message);
    }

    return Buffer.from(NaclWrapper.sign(message, keyPair.sk));
  }
github BitGo / BitGoJS / modules / core / src / v2 / coins / algo.ts View on Github external
generateKeyPair(seed?: Buffer): KeyPair {
    const pair = seed ? generateAccountFromSeed(seed) : generateAccount();
    return {
      pub: pair.addr, // encoded pub
      prv: Seed.encode(pair.sk), // encoded seed
    };
  }