-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (56 loc) · 1.73 KB
/
index.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
const Web3 = require("web3")
class erc721 {
constructor(network, api, address) {
this.mainnet = network
this.web3 = new Web3(new Web3.providers.HttpProvider(this.mainnet));
this.Contract = new this.web3.eth.Contract(api, address);
}
contractCall(func, args) {
if (Array.isArray(args)) {
let funcStr = "this.Contract.methods[func](" + JSON.stringify(args).replace("[", "").replace("]", "") + ")"
return eval(funcStr)
}
// in case someone decides to pass a single arg
else {
return this.Contract.methods[func](args)
}
}
async getTokens(addr) {
var tokens = []
// get number of tokens addr has
var numTokens = await this.contractCall("balanceOf", addr).call()
// increment through tokens
for (let i = 0; i < numTokens; i++) {
// returns a token id
let currentToken = await this.contractCall("tokenOfOwnerByIndex", [addr, i]).call()
// gets token uri given token id
let tokenURI = await this.contractCall("tokenURI", currentToken).call()
tokens.push({
tokenId: currentToken,
tokenURI: tokenURI
})
}
return tokens
}
}
class contract {
constructor(network, api, address) {
this.mainnet = network
this.web3 = new Web3(new Web3.providers.HttpProvider(this.mainnet));
this.Contract = new this.web3.eth.Contract(api, address);
}
contractCall(func, args) {
if (Array.isArray(args)) {
let funcStr = "this.Contract.methods[func](" + JSON.stringify(args).replace("[", "").replace("]", "") + ")"
return eval(funcStr)
}
// in case someone decides to pass a single arg
else {
return this.Contract.methods[func](args)
}
}
}
module.exports = {
erc721,
default: contract
}