-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayc_bc_api.js
executable file
·121 lines (112 loc) · 3.87 KB
/
bayc_bc_api.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
const version = '0.0.1';
const ethers = require('ethers');
const request = require("got");
module.exports = {
headersForRequest: [],
bcAddress: '',
bcHttpProvider: null,
privateKey: '',
contractABI: '',
contractAddress: '',
contract: null,
contractVersion: '',
config: '',
setConfig: function (configGlobal) {
this.config = configGlobal.config;
this.bcAddress = this.config.chain.bcAddressProd;
this.contractAddress = this.config.token.contractAddressProd;
this.contractABI = this.config.token.pathToABIProd;
this.privateKey = this.config.chain.privateKeyProd;
},
getABI: async function (URI) {
return new Promise(async (resolve, reject) => {
let requestSettings = {
method: 'GET',
url: this.config.chain.bcGetABI + 'api?module=contract&action=getabi&address=' + this.config.token.contractAddressProd
};
let objRet = {
err: '',
result: ''
};
console.log('Retrieving ABI ...')
let response = await request(requestSettings).catch((err) => {
console.error(err, 'request error');
objRet.err = err;
});
if (response) {
let body = response.body;
objRet.result = JSON.parse(body);
}
resolve(objRet);
});
},
init: async function () {
return new Promise(async (resolve, reject) => {
console.log(this.bcAddress, "Blockchain");
//this.contractABI = JSON.parse(fs.readFileSync(this.contractABI).toString());
let ABIFromUrl = await this.getABI();
this.contractABI = JSON.parse(ABIFromUrl.result.result);
console.log(this.contractABI);
this.bcHttpProvider = new ethers.providers.JsonRpcProvider(this.bcAddress);
this.bcHttpProvider.getBlockNumber().then((result) => {
console.log(result, "Current block number");
});
this.contract = new ethers.Contract(
this.contractAddress,
this.contractABI,
this.bcHttpProvider
);
resolve(await this.name());
});
},
name: function () {
return new Promise(async (resolve, reject) => {
this.contract.name()
.then((result) => {
console.log(result, "name token");
resolve(result);
})
.catch(err => {
console.error(err.reason, "name token");
reject(err.reason);
})
});
},
totalSupply: function () {
return new Promise(async (resolve, reject) => {
this.contract.totalSupply()
.then((result) => {
resolve(result);
})
.catch(err => {
console.error(err.reason, "totalSupply token");
reject(err.reason);
})
});
},
baseURI: function () {
return new Promise(async (resolve, reject) => {
this.contract.baseURI()
.then((result) => {
resolve(result);
})
.catch(err => {
console.error(err.reason, "base URI");
reject(err.reason);
})
});
},
tokenURI: function (_tokenID) {
return new Promise(async (resolve, reject) => {
let tokenID = _tokenID;
this.contract.tokenURI(tokenID)
.then((result) => {
resolve(result);
})
.catch(err => {
console.error(err.reason, "token URI");
reject(err.reason);
})
});
}
};