-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
69 lines (60 loc) · 1.78 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
import Irys from '@irys/sdk';
import * as dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
dotenv.config();
const getDirSize = (dirPath) => {
let size = 0;
const files = fs.readdirSync(dirPath);
for (let i = 0; i < files.length; i++) {
const filePath = path.join(dirPath, files[i]);
const stats = fs.statSync(filePath);
if (stats.isFile()) {
// only add if greater than because uploads less than 100kb are free
if (stats.size > 102400) size += stats.size;
} else if (stats.isDirectory()) {
size += getDirSize(filePath);
}
}
return size;
};
const deploy = async () => {
const providerUrl = 'https://sepolia.infura.io/v3/';
const token = 'ethereum';
const irys = new Irys({
network: 'devnet',
token,
key: process.env.WALLET_PK,
config: { providerUrl },
});
await irys.ready();
const folderToUpload = './dist';
const balance = irys.utils.fromAtomic(await irys.getLoadedBalance());
console.log('Current Balance: ', balance);
let size = getDirSize(folderToUpload);
console.log('Current size: ', size);
const cost = irys.utils.fromAtomic(await irys.getPrice(size));
console.log('Cost to Upload: ', cost);
if (balance.lt(cost)) {
console.log('Insufficient balance to upload');
return;
}
try {
const receipt = await irys.uploadFolder(folderToUpload, {
indexFile: 'index.html',
});
console.log(
`Files uploaded. Manifest ID: https:gateway.irys.xyz/tx/${receipt.id}`
);
const balanceAfterUpload = irys.utils.fromAtomic(
await irys.getLoadedBalance()
);
console.log('Balance after upload: ', balanceAfterUpload);
} catch (e) {
console.log('Error uploading file ', e);
}
};
deploy().catch((err) => {
console.error(err);
process.exit(1);
});