Best JavaScript code snippet using wpt
CToken.ts
Source:CToken.ts
1import { PromiEvent, TransactionReceipt } from "web3-core";2import BN from "bn.js";3import MarketContract from "./MarketContract";4import CTokenArtifact from "../abi/CToken.json";5import CTokenV2Artifact from "../abi/CTokenV2.json";6import { CToken as CTokenWeb3Interface } from "../types/CToken";7import { CTokenV2 as CTokenV2Web3Interface } from "../types/CTokenV2";8import { NonPayableTx } from "../types/types";9import { Comptroller, ComptrollerV2 } from "./Comptroller";10import MarketSDK from "./MarketSDK";11class CToken extends MarketContract<CTokenWeb3Interface> {12 constructor(sdk: MarketSDK, address: string){13 super(sdk, address, CTokenArtifact.abi);14 }15 _acceptAdmin(16 tx?: NonPayableTx17 ): PromiEvent<TransactionReceipt> {18 return this.contract.methods._acceptAdmin().send(tx);19 }20 _reduceReserves(21 redduceAmount: number | string | BN,22 tx?: NonPayableTx,23 ): PromiEvent<TransactionReceipt> {24 return this.contract.methods._reduceReserves(redduceAmount).send(tx);25 }26 _renounceAdminRights(27 tx?: NonPayableTx28 ): PromiEvent<TransactionReceipt> {29 return this.contract.methods._renounceAdminRights().send(tx);30 }31 _renounceFuseAdminRights(32 tx?: NonPayableTx33 ): PromiEvent<TransactionReceipt> {34 return this.contract.methods._renounceFuseAdminRights().send(tx);35 }36 _setAdminFee(37 newAdminFeeMantissa: number | string | BN,38 tx?: NonPayableTx39 ): PromiEvent<TransactionReceipt> {40 return this.contract.methods._setAdminFee(newAdminFeeMantissa).send(tx);41 }42 _setComptroller(43 newComptroller: Comptroller,44 tx?: NonPayableTx45 ): PromiEvent<TransactionReceipt> {46 return this.contract.methods._setComptroller(newComptroller.address).send(tx);47 }48 _setFuseFee(49 tx?: NonPayableTx50 ): PromiEvent<TransactionReceipt> {51 return this.contract.methods._setFuseFee().send(tx);52 }53 _setInterestRateModel(54 newInterestRateModel: string, // change to InterestRateModel class later55 tx?: NonPayableTx56 ): PromiEvent<TransactionReceipt> {57 return this.contract.methods._setInterestRateModel(newInterestRateModel).send(tx);58 }59 _setPendingAdmin(60 newPendingAdmin: string,61 tx?: NonPayableTx62 ): PromiEvent<TransactionReceipt> {63 return this.contract.methods._setPendingAdmin(newPendingAdmin).send(tx);64 }65 _setReserveFactor(66 newReserveFactorMantissa: number | string | BN,67 tx?: NonPayableTx68 ): PromiEvent<TransactionReceipt> {69 return this.contract.methods._setReserveFactor(newReserveFactorMantissa).send(tx);70 }71 _withdrawAdminFees(72 withdrawAmount: number | string | BN,73 tx?: NonPayableTx74 ): PromiEvent<TransactionReceipt> {75 return this.contract.methods._withdrawAdminFees(withdrawAmount).send(tx);76 }77 _withdrawFuseFees(78 withdrawAmount: number | string | BN,79 tx?: NonPayableTx80 ): PromiEvent<TransactionReceipt> {81 return this.contract.methods._withdrawFuseFees(withdrawAmount).send(tx);82 }83 accrualBlockNumber(84 tx?: NonPayableTx85 ): Promise<string> {86 return this.contract.methods.accrualBlockNumber().call(tx);87 }88 accrueInterest(89 tx?: NonPayableTx90 ): Promise<string> {91 return this.contract.methods.accrueInterest().call(tx);92 }93 admin(94 tx?: NonPayableTx95 ): Promise<string> {96 return this.contract.methods.admin().call(tx);97 }98 adminFeeMantissa(99 tx?: NonPayableTx100 ): Promise<string> {101 return this.contract.methods.adminFeeMantissa().call(tx);102 }103 adminHasRights(104 tx?: NonPayableTx105 ): Promise<boolean> {106 return this.contract.methods.adminHasRights().call(tx);107 }108 allowance(109 owner: string,110 spender: string,111 tx?: NonPayableTx112 ): PromiEvent<TransactionReceipt> {113 return this.contract.methods.allowance(owner, spender).send(tx);114 }115 approve(116 spender: string,117 amount: number | string | BN,118 tx?: NonPayableTx119 ): PromiEvent<TransactionReceipt> {120 return this.contract.methods.approve(spender, amount).send(tx);121 }122 balanceOf(123 owner: string,124 tx?: NonPayableTx125 ): Promise<string> {126 return this.contract.methods.balanceOf(owner).call(tx);127 }128 balanceOfUnderlying(129 owner: string,130 tx?: NonPayableTx131 ): Promise<string> {132 return this.contract.methods.balanceOfUnderlying(owner).call(tx);133 }134 borrow(135 borrowAmount: number | string | BN,136 tx?: NonPayableTx137 ): PromiEvent<TransactionReceipt> {138 return this.contract.methods.borrow(borrowAmount).send(tx);139 }140 borrowBalanceCurrent(141 account: string,142 tx?: NonPayableTx143 ): Promise<string> {144 return this.contract.methods.borrowBalanceCurrent(account).call(tx);145 }146 borrowBalanceStored(147 account: string,148 tx?: NonPayableTx149 ): Promise<string> {150 return this.contract.methods.borrowBalanceStored(account).call(tx);151 }152 borrowIndex(153 tx?: NonPayableTx154 ): Promise<string> {155 return this.contract.methods.borrowIndex().call(tx);156 }157 borrowRatePerBlock(158 tx?: NonPayableTx159 ): Promise<string> {160 return this.contract.methods.borrowRatePerBlock().call(tx);161 }162 async comptroller(163 tx?: NonPayableTx164 ): Promise<Comptroller> {165 const addr = await this.contract.methods.comptroller().call(tx);166 return new Comptroller(this.sdk, addr);167 }168 169 decimals(170 tx?: NonPayableTx171 ): Promise<string> {172 return this.contract.methods.decimals().call(tx);173 }174 exchangeRateCurrent(175 tx?: NonPayableTx176 ): Promise<string> {177 return this.contract.methods.exchangeRateCurrent().call(tx);178 }179 exchangeRateStored(180 tx?: NonPayableTx181 ): Promise<string> {182 return this.contract.methods.exchangeRateStored().call(tx);183 }184 fuseAdminHasRights(185 tx?: NonPayableTx186 ): Promise<boolean> {187 return this.contract.methods.fuseAdminHasRights().call(tx);188 }189 fuseFeeMantissa(190 tx?: NonPayableTx191 ): Promise<string> {192 return this.contract.methods.fuseFeeMantissa().call(tx);193 }194 getAccountSnapshot(195 account: string,196 tx?: NonPayableTx197 ): Promise<{198 0: string;199 1: string;200 2: string;201 3: string;202 }> {203 return this.contract.methods.getAccountSnapshot(account).call(tx);204 }205 getCash(206 tx?: NonPayableTx207 ): Promise<string> {208 return this.contract.methods.getCash().call(tx);209 }210 interestRateModel(211 tx?: NonPayableTx212 ): Promise<string> {213 return this.contract.methods.interestRateModel().call(tx);214 }215 isCEther(216 tx?: NonPayableTx217 ): Promise<boolean> {218 return this.contract.methods.isCEther().call(tx);219 }220 isCToken(221 tx?: NonPayableTx222 ): Promise<boolean> {223 return this.contract.methods.isCToken().call(tx);224 }225 liquidateBorrow(226 borrower: string,227 repayAmount: number | string | BN,228 cTokenCollateral: CToken | string,229 tx?: NonPayableTx230 ): PromiEvent<TransactionReceipt> {231 if(cTokenCollateral instanceof CToken){232 cTokenCollateral = cTokenCollateral.address;233 }234 return this.contract.methods.liquidateBorrow(borrower, repayAmount, cTokenCollateral).send(tx);235 }236 mint(237 mintAmount: number | string | BN,238 tx?: NonPayableTx239 ): PromiEvent<TransactionReceipt> {240 return this.contract.methods.mint(mintAmount).send(tx);241 }242 name(243 tx?: NonPayableTx244 ): Promise<string> {245 return this.contract.methods.name().call(tx);246 }247 pendingAdmin(248 tx?: NonPayableTx249 ): Promise<string> {250 return this.contract.methods.pendingAdmin().call(tx);251 }252 redeem(253 redeemTokens: number | string | BN,254 tx?: NonPayableTx255 ): PromiEvent<TransactionReceipt> {256 return this.contract.methods.redeem(redeemTokens).send(tx);257 }258 redeemUnderlying(259 redeemAdmount: number | string | BN,260 tx?: NonPayableTx261 ): PromiEvent<TransactionReceipt> {262 return this.contract.methods.redeemUnderlying(redeemAdmount).send(tx);263 }264 repayBorrow(265 repayAmount: number | string | BN,266 tx?: NonPayableTx267 ): PromiEvent<TransactionReceipt> {268 return this.contract.methods.repayBorrow(repayAmount).send(tx);269 }270 repayBorrowBehalf(271 borrower: string,272 repayAmount: number | string | BN,273 tx?: NonPayableTx274 ): PromiEvent<TransactionReceipt> {275 return this.contract.methods.repayBorrowBehalf(borrower, repayAmount).send(tx);276 }277 reserveFactorMantissa(278 tx?: NonPayableTx279 ): Promise<string> {280 return this.contract.methods.reserveFactorMantissa().call(tx);281 }282 seize(283 liquidator: string, 284 borrower: string,285 seizeTokens: number | string | BN,286 tx?: NonPayableTx287 ): PromiEvent<TransactionReceipt> {288 return this.contract.methods.seize(liquidator, borrower, seizeTokens).send(tx);289 }290 supplyRatePerBlock(291 tx?: NonPayableTx292 ): Promise<string> {293 return this.contract.methods.supplyRatePerBlock().call(tx);294 }295 symbol(296 tx?: NonPayableTx297 ): Promise<string> {298 return this.contract.methods.symbol().call(tx);299 }300 totalAdminFees(301 tx?: NonPayableTx302 ): Promise<string> {303 return this.contract.methods.totalAdminFees().call(tx);304 }305 totalBorrows(306 tx?: NonPayableTx307 ): Promise<string> {308 return this.contract.methods.totalBorrows().call(tx);309 }310 totalBorrowsCurrent(311 tx?: NonPayableTx312 ): Promise<string> {313 return this.contract.methods.totalBorrowsCurrent().call(tx);314 }315 totalFuseFees(316 tx?: NonPayableTx317 ): Promise<string> {318 return this.contract.methods.totalFuseFees().call(tx);319 }320 totalReserves(321 tx?: NonPayableTx322 ): Promise<string> {323 return this.contract.methods.totalReserves().call(tx);324 }325 totalSupply(326 tx?: NonPayableTx327 ): Promise<string> {328 return this.contract.methods.totalSupply().call(tx);329 }330 transfer(331 dst: string,332 amount: number | string | BN,333 tx?: NonPayableTx334 ): PromiEvent<TransactionReceipt> {335 return this.contract.methods.transfer(dst, amount).send(tx);336 }337 transferFrom(338 src: string,339 dst: string,340 amount: number | string | BN,341 tx?: NonPayableTx342 ): PromiEvent<TransactionReceipt> {343 return this.contract.methods.transferFrom(src, dst, amount).send(tx);344 }345 underlying(346 tx?: NonPayableTx347 ): Promise<string> {348 return this.contract.methods.underlying().call(tx);349 }350}351class CTokenV2 extends MarketContract<CTokenV2Web3Interface> {352 constructor(sdk: MarketSDK, address: string){353 super(sdk, address, CTokenV2Artifact.abi);354 }355 356 _reduceReserves(357 redduceAmount: number | string | BN,358 tx?: NonPayableTx,359 ): PromiEvent<TransactionReceipt> {360 return this.contract.methods._reduceReserves(redduceAmount).send(tx);361 }362 363 _setAdminFee(364 newAdminFeeMantissa: number | string | BN,365 tx?: NonPayableTx366 ): PromiEvent<TransactionReceipt> {367 return this.contract.methods._setAdminFee(newAdminFeeMantissa).send(tx);368 }369 370 _setInterestRateModel(371 newInterestRateModel: string, // change to InterestRateModel class later372 tx?: NonPayableTx373 ): PromiEvent<TransactionReceipt> {374 return this.contract.methods._setInterestRateModel(newInterestRateModel).send(tx);375 }376 _setNameAndSymbol(377 name: string,378 symbol: string,379 tx?: NonPayableTx380 ): PromiEvent<TransactionReceipt> {381 return this.contract.methods._setNameAndSymbol(name, symbol).send(tx);382 }383 _setReserveFactor(384 newReserveFactorMantissa: number | string | BN,385 tx?: NonPayableTx386 ): PromiEvent<TransactionReceipt> {387 return this.contract.methods._setReserveFactor(newReserveFactorMantissa).send(tx);388 }389 _withdrawAdminFees(390 withdrawAmount: number | string | BN,391 tx?: NonPayableTx392 ): PromiEvent<TransactionReceipt> {393 return this.contract.methods._withdrawAdminFees(withdrawAmount).send(tx);394 }395 _withdrawFuseFees(396 withdrawAmount: number | string | BN,397 tx?: NonPayableTx398 ): PromiEvent<TransactionReceipt> {399 return this.contract.methods._withdrawFuseFees(withdrawAmount).send(tx);400 }401 accrualBlockNumber(402 tx?: NonPayableTx403 ): Promise<string> {404 return this.contract.methods.accrualBlockNumber().call(tx);405 }406 accrueInterest(407 tx?: NonPayableTx408 ): Promise<string> {409 return this.contract.methods.accrueInterest().call(tx);410 }411 adminFeeMantissa(412 tx?: NonPayableTx413 ): Promise<string> {414 return this.contract.methods.adminFeeMantissa().call(tx);415 }416 allowance(417 owner: string,418 spender: string,419 tx?: NonPayableTx420 ): PromiEvent<TransactionReceipt> {421 return this.contract.methods.allowance(owner, spender).send(tx);422 }423 approve(424 spender: string,425 amount: number | string | BN,426 tx?: NonPayableTx427 ): PromiEvent<TransactionReceipt> {428 return this.contract.methods.approve(spender, amount).send(tx);429 }430 balanceOf(431 owner: string,432 tx?: NonPayableTx433 ): Promise<string> {434 return this.contract.methods.balanceOf(owner).call(tx);435 }436 balanceOfUnderlying(437 owner: string,438 tx?: NonPayableTx439 ): Promise<string> {440 return this.contract.methods.balanceOfUnderlying(owner).call(tx);441 }442 borrow(443 borrowAmount: number | string | BN,444 tx?: NonPayableTx445 ): PromiEvent<TransactionReceipt> {446 return this.contract.methods.borrow(borrowAmount).send(tx);447 }448 borrowBalanceCurrent(449 account: string,450 tx?: NonPayableTx451 ): Promise<string> {452 return this.contract.methods.borrowBalanceCurrent(account).call(tx);453 }454 borrowBalanceStored(455 account: string,456 tx?: NonPayableTx457 ): Promise<string> {458 return this.contract.methods.borrowBalanceStored(account).call(tx);459 }460 borrowIndex(461 tx?: NonPayableTx462 ): Promise<string> {463 return this.contract.methods.borrowIndex().call(tx);464 }465 borrowRatePerBlock(466 tx?: NonPayableTx467 ): Promise<string> {468 return this.contract.methods.borrowRatePerBlock().call(tx);469 }470 471 async comptroller(472 tx?: NonPayableTx473 ): Promise<ComptrollerV2> {474 const addr = await this.contract.methods.comptroller().call(tx);475 return new ComptrollerV2(this.sdk, addr);476 }477 decimals(478 tx?: NonPayableTx479 ): Promise<string> {480 return this.contract.methods.decimals().call(tx);481 }482 exchangeRateCurrent(483 tx?: NonPayableTx484 ): Promise<string> {485 return this.contract.methods.exchangeRateCurrent().call(tx);486 }487 exchangeRateStored(488 tx?: NonPayableTx489 ): Promise<string> {490 return this.contract.methods.exchangeRateStored().call(tx);491 }492 fuseFeeMantissa(493 tx?: NonPayableTx494 ): Promise<string> {495 return this.contract.methods.fuseFeeMantissa().call(tx);496 }497 getAccountSnapshot(498 account: string,499 tx?: NonPayableTx500 ): Promise<{501 0: string;502 1: string;503 2: string;504 3: string;505 }> {506 return this.contract.methods.getAccountSnapshot(account).call(tx);507 }508 getCash(509 tx?: NonPayableTx510 ): Promise<string> {511 return this.contract.methods.getCash().call(tx);512 }513 "initialize(address,address,uint256,string,string,uint8,uint256,uint256)"(514 comptroller: ComptrollerV2 | string,515 interestRateModel: string,516 initialExchangeRateMantissa: number | string | BN,517 name: string,518 symbol: string,519 decimals: number | string | BN,520 reserveFactorMantissa: number | string | BN,521 adminFeeMantissa: number | string | BN,522 tx?: NonPayableTx523 ): PromiEvent<TransactionReceipt> {524 comptroller = comptroller instanceof ComptrollerV2 ? comptroller.address : comptroller;525 return this.contract.methods["initialize(address,address,uint256,string,string,uint8,uint256,uint256)"](comptroller, interestRateModel, initialExchangeRateMantissa, name, symbol, decimals, reserveFactorMantissa, adminFeeMantissa).send(tx);526 }527 "initialize(address,address,string,string,uint256,uint256)"(528 comptroller: ComptrollerV2 | string,529 interestRateModel: string,530 name: string,531 symbol: string,532 reserveFactorMantissa: number | string | BN,533 adminFeeMantissa: number | string | BN,534 tx?: NonPayableTx535 ): PromiEvent<TransactionReceipt> {536 comptroller = comptroller instanceof ComptrollerV2 ? comptroller.address : comptroller;537 return this.contract.methods["initialize(address,address,string,string,uint256,uint256)"](comptroller, interestRateModel, name, symbol, reserveFactorMantissa, adminFeeMantissa).send(tx);538 }539 540 interestRateModel(541 tx?: NonPayableTx542 ): Promise<string> {543 return this.contract.methods.interestRateModel().call(tx);544 }545 isCEther(546 tx?: NonPayableTx547 ): Promise<boolean> {548 return this.contract.methods.isCEther().call(tx);549 }550 isCToken(551 tx?: NonPayableTx552 ): Promise<boolean> {553 return this.contract.methods.isCToken().call(tx);554 }555 liquidateBorrow(556 borrower: string,557 cTokenCollateral: CTokenV2 | string,558 tx?: NonPayableTx559 ): PromiEvent<TransactionReceipt> {560 if(cTokenCollateral instanceof CTokenV2){561 cTokenCollateral = cTokenCollateral.address;562 }563 return this.contract.methods.liquidateBorrow(borrower, cTokenCollateral).send(tx);564 }565 mint(566 tx?: NonPayableTx567 ): PromiEvent<TransactionReceipt> {568 return this.contract.methods.mint().send(tx);569 }570 name(571 tx?: NonPayableTx572 ): Promise<string> {573 return this.contract.methods.name().call(tx);574 }575 protocolSeizeShareMantissa(576 tx?: NonPayableTx577 ): Promise<string> {578 return this.contract.methods.protocolSeizeShareMantissa().call(tx);579 }580 581 redeem(582 redeemTokens: number | string | BN,583 tx?: NonPayableTx584 ): PromiEvent<TransactionReceipt> {585 return this.contract.methods.redeem(redeemTokens).send(tx);586 }587 redeemUnderlying(588 redeemAdmount: number | string | BN,589 tx?: NonPayableTx590 ): PromiEvent<TransactionReceipt> {591 return this.contract.methods.redeemUnderlying(redeemAdmount).send(tx);592 }593 repayBorrow(594 tx?: NonPayableTx595 ): PromiEvent<TransactionReceipt> {596 return this.contract.methods.repayBorrow().send(tx);597 }598 repayBorrowBehalf(599 borrower: string,600 tx?: NonPayableTx601 ): PromiEvent<TransactionReceipt> {602 return this.contract.methods.repayBorrowBehalf(borrower).send(tx);603 }604 reserveFactorMantissa(605 tx?: NonPayableTx606 ): Promise<string> {607 return this.contract.methods.reserveFactorMantissa().call(tx);608 }609 seize(610 liquidator: string, 611 borrower: string,612 seizeTokens: number | string | BN,613 tx?: NonPayableTx614 ): PromiEvent<TransactionReceipt> {615 return this.contract.methods.seize(liquidator, borrower, seizeTokens).send(tx);616 }617 supplyRatePerBlock(618 tx?: NonPayableTx619 ): Promise<string> {620 return this.contract.methods.supplyRatePerBlock().call(tx);621 }622 symbol(623 tx?: NonPayableTx624 ): Promise<string> {625 return this.contract.methods.symbol().call(tx);626 }627 totalAdminFees(628 tx?: NonPayableTx629 ): Promise<string> {630 return this.contract.methods.totalAdminFees().call(tx);631 }632 totalBorrows(633 tx?: NonPayableTx634 ): Promise<string> {635 return this.contract.methods.totalBorrows().call(tx);636 }637 totalBorrowsCurrent(638 tx?: NonPayableTx639 ): Promise<string> {640 return this.contract.methods.totalBorrowsCurrent().call(tx);641 }642 totalFuseFees(643 tx?: NonPayableTx644 ): Promise<string> {645 return this.contract.methods.totalFuseFees().call(tx);646 }647 totalReserves(648 tx?: NonPayableTx649 ): Promise<string> {650 return this.contract.methods.totalReserves().call(tx);651 }652 totalSupply(653 tx?: NonPayableTx654 ): Promise<string> {655 return this.contract.methods.totalSupply().call(tx);656 }657 transfer(658 dst: string,659 amount: number | string | BN,660 tx?: NonPayableTx661 ): PromiEvent<TransactionReceipt> {662 return this.contract.methods.transfer(dst, amount).send(tx);663 }664 transferFrom(665 src: string,666 dst: string,667 amount: number | string | BN,668 tx?: NonPayableTx669 ): PromiEvent<TransactionReceipt> {670 return this.contract.methods.transferFrom(src, dst, amount).send(tx);671 }672 underlying(673 tx?: NonPayableTx674 ): Promise<string> {675 return this.contract.methods.underlying().call(tx);676 }677}...
wallet.js
Source:wallet.js
1const ecdsa = require("elliptic"); // íì곡ì ëì§í¸ìëª
ìê³ ë¦¬ì¦2const fs = require("fs");3const _ = require("lodash");4const TX = require("./transaction");5const EC = new ecdsa.ec("secp256k1");6const privateKeyLocation =7 process.env.PRIVATE_KEY || "miner1/wallet/private_key";8// ì§ê°ìì ë¹ë°í¤ ê°ì ¸ì¤ê¸°9const getPrivateFromWallet = () => {10 // privateKeyLocationê²½ë¡ì íì¼ì ë´ì©ì bufferì ë´ê³ 문ìì´ë¡ ë°í11 const buffer = fs.readFileSync(privateKeyLocation, "utf8");12 return buffer.toString();13};14// ì§ê°ìì ê³µê°í¤ ê°ì ¸ì¤ê¸°15const getPublicFromWallet = () => {16 // ë´ ë¹ë°í¤ë¥¼ ê°ì ¸ìì17 const privateKey = getPrivateFromWallet();18 // ê·¸ ë¹ë°í¤ë¡ë¶í° í¤ìì ë§ë¤ê³ 19 const key = EC.keyFromPrivate(privateKey, "hex");20 // ê·¸ í¤ìì¼ë¡ë¶í° ê³µê°í¤ë¥¼ ë§ë¤ì´ 16ì§ì íí(130ì리)ë¡ ë³ííì¬ ë°í21 return key.getPublic().encode("hex");22};23// ë¹ë°í¤ ìì±24const generatePrivateKey = () => {25 // í¤ìì ìì±í´ì26 const keyPair = EC.genKeyPair();27 // ê·¸ í¤ìì¼ë¡ë¶í° ë¹ë°í¤ë¥¼ ë§ë¤ì´28 const privateKey = keyPair.getPrivate();29 // 16ì§ì 문ìì´ë¡ ë°í30 return privateKey.toString(16);31};32// ì§ê° ì´ê¸°í33const initWallet = () => {34 // í´ë¹ ê²½ë¡ì ë¹ë°í¤ê° ì¡´ì¬íë©´ ì무ê²ë ìí¨35 if (fs.existsSync(privateKeyLocation)) {36 return;37 }38 // ì§ê°(ë¹ë°í¤)ì´ ìì¼ë©´ ë¹ë°í¤ë¥¼ ë§ë¤ì´ì39 const newPrivateKey = generatePrivateKey();40 // ì§ê° ê²½ë¡ì ìë¡ íì¼ì ë§ë¤ê³ íì¼ ë´ì©ì ë¹ë°í¤(16ì§ì 64ì리)를 기ì
í´ì£¼ê¸°41 fs.writeFileSync(privateKeyLocation, newPrivateKey);42 console.log("ìë¡ ì§ê° ë§ë¤ìì´ì¼ ->", privateKeyLocation);43};44// ì§ê° ìì 45const deleteWallet = () => {46 // í´ë¹ ê²½ë¡ì ì§ê°ì´ ìì¼ë©´47 if (fs.existsSync(privateKeyLocation)) {48 // ê·¸ ê²½ë¡ì ìë ì§ê°(private_key)49 fs.unlinkSync(privateKeyLocation);50 }51};52// ì§ê° ìê³ ì¡°í53const getBalance = (address, unspentTxOuts) => {54 // 미ì¬ì© í¸ëìì
ìì í´ë¹ ì§ê°ì£¼ìë§ ì°¾ìì í©ì¹ê¸°55 return _(findUnspentTxOuts(address, unspentTxOuts))56 .map((uTxO) => uTxO.amount)57 .sum();58};59// 미ì¬ì© í¸ëìì
ë¤ì§ê¸°60const findUnspentTxOuts = (ownerAddress, unspentTxOuts) => {61 // 미ì¬ì© í¸ëìì
ìì ì구íë ì§ê°ì£¼ì(ownerAddress)ì ì¼ì¹íë62 // ì§ê°ì£¼ìë¤(uTxO.address) ì°¾ìì ë°í63 return _.filter(unspentTxOuts, (uTxO) => uTxO.address === ownerAddress);64};65// ì§ë¶í ê¸ì¡ì ëí ë´ ììíë¤ ì°¾ê¸°66const findTxOutsForAmount = (amount, myUnspentTxOuts) => {67 let currentAmount = 0; // íì¬ê¸ì¡ 068 const includedUnspentTxOuts = []; // í¬í¨ë uTxOs69 // ë´ uTxO ê°¯ìë§í¼ ë°ë³µ70 for (const myUnspentTxOut of myUnspentTxOuts) {71 // í¬í¨ëuTxOs 목ë¡ì í´ë¹ uTxO ë£ê¸°72 includedUnspentTxOuts.push(myUnspentTxOut);73 // íì¬ ê¸ì¡ì í´ë¹ uTxOì ê¸ì¡ë§í¼ ëí´ì£¼ê¸°74 currentAmount = currentAmount + myUnspentTxOut.amount;75 // íì¬ ê¸ì¡ì´ ì§ë¶í ê¸ì¡ ì´ìì´ë©´ 짤짤ì´ê° ìê¹76 if (currentAmount >= amount) {77 // ì§¤ì§¤ì´ = ë´ íì¬ê¸ì¡ - ì§ë¶í ê¸ì¡78 const leftOverAmount = currentAmount - amount;79 // í¬í¨ëuTxOs, ì§¤ì§¤ì´ ë°í80 return { includedUnspentTxOuts, leftOverAmount };81 }82 } /* ë´uTxOë¤ì ê¸ì¡ë¤ì´ ê°ê° ë¤ì´ìëë°83 ê·¸ ê¸ì¡ë¤ì ì§ë¶í ê¸ì¡ì ëë¬í ëê¹ì§ í©ì¹ê³ 84 í©ì³ì§ uTxOë¤ì ì¶íì ì ì°í ë íê°ì uTxOê° ë ê²ì´ë¤85 ì) ë´ ì§ê°ì 12ìì§ë¦¬, 8ìì§ë¦¬ 5ìì§ë¦¬, 30ìì§ë¦¬ê° ìë¤.86 ë´ê° Aìê² 21ìì ì¤ì¼ íë¤.87 12ìì§ë¦¬, 8ìì§ë¦¬, 5ìì§ë¦¬ë¥¼ í©ì¹ë 25ìì´ë¤(25ì>=21ì)88 í¬í¨ëuTxOsìë 12ìì§ë¦¬, 8ìì§ë¦¬, 5ìì§ë¦¬ uTxOê° ë¤ì´ê°ê³ 89 짤짤ì´ë 4ìì´ ì긴ë¤(25ì-21ì)90 í¬í¨ëuTxOsë ì¶íì ê³µì©ì¥ë¶ìì ë ë ¤ë²ë¦´ê²ì´ê³ 91 짤짤ì´ë ì¶íì ëìê² 4ìì ë³´ë´ë ììíì´ ëì´ ê³µì©ì¥ë¶ì ë¤ì´ê°ê²ì´ë¤ */92 // ì§ë¶í ê¸ì¡ì´ 모ìë¼ë©´ ë©ìì§ ì¶ë ¥93 const eMsg = "í¸ëìì
ì ë§ë¤ ì ìì´ì. uTxOê° ëª¨ìë¼ëê° ê¸ì¡ì´ 모ìë¼ì";94 throw Error(eMsg);95};96// í¸ëìì
ì ë¤ì´ê° ììíë¤ ë§ë¤ê¸°97const createTxOuts = (receiverAddress, myAddress, amount, leftOverAmount) => {98 // ë°ëì´(receiverAddress)ìê² ì¼ë§(amount) ì£¼ê² ë¤ê³ ììí ìì±99 const txOut1 = new TX.TxOut(receiverAddress, amount);100 // ì£¼ê³ ê±°ì¬ë¬ ë°ì ì½ì¸ì´ ìì¼ë©´ í´ë¹ ììí ê·¸ëë¡ ë°í101 if (leftOverAmount === 0) {102 return [txOut1];103 } else {104 // ê±°ì¬ë¬ ë°ìì¼ í ì½ì¸ì´ ìì¼ë©´105 // ë´ ì§ê°ì£¼ìë¡ ê±°ì¬ë¬ ë°ì ê¸ì¡ì´ ì í ììíë ê°ì´ ë°í106 const leftOverTx = new TX.TxOut(myAddress, leftOverAmount);107 return [txOut1, leftOverTx];108 }109};110// ë´ ëª
ìì uTxO를 íì ìë txì ë¹êµíì¬ ì¬ì©ë ê² íí°ë§ í´ì£¼ê¸°111// (í¸ëìì
ì¼ë¡ ì´ë¯¸ ë§ë¤ì´ì ¸ íì ë¤ì´ê° ìë ë´ ëª
ìì uTxOë)112// (ë¸ë¡ì´ ì±êµ´ë ëê¹ì§ ì ì°ëì§ ììì¼ë¯ë¡ ë íì ë¤ì´ê° ì ìë¤)113const filterTxPoolTxs = (unspentTxOuts, transactionPool) => {114 // íì ì¸íë¤ì ê°ì²´(txOutId, txOutIndex, signature)를 ëì§ì´ë´ì ë°°ì´ë¡ ì ì¥115 const txIns = _(transactionPool)116 .map((tx) => tx.txIns)117 .flatten() // ë°°ì´ ìì ë°°ì´ì 1ê¹ì´? 1ìì¤? ë§í¼ íì´ì£¼ë ë
ì118 .value(); // .flatten()ì ê²°ê³¼ë ê°ì²´ì .value()ë ê·¸ ê°ì²´ì ê°ì ì¶ì¶íëë
ì119 // .flatten().value() -> [ [[a],[b]],[c] ] -> [[a,b],c]120 // ì ê±°í uTxO(removable)121 const removable = [];122 // ë´ ëª
ìì uTxO ê°¯ìë§í¼ ë°ë³µí ê±°ì123 for (const unspentTxOut of unspentTxOuts) {124 // íìì ëì§ì´ë¸ ì¸íë¤[]ìì í´ë¹uTxOì ì¼ì¹íëê²ì ì°¾ìì txInì ë´ê¸°125 const txIn = _.find(txIns, (aTxIn) => {126 return (127 aTxIn.txOutIndex === unspentTxOut.txOutIndex &&128 aTxIn.txOutId === unspentTxOut.txOutId129 );130 });131 // ê·¸ txInì´ undefinedë©´(ì¼ì¹íëê² ìì¼ë©´)132 if (txIn === undefined) {133 } else {134 // í´ë¹ uTxO를 ì ê±°í uTxO(removable)ì ì¶ê°135 removable.push(unspentTxOut);136 }137 }138 // ì¥ë¶ìì ì ê±°í uTxOë¤ ì ê±°íê³ ë°í139 return _.without(unspentTxOuts, ...removable);140};141// í¸ëìì
ë§ë¤ì´ì£¼ê¸° / 매ê°ë³ìë(ë°ëì´ì£¼ì, ì½ì¸ì, ë¹ë°í¤, ê³µì©ì¥ë¶, í¸ëìì
í)142const createTransaction = (143 receiverAddress,144 amount,145 privateKey,146 unspentTxOuts,147 txPool148) => {149 // ë´ ì£¼ì = ë´ ë¹ë°í¤ë¥¼ ê°ì§ê³ ë§ë ê³µê°í¤150 const myAddress = TX.getPublicKey(privateKey);151 // ê³µì©ì¥ë¶ìì ë´ ëª
ìë¡ ë uTxOë¤ ê°ì ¸ì¤ê¸°152 const myUnspentTxOutsA = unspentTxOuts.filter(153 (uTxO) => uTxO.address === myAddress154 );155 // ë´ëª
ììuTxOë¤ ì¤ ìì§ íì ë¤ì´ê°ì§ ìì uTxOë¤ì ë³ìmyUnspentTxOutsì ì ì¥156 const myUnspentTxOuts = filterTxPoolTxs(myUnspentTxOutsA, txPool);157 // ë´uTxOs ìì ì§ë¶í ê¸ì¡ë§í¼ includedUnspentTxOuts(í¬í¨ëuTxOs)ë158 // leftOverAmount(ê±°ì¤ë¦ë) ë§ë¤ì´ë기159 const { includedUnspentTxOuts, leftOverAmount } = findTxOutsForAmount(160 amount,161 myUnspentTxOuts162 );163 // ìëª
ìë í¸ëìì
ì¸í ë§ë¤ê¸°164 const toUnsignedTxIn = (unspentTxOut) => {165 const txIn = new TX.TxIn();166 txIn.txOutId = unspentTxOut.txOutId;167 txIn.txOutIndex = unspentTxOut.txOutIndex;168 return txIn;169 };170 // í¬í¨ëuTxOsë¡ë¶í° ì¸íë¤ ë§ë¤ì´ì171 // unsignedTxIns(ìëª
ìë í¸ëìì
ì¸íë¤ ëª©ë¡ì ì ì¥)172 const unsignedTxIns = includedUnspentTxOuts.map(toUnsignedTxIn);173 const tx = new TX.Transaction(); // ë¹ í¸ëìì
ìì±174 tx.txIns = unsignedTxIns; // ì¸íë¤ì ìëª
ìë í¸ëìì
ì¸íë¤ì ë£ê³ 175 // ë°ëì´ìê² ì¼ë§ë³´ë´ëì§ì ììí,176 // (ê±°ì¤ë¦ëì´ ì기면)ê±°ì¤ë¦ëì ëìê² ë³´ë´ë ììíì ë§ë¤ì´177 // í¸ëìì
ì ììíë¤(txOuts[])ì ë´ê¸°178 tx.txOuts = createTxOuts(receiverAddress, myAddress, amount, leftOverAmount);179 // ê·¸ë¬ê³ ëì ì¸íë¤(txIns)ê³¼ ììíë¤(txOuts)ì ê°ì§ê³ idë§ë¤ì´ ë´ê¸°180 tx.id = TX.getTransactionId(tx);181 // ë´ ë¹ë°í¤ë¥¼ ê°ì§ê³ ìëª
ìì´ ë´ê²¨ìë ì¸íë¤ì ìëª
ë£ê¸°182 tx.txIns = tx.txIns.map((txIn, index) => {183 txIn.signature = TX.signTxIn(tx, index, privateKey, unspentTxOuts);184 return txIn;185 });186 return tx; // ì´ë ê² ìì±ë í¸ëìì
ë°í187};188module.exports = {189 createTransaction,190 getPublicFromWallet,191 getPrivateFromWallet,192 getBalance,193 generatePrivateKey,194 initWallet,195 deleteWallet,196 findUnspentTxOuts,...
Using AI Code Generation
1wpt.tx(function(tx) {2 tx.executeSql("CREATE TABLE IF NOT EXISTS test (id unique, data)");3 tx.executeSql("INSERT INTO test (id, data) VALUES (1, 'First row')");4 tx.executeSql("INSERT INTO test (id, data) VALUES (2, 'Second row')");5 tx.executeSql("INSERT INTO test (id, data) VALUES (3, 'Third row')");6 tx.executeSql("INSERT INTO test (id, data) VALUES (4, 'Fourth row')");7 tx.executeSql("INSERT INTO test (id, data) VALUES (5, 'Fifth row')");8 tx.executeSql("INSERT INTO test (id, data) VALUES (6, 'Sixth row')");9 tx.executeSql("INSERT INTO test (id, data) VALUES (7, 'Seventh row')");10 tx.executeSql("INSERT INTO test (id, data) VALUES (8, 'Eighth row')");11 tx.executeSql("INSERT INTO test (id, data) VALUES (9, 'Ninth row')");12 tx.executeSql("INSERT INTO test (id, data) VALUES (10, 'Tenth row')");13 tx.executeSql("INSERT INTO test (id, data) VALUES (11, 'Eleventh row')");14 tx.executeSql("INSERT INTO test (id, data) VALUES (12, 'Twelfth row')");15 tx.executeSql("INSERT INTO test (id, data) VALUES (13, 'Thirteenth row')");16 tx.executeSql("INSERT INTO test (id, data) VALUES (14, 'Fourteenth row')");17 tx.executeSql("INSERT INTO test (id, data) VALUES (15, 'Fifteenth row')");18 tx.executeSql("INSERT INTO test (id, data) VALUES (16, 'Sixteenth row')");19 tx.executeSql("INSERT INTO test (id, data) VALUES (17, 'Seventeenth row')");20 tx.executeSql("INSERT INTO test (id, data) VALUES (18, 'Eighteenth row')");21 tx.executeSql("INSERT INTO test (id, data) VALUES (19, 'Nineteenth row')");22 tx.executeSql("INSERT INTO test (id, data) VALUES (20, 'Twentieth row')");23 tx.executeSql("INSERT INTO test (id, data) VALUES (21, 'Twenty-first row')");24 tx.executeSql("INSERT INTO test (id
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org', 'A.12345678901234567890123456789012');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test submitted. Polling for results...');7 test.waitForTestRun(data.data.testId, function(err, data) {8 if (err) {9 console.log('Error: ' + err);10 } else {11 console.log('Test results: ' + data.data.summary);12 }13 });14 }15});16var wpt = require('webpagetest');17var test = new wpt('www.webpagetest.org', 'A.12345678901234567890123456789012');18 if (err) {19 console.log('Error: ' + err);20 } else {21 console.log('Test submitted. Polling for results...');22 test.waitForTestRun(data.data.testId, function(err, data) {23 if (err) {24 console.log('Error: ' + err);25 } else {26 console.log('Test results: ' + data.data.summary);27 }28 });29 }30});31var wpt = require('webpagetest');32var test = new wpt('www.webpagetest.org', 'A.12345678901234567890123456789012');33 if (err) {34 console.log('Error: ' + err);35 } else {36 console.log('Test submitted. Polling for results...');37 test.waitForTestRun(data.data.testId, function(err, data) {38 if (err) {39 console.log('Error: ' + err);40 } else {41 console.log('Test results: ' + data.data.summary);42 }43 });44 }45});46var wpt = require('webpagetest');47var test = new wpt('www.webpagetest.org', '
Using AI Code Generation
1function tx() {2 var tx = document.getElementById("tx");3 if (tx.value == "0") {4 tx.value = "1";5 } else {6 tx.value = "0";7 }8 wpt.tx(tx.value);9}10function rx() {11 var rx = document.getElementById("rx");12 if (rx.value == "0") {13 rx.value = "1";14 } else {15 rx.value = "0";16 }17 wpt.rx(rx.value);18}19function txrx() {20 var txrx = document.getElementById("txrx");21 if (txrx.value == "0") {22 txrx.value = "1";23 } else {24 txrx.value = "0";25 }26 wpt.txrx(txrx.value);27}28function txrx() {29 var txrx = document.getElementById("txrx");30 if (txrx.value == "0") {31 txrx.value = "1";32 } else {33 txrx.value = "0";34 }35 wpt.txrx(txrx.value);36}37function txrx() {38 var txrx = document.getElementById("txrx");39 if (txrx.value == "0") {40 txrx.value = "1";41 } else {42 txrx.value = "0";43 }44 wpt.txrx(txrx.value);45}46function txrx() {47 var txrx = document.getElementById("txrx");48 if (txrx.value == "0") {49 txrx.value = "1";50 } else {51 txrx.value = "0";52 }53 wpt.txrx(txrx.value);54}55function txrx() {56 var txrx = document.getElementById("txrx");57 if (txrx.value == "0") {58 txrx.value = "1";59 } else {60 txrx.value = "0";61 }62 wpt.txrx(txrx.value);63}64function txrx() {65 var txrx = document.getElementById("txrx");66 if (txrx.value == "0
Using AI Code Generation
1var webpagetest = require('webpagetest');2var wpt = new webpagetest('www.webpagetest.org', 'A.3f3b8a7b0f0b9d7a9a9c8f9b6e1e1a1e');3 if (err) return console.error(err);4 console.log('Test ID: %s', data.data.testId);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Test completed in %d seconds', data.data.average.firstView.loadTime);8 console.log('View the test at %s', data.data.userUrl);9 });10});11var wpt = new webpagetest('www.webpagetest.org', 'A.3f3b8a7b0f0b9d7a9a9c8f9b6e1e1a1e');12 if (err) return console.error(err);13 console.log('Test ID: %s', data.data.testId);14 wpt.getTestResults(data.data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log('Test completed in %d seconds', data.data.average.firstView.loadTime);17 console.log('View the test at %s', data.data.userUrl);18 });19});20var webpagetest = require('webpagetest');21var wpt = new webpagetest('www.webpagetest.org', 'A.3f3b8a7b0f0b9d7a9a9c8f9b6e1e1a1e');22 if (err) return console.error(err);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!