-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
113 lines (96 loc) · 3.55 KB
/
deploy.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
const path = require("path");
const { readdirSync, readFileSync, writeFileSync } = require("fs");
const { keyStores, connect, Contract } = require("near-api-js");
const { homedir } = require("os");
const ACCOUNT = "communityvoice.ndctools.near"
// const ACCOUNT = "silkking.near"
function getAllFilesNames(addedPath = "") {
const pathToSearch = path.join("./src", addedPath)
const foldersToExclude = ["bosTests", "testingWidgets", "tests"]
const filesToExclude = ["HelloCV.jsx"]
const files = []
readdirSync(pathToSearch).forEach(file => {
if(filesToExclude.includes(file) || foldersToExclude.includes(file)) return
const isFile = file.endsWith(".jsx")
if(isFile) {
files.push(path.join(addedPath, file))
} else {
files.push(...getAllFilesNames(path.join(addedPath, file)))
}
})
return files
}
async function getContract() {
const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = path.join(homedir(), CREDENTIALS_DIR);
console.log("credentialsPath", credentialsPath )
const myKeyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
const connectionConfig = {
networkId: "mainnet",
keyStore: myKeyStore, // first create a key store
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://nearblocks.io",
};
const nearConnection = await connect(connectionConfig);
// const walletConnection = new WalletConnection(nearConnection);
const account = await nearConnection.account(ACCOUNT);
const contract = new Contract(
account , // the account object that is connecting
"social.near",
{
// name of contract you're connecting to
viewMethods: [], // view methods do not change state but usually return a value
changeMethods: ["set"], // change methods modify state
}
);
return contract
}
function getWidgetsJsons(files) {
return files.map(file => {
const fileContent = readFileSync(path.join("./src", file), "utf-8").toString()
const widgetName = file.replace(".jsx", "").split("/").join(".")
return {
[ACCOUNT]: {
widget: {
[widgetName]: {
"": fileContent
}
}
}
}
})
}
function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
};
async function run() {
const indexesToDeploy = []
const indexesWithError = []
const files = getAllFilesNames()
const widgetJsons = getWidgetsJsons(files)
const socialContract = await getContract()
for(let i = 29; i < widgetJsons.length; i++) {
if(indexesToDeploy.length > 0 && !indexesToDeploy.includes(i)) continue
const json = widgetJsons[i]
const widgetName = Object.keys(json[ACCOUNT].widget)[0]
console.log("Deploying widget with index", i, widgetName)
try {
await socialContract.set({
data: json
},
"300000000000000",
"1" + "0".repeat(21))
console.log("Deployed", widgetName)
} catch(err) {
console.log("Error deploying widget with index", i)
console.log(err)
indexesWithError.push(i)
} finally {
await sleep(1521)
}
}
console.log("Indexes with error", indexesWithError)
}
run()