dracoder-web3-package 1.0.1 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +215 -295
- package/package.json +12 -4
- package/test/app.js +16 -0
- package/test/test.html +106 -0
- package/webpack.config.js +29 -0
package/index.js
CHANGED
@@ -1,322 +1,242 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
|
3
|
-
const Web3 = require(
|
4
|
-
const
|
5
|
-
const { abiToken } = require('./helpers/factory_data');
|
3
|
+
const {Web3} = require("web3");
|
4
|
+
const {abiToken} = require("./helpers/factory_data");
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
let clientTokenTransferUrl = 'client_token_transfer';
|
11
|
-
let getTokenTransactionUrl = 'get_token_transactions';
|
12
|
-
let clientTokenOwnersBalanceUrl = 'token_owners_balance';
|
13
|
-
let balanceUrl = 'balance';
|
14
|
-
let deleteUrl = 'delete';
|
15
|
-
let clientServerUrl = "";
|
16
|
-
|
17
|
-
const setClientServerUrl = (url) => {
|
18
|
-
clientServerUrl = url;
|
19
|
-
}
|
20
|
-
|
21
|
-
const setEndpointsUrls = ({
|
22
|
-
walletConnectionCustomUrl = walletConnectionUrl,
|
23
|
-
tokenCreationCustomUrl = tokenCreationUrl,
|
24
|
-
tokenTransferCustomUrl = tokenTransferUrl,
|
25
|
-
clientTokenTransferCustomUrl = clientTokenTransferUrl,
|
26
|
-
getTokenTransactionCustomUrl = getTokenTransactionUrl,
|
27
|
-
clientTokenOwnersBalanceCustomUrl = clientTokenOwnersBalanceUrl,
|
28
|
-
balanceCustomUrl = balanceUrl,
|
29
|
-
deleteCustomUrl = deleteUrl,
|
30
|
-
}) => {
|
31
|
-
walletConnectionUrl = walletConnectionCustomUrl;
|
32
|
-
tokenCreationUrl = tokenCreationCustomUrl;
|
33
|
-
tokenTransferUrl = tokenTransferCustomUrl;
|
34
|
-
clientTokenTransferUrl = clientTokenTransferCustomUrl;
|
35
|
-
getTokenTransactionUrl = getTokenTransactionCustomUrl;
|
36
|
-
clientTokenOwnersBalanceUrl = clientTokenOwnersBalanceCustomUrl;
|
37
|
-
balanceUrl = balanceCustomUrl;
|
38
|
-
deleteUrl = deleteCustomUrl;
|
6
|
+
const METAMASK_NOT_INSTALLED_MESSAGE = {
|
7
|
+
ok: false,
|
8
|
+
message: "You don't have Metamask installed.",
|
39
9
|
}
|
40
10
|
|
41
|
-
const
|
42
|
-
// let walletConnectionResponse;
|
43
|
-
|
44
|
-
if (typeof window.ethereum !== 'undefined') {
|
45
|
-
const [account] = await ethereum.request({method: 'eth_requestAccounts'}).catch(async (error) => {
|
46
|
-
|
47
|
-
throw {
|
48
|
-
ok: false,
|
49
|
-
message: 'Something went wrong connecting to Metamask.',
|
50
|
-
metamaskMessage: error
|
51
|
-
};
|
52
|
-
});
|
53
|
-
|
54
|
-
if (!account) {
|
55
|
-
const message = "There is no account to connect to";
|
56
|
-
|
57
|
-
throw {
|
58
|
-
ok: false,
|
59
|
-
message: message
|
60
|
-
};
|
61
|
-
}
|
62
|
-
|
63
|
-
return {
|
64
|
-
ok: true,
|
65
|
-
message: "Connected successfully.",
|
66
|
-
account: account
|
67
|
-
};
|
68
|
-
}
|
69
|
-
|
70
|
-
const message = "There was a problem connecting to your Metamask wallet. Do you have Metamask installed?";
|
71
|
-
|
72
|
-
// walletConnection({walletAddress: null, response: message, ...extraParameters});
|
11
|
+
const Metamask = window.ethereum
|
73
12
|
|
74
|
-
|
75
|
-
|
76
|
-
message: message
|
77
|
-
};
|
13
|
+
const checkIfMetamaskIsAvailable = () => {
|
14
|
+
return typeof Metamask !== "undefined"
|
78
15
|
}
|
79
16
|
|
80
|
-
const
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
return {
|
90
|
-
ok: false,
|
91
|
-
message: "Wallet address is required and needs to be valid."
|
92
|
-
}
|
93
|
-
}
|
94
|
-
|
95
|
-
return await axios.post(`${clientServerUrl}/${tokenCreationUrl}`, {
|
96
|
-
nameOfToken,
|
97
|
-
numberOfToken,
|
98
|
-
symbolOfToken,
|
99
|
-
numberOfDecimals,
|
100
|
-
userWalletAddress,
|
101
|
-
...extraParameters
|
102
|
-
});
|
103
|
-
}
|
104
|
-
|
105
|
-
const tokenTransfer = async ({
|
106
|
-
walletAddressToTransferFrom,
|
107
|
-
walletAddressToTransferTo,
|
108
|
-
tokenAddress,
|
109
|
-
amount,
|
110
|
-
...extraParameters
|
111
|
-
}) => {
|
112
|
-
const {data} = await axios.post(`${clientServerUrl}/${tokenTransferUrl}`, {
|
113
|
-
walletAddressToTransferFrom: walletAddressToTransferFrom,
|
114
|
-
walletAddressToTransferTo: walletAddressToTransferTo,
|
115
|
-
tokenAddress: tokenAddress,
|
116
|
-
amount: amount,
|
117
|
-
...extraParameters
|
118
|
-
});
|
119
|
-
|
120
|
-
return data;
|
121
|
-
}
|
122
|
-
|
123
|
-
const clientTokenTransfer = async ({walletAddressToTransferTo, tokenAddress, amount, ...extraParameters}) => {
|
124
|
-
const response = await metamaskConnect().catch(error => {
|
125
|
-
throw error;
|
17
|
+
const getMetamaskAccount = async () => {
|
18
|
+
const [account] = await Metamask
|
19
|
+
.request({method: "eth_requestAccounts"})
|
20
|
+
.catch(async (error) => {
|
21
|
+
throw {
|
22
|
+
ok: false,
|
23
|
+
message: "Something went wrong connecting to Metamask.",
|
24
|
+
metamaskMessage: error,
|
25
|
+
};
|
126
26
|
});
|
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
|
-
|
27
|
+
if (!account) {
|
28
|
+
throw {
|
29
|
+
ok: false,
|
30
|
+
message: "There is no account to connect to",
|
31
|
+
};
|
32
|
+
}
|
33
|
+
return {
|
34
|
+
ok: true,
|
35
|
+
message: "Connected successfully.",
|
36
|
+
account: account,
|
37
|
+
};
|
38
|
+
};
|
39
|
+
|
40
|
+
const clientTokenTransfer = async ({
|
41
|
+
walletAddressToTransferTo,
|
42
|
+
tokenAddress,
|
43
|
+
amount,
|
44
|
+
}) => {
|
45
|
+
if (!checkIfMetamaskIsAvailable()) {
|
46
|
+
return METAMASK_NOT_INSTALLED_MESSAGE
|
47
|
+
}
|
48
|
+
const response = await getMetamaskAccount().catch((error) => {
|
49
|
+
throw error;
|
50
|
+
});
|
51
|
+
const account = response.account;
|
52
|
+
let web3;
|
53
|
+
|
54
|
+
try {
|
55
|
+
web3 = new Web3(Metamask);
|
56
|
+
} catch (error) {
|
57
|
+
throw error;
|
58
|
+
}
|
59
|
+
|
60
|
+
if (!web3.utils.isAddress(account)) {
|
61
|
+
throw new Error("Not valid account");
|
62
|
+
}
|
63
|
+
if (!web3.utils.isAddress(tokenAddress)) {
|
64
|
+
throw new Error("Is not a Token Address");
|
65
|
+
}
|
66
|
+
if (!web3.utils.isAddress(walletAddressToTransferTo)) {
|
67
|
+
throw new Error("Is not a Wallet Address to transfer to");
|
68
|
+
}
|
69
|
+
if (amount <= 0) {
|
70
|
+
throw new Error("Amount must be greater than 0.");
|
71
|
+
}
|
72
|
+
|
73
|
+
const MyTokenContractInstance = new web3.eth.Contract(abiToken, tokenAddress);
|
74
|
+
const decimals = await MyTokenContractInstance.methods.decimals().call();
|
75
|
+
const amountToTransfer = (amount * Number(`1e${decimals}`)).toString();
|
76
|
+
|
77
|
+
const pendingTokenTransfer = MyTokenContractInstance.methods.transfer(
|
78
|
+
walletAddressToTransferTo,
|
79
|
+
amountToTransfer
|
80
|
+
);
|
81
|
+
|
82
|
+
try {
|
83
|
+
const transactionReceipt = await pendingTokenTransfer.send({
|
84
|
+
from: account,
|
174
85
|
});
|
175
|
-
}
|
176
|
-
|
177
|
-
const getTokenOwnersBalance = async ({tokenAddress, ...extraParams}) => {
|
178
|
-
if (!tokenAddress) {
|
179
|
-
throw new Error('Is not a Token Address');
|
180
|
-
}
|
181
86
|
|
182
|
-
return
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
}
|
87
|
+
return {
|
88
|
+
ok: true,
|
89
|
+
message: "Transfer successful",
|
90
|
+
transactionHash: transactionReceipt.transactionHash,
|
91
|
+
};
|
92
|
+
} catch (error) {
|
93
|
+
throw {
|
94
|
+
ok: false,
|
95
|
+
message: "Transfer failed",
|
96
|
+
error: error,
|
97
|
+
};
|
98
|
+
}
|
99
|
+
};
|
187
100
|
|
188
101
|
const sendTokenToWallet = async ({tokenAddress}) => {
|
189
|
-
|
190
|
-
|
102
|
+
if (!checkIfMetamaskIsAvailable()) {
|
103
|
+
return METAMASK_NOT_INSTALLED_MESSAGE
|
104
|
+
}
|
105
|
+
|
106
|
+
await getMetamaskAccount().catch((error) => {
|
107
|
+
throw error;
|
108
|
+
});
|
109
|
+
|
110
|
+
let web3 = new Web3(Metamask);
|
111
|
+
let tokenAdded = false;
|
112
|
+
|
113
|
+
const MyTokenContractInstance = new web3.eth.Contract(
|
114
|
+
abiToken,
|
115
|
+
tokenAddress
|
116
|
+
);
|
117
|
+
|
118
|
+
try {
|
119
|
+
const symbol = await MyTokenContractInstance.methods.symbol().call();
|
120
|
+
const decimals = web3.utils.toNumber(await MyTokenContractInstance.methods.decimals().call())
|
121
|
+
const response = await Metamask.request({
|
122
|
+
method: 'wallet_watchAsset',
|
123
|
+
params: {
|
124
|
+
type: 'ERC20',
|
125
|
+
options: {
|
126
|
+
address: tokenAddress,
|
127
|
+
symbol: symbol,
|
128
|
+
decimals: decimals,
|
129
|
+
},
|
130
|
+
},
|
191
131
|
});
|
132
|
+
tokenAdded = !!response;
|
133
|
+
} catch (error) {
|
134
|
+
return error
|
135
|
+
}
|
136
|
+
return tokenAdded;
|
192
137
|
|
193
|
-
|
194
|
-
let tokenAdded = false;
|
195
|
-
|
196
|
-
if (typeof window.ethereum !== 'undefined') {
|
197
|
-
const MyTokenContractInstance = new web3.eth.Contract(abiToken, tokenAddress);
|
198
|
-
|
199
|
-
const symbol = await MyTokenContractInstance.methods.symbol().call();
|
200
|
-
const decimals = await MyTokenContractInstance.methods.decimals().call();
|
201
|
-
const response = await ethereum
|
202
|
-
.request({
|
203
|
-
method: 'wallet_watchAsset',
|
204
|
-
params: {
|
205
|
-
type: 'ERC20',
|
206
|
-
options: {
|
207
|
-
address: tokenAddress,
|
208
|
-
symbol: symbol,
|
209
|
-
decimals: decimals,
|
210
|
-
},
|
211
|
-
},
|
212
|
-
});
|
213
|
-
|
214
|
-
tokenAdded = !!response;
|
215
|
-
}
|
216
|
-
|
217
|
-
return tokenAdded;
|
218
|
-
}
|
138
|
+
};
|
219
139
|
|
220
140
|
const isMetamaskInstalled = () => {
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
141
|
+
if (!checkIfMetamaskIsAvailable()) {
|
142
|
+
return METAMASK_NOT_INSTALLED_MESSAGE
|
143
|
+
}
|
144
|
+
return {
|
145
|
+
ok: true,
|
146
|
+
message: "Metamask is installed",
|
147
|
+
};
|
227
148
|
|
228
|
-
|
229
|
-
ok: true,
|
230
|
-
message: "Metamask is installed"
|
231
|
-
}
|
232
|
-
}
|
149
|
+
};
|
233
150
|
|
234
151
|
const checkWalletFormat = (account) => {
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
return {
|
249
|
-
ok: false,
|
250
|
-
message: "Wrong address format"
|
251
|
-
}
|
252
|
-
}
|
152
|
+
let web3;
|
153
|
+
if (!checkIfMetamaskIsAvailable()) {
|
154
|
+
return METAMASK_NOT_INSTALLED_MESSAGE
|
155
|
+
}
|
156
|
+
try {
|
157
|
+
web3 = new Web3(Metamask);
|
158
|
+
} catch (error) {
|
159
|
+
return {
|
160
|
+
ok: false,
|
161
|
+
message: "Web3 can not be loaded. Metamask must be installed",
|
162
|
+
error: error,
|
163
|
+
};
|
164
|
+
}
|
253
165
|
|
166
|
+
if (!web3.utils.isAddress(account)) {
|
254
167
|
return {
|
255
|
-
|
256
|
-
|
257
|
-
}
|
258
|
-
}
|
168
|
+
ok: false,
|
169
|
+
message: "Wrong address format",
|
170
|
+
};
|
171
|
+
}
|
172
|
+
|
173
|
+
return {
|
174
|
+
ok: true,
|
175
|
+
message: "Proper address format",
|
176
|
+
};
|
177
|
+
};
|
259
178
|
|
260
179
|
const selectOrAddPolygonMainNetwork = async () => {
|
261
|
-
|
262
|
-
|
180
|
+
if (!checkIfMetamaskIsAvailable()) {
|
181
|
+
return METAMASK_NOT_INSTALLED_MESSAGE
|
182
|
+
}
|
183
|
+
await getMetamaskAccount().catch((error) => {
|
184
|
+
throw error;
|
185
|
+
});
|
186
|
+
try {
|
187
|
+
await Metamask.request({
|
188
|
+
method: "wallet_switchEthereumChain",
|
189
|
+
params: [{chainId: "0x89"}],
|
263
190
|
});
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
191
|
+
return {
|
192
|
+
ok: true,
|
193
|
+
message: "Already on polygon main network",
|
194
|
+
};
|
195
|
+
} catch (switchError) {
|
196
|
+
if (switchError.code === 4902) {
|
197
|
+
const params = [
|
198
|
+
{
|
199
|
+
chainId: "0x89",
|
200
|
+
chainName: "Matic MainNet",
|
201
|
+
nativeCurrency: {
|
202
|
+
name: "MATIC",
|
203
|
+
symbol: "MATIC",
|
204
|
+
decimals: 18,
|
205
|
+
},
|
206
|
+
rpcUrls: [
|
207
|
+
"https://polygon-rpc.com/",
|
208
|
+
"https://rpc-mainnet.maticvigil.com/",
|
209
|
+
],
|
210
|
+
blockExplorerUrls: ["https://polygonscan.com/"],
|
211
|
+
},
|
212
|
+
];
|
213
|
+
try {
|
214
|
+
const response = await Metamask.request({
|
215
|
+
method: "wallet_addEthereumChain",
|
216
|
+
params: params,
|
217
|
+
});
|
218
|
+
return {
|
219
|
+
ok: true,
|
220
|
+
message: "Matic MainNet added successfully",
|
221
|
+
response: response,
|
222
|
+
};
|
223
|
+
} catch (error) {
|
224
|
+
return {
|
225
|
+
ok: false,
|
226
|
+
message: "Matic MainNet can not be added",
|
227
|
+
response: error,
|
228
|
+
};
|
229
|
+
}
|
230
|
+
}else{
|
231
|
+
throw switchError
|
305
232
|
}
|
306
|
-
|
307
|
-
}
|
233
|
+
}
|
234
|
+
};
|
308
235
|
|
309
236
|
module.exports = {
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
tokenTransferWithTokenAddressOnly,
|
317
|
-
getTokenOwnersBalance,
|
318
|
-
sendTokenToWallet,
|
319
|
-
isMetamaskInstalled,
|
320
|
-
checkWalletFormat,
|
321
|
-
selectOrAddPolygonMainNetwork
|
322
|
-
}
|
237
|
+
clientTokenTransfer,
|
238
|
+
sendTokenToWallet,
|
239
|
+
isMetamaskInstalled,
|
240
|
+
checkWalletFormat,
|
241
|
+
selectOrAddPolygonMainNetwork,
|
242
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "dracoder-web3-package",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.1",
|
4
4
|
"description": "Dracoder package used for web3 provider integration.",
|
5
5
|
"main": "index.js",
|
6
6
|
"keywords": [
|
@@ -10,12 +10,20 @@
|
|
10
10
|
"metamask"
|
11
11
|
],
|
12
12
|
"scripts": {
|
13
|
-
"
|
13
|
+
"build": "webpack",
|
14
|
+
"start": "webpack && node test/app.js"
|
14
15
|
},
|
15
16
|
"author": "Dracoder S.L.",
|
16
17
|
"license": "MIT",
|
17
18
|
"dependencies": {
|
18
|
-
"
|
19
|
-
|
19
|
+
"web3": "^4.4.0"
|
20
|
+
},
|
21
|
+
"devDependencies": {
|
22
|
+
"@babel/core": "^7.15.0",
|
23
|
+
"@babel/preset-env": "^7.15.0",
|
24
|
+
"babel-loader": "^8.2.2",
|
25
|
+
"express": "^4.18.2",
|
26
|
+
"webpack": "^5.51.1",
|
27
|
+
"webpack-cli": "^4.8.0"
|
20
28
|
}
|
21
29
|
}
|
package/test/app.js
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
const express = require('express');
|
2
|
+
const {join} = require("path");
|
3
|
+
|
4
|
+
const port = 3000;
|
5
|
+
const app = express();
|
6
|
+
|
7
|
+
app.use('/static', express.static(join(__dirname+'/../lib/')));
|
8
|
+
|
9
|
+
app.use(express.urlencoded({ extended: false }));
|
10
|
+
app.use(express.json());
|
11
|
+
|
12
|
+
app.get('/', (req, res) => {
|
13
|
+
res.sendFile(join(__dirname+'/test.html'))
|
14
|
+
});
|
15
|
+
|
16
|
+
app.listen(port, () => console.log(`Servidor iniciado en el puerto ${port}`));
|
package/test/test.html
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
|
4
|
+
<head>
|
5
|
+
<meta charset="UTF-8">
|
6
|
+
<title>Test</title>
|
7
|
+
</head>
|
8
|
+
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<h1>Test web3 integration functionalities</h1>
|
12
|
+
<div id="check-metamask-installed-functionality">
|
13
|
+
<h3>Check if metamask is installed</h3>
|
14
|
+
<button onclick="checkIfMetamaskIsInstalled()">Check</button>
|
15
|
+
<pre>Click "Check" button</pre>
|
16
|
+
</div>
|
17
|
+
|
18
|
+
<div id="check-add-polygon-network">
|
19
|
+
<h3>Check if can add polygon network</h3>
|
20
|
+
<button onclick="checkIfCanAddPolygon()">Check</button>
|
21
|
+
<pre>Click "Check" button</pre>
|
22
|
+
</div>
|
23
|
+
|
24
|
+
<div id="check-wallet-format">
|
25
|
+
<h3>Check wallet format</h3>
|
26
|
+
<input id="inputCheckWalletFormat" style="width: 25rem" type="text"
|
27
|
+
value="0x104d7e4b44F64c633Be6B099995eecb0Ac344611"/>
|
28
|
+
<button onclick="checkWalletFormat()">Check</button>
|
29
|
+
<pre>Click "Check" button</pre>
|
30
|
+
</div>
|
31
|
+
|
32
|
+
<div id="check-add-token-to-metamask">
|
33
|
+
<h3>Check add token to metamask</h3>
|
34
|
+
<input id="inputCheckAddTokenToMetamask" style="width: 25rem" type="text"
|
35
|
+
value="0xF516d5b66a954ae85aC08b33729dFe9BCF380207"/>
|
36
|
+
<button onclick="checkAddTokenToMetamask()">Check</button>
|
37
|
+
<pre>Click "Check" button</pre>
|
38
|
+
</div>
|
39
|
+
|
40
|
+
<div id="transfer-token">
|
41
|
+
<h3>Transfer Token</h3>
|
42
|
+
<input id="inputTokenAddress" style="width: 25rem" type="text" disabled
|
43
|
+
value="0xFdD0631B7B4a24efE135fDC816C3951cE2dB8854"/>
|
44
|
+
<input id="inputRecipientAddress" style="width: 25rem" type="text" value="0x104d7e4b44F64c633Be6B099995eecb0Ac344611" placeholder="Recipient Address"/>
|
45
|
+
<input id="inputAmount" style="width: 25rem" type="number" value="1" disabled/>
|
46
|
+
<button onclick="transferToken()">Send</button>
|
47
|
+
<pre id="transferTokenResult">Click "Send" button</pre>
|
48
|
+
</div>
|
49
|
+
|
50
|
+
<script src="/static/dracoderWeb3Integration.js"></script>
|
51
|
+
<script>
|
52
|
+
function checkIfMetamaskIsInstalled() {
|
53
|
+
const spanToPrintMessage = document.querySelector('#check-metamask-installed-functionality>pre')
|
54
|
+
spanToPrintMessage.innerHTML = JSON.stringify(dracoderWeb3Integration.isMetamaskInstalled())
|
55
|
+
}
|
56
|
+
|
57
|
+
async function checkIfCanAddPolygon() {
|
58
|
+
const spanToPrintMessage = document.querySelector('#check-add-polygon-network>pre')
|
59
|
+
try {
|
60
|
+
const response = await dracoderWeb3Integration.selectOrAddPolygonMainNetwork({})
|
61
|
+
spanToPrintMessage.innerHTML = JSON.stringify(response)
|
62
|
+
} catch (e) {
|
63
|
+
spanToPrintMessage.innerHTML = JSON.stringify(e)
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
function checkWalletFormat() {
|
68
|
+
const input = document.querySelector('#check-wallet-format>input')
|
69
|
+
const spanToPrintMessage = document.querySelector('#check-wallet-format>pre')
|
70
|
+
spanToPrintMessage.innerHTML = JSON.stringify(dracoderWeb3Integration.checkWalletFormat(input.value))
|
71
|
+
}
|
72
|
+
|
73
|
+
async function checkAddTokenToMetamask() {
|
74
|
+
const inputTokenAddress = document.querySelector('#inputCheckAddTokenToMetamask').value
|
75
|
+
|
76
|
+
const spanToPrintMessage = document.querySelector('#check-add-token-to-metamask>pre')
|
77
|
+
try {
|
78
|
+
const result = await dracoderWeb3Integration.sendTokenToWallet({tokenAddress: inputTokenAddress})
|
79
|
+
spanToPrintMessage.innerHTML = JSON.stringify(result)
|
80
|
+
} catch (error) {
|
81
|
+
spanToPrintMessage.innerHTML = JSON.stringify(error)
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
function transferToken() {
|
86
|
+
const inputRecipientAddress = document.getElementById("inputRecipientAddress").value;
|
87
|
+
const inputAmount = document.getElementById("inputAmount").value;
|
88
|
+
const transferTokenResult = document.getElementById("transferTokenResult");
|
89
|
+
|
90
|
+
dracoderWeb3Integration
|
91
|
+
.clientTokenTransfer({
|
92
|
+
walletAddressToTransferTo: inputRecipientAddress,
|
93
|
+
tokenAddress: "0xFdD0631B7B4a24efE135fDC816C3951cE2dB8854",
|
94
|
+
amount: inputAmount,
|
95
|
+
})
|
96
|
+
.then((response) => {
|
97
|
+
transferTokenResult.innerHTML = JSON.stringify(response);
|
98
|
+
})
|
99
|
+
.catch((error) => {
|
100
|
+
transferTokenResult.innerHTML = JSON.stringify(error);
|
101
|
+
});
|
102
|
+
}
|
103
|
+
</script>
|
104
|
+
</body>
|
105
|
+
|
106
|
+
</html>
|
@@ -0,0 +1,29 @@
|
|
1
|
+
const path = require("path");
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
entry: "./index.js",
|
5
|
+
output: {
|
6
|
+
path: path.resolve(__dirname, "lib"),
|
7
|
+
filename: "dracoderWeb3Integration.js",
|
8
|
+
library: {
|
9
|
+
name: "dracoderWeb3Integration",
|
10
|
+
type: "umd",
|
11
|
+
},
|
12
|
+
globalObject: "this",
|
13
|
+
},
|
14
|
+
mode: "development",
|
15
|
+
module: {
|
16
|
+
rules: [
|
17
|
+
{
|
18
|
+
test: /\.m?js$/,
|
19
|
+
exclude: /node_modules/,
|
20
|
+
use: {
|
21
|
+
loader: "babel-loader",
|
22
|
+
options: {
|
23
|
+
presets: ["@babel/preset-env"],
|
24
|
+
},
|
25
|
+
},
|
26
|
+
},
|
27
|
+
],
|
28
|
+
},
|
29
|
+
};
|