UNPKG

jcc-ethereum-utils

Version:

Toolkit of crossing chain from Ethereum to SWTC chain

129 lines 5.33 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = require("bignumber.js"); const erc20ABI_1 = require("./abi/erc20ABI"); const ethereum_1 = require("./ethereum"); const smartContract_1 = require("./smartContract"); const validator_1 = require("./validator"); /** * toolkit of erc20 * * @class ERC20 */ class ERC20 extends smartContract_1.default { /** * Creates an instance of ERC20 * @memberof ERC20 */ constructor() { super(); } /** * init instance of erc20 contract * * @param {string} contractAddress contract address of erc20 token * @memberof ERC20 */ init(contractAddress, ethereum) { super.init(contractAddress, ethereum, erc20ABI_1.default); } /** * destroy instance of erc20 contract * * @memberof ERC20 */ destroy() { super.destroy(); } /** * request decimals of erc20 token * * @returns {Promise<number>} * @memberof ERC20 */ decimals() { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const decimals = (yield _super.callABI.call(this, "decimals")); return parseInt(decimals, 10); }); } /** * request balance of erc20 token * * @param {string} address ethereum address * @returns {Promise<string>} resolve "0" if request failed * @memberof ERC20 */ balanceOf(address) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const bnBalance = (yield _super.callABI.call(this, "balanceOf", address)); const decimals = yield this.decimals(); const balance = new bignumber_js_1.default(bnBalance).dividedBy(Math.pow(10, decimals)).toString(10); return balance; }); } /** * transfer token to erc20 contract address * * @param {string} secret ethereum secret of sender address * @param {string} to address of destination * @param {string} amount amount * @param {string} [nonce] nonce * @returns {Promise<string>} resolve hash if success * @memberof ERC20 */ transfer(secret, to, amount, nonce) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const decimals = yield this.decimals(); const sender = ethereum_1.default.getAddress(secret); const web3 = this.ethereum.getWeb3(); const value = web3.utils.numberToHex(new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals)).toString(10)); const gasPrice = yield this.ethereum.getGasPrice(); const resolvedNonce = nonce !== undefined && new bignumber_js_1.default(nonce).isInteger() ? nonce : yield this.ethereum.getNonce(sender); const calldata = (yield _super.callABI.call(this, "transfer", to, value)); const tx = this.ethereum.getTx(sender, this.contractAddress, resolvedNonce, 90000, gasPrice, "0", calldata); const sign = yield this.ethereum.signTransaction(tx, secret); const hash = yield this.ethereum.sendSignedTransaction(sign); return hash; }); } } __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumAddress) ], ERC20.prototype, "init", null); __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumSecret), __param(1, validator_1.isValidEthereumAddress), __param(2, validator_1.isValidAmount) ], ERC20.prototype, "transfer", null); exports.default = ERC20; //# sourceMappingURL=erc20.js.map