-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
142 lines (117 loc) · 3.33 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
137
138
139
140
141
142
const ethers = require('ethers');
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 provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const signer = provider.getSigner("0x5be02bCFE218f33DD7Fa93EBb5791e925ae7b473");
const contractFirst = new ethers.Contract(
contractAddress,
abi,
signer
);
const func = new Object();
func.balanceOf = async (address) =>
{
const balance = await contractFirst['balanceOf'](address);
return balance;
}
func.getOwner = async (domain, metadata = false) =>
{
const ownerAddress = await contractFirst['getOwner'](domain);
const obj = new Object();
obj.owner = ownerAddress;
obj.native = "";
var arg = [];
if (metadata == true){
const tokenId = await contractFirst['genTokenId'](domain);
const values = await contractFirst['getMany'](defaultKeys, tokenId);
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['reverseOf'](_address);
return defaultDomain
}catch{}
return "";
}
func.getDomains = async (address) =>
{
const domains = [];
try{
const arg = await contractFirst['getDomainbyAddress'](address);
return arg.domains;
}catch{}
return domains;
}
func.getMetadata = async (key, domain) =>
{
const tokenId = await contractFirst['genTokenId'](domain);
var value = await contractFirst['get'](key, tokenId);
var obj = new Object();
obj.key = key
obj.value = value
return obj;
}
func.getMetadatas = async (keys, domain) =>
{
const tokenId = await contractFirst['genTokenId'](domain);
const values = await contractFirst['getMany'](keys, tokenId);
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['genTokenId'](domain);
return hash;
}
return func;
}