-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-contract.ts
171 lines (144 loc) · 4.68 KB
/
validate-contract.ts
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
import axios from 'axios'
const apikey = 'your etherscan api key';
enum Standard {
ERC721 = 'erc721',
ERC1155 = 'erc1155',
UNKNOWN = 'unknown'
}
// 721
function checkTransferEvent(abi: any[]): boolean {
let item = abi.find(item => item.name === 'Transfer' && item.type === 'event');
if (!item) return false;
const expect = [
{ indexed: true, type: 'address' },
{ indexed: true, type: 'address' },
{ indexed: true, type: 'uint256' },
];
if (item.inputs.length !== expect.length) return false;
for (let i = 0; i < expect.length; i++) {
if (item.inputs[i].indexed !== expect[i].indexed || item.inputs[i].type !== expect[i].type) return false;
}
return true;
}
// 721
function checkTokenURI(abi: any[]) {
let item = abi.find(item => item.name === 'tokenURI' && item.type === 'function');
if (!item) return false;
if (item.inputs.length !== 1) return false;
if (item.inputs[0].type !== 'uint256') return false;
if (item.outputs.length !== 1) return false;
if (item.outputs[0].type !== 'string') return false;
return true;
}
// 1155
function checkTransferSingleEvent(abi: any[]): boolean {
let item = abi.find(item => item.name === 'TransferSingle' && item.type === 'event');
if (!item) return false;
const expect = [
{ indexed: true, type: 'address' },
{ indexed: true, type: 'address' },
{ indexed: true, type: 'address' },
{ indexed: false, type: 'uint256' },
{ indexed: false, type: 'uint256' },
];
if (item.inputs.length !== expect.length) return false;
for (let i = 0; i < expect.length; i++) {
if (item.inputs[i].indexed !== expect[i].indexed || item.inputs[i].type !== expect[i].type) return false;
}
return true;
}
// 1155
function checkTransferBatchEvent(abi: any[]): boolean {
let item = abi.find(item => item.name === 'TransferBatch' && item.type === 'event');
if (!item) return false;
const expect = [
{ indexed: true, type: 'address' },
{ indexed: true, type: 'address' },
{ indexed: true, type: 'address' },
{ indexed: false, type: 'uint256[]' },
{ indexed: false, type: 'uint256[]' },
];
if (item.inputs.length !== expect.length) return false;
for (let i = 0; i < expect.length; i++) {
if (item.inputs[i].indexed !== expect[i].indexed || item.inputs[i].type !== expect[i].type) return false;
}
return true;
}
// 1155
function checkURI(abi: any[]): boolean {
let item = abi.find(item => item.name === 'uri' && item.type === 'function');
if (!item) return false;
if (item.inputs.length !== 1) return false;
if (item.inputs[0].type !== 'uint256') return false;
if (item.outputs.length !== 1) return false;
if (item.outputs[0].type !== 'string') return false;
return true;
}
function check721(abi: any[]) {
const Transfer = checkTransferEvent(abi);
const tokenURI = checkTokenURI(abi);
const pass = Transfer && tokenURI;
return {
pass,
points: {
Transfer,
tokenURI,
},
};
}
function check1155(abi: any[]) {
const TransferSingle = checkTransferSingleEvent(abi);
const TransferBatch = checkTransferBatchEvent(abi);
const uri = checkURI(abi);
const pass = TransferSingle && TransferBatch && uri;
return {
pass,
points: {
TransferSingle,
TransferBatch,
uri,
},
};
}
async function validateContract(contract: string) {
const url = `http://api.etherscan.io/api?module=contract&action=getabi&address=${contract}&apikey=${apikey}`;
const res = await axios.get(url);
const abi = JSON.parse(res.data.result);
const erc721 = check721(abi);
const erc1155 = check1155(abi);
return {
erc721,
erc1155,
};
}
async function run(contracts: string[]) {
for (const contract of contracts) {
const res = await validateContract(contract);
let standard: string;
if (res.erc721.pass) {
standard = Standard.ERC721;
} else if (res.erc1155.pass) {
standard = Standard.ERC1155;
} else {
standard = Standard.UNKNOWN;
}
console.log(`contract: ${contract} standard: ${standard}`);
}
}
const contracts = [
'0x959e104e1a4db6317fa58f8295f586e1a978c297',
'0xc1f4b0eea2bd6690930e6c66efd3e197d620b9c2',
'0xc3af02c0fd486c8e9da5788b915d6fff3f049866',
'0xf64dc33a192e056bb5f0e5049356a0498b502d50',
'0x32b7495895264ac9d0b12d32afd435453458b1c6',
'0xd35147be6401dcb20811f2104c33de8e97ed6818',
'0x3163d2cfee3183f9874e2869942cc62649eeb004',
'0x201c3af8c471e5842428b74d1e7c0249adda2a92',
'0x6a99abebb48819d2abe92c5e4dc4f48dc09a3ee8',
'0x1e1d4e6262787c8a8783a37fee698bd42aa42bec',
'0xbf53c33235cbfc22cef5a61a83484b86342679c5',
'0x75a3752579dc2d63ca229eebbe3537fbabf85a12',
'0x574f64ac2e7215cba9752b85fc73030f35166bc0',
'0x34ed0aa248f60f54dd32fbc9883d6137a491f4f3',
];
run(contracts);