-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrevokeAllSerialAllowances.js
230 lines (191 loc) · 6.12 KB
/
revokeAllSerialAllowances.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import fetch from 'cross-fetch';
import dotenv from 'dotenv';
import { AccountAllowanceApproveTransaction, AccountId, Client, PrivateKey, TokenId } from '@hashgraph/sdk';
import readlineSync from 'readline-sync';
dotenv.config();
const maxRetries = 10;
let operatorId = process.env.MY_ACCOUNT_ID;
let privateKey = process.env.MY_PRIVATE_KEY;
let env = process.env.ENV || 'mainnet';
let client;
let mirrornode;
if (!operatorId || !privateKey) {
console.error('Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present');
process.exit(1);
}
operatorId = AccountId.fromString(operatorId);
try {
privateKey = PrivateKey.fromStringED25519(privateKey);
}
catch (err) {
try {
privateKey = PrivateKey.fromStringECDSA(privateKey);
}
catch (err) {
console.error('Environment variable MY_PRIVATE_KEY must be a valid Ed25519 or ECDSA private key');
process.exit(1);
}
}
async function main() {
prepareEnv();
console.log('Operator:', operatorId.toString());
console.log('Environment:', env);
// get all NFTs approved for all by the operator
const tokenSpenderMap = await getApprovedForAll(operatorId.toString());
// consolidate all spenders to display a summary of speender # of allowances
const spenderMap = new Map();
for (const spender of tokenSpenderMap.values()) {
const count = spenderMap.get(spender) || 0;
spenderMap.set(spender, count + 1);
}
console.log('Spender Map', spenderMap);
// based on the keys of Spender Map ask user which contract they want to revoke all allowances from
const spenderKeys = Array.from(spenderMap.keys());
const spenderIndex = readlineSync.keyInSelect(spenderKeys, 'Select spender to revoke all allowances from');
if (spenderIndex == -1) {
console.log('No spender selected');
return;
}
const spender = spenderKeys[spenderIndex];
// get all tokens approved for all by the operator for the selected spender
const tokens = [];
for (const [token, s] of tokenSpenderMap) {
if (s == spender) {
tokens.push(TokenId.fromString(token));
}
}
console.log('Revoking all allowances for', spender, 'on', tokens.length, 'tokens');
// revoke all allowances
await revokeAllAllowances(operatorId, tokens, spender);
await sleep(5000);
// check if all allowances for that spender are revoked
const tokenSpenderMapAfter = await getApprovedForAll(operatorId.toString());
const tokensAfter = [];
for (const [token, s] of tokenSpenderMapAfter) {
if (s == spender) {
tokensAfter.push(token);
}
}
console.log(`Tokens with allowances after for Spender [${spender}] = tokensAfter.length`);
if (tokensAfter.length > 0) {
console.log('Tokens with allowances after:', tokensAfter);
}
else {
console.log('All allowances revoked');
}
}
/**
* @param {AccountId | string} owner
* @param {NftId[]} tokens
* @param {AccountId | string} spender
*/
async function revokeAllAllowances(owner, tokens, spender) {
// split tokens into batches of 20
const batchSize = 20;
const batches = [];
for (let i = 0; i < tokens.length; i += batchSize) {
batches.push(tokens.slice(i, i + batchSize));
}
for (const batch of batches) {
const revokeTx = new AccountAllowanceApproveTransaction();
for (const token of batch) {
console.log('Revoking all allowances for', token.toString(), 'to Spender:', spender);
revokeTx.deleteTokenNftAllowanceAllSerials(token, owner, spender);
}
const txResponse = await revokeTx.execute(client);
const receipt = await txResponse.getReceipt(client);
console.log('Receipt:', receipt.status.toString(), 'Tx Id:', txResponse.transactionId.toString());
}
}
async function getApprovedForAll(owner) {
let url = `${mirrornode}/api/v1/accounts/${owner}/allowances/nfts?limit=100`;
const tokenSpenderMap = new Map();
while (url) {
const json = await fetchJson(url);
if (json == null) {
console.log('FATAL ERROR: no NFTs found', url);
// unlikely to get here but a sensible default
return;
}
const allowances = json.allowances;
for (let n = 0; n < allowances.length; n++) {
const value = allowances[n];
if (value.approved_for_all) {
tokenSpenderMap.set(value.token_id, value.spender);
}
}
url = json.links.next;
}
return tokenSpenderMap;
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) return null;
depth++;
try {
const res = await fetchWithTimeout(url);
if (res.status != 200) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
return res.json();
}
catch (err) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
}
function prepareEnv() {
if (env.toUpperCase() == 'TEST' || env.toUpperCase() == 'TESTNET') {
client = Client.forTestnet();
env = 'testnet';
mirrornode = 'https://testnet.mirrornode.hedera.com';
console.log('testing in *TESTNET*');
}
else if (env.toUpperCase() == 'MAIN' || env.toUpperCase() == 'MAINNET') {
client = Client.forMainnet();
env = 'mainnet';
mirrornode = 'https://mainnet.mirrornode.hedera.com';
console.log('running in *MAINNET*');
}
else if (env.toUpperCase() == 'PREVIEW' || env.toUpperCase() == 'PREVIEWNET') {
client = Client.forPreviewnet();
env = 'previewnet';
mirrornode = 'https://previewnet.mirrornode.hedera.com';
console.log('testing in *PREVIEWNET*');
}
else if (env.toUpperCase() == 'LOCAL') {
const node = { '127.0.0.1:50211': new AccountId(3) };
env = 'local';
client = Client.forNetwork(node).setMirrorNetwork('127.0.0.1:5600');
mirrornode = 'http://127.0.0.1:5600';
console.log('testing in *LOCAL*');
}
else {
console.log(
'ERROR: Must specify either MAIN or TEST or PREVIEW or LOCAL as environment in .env file',
);
return;
}
client.setOperator(operatorId, privateKey);
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 5000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main().then(() => {
console.log('Done');
process.exit(0);
}).catch(err => {
console.error(err);
process.exit(1);
});