-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcease.js
94 lines (81 loc) · 3.18 KB
/
cease.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
const { balanceOf } = require("@algo-builder/algob");
const { types } = require("@algo-builder/web");
const accounts = require("../common/accounts");
const { fundAccount, optInAccountToApp, tryExecuteTx } = require("../common/common");
const { issue } = require("./issue");
const { whitelist } = require("../permissions/whitelist");
/**
* If there is a crime evidence, we will need to be able to cease assets back to the issuer.
* NOTE: this is similar to force transfer, but receiver is the asset-reserve.
* @param {string} address address to cease tokens from
* @param {number | bigint} amount amount of tokens to cease
*/
async function cease(deployer, address, amount) {
const owner = deployer.accountsByName.get(accounts.owner);
const tesla = deployer.asa.get("tesla");
const controllerAppInfo = deployer.getApp("Controller");
const clawbackLsig = deployer.getLsig("ClawbackLsig");
const clawbackAddress = clawbackLsig.address();
const asaReserveAddress = (await deployer.getAssetByID(tesla.assetIndex)).params.reserve;
// similar to forceTransfer group, but receiver is asaReserveAddress, and call to permissions
// is not required
const ceaseTxGroup = [
/*
* tx 0 - Call to controller stateful smart contract (by ASA.manager)
* with application arg: 'force_transfer' */
{
type: types.TransactionType.CallApp,
sign: types.SignType.SecretKey,
fromAccount: owner,
appID: controllerAppInfo.appID,
payFlags: { totalFee: 1000 },
appArgs: ["str:force_transfer"],
foreignAssets: [tesla.assetIndex], // to verify token reserve, manager
},
/*
* tx 1 - Asset transfer transaction from sender -> receiver (= asset reserve), using clawbackLsig. */
{
type: types.TransactionType.RevokeAsset,
sign: types.SignType.LogicSignature,
fromAccountAddr: clawbackAddress,
recipient: asaReserveAddress,
assetID: tesla.assetIndex,
revocationTarget: address,
amount: amount,
lsig: clawbackLsig,
payFlags: { totalFee: 1000 },
},
/*
* tx 2 - cover fee of tx1 */
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.SecretKey,
fromAccount: owner,
toAccountAddr: clawbackAddress,
amountMicroAlgos: 1000,
payFlags: { totalFee: 1000 },
},
];
console.log(`* Ceasing ${amount} tokens from [${address}] *`);
await tryExecuteTx(deployer, ceaseTxGroup);
console.log(`* ${address} asset holding after cease: *`);
console.log(await balanceOf(deployer, address, tesla.assetIndex));
console.log("* Cease Successful *");
}
async function run(runtimeEnv, deployer) {
const owner = deployer.accountsByName.get(accounts.owner);
const permissionsManager = owner;
const permissionsAppInfo = deployer.getApp("Permissions");
// fund owner and bob
const bob = deployer.accountsByName.get("bob");
await fundAccount(deployer, [owner, bob]);
// whitelist bob
optInAccountToApp(deployer, bob, permissionsAppInfo.appID, {}, {});
await whitelist(deployer, permissionsManager, bob.addr);
// let's issue few tokens to bob
deployer.optInAccountToASA("tesla", bob.name, {});
await issue(deployer, bob.addr, 300);
// cease 250 tokens as penalty back from bob (eg. bob was fired)
await cease(deployer, bob.addr, 250);
}
module.exports = { default: run };