-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
233 lines (214 loc) · 6.13 KB
/
main.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
async function getHolders(collectionID) {
let res = await fetch("https://api-v2-mainnet.paras.id/collection-stats?collection_id=" + collectionID).then(res => res.json()).catch((error) => {
console.error(error);
});
holders = res.data.results.owner_ids
return holders
}
async function getCollectionsOf(user) {
return await fetch(
"https://api-v2-mainnet.paras.id/collections?creator_id=" + user
), then(res => res.json())
.then((res) => {
return (res.data.data.results);
})
.catch((error) => {
console.error(error);
});
}
function test() {
let meta = {
"jsonrpc": "2.0",
"id": "dontcare",
"method": "query",
"params": {
"request_type": "call_function",
"finality": "final",
"account_id": "nearch4n.sputnikdao.near",
"method_name": "get_policy",
"args_base64": ""
}
}
getDaoInfo(meta).then(res => console.log(res))
}
async function getDaoInfo(meta) {
let temp = await fetch("http://archival-rpc.mainnet.near.org", meta)
return dataToString(temp);
}
function dataToString(data) {
let hex = toHexString(data.result.result)
let str = hex_to_ascii(hex)
return str.substring(2, str.length - 1)
}
function tob64(s) {
return Buffer.from(s).toString('base64');
}
function hex_to_ascii(str1) {
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
}
function toHexString(byteArray) {
var s = '0x';
byteArray.forEach(function (byte) {
s += ('0' + (byte & 0xFF).toString(16)).slice(-2);
});
return s;
}
// open a connection to the NEAR platform
async function init() {
const { connect, keyStores, WalletConnection } = nearApi;
const config = {
networkId: "mainnet",
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://explorer.mainnet.near.org",
};
// connect to NEAR
const near = await connect(config);
// create wallet connection
const wallet = new WalletConnection(near,"dao seat connector");
window.near = near
window.wallet = wallet
// redirects user to wallet to authorize your dApp
// this creates an access key that will be stored in the browser's local storage
// access key can then be used to connect to NEAR and sign transactions via keyStore
const signIn = () => {
wallet.requestSignIn(
"sputnikdao.near", // contract requesting access
"sputnik dao seat adder for holders",
);
};
if(!wallet.isSignedIn()) {
signIn();
}
};
document.getElementById("connect").addEventListener("click",()=>{
console.log("click")
init();
document.getElementById("connect").hidden = true;
document.getElementById("seatAdder").hidden = false;
})
async function getDaoContract(addy) {
const methodOptions = {
"viewMethods": [
"version",
"get_config",
"get_policy",
"get_staking_contract",
"has_blob",
"get_available_amount",
"delegation_total_supply",
"delegation_balance_of",
"get_last_proposal_id",
"get_proposals",
"get_proposal",
"get_bounty",
"get_last_bounty_id",
"get_bounties",
"get_bounty_claims",
"get_bounty_number_of_claims"
],
"changeMethods": [
"new",
"migrate",
"store_blob",
"remove_blob",
"add_proposal",
"act_proposal",
"bounty_claim",
"bounty_done",
"bounty_giveup",
"register_delegation",
"delegate",
"undelegate"
],
};
try {
let contract = new nearApi.Contract(
window.wallet.account(),
addy,
methodOptions
);
window.contract=contract;
return await contract.get_policy();
} catch (err) {
window.alert("contract doesnt support get_policy")
}
}
function mergeholders(holders, policy) {
// let newList=[];
let newList = holders.filter(o1 => policy.some(o2 => o1.owner.id === o2.owner.id));
return newList;
}
function setNewPolicy( policy) {
// console.log( policy)
window.contract.add_proposal({
proposal: {
"description": "change policy members / roles",
"kind": {
ChangePolicy: {policy}
}
}
},null,policy.proposal_bond);
}
// document.getElementById("contractid").addEventListener("change",async (e)=>{
// let holders = await getHolders(e.target.value)
// let list= document.getElementById("holdersList")
// // holders.forEach(hol=>{
// // list.insertAdjacentHTML("beforeend",`<li>${hol}</li>`)
// // })
// })
// document.getElementById("daoaddy").addEventListener("change",async (e)=>{
// let policy = await getDaoContract(e.target.value)
// let list= document.getElementById("holdersList")
// console.log(policy);
// })
async function buttonClick(){
let holders = await getHolders(document.getElementById("contractid").value)
let contract=document.getElementById("daoaddy").value
let policy = await getDaoContract(contract)
let holderRole={}
let holderIndex;
policy.roles.forEach((x,i)=>{
if(x.name=="Holders"){
holderIndex=i;
holderRole=x;
}
})
if(holderIndex==undefined){
holderIndex = policy.roles.length;
holderRole = {
"name": "Holders",
"kind": {
"Group": []
},
"permissions": [
"*:VoteReject",
"*:VoteRemove",
"*:VoteApprove",
"*:AddProposal",
"*:Finalize"
],
"vote_policy": {}
}
}
console.log(holderRole,holderRole.kind.Group.length)
holderRole.kind.Group= [...holderRole.kind.Group,...holders];
console.log(holderRole)
policy.roles[holderIndex]=holderRole;
setNewPolicy(policy)
// let newList= holderRole.kind.Group(o1 => holders.some(o2 => o1 === o2));
}
document.getElementById("fancybtn").addEventListener("click",buttonClick)
window.onload=()=>{
if (window.location.search.indexOf("transactionHashes") > -1) {
const params = new URLSearchParams(window.location.search)
document.getElementById("connect").outerHTML=`proposal added <br> <a href="https://explorer.mainnet.near.org/transactions/${params.get("transactionHashes")}">Click here to view the tx</a><br> it may take a few minutes to show on astro dao`
}
}