forked from lukso-network/lsp-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-ios.ts
66 lines (51 loc) · 1.86 KB
/
make-ios.ts
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
const fs = require("fs");
const hre = require("hardhat");
// first generate the JSON artifacts to have a folder "./artifacts/{Contract}.json"
hre.run("prepare-package").then(() => {
const swiftAbis = {};
// 1. for each contract included in the package
const contracts = hre.config.packager.contracts;
for (const contract of contracts) {
// 1.1 read the artifact file (one at a time)
let artifact = fs.readFileSync(`./artifacts/${contract}.json`);
// 1.2 get the abi field in the JSON file
let abi = JSON.parse(artifact).abi;
// 1.3 save each abi as a string in a list object
// escaping double quotes '\\"', so that it can be parsed correctly
swiftAbis[contract] = JSON.stringify(abi).replace(/"/g, '\\"');
}
let upSwiftFile = "./ios/UPContractsAbi.swift";
// 2. add each swift abi in the file content,
let swiftFileContent = `//
// UpContractsAbi.swift
// UniversalProfile
//
// Created by lukso-network.
// LUKSO Blockchain GmbH © ${new Date().getFullYear()}
//
import Foundation
/// LSP ABIs that were auto-generated by the
/// [lsp-smart-contracts](https://github.com/lukso-network/lsp-smart-contracts) repository.
public final class UpContractsAbi {
`;
const getAbisAsArray = () => {
let body = [];
for (const [key, value] of Object.entries(swiftAbis)) {
body.push(`public static let ${key}_ABI = "${value}"`);
}
return body;
};
let variablesList = getAbisAsArray();
let swiftFileEnd = `
}
`;
swiftFileContent = swiftFileContent.concat(variablesList.join("\n\n "));
swiftFileContent = swiftFileContent.concat(swiftFileEnd);
// 3. finally, write the content inside the file
fs.appendFile(upSwiftFile, swiftFileContent, (err) => {
if (err) console.log(err);
console.log(
`\u2713 \uF8FF Successfully created Swift contract ABIs (see file: ${upSwiftFile})`
);
});
});