@algodex/algodex-sdk-patch
Version:
API calls for interacting with the Algorand blockchain
149 lines (115 loc) • 4.99 kB
JavaScript
//
// USAGE:
// node scripts/updateApp.js --environment=[local|test|production] --appId=[appId]
// --orderbook=[algo|asa] --sender=[sender] --saveToDisk=[true|false]
const algosdk = require('algosdk');
const algodex = require('../index.js');
const constants = require('../constants.js');
const fs = require('fs');
// user declared account mnemonics
//fund the two accounts below before creating
//WYWRYK42XADLY3O62N52BOLT27DMPRA3WNBT2OBRT65N6OEZQWD4OSH6PI
//const creatorMnemonic = "mass army warrior number blush distance enroll vivid horse become spend asthma hat desert amazing room asset ivory lucky ridge now deputy erase absorb above";
//UUEUTRNQY7RUXESXRDO7HSYRSJJSSVKYVB4DI7X2HVVDWYOBWJOP5OSM3A
//const userMnemonic = "three satisfy build purse lens another idle fashion base equal echo recall proof hill shadow coach early palm act wealth dawn menu portion above mystery";
// declare application state storage (immutable)
localInts = 2;
localBytes = 1;
globalInts = 0;
globalBytes = 1;
// user declared approval program (initial)
// helper function to compile program source
async function compileProgram(client, programSource) {
let encoder = new TextEncoder();
let programBytes = encoder.encode(programSource);
let compileResponse = await client.compile(programBytes).do();
let compiledBytes = new Uint8Array(Buffer.from(compileResponse.result, "base64"));
return compiledBytes;
}
const getCreatorAccount = (mnemonic, fromAddr) => {
let accountStr = null;
let account = null;
if (mnemonic) {
account = algosdk.mnemonicToSecretKey(mnemonic);
} else {
accountStr = fromAddr;
account = {
addr: fromAddr,
sk: null
}
}
if (mnemonic && fromAddr && fromAddr != accountStr) {
throw 'fromAddr does not match mnemonic addr!';
}
return account;
}
// create new application
async function updateApp(client, appId, creatorAccount, approvalProgram, clearProgram, saveToDisk=false) {
// define sender as creator
sender = creatorAccount.addr;
// declare onComplete as NoOp
onComplete = algosdk.OnApplicationComplete.NoOpOC;
// get node suggested parameters
let params = await client.getTransactionParams().do();
// create unsigned transaction
let txn = algosdk.makeApplicationUpdateTxn(sender, params, appId,
approvalProgram, clearProgram);
let txId = txn.txID().toString();
if (saveToDisk) {
fs.writeFileSync('./unsigned.txn', algosdk.encodeUnsignedTransaction( txn ));
console.log("Saved [unsigned.txn] to disk! returning early");
return -1;
}
// Sign the transaction
let signedTxn = txn.signTxn(creatorAccount.sk);
console.log("Signed transaction with txID: %s", txId);
// Submit the transaction
await client.sendRawTransaction(signedTxn).do();
// Wait for confirmation
console.log("waiting for confirmation...");
await algodex.waitForConfirmation(txId);
// display results
console.log("Updated app-id: ",appId);
return appId;
}
async function main() {
try {
const args = require('minimist')(process.argv.slice(2))
const environment = args['environment'];
const orderbook = args['orderbook'];
const mnemonic = args['mnemonic'];
const fromAddr = args['sender'];
const saveToDisk = args['saveToDisk'] === 'true';
const inputAppId = args['appId'];
let approvalProgramSourceInitial = null;
// asa or algo
if (orderbook === 'algo') {
approvalProgramSourceInitial = algodex.getAlgoOrderBookTeal();
} else {
approvalProgramSourceInitial = algodex.getAsaOrderBookTeal();
}
if (constants.DEBUG_SMART_CONTRACT_SOURCE) {
console.log(approvalProgramSourceInitial);
}
// initialize an algodClient
let algodClient = algodex.initAlgodClient(environment);
algodex.initIndexer(environment);
const appId = inputAppId || algodex.getOrderBookId(orderbook === 'algo');
let creatorAccount = getCreatorAccount(mnemonic, fromAddr);
console.log({creatorAccount});
// declare clear state program source
clearProgramSource = `#pragma version 2
int 1
`;
// compile programs
let approvalProgram = await compileProgram(algodClient, approvalProgramSourceInitial);
let clearProgram = await compileProgram(algodClient, clearProgramSource);
// create new application
await updateApp(algodClient, appId, creatorAccount, approvalProgram, clearProgram, saveToDisk);
console.log( "APPID="+appId);
}
catch (err){
console.log("err", err);
}
}
main();