-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
258 lines (229 loc) · 7.78 KB
/
main.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { readFileSync } from "fs";
import { writeFile } from "fs/promises";
function payableNonPayableFunctions(
functionName: string,
current: any,
type_mapping: any
): string {
if (current.outputs > 0) {
throw Error(`We don't support write functions with multiple outputs.`);
}
let inputString = "";
let inputToFunctionString = "";
if (current.inputs.length == 1) {
inputString = `${current.inputs[0].name}: ${
type_mapping[current.inputs[0].type]
}`;
inputToFunctionString = `${current.inputs[0].name}`;
} else if (current.inputs.length > 1) {
for (let i = 0; i < current.inputs.length - 1; i++) {
inputString += `${current.inputs[i].name}: ${
type_mapping[current.inputs[i].type]
}, `;
inputToFunctionString += `${current.inputs[i].name}, `;
}
inputString += `${current.inputs[current.inputs.length - 1].name}: ${
type_mapping[current.inputs[current.inputs.length - 1].type]
}`;
inputToFunctionString += `${
current.inputs[current.inputs.length - 1].name
}`;
}
if (current.stateMutability === "payable") {
inputString +=
current.inputs.length === 0
? "valueInEth: Number"
: ", valueInEth: Number";
inputToFunctionString +=
current.inputs.length === 0
? "{value: ethers.utils.parseEther(valueInEth.toString())}"
: ",{value: ethers.utils.parseEther(valueInEth.toString())}";
}
return `\n\t${functionName} = async (${inputString}): Promise<ethers.providers.TransactionReceipt> => {
const transaction: ethers.providers.TransactionResponse =
await this.connectedContract.${current.name}(${inputToFunctionString});
return await transaction.wait();};`;
}
function viewPureFunctions(
functionName: string,
current: any,
type_mapping: any
): string {
let inputString = "";
let inputToFunctionString = "";
let outputType = type_mapping[current.outputs[0].type];
if (current.inputs.length == 1) {
inputString = `${current.inputs[0].name}: ${
type_mapping[current.inputs[0].type]
}`;
inputToFunctionString = `${current.inputs[0].name}`;
} else if (current.inputs.length > 1) {
for (let i = 0; i < current.inputs.length - 1; i++) {
inputString += `${current.inputs[i].name}: ${
type_mapping[current.inputs[i].type]
}, `;
inputToFunctionString += `${current.inputs[i].name}, `;
}
inputString += `${current.inputs[current.inputs.length - 1].name}: ${
type_mapping[current.inputs[current.inputs.length - 1].type]
}`;
inputToFunctionString += `${
current.inputs[current.inputs.length - 1].name
}`;
}
if (current.outputs.length > 1) {
console.log("fits the bill");
console.log(current.name);
console.log(current.outputs);
outputType = "{";
for (let i = 0; i < current.outputs.length - 1; i++) {
outputType += `${current.outputs[i].name}: ${
type_mapping[current.outputs[i].type]
}, `;
}
outputType += `${current.outputs[current.outputs.length - 1].name}: ${
type_mapping[current.outputs[current.outputs.length - 1].type]
}`;
outputType += "}";
}
return `\n\t${functionName} = async (${inputString}): Promise<${outputType}> => {
return await this.connectedContract.${current.name}(${inputToFunctionString});\n\t}`;
}
const addFunctions = (
contractInterface: { [x: string]: any[] },
readOnly: boolean
) => {
const types_dict: any = {};
const type_mapping: any = {
address: "string",
bool: "Boolean",
bytes: "string",
bytes4: "string",
string: "string",
uint256: "Number",
uint32: "Number",
uint: "Number",
uint64: "Number",
};
const functionCount: any = {};
let result = contractInterface["abi"].map((current: any) => {
if (current.type === "function") {
if (!functionCount[current.name]) {
functionCount[current.name] = 1;
} else {
functionCount[current.name] += 1;
}
// console.log("function name");
// console.log(current.name);
types_dict[current.name as string] = {
inputs: [],
outputs: [],
};
// console.log("inputs:");
current.inputs.map((currentInput: any) => {
// console.log(`\t${currentInput.name}`);
// console.log(`\t${currentInput.type}`);
// console.log(`\t${type_mapping[currentInput.type]}`);
types_dict[current.name as string].inputs.push({
name: currentInput.name,
type: type_mapping[currentInput.type],
});
});
// console.log("outputs:");
current.outputs.map((currentInput: any) => {
// console.log(`\t${currentInput.name}`);
// console.log(`\t${currentInput.type}`);
// console.log(`\t${type_mapping[currentInput.type]}`);
types_dict[current.name as string].outputs.push({
name: currentInput.name,
type: type_mapping[currentInput.type],
});
});
console.log(functionCount);
console.log(current);
const functionName =
functionCount[current.name] == 1
? current.name
: `${current.name}${functionCount[current.name]}`;
if (["view", "pure"].includes(current.stateMutability)) {
console.log(current.name);
return viewPureFunctions(functionName, current, type_mapping);
} else {
if (!readOnly) {
return payableNonPayableFunctions(
functionName,
current,
type_mapping
);
}
}
}
return "";
});
return result.join("");
};
const main = async () => {
if (process.argv.length === 2) {
throw Error("Need to ABI file location");
}
const fileLocation = process.argv[2];
const readyOnly =
process.argv.length === 4 && process.argv[3] === "--readOnly";
const contractInterface = JSON.parse(readFileSync(fileLocation, "utf-8"));
let start = !readyOnly
? `
import { ethers } from "ethers";
class ${contractInterface["contractName"]}SDK {
connectedContract: any;
constructor(node_url: string, private_key: string, contract_address: string) {
const provider = new ethers.providers.JsonRpcProvider(node_url);
let signer = new ethers.Wallet(private_key, provider);
this.connectedContract = new ethers.Contract(contract_address, ${JSON.stringify(
contractInterface.abi
)}, signer);
}`
: `
import { ethers } from "ethers";
class ${contractInterface["contractName"]}ReadOnlySDK {
connectedContract: any;
constructor(node_url: string, contract_address: string) {
const provider = new ethers.providers.JsonRpcProvider(node_url);
this.connectedContract = new ethers.Contract(contract_address, ${JSON.stringify(
contractInterface.abi
)});
}`;
start += addFunctions(contractInterface, readyOnly);
start += `\nconvertEthereumToNumber = (value: any): Number => { return Number(ethers.utils.formatEther(value));}\n}`;
await writeFile(
`./${contractInterface["contractName"]}${
readyOnly ? "ReadOnly" : ""
}SDK.ts`,
start,
"utf-8"
);
};
main().then(() => {
console.log("done");
});
/**
*
const main = async () => {
const alchemy_https =
"https://eth-goerli.g.alchemy.com/v2/SHznwE89Iv3dnva3Whsv2hM42549lVXF";
const private_key =
"872d2bf1f1c568539ede9c7fee4b7c558e92945b88d7a02ade40fe3d0273d0b5";
const contractAddress = "0x9b84ACBA9780Df6AfCb2C199929fC7eb62ab7116";
const hello = new CypherpunkSDK(alchemy_https, private_key, contractAddress);
let result = await hello.price();
console.log(typeof result);
console.log(result);
console.log(hello.convertEthereumToNumber(result));
let newResult = await hello.mint(hello.convertEthereumToNumber(result));
console.log(newResult);
};
main().then(() => {
console.log("done?");
});
*
*
*/