-
Notifications
You must be signed in to change notification settings - Fork 0
/
perc20.sh
executable file
·396 lines (318 loc) · 11.8 KB
/
perc20.sh
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
259
260
261
262
263
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/bin/sh
sudo apt-get update && sudo apt-get upgrade -y
clear
echo "Installing dependencies..."
npm install --save-dev hardhat
npm install dotenv
npm install @swisstronik/utils
npm install @openzeppelin/contracts
echo "Installation completed."
echo "Creating a Hardhat project..."
npx hardhat
rm -f contracts/Lock.sol
echo "Lock.sol removed."
echo "Hardhat project created."
echo "Installing Hardhat toolbox..."
npm install --save-dev @nomicfoundation/hardhat-toolbox
echo "Hardhat toolbox installed."
echo "Creating .env file..."
read -p "Enter your private key: " PRIVATE_KEY
echo "PRIVATE_KEY=$PRIVATE_KEY" > .env
echo ".env file created."
echo "Configuring Hardhat..."
cat <<EOL > hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
defaultNetwork: "swisstronik",
solidity: "0.8.20",
networks: {
swisstronik: {
url: "https://json-rpc.testnet.swisstronik.com/",
accounts: [\`0x\${process.env.PRIVATE_KEY}\`],
},
},
};
EOL
echo "Hardhat configuration completed."
read -p "Enter the token name: " TOKEN_NAME
read -p "Enter the token symbol: " TOKEN_SYMBOL
echo "Creating IPERC20.sol contract..."
mkdir -p contracts
cat <<EOL > contracts/IPERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.17;
interface IPERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
EOL
echo "IPERC20.sol contract created."
echo "Creating IPERC20Metadata.sol contract..."
cat <<EOL > contracts/IPERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.17;
import "./IPERC20.sol";
interface IERC20Metadata is IPERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
EOL
echo "IPERC20Metadata.sol contract created."
echo "Creating PERC20.sol contract..."
cat <<EOL > contracts/PERC20.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./IPERC20.sol";
import "./IPERC20Metadata.sol";
import "@openzeppelin/contracts/utils/Context.sol";
contract PERC20 is Context, IPERC20, IERC20Metadata {
mapping(address => uint256) internal _balances;
mapping(address => mapping(address => uint256)) internal _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
function name() public view virtual override returns (string memory) {
return _name;
}
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
function decimals() public view virtual override returns (uint8) {
return 18;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address) public view virtual override returns (uint256) {
revert("PERC20: default \`balanceOf\` function is disabled");
}
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address, address) public view virtual override returns (uint256) {
revert("PERC20: default \`allowance\` function is disabled");
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "PERC20: transfer amount exceeds allowance");
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "PERC20: decreased allowance below zero");
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
}
return true;
}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), "PERC20: transfer from the zero address");
require(recipient != address(0), "PERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "PERC20: transfer amount exceeds balance");
unchecked {
_balances[sender] = senderBalance - amount;
}
_balances[recipient] += amount;
_afterTokenTransfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "PERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
_afterTokenTransfer(address(0), account, amount);
}
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "PERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "PERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;
_afterTokenTransfer(account, address(0), amount);
}
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "PERC20: approve from the zero address");
require(spender != address(0), "PERC20: approve to the zero address");
_allowances[owner][spender] = amount;
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
EOL
echo "PERC20.sol contract created."
echo "Creating PERC20Sample.sol contract..."
cat <<EOL > contracts/PERC20Sample.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./PERC20.sol";
contract PERC20Sample is PERC20 {
constructor() PERC20("$TOKEN_NAME", "$TOKEN_SYMBOL") {}
function mint100tokens() public {
_mint(msg.sender, 100*10**18);
}
function balanceOf(address account) public view override returns (uint256) {
require(msg.sender == account, "PERC20Sample: msg.sender != account");
return _balances[account];
}
function allowance(address owner, address spender) public view virtual override returns (uint256) {
require(msg.sender == spender, "PERC20Sample: msg.sender != account");
return _allowances[owner][spender];
}
}
EOL
echo "PERC20Sample.sol contract created."
echo "Compiling the contract..."
npx hardhat compile
echo "Contract compiled."
echo "Creating deploy.js script..."
mkdir -p scripts
cat <<EOL > scripts/deploy.js
const { ethers } = require("hardhat");
const fs = require("fs");
async function main() {
const perc20 = await ethers.deployContract("PERC20Sample");
await perc20.waitForDeployment();
const deployedContract = await perc20.getAddress();
fs.writeFileSync("contract.txt", deployedContract);
console.log(\`PERC20Sample was deployed to: \${deployedContract}\`)
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
EOL
echo "deploy.js script created."
echo "Deploying the contract..."
npx hardhat run scripts/deploy.js --network swisstronik
echo "Contract deployed."
echo "Creating mint.js script..."
cat <<EOL > scripts/mint.js
const hre = require("hardhat");
const fs = require("fs");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
const sendShieldedTransaction = async (signer, destination, data, value) => {
const rpcLink = hre.network.config.url;
const [encryptedData] = await encryptDataField(rpcLink, data);
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
});
};
async function main() {
const contractAddress = fs.readFileSync("contract.txt", "utf8").trim();
const [signer] = await hre.ethers.getSigners();
const contractFactory = await hre.ethers.getContractFactory("PERC20Sample");
const contract = contractFactory.attach(contractAddress);
const functionName = "mint100tokens";
const mint100TokensTx = await sendShieldedTransaction(
signer,
contractAddress,
contract.interface.encodeFunctionData(functionName),
0
);
await mint100TokensTx.wait();
console.log("Transaction Receipt: ", \`Minting token has been success! Transaction hash: https://explorer-evm.testnet.swisstronik.com/tx/\${mint100TokensTx.hash}\`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
EOL
echo "mint.js script created."
echo "Minting tokens..."
npx hardhat run scripts/mint.js --network swisstronik
echo "Tokens minted."
echo "Creating transfer.js script..."
cat <<EOL > scripts/transfer.js
const hre = require("hardhat");
const fs = require("fs");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
const sendShieldedTransaction = async (signer, destination, data, value) => {
const rpcLink = hre.network.config.url;
const [encryptedData] = await encryptDataField(rpcLink, data);
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
});
};
async function main() {
const contractAddress = fs.readFileSync("contract.txt", "utf8").trim();
const [signer] = await hre.ethers.getSigners();
const contractFactory = await hre.ethers.getContractFactory("PERC20Sample");
const contract = contractFactory.attach(contractAddress);
const functionName = "transfer";
const amount = 1 * 10 ** 18;
const functionArgs = ["0x16af037878a6cAce2Ea29d39A3757aC2F6F7aac1", amount.toString()];
const transaction = await sendShieldedTransaction(
signer,
contractAddress,
contract.interface.encodeFunctionData(functionName, functionArgs),
0
);
await transaction.wait();
console.log("Transaction Response: ", \`Transfer token has been success! Transaction hash: https://explorer-evm.testnet.swisstronik.com/tx/\${transaction.hash}\`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
EOL
echo "transfer.js script created."
echo "Transferring tokens..."
npx hardhat run scripts/transfer.js --network swisstronik
echo "Tokens transferred."
echo "Done!