-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgold-delegated-lsig.js
72 lines (63 loc) · 2.08 KB
/
gold-delegated-lsig.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
/**
* Description:
* This file demonstrates the example to transfer Algorand Standard Assets(ASA) & MicroAlgos
* using delegated lsig (between 2 user accounts).
*/
const { tryExecuteTx } = require("./common");
const { types } = require("@algo-builder/web");
async function run(runtimeEnv, deployer) {
const goldOwner = deployer.accountsByName.get("alice");
const john = deployer.accountsByName.get("john");
const bob = deployer.accountsByName.get("bob");
// Transactions for GOLD ASA contract : '4-gold-asa.teal' (Delegated Approval Mode)
const lsigGoldOwner = deployer.getLsig("Gold_d_asa_lsig");
let txnParam = {
type: types.TransactionType.TransferAsset,
sign: types.SignType.LogicSignature,
fromAccountAddr: goldOwner.addr,
toAccountAddr: john.addr,
amount: 500,
assetID: "gold", // passing asa name is also supported
lsig: lsigGoldOwner,
payFlags: { totalFee: 1000 },
};
// Transaction PASS
await tryExecuteTx(deployer, txnParam);
// Transaction FAIL - rejected by lsig because amount is not <= 1000
txnParam.amount = 1500;
try {
await tryExecuteTx(deployer, txnParam);
} catch (e) {
console.error(e);
}
// Transaction FAIL - rejected by lsig because sender must be the delegator i.e
// account which signed the lsig (goldOwner in this case)
txnParam.amount = 100;
txnParam.fromAccountAddr = bob.addr;
try {
await tryExecuteTx(deployer, txnParam);
} catch (e) {
console.error(e);
}
// Transaction for ALGO - Contract : '3-gold-delegated-asc.teal' (Delegated Approval Mode)
const logicSignature = deployer.getLsig("Gold_D_Lsig");
txnParam = {
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
fromAccountAddr: goldOwner.addr,
toAccountAddr: bob.addr,
amountMicroAlgos: 58,
lsig: logicSignature,
payFlags: { totalFee: 1000 },
};
// Transaction PASS
await tryExecuteTx(deployer, txnParam);
// Transaction FAIL - rejected by lsig because amount is not <= 100
txnParam.amountMicroAlgos = 580;
try {
await tryExecuteTx(deployer, txnParam);
} catch (e) {
console.error(e);
}
}
module.exports = { default: run };