-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaticErc20.vue
225 lines (217 loc) · 6.27 KB
/
MaticErc20.vue
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
<template>
<b-card no-body class="action-wrap">
<b-card-header
:class="visible ? null : 'collapsed'"
:aria-expanded="visible ? 'true' : 'false'"
:aria-controls="`collapse-${idValue}`"
@click="visible = !visible"
>
<b-row>
<b-col cols="1" sm="1" md="1">
<img src="../../../assets/matic-logo.svg" height="25px" />
</b-col>
<b-col cols="9" sm="9" class="text-left" md="9">
<div class="text text-capitalize">{{ data.title }}</div>
</b-col>
<b-col cols="2" sm="2" md="2">
<b-badge class="btn-score" @click="update()" v-if="!done">
<img src="../../../assets/plus.svg" />
{{ data.score }}
</b-badge>
<img
class="check-mark"
src="../../../assets/check-circle-fill.svg"
height="25px"
v-if="done"
/>
</b-col>
</b-row>
</b-card-header>
<b-collapse :id="`collapse-${idValue}`" v-model="visible">
<b-card-body class="user-details">
<b-row v-if="!showerror">
<b-col cols="12" sm="12" md="12">
<div class="metamask">
<b-form-input
type="text"
:placeholder="data.placeHolder"
v-model="value.userWalletAddress"
:disabled="true"
:required="data.isManadatory"
></b-form-input>
<button class="btn text-black" @click="invokeMetamask()" v-if="!done" >
<img
src="../../../assets/metamask.svg"
height="25px"
width="25px"
/>
</button>
</div>
</b-col>
</b-row>
<b-row v-else>
<b-col cols="12" sm="12" md="12">
<ErrorMessage errorMessage="Install Metamask browser extension" />
</b-col>
</b-row>
<b-row v-if="!done">
<b-col cols="12" sm="12" md="12" >
<button class="btn btn-link center" @click="update()">Continue</button>
</b-col>
</b-row>
</b-card-body>
</b-collapse>
</b-card>
</template>
<style scoped>
.center{
display: block; margin-left: auto;margin-right: auto
}
</style>
<script>
import eventBus from "../../../eventBus.js";
import apiClient from "../../../mixins/apiClientMixin.js";
import {
isValidURL,
isValidText,
isEmpty,
} from "../../../mixins/fieldValidationMixin";
import notificationMixins from "../../../mixins/notificationMixins";
import Messages from "../../../utils/messages/participants/en";
import ErrorMessage from "../ErrorMessage.vue";
import Web3 from "web3";
export default {
name: "MaticErc20",
props: {
idValue: {
required: true,
},
data: {
required: true,
},
},
components: {
ErrorMessage,
},
data() {
return {
visible: false,
done: this.data.isDone,
authToken: localStorage.getItem("authToken"),
showerror: false,
signature: "",
message_sign: "",
value: {
contractAddress: "",
userWalletAddress: "",
thresholdBalance: 0
}
};
},
mounted() {
if(this.data.value){
Object.assign(this.value, {...JSON.parse(this.data.value) })
}
eventBus.$on(`disableInput${this.data._id}`, this.disableInput);
this.checkWeb3Injection();
},
methods: {
checkWeb3Injection() {
try {
if (ethereum && ethereum.isMetaMask) {
this.web3 = new Web3(window.ethereum);
}
} catch (error) {
console.log(error);
this.showerror = true;
}
},
async signMessage() {
const message =
"You are Signing this message to ensure your participation in this event";
this.message_sign = message;
return await this.web3.eth.personal.sign(
message,
ethereum.selectedAddress
);
},
async invokeMetamask() {
try {
if (ethereum.isMetaMask) {
const wallet = await ethereum.request({
method: "eth_requestAccounts",
});
this.signature = await this.signMessage();
const generatedWalletAddr = await this.web3.eth.personal.ecRecover(this.message_sign, this.signature)
if(generatedWalletAddr === wallet[0]){
this.value.userWalletAddress = wallet[0];
} else{
return this.notifyErr(Messages.EVENT_ACTIONS.ETH.INVALID_SIG)
}
}
} catch (error) {
return this.notifyErr(error.message)
}
},
async update() {
if (!this.isFieldValid() || this.value.userWalletAddress === "") {
return this.notifyErr(Messages.EVENT_ACTIONS.ETH.CONNECT_METAMASK);
} else {
try {
let balance = await this.fetchBalance();
if (balance !== undefined) {
if (balance >= Number.parseFloat(this.value.thresholdBalance)) {
this.$emit("input", JSON.stringify({
...this.value,
}));
} else {
throw new Error(Messages.EVENT_ACTIONS.ETH.INSUFFICIENT_BALANCE);
}
}
} catch (error) {
this.data.value = ""
return this.notifyErr(error);
}
}
},
isFieldValid() {
if (isEmpty(this.value.userWalletAddress)) {
return false;
}
if (isValidURL(this.value.userWalletAddress)) {
return false;
}
if (!isValidText(this.value.userWalletAddress)) {
return false;
}
return true;
},
async fetchBalance() {
const body = {
actionType: this.data.type,
data: this.value.userWalletAddress,
contractAddress: this.value.contractAddress,
signature: this.signature,
message: this.message_sign,
};
let url = `${this.$config.studioServer.BASE_URL}api/v1/action/contract/call`;
let headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.authToken}`,
};
const res = await apiClient.makeCall({
method: "POST",
url: url,
body: body,
header: headers,
});
const result = res.data;
return result;
},
disableInput(data) {
this.done = data;
},
},
mixins: [notificationMixins],
};
</script>