-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter-ethers.js
98 lines (78 loc) · 3.05 KB
/
converter-ethers.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
require('./init');
const fs = require('fs');
var files = fs.readdirSync(`${__dirname}/contracts/`);
for( let file of files ){
let fileName = file.split('.')[0];
let className = 'Contract' + fileName;
let abi = JSON.parse(fs.readFileSync(`${__dirname}/contracts/${fileName}.json`)).abi;
let classFilePath = `${__dirname}/class/ethers/${className}.js`;
let classBuild = `
import {ethers} from "ethers";
class ${className}{
constructor( provider, signer, address, setError, setSuccess ) {
this.provider = provider;
this.address = address;
this.abi = ${JSON.stringify(abi)}; // TO BE POPULATED
this.contract = null; // TO BE INTIALIZED
this.signer = signer;
this.setError = setError ? setError : console.log;
this.setSuccess = setSuccess ? setSuccess : console.log;
}
async intialize() {
this.contract = await new ethers.Contract( this.address, this.abi, this.signer );
}
`
function functionArgsFromAbiElem( elem ){
let args = [];
let notNamed = 0;
for( let arg of elem.inputs ){
if(!arg.name) {
args.push(elem.name + notNamed.toString());
notNamed ++;
}
else args.push( arg.name )
}
return args;
}
function getBlockchainInteractionFromAbiElem( elem, args ){
if( ['pure', 'view'].includes(elem.stateMutability) ){
return `
let result = await this.contract.${elem.name}(${args})
.catch( err => { this.setError( err, err.code, err.message ); return null });
if( result == null || result == undefined ) return null;
this.setSuccess( result );
return result;`
} else if ( ['payable'].includes(elem.stateMutability) ) {
return `
let result = await this.contract.${elem.name}(${ args + ( args ? ', ' : '' ) } { value: payableValue })
.catch( err => { this.setError( err, err.code, err.message ); return null });
if( result == null || result == undefined ) return null;
this.setSuccess( result );
return result;`
} else {
return `
let result = await this.contract.${elem.name}(${args})
.catch( err => { this.setError( err, err.code, err.message ); return null });
if( result == null || result == undefined ) return null;
this.setSuccess( result );
return result;`
}
}
for( let elem of abi ){
if( elem.type == 'function' ){
let args = functionArgsFromAbiElem(elem).join(", ").trim();
let isPayable = ['payable'].includes(elem.stateMutability);
classBuild += `
async ${elem.name}(${ args + ( args && isPayable ? ', ' : '' ) } ${ isPayable ? 'payableValue' : '' }){
if(!this.contract) await this.intialize();
${ getBlockchainInteractionFromAbiElem( elem, args ) }
}
`
}
}
classBuild += `
}
export default ${className};
`;
fs.writeFileSync( classFilePath, classBuild );
}