-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (112 loc) · 3.34 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
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
const Web3 = require('web3');
const contract = require('./contracts/abi.json');
const abi = contract.abi;
const config =
{
testnet:{
rpcUrl: "",
contractAddress: ""
},
mainnet:{
rpcUrl: "https://rpc.astar.network:8545",
contractAddress: "0xA1019535E6b364523949EaF45F4B17521c1cb074"
},
defaultNetwork: "mainnet"
}
const defaultKeys = ["avatar","cover","website","email","social:twitter","social:facebook","social:telegram","social:discord","social:instagram"];
var exports=module.exports={};
exports.SDK = function (options) {
var _config = config;
if (options){
_config = options;
}
var rpcUrl = config.mainnet.rpcUrl;
var contractAddress = config.mainnet.contractAddress;
if (_config.defaultNetwork == 'testnet'){
rpcUrl = _config.testnet.rpcUrl;
contractAddress = _config.testnet.contractAddress;
if (typeof contractAddress == 'undefined'){
contractAddress = _config.testnet.contactAddress;
}
}
if (_config.defaultNetwork == 'mainnet'){
rpcUrl = _config.mainnet.rpcUrl;
contractAddress = _config.mainnet.contractAddress;
if (typeof contractAddress == 'undefined'){
contractAddress = _config.mainnet.contactAddress;
}
}
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const contractFirst = new web3.eth.Contract(abi, contractAddress);
const func = new Object();
func.balanceOf = async (address) =>
{
const balance = await contractFirst.methods.balanceOf(address).call();
return balance;
}
func.getOwner = async (domain, metadata = false) =>
{
const ownerAddress = await contractFirst.methods.getOwner(domain).call();
const obj = new Object();
obj.owner = ownerAddress;
obj.native = "";
var arg = [];
if (metadata == true){
const tokenId = await contractFirst.methods.genTokenId(domain).call();
const values = await contractFirst.methods.getMany(defaultKeys, tokenId).call();
for (let i = 0; i < defaultKeys.length; ++i) {
const _obj = new Object();
_obj.key = defaultKeys[i];
_obj.value = values[i];
arg.push(_obj)
}
}
obj.metadata = arg;
return obj;
}
func.getDomain = async (_address) =>
{
try{
const defaultDomain = await contractFirst.methods.reverseOf(_address).call();
return defaultDomain
}catch{}
return "";
}
func.getDomains = async (address) =>
{
const domains = [];
try{
const arg = await contractFirst.methods.getDomainbyAddress(address).call();
return arg.domains;
}catch{}
return domains;
}
func.getMetadata = async (key, domain) =>
{
const tokenId = await contractFirst.methods.genTokenId(domain).call();
var value = await contractFirst.methods.get(key, tokenId).call();
var obj = new Object();
obj.key = key
obj.value = value
return obj;
}
func.getMetadatas = async (keys, domain) =>
{
const tokenId = await contractFirst.methods.genTokenId(domain).call();
const values = await contractFirst.methods.getMany(keys, tokenId).call();
var arg = [];
for (let i = 0; i < keys.length; ++i) {
var obj = new Object();
obj.key = keys[i];
obj.value = values[i];
arg.push(obj)
}
return arg;
}
func.hashname = async (domain) =>
{
const hash = await contractFirst.methods.genTokenId(domain).call();
return hash;
}
return func;
}