-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabiUtils.js
155 lines (137 loc) · 5.69 KB
/
abiUtils.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
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
require('dotenv').config();
const fs = require('fs');
const buildDir = "../abi/data/";
const writeDir = "../store/modules/abisMethods/"
if (!fs.existsSync(buildDir)) {
throw new Error('ABI buildDir not exists');
}
if (!fs.existsSync(writeDir)) {
throw new Error('ABI abisDirMap not exists');
}
const files = fs.readdirSync(buildDir);
console.log("contract number:", files.length)
//create index.js export all contract
let indexStr = ``, exportStr = `export default { `;
//deal all contract
for (let i = files.length - 1; i >= 0; i--) {
// get contract name 、 abi
let contract = JSON.parse(fs.readFileSync(buildDir + files[i]), 'utf8');
let name = files[i].substr(0,files[i].length-5);
let abi = contract;
let tempFileStr = `
import getContract from "@/abi/index.js";
function judgeToken(rootState) {
if (!state.token) state.token = getContract.getContractByName('${name}', rootState.app.web3)
}
const state = {};
const mutations = {};
`
let actions = ` const actions = { \n`;
console.log("create" + name + ".js")
// deal index.js
indexStr += `import ${name} from "@/store/modules/abisMethods/${name}";`
exportStr += `${name},`
// end deal index.js
// deal Methods
for (let j = 0; j < abi.length; j++) {
let functionObj = abi[j];
//call Methods
if(functionObj.type == "function" &&functionObj.stateMutability == "view"){
// deal Methods
let params = functionObj.inputs;
let tempParamStr = ``
for (let k = 0; k < params.length; k++) {
tempParamStr += (params[k].name?params[k].name:'param'+k) + ","
}
// deal Methods end
tempParamStr = tempParamStr.substr(0,tempParamStr.length-1)
actions += `${functionObj.name} ({rootState}${tempParamStr.length>0?',':''} ${tempParamStr?'{'+tempParamStr+'}':tempParamStr}){
judgeToken(rootState)
return new Promise((resolve,reject) => {
state.token.methods.${functionObj.name}(${tempParamStr}).call().then(res => {
resolve(res)
}).catch(err=>{
reject(JSON.parse(err.message.substr(24,err.message.length)).message)
})
})
},\n`
}
//end call Methods
//send Methods
if(functionObj.type == "function" &&functionObj.stateMutability == "nonpayable"){
// deal Methods
let params = functionObj.inputs;
let tempParamStr = ``
for (let k = 0; k < params.length; k++) {
tempParamStr += (params[k].name?params[k].name:'param'+k) + ","
}
// deal Methods end
tempParamStr = tempParamStr.substr(0,tempParamStr.length-1)
actions += `${functionObj.name} ({rootState}${tempParamStr.length>0?',':''} ${tempParamStr?'{'+tempParamStr+'}':tempParamStr}){
judgeToken(rootState)
return new Promise((resolve,reject) => {
state.token.methods.${functionObj.name}(${tempParamStr}).estimateGas({
from: rootState.app.account,
}).then(gas => {
state.token.methods.${functionObj.name}(${tempParamStr}).send({
from: rootState.app.account,
gas: parseInt(gas * 1.2)
}).then(res => {
resolve(res)
})
}).catch(err=>{
reject(JSON.parse(err.message.substr(24,err.message.length)).message)
})
})
},\n`
}
//end send Methods
// send with value Methods
if(functionObj.type == "function" &&functionObj.stateMutability == "payable"){
// deal params
let params = functionObj.inputs;
let tempParamStr = ``
for (let k = 0; k < params.length; k++) {
tempParamStr += (params[k].name?params[k].name:'param'+k) + ","
}
// deal params end
tempParamStr = tempParamStr.substr(0,tempParamStr.length-1)
actions += `${functionObj.name} ({rootState}, value ${tempParamStr.length>0?',':''} ${tempParamStr?'{'+tempParamStr+'}':tempParamStr}{
judgeToken(rootState)
return new Promise((resolve,reject) => {
state.token.methods.${functionObj.name}(${tempParamStr}).estimateGas({
from: rootState.app.account,
}).then(gas => {
state.token.methods.${functionObj.name}(${tempParamStr}).send({
from: rootState.app.account,
gas: parseInt(gas * 1.2),
value: rootState.app.web3.utils.toWei(value)
}).then(res => {
resolve(res)
})
}).catch(err=>{
reject(JSON.parse(err.message.substr(24,err.message.length)).message)
})
})
},\n`
}
//end send with value Methods
}
actions += `}`
tempFileStr += actions
// Methods end
tempFileStr += `
export default {
namespaced: true,
mutations,
state,
actions
}
`
fs.writeFileSync(`${writeDir}${name}.js`, tempFileStr, 'utf8');
}
//end deal all contract
exportStr += `}`
indexStr += exportStr
fs.writeFileSync(`${writeDir}index.js`, indexStr, 'utf8');
console.log("you get all vue store")