UNPKG

jcc-ethereum-utils

Version:

Toolkit of crossing chain from Ethereum to SWTC chain

232 lines 10.1 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 fingateABI_1 = require("./abi/fingateABI"); const ethereum_1 = require("./ethereum"); const smartContract_1 = require("./smartContract"); const validator_1 = require("./validator"); /** * Toolkit of Erc20Fingate * * @export * @class Erc20Fingate * @extends {EthereumFingate} */ class Fingate extends smartContract_1.default { /** * Creates an instance of ERC20 * @memberof Fingate */ constructor() { super(); this._etherGasLimit = 150000; } /** * set & get _etherGasLimit * * @memberof EtherFingate */ set etherGasLimit(v) { this._etherGasLimit = v; } get etherGasLimit() { return this._etherGasLimit; } /** * init erc20 contract * * @param {string} etherContractAddress ether fingate address * @param {string} tokenContractAddress contract address of erc20 token * @memberof Erc20Fingate */ init(fingateAddress, ethereum) { super.init(fingateAddress, ethereum, fingateABI_1.default); } initErc20(erc20) { this._erc20 = erc20; } /** * destroy instance of contract * * @memberof Fingate */ destroy() { super.destroy(); } /** * check state if pending * * @param {((BigNumber | string)[])} state * @returns {boolean} return true if state is pending * @memberof Fingate */ isPending(state) { return state[0].toString(10) !== "0" || state[1] !== ""; } /** * request deposit state * * @param {string} address ethereum address * @param {string} [contractAddress="0x0000000000000000000000000000000000000000"] contract address * @returns {(Promise<(BigNumber | string)[]>)} * @memberof Fingate */ depositState(address_1) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, arguments, void 0, function* (address, contractAddress = "0x0000000000000000000000000000000000000000") { address = ethereum_1.default.prefix0x(address); const state = yield _super.callABI.call(this, "depositState", contractAddress, address); return state; }); } /** * deposit ether * * @param {string} secret ethereum secret * @param {string} jingtumAddress jingtum address * @param {string} amount deposit value * @param {string} [nonce] nonce * @returns {Promise<string>} resolve hash if success * @memberof Fingate */ deposit(secret, jingtumAddress, amount, nonce) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const address = ethereum_1.default.getAddress(secret); const calldata = (yield _super.callABI.call(this, "deposit", jingtumAddress)); const gasPrice = yield this.ethereum.getGasPrice(); const resolvedNonce = nonce !== undefined && new bignumber_js_1.default(nonce).isInteger() ? nonce : yield this.ethereum.getNonce(address); const tx = this.ethereum.getTx(address, this.contractAddress, resolvedNonce, this.etherGasLimit, gasPrice, amount, calldata); const sign = yield this.ethereum.signTransaction(tx, secret); const hash = yield this.ethereum.sendSignedTransaction(sign); return hash; }); } /** * deposit erc20 token * * @param {string} jtAddress swtc address * @param {string} tokenAddress erc20 contract address * @param {number} decimals token decimals * @param {string} amount amount of deposit * @param {string} hash generated by `transfer` api of ERC20 * @param {string} secret ethereum secret * @param {string} [nonce] nonce * @returns {Promise<string>} reslove hash of transaction if success * @memberof Fingate */ depositToken(jtAddress, tokenAddress, decimals, amount, hash, secret, nonce) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const value = this.ethereum.getWeb3().utils.numberToHex(new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals)).toString(10)); const address = ethereum_1.default.getAddress(secret); const resolvedNonce = nonce !== undefined && new bignumber_js_1.default(nonce).isInteger() ? nonce : yield this.ethereum.getNonce(address); const gasPrice = yield this.ethereum.getGasPrice(); const calldata = (yield _super.callABI.call(this, "depositToken", jtAddress, tokenAddress, value, hash)); const tx = this.ethereum.getTx(address, this.contractAddress, resolvedNonce, 450000, gasPrice, "0", calldata); const sign = yield this.ethereum.signTransaction(tx, secret); const txHash = yield this.ethereum.sendSignedTransaction(sign); return txHash; }); } /** * deposit erc20 * * @param {string} secret ethereum secret * @param {string} jtAddress jingtum address * @param {string} amount deposit value * @param {string} [nonce] nonce * @returns {Promise<string[]>} resolve hashs if success * @memberof Fingate */ depositErc20(secret, jtAddress, amount, nonce) { const _super = Object.create(null, { callABI: { get: () => super.callABI } }); return __awaiter(this, void 0, void 0, function* () { const decimals = yield this._erc20.decimals(); const web3 = this.ethereum.getWeb3(); const sender = ethereum_1.default.getAddress(secret); 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 this._erc20.callABI("transfer", this.contractAddress, value)); const tx = this.ethereum.getTx(sender, this._erc20.contractAddress, resolvedNonce, 90000, gasPrice, "0", calldata); const sign = yield this.ethereum.signTransaction(tx, secret); const hash = web3.utils.sha3(sign); const calldata1 = (yield _super.callABI.call(this, "depositToken", jtAddress, this._erc20.contractAddress, value, hash)); const tx1 = this.ethereum.getTx(sender, this.contractAddress, new bignumber_js_1.default(resolvedNonce).plus(1).toNumber(), 450000, gasPrice, "0", calldata1); const sign1 = yield this.ethereum.signTransaction(tx1, secret); const batch = new web3.BatchRequest(); batch.add({ jsonrpc: "2.0", id: 1, method: "eth_sendRawTransaction", params: [sign] }); batch.add({ jsonrpc: "2.0", id: 2, method: "eth_sendRawTransaction", params: [sign1] }); const receipts = yield batch.execute(); return receipts.map(({ result }) => result); }); } } __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumAddress) ], Fingate.prototype, "init", null); __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumAddress), __param(1, validator_1.isValidEthereumAddress) ], Fingate.prototype, "depositState", null); __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumSecret), __param(1, validator_1.isValidJingtumAddress), __param(2, validator_1.isValidAmount) ], Fingate.prototype, "deposit", null); __decorate([ validator_1.validate, __param(0, validator_1.isValidJingtumAddress), __param(1, validator_1.isValidEthereumAddress), __param(3, validator_1.isValidAmount), __param(4, validator_1.isValidHash), __param(5, validator_1.isValidEthereumSecret) ], Fingate.prototype, "depositToken", null); __decorate([ validator_1.validate, __param(0, validator_1.isValidEthereumSecret), __param(1, validator_1.isValidJingtumAddress), __param(2, validator_1.isValidAmount) ], Fingate.prototype, "depositErc20", null); exports.default = Fingate; //# sourceMappingURL=fingate.js.map