-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.js
164 lines (152 loc) · 6.85 KB
/
algo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// import algosdk
const algosdk = require('algosdk');
const fs = require('fs');
const path = require('path');
// API server address and API token
const server = 'https://testnet-algorand.api.purestake.io/ps2';
const port = '';
const token = {
'X-API-Key': '99k6O5HNSJ2OdQeglygHq1CcUceSa0Swa5zWErTI'
}
// instantiate the algorand client
const algodclient = new algosdk.Algodv2(token, server, port);
// Utility function to wait for tx confirmaiton
function waitForConfirmation(algodclient, txId) {
var p = new Promise(function (resolve, reject) {
console.log("Waiting transaction: " + txId + " to be confirmed...");
var counter = 1000;
let interval = setInterval(() => {
if (--counter === 0) reject("Confirmation Timeout");
algodclient.pendingTransactionInformation(txId).do().then((pendingInfo) => {
if (pendingInfo != undefined) {
let confirmedRound = pendingInfo["confirmed-round"];
if (confirmedRound !== null && confirmedRound > 0) {
clearInterval(interval);
resolve("Transaction confirmed in round " + confirmedRound);
}
}
}).catch(reject);
}, 2000);
});
return p;
}
// Function for sending payment transaction
function sendPaymentTransaction(account, to, amount) {
var p = new Promise(function (resolve) {
// use closeRemainderTo paramerter when you want to close an account
let closeRemainderTo = undefined;
// use note parameter when you want to attach a string to the transaction
let note = undefined;
algodclient.getTransactionParams().do().then((params) => {
let txn = algosdk.makePaymentTxnWithSuggestedParams(account.addr, to, amount, closeRemainderTo, note, params);
// sign the transaction
var signedTxn = algosdk.signTransaction(txn, account.sk);
algodclient.sendRawTransaction(signedTxn.blob).do().then((tx) => {
waitForConfirmation(algodclient, tx.txId)
.then(resolve)
.catch(console.log);
}).catch(console.log);
}).catch(console.log);
})
return p;
}
// Function for sending an asset creation transaction
function createAsset(account, assetName, unitName, decimals, totalIssuance, assetUrl, assetMetadataHash, manager, reserve, freeze, clawback, defaultFrozen) {
var p = new Promise(function (resolve, reject) {
// get chain parameters for sending transactions
algodclient.getTransactionParams().do().then((params) => {
// use note parameter when you want to attach a string to the transaction
let note = undefined;
let assetMetadataHash = undefined;
// construct the asset creation transaction
let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(account.addr, note, totalIssuance, decimals, defaultFrozen,
manager, reserve, freeze, clawback, unitName, assetName, assetUrl, assetMetadataHash, params);
var signedTxn = algosdk.signTransaction(txn, account.sk);
algodclient.sendRawTransaction(signedTxn.blob).do().then((tx) => {
waitForConfirmation(algodclient, tx.txId).then((msg) => {
console.log(msg);
algodclient.pendingTransactionInformation(tx.txId).do().then((ptx) => {
// get the asset ID
let assetId = ptx["asset-index"];
resolve(assetId);
}).catch(reject);
}).catch(console.log);
}).catch(console.log);
}).catch(reject);
})
return p;
}
// Function for sending asset transaction
function sendAssetTransaction(account, to, amount, assetId) {
var p = new Promise(function (resolve) {
// use closeRemainderTo paramerter when you want to close an account
let closeRemainderTo = undefined;
// use note parameter when you want to attach a string to the transaction
let note = undefined;
// use revocationTarget when you want to clawback assets
let revocationTarget = undefined;
algodclient.getTransactionParams().do().then((params) => {
let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(account.addr, to, closeRemainderTo, revocationTarget,
amount, note, assetId, params);
// sign the transaction
var signedTxn = algosdk.signTransaction(txn, account.sk);
algodclient.sendRawTransaction(signedTxn.blob).do().then((tx) => {
waitForConfirmation(algodclient, tx.txId)
.then(resolve)
.catch(console.log);
}).catch(console.log);
}).catch(console.log);
})
return p;
}
function compileContract(contractDir) {
var p = new Promise(function (resolve) {
// read the contract file
const filePath = path.join(__dirname, contractDir);
const data = fs.readFileSync(filePath);
// Compile teal contract
algodclient.compile(data).do().then(resolve).catch(console.log);
})
return p;
}
function createApp(account,approvalProgram, clearProgram,appArgs) {
var p = new Promise(function (resolve) {
algodclient.getTransactionParams().do().then((params) => {
let onComplete = algosdk.OnApplicationComplete.NoOpOC;
const localInts = 1;
const localBytes = 0;
const globalInts = 5;
const globalBytes = 3;
let txn = algosdk.makeApplicationCreateTxn(account.addr,params,onComplete,approvalProgram,clearProgram,localInts,localBytes,globalInts,globalBytes,appArgs);
var signedTxn = algosdk.signTransaction(txn, account.sk);
algodclient.sendRawTransaction(signedTxn.blob).do().then((tx) => {
waitForConfirmation(algodclient, tx.txId)
.then(resolve)
.catch(console.log);
}).catch(console.log);
}).catch(console.log);
})
return p;
}
function updateApp(account,appId,approvalProgram, clearProgram, appArgs) {
var p = new Promise(function (resolve) {
algodclient.getTransactionParams().do().then((params) => {
let txn = algosdk.makeApplicationUpdateTxn(account.addr,params,appId,approvalProgram,clearProgram, appArgs);
var signedTxn = algosdk.signTransaction(txn, account.sk);
algodclient.sendRawTransaction(signedTxn.blob).do().then((tx) => {
waitForConfirmation(algodclient, tx.txId)
.then(resolve)
.catch(console.log);
}).catch(console.log);
}).catch(console.log);
})
return p;
}
module.exports = {
sendPaymentTransaction,
createAsset,
sendAssetTransaction,
compileContract,
createApp,
updateApp
}