jcc-ethereum-utils
Version:
Toolkit of crossing chain from Ethereum to SWTC chain
329 lines • 15.9 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var bignumber_js_1 = require("bignumber.js");
var fingateABI_1 = require("./abi/fingateABI");
var promisifyBatchRequest_1 = require("./promisifyBatchRequest");
var ethereum_1 = require("./ethereum");
var smartContract_1 = require("./smartContract");
var validator_1 = require("./validator");
/**
* Toolkit of Erc20Fingate
*
* @export
* @class Erc20Fingate
* @extends {EthereumFingate}
*/
var Fingate = /** @class */ (function (_super) {
__extends(Fingate, _super);
/**
* Creates an instance of ERC20
* @memberof Fingate
*/
function Fingate() {
var _this = _super.call(this) || this;
_this._etherGasLimit = 150000;
return _this;
}
Object.defineProperty(Fingate.prototype, "etherGasLimit", {
get: function () {
return this._etherGasLimit;
},
/**
* set & get _etherGasLimit
*
* @memberof EtherFingate
*/
set: function (v) {
this._etherGasLimit = v;
},
enumerable: false,
configurable: true
});
/**
* init erc20 contract
*
* @param {string} etherContractAddress ether fingate address
* @param {string} tokenContractAddress contract address of erc20 token
* @memberof Erc20Fingate
*/
Fingate.prototype.init = function (fingateAddress, ethereum) {
_super.prototype.init.call(this, fingateAddress, ethereum, fingateABI_1.default);
};
Fingate.prototype.initErc20 = function (erc20) {
this._erc20 = erc20;
};
/**
* destroy instance of contract
*
* @memberof Fingate
*/
Fingate.prototype.destroy = function () {
_super.prototype.destroy.call(this);
};
/**
* check state if pending
*
* @param {((BigNumber | string)[])} state
* @returns {boolean} return true if state is pending
* @memberof Fingate
*/
Fingate.prototype.isPending = function (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
*/
Fingate.prototype.depositState = function (address, contractAddress) {
if (contractAddress === void 0) { contractAddress = "0x0000000000000000000000000000000000000000"; }
return __awaiter(this, void 0, void 0, function () {
var state;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
address = ethereum_1.default.prefix0x(address);
return [4 /*yield*/, _super.prototype.callABI.call(this, "depositState", contractAddress, address)];
case 1:
state = _a.sent();
return [2 /*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
*/
Fingate.prototype.deposit = function (secret, jingtumAddress, amount, nonce) {
return __awaiter(this, void 0, void 0, function () {
var address, calldata, gasPrice, _a, tx, sign, hash;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
address = ethereum_1.default.getAddress(secret);
return [4 /*yield*/, _super.prototype.callABI.call(this, "deposit", jingtumAddress)];
case 1:
calldata = _b.sent();
return [4 /*yield*/, this.ethereum.getGasPrice()];
case 2:
gasPrice = _b.sent();
if (!new bignumber_js_1.default(nonce).isInteger()) return [3 /*break*/, 3];
_a = nonce;
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, this.ethereum.getNonce(address)];
case 4:
_a = _b.sent();
_b.label = 5;
case 5:
nonce = _a;
tx = this.ethereum.getTx(address, this.contractAddress, nonce, this.etherGasLimit, gasPrice, amount, calldata);
return [4 /*yield*/, this.ethereum.signTransaction(tx, secret)];
case 6:
sign = _b.sent();
return [4 /*yield*/, this.ethereum.sendSignedTransaction(sign)];
case 7:
hash = _b.sent();
return [2 /*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
*/
Fingate.prototype.depositToken = function (jtAddress, tokenAddress, decimals, amount, hash, secret, nonce) {
return __awaiter(this, void 0, void 0, function () {
var value, address, _a, gasPrice, calldata, tx, sign, txHash;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
value = this.ethereum.getWeb3().utils.toHex(new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals)).toString(10));
address = ethereum_1.default.getAddress(secret);
if (!new bignumber_js_1.default(nonce).isInteger()) return [3 /*break*/, 1];
_a = nonce;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, this.ethereum.getNonce(address)];
case 2:
_a = _b.sent();
_b.label = 3;
case 3:
nonce = _a;
return [4 /*yield*/, this.ethereum.getGasPrice()];
case 4:
gasPrice = _b.sent();
return [4 /*yield*/, _super.prototype.callABI.call(this, "depositToken", jtAddress, tokenAddress, value, hash)];
case 5:
calldata = _b.sent();
tx = this.ethereum.getTx(address, this.contractAddress, nonce, 450000, gasPrice, "0", calldata);
return [4 /*yield*/, this.ethereum.signTransaction(tx, secret)];
case 6:
sign = _b.sent();
return [4 /*yield*/, this.ethereum.sendSignedTransaction(sign)];
case 7:
txHash = _b.sent();
return [2 /*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
*/
Fingate.prototype.depositErc20 = function (secret, jtAddress, amount, nonce) {
return __awaiter(this, void 0, void 0, function () {
var decimals, web3, sender, value, gasPrice, _a, calldata, tx, sign, hash, calldata1, tx1, sign1, batch, receipts;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this._erc20.decimals()];
case 1:
decimals = _b.sent();
web3 = this.ethereum.getWeb3();
sender = ethereum_1.default.getAddress(secret);
value = web3.utils.toHex(new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals)).toString(10));
return [4 /*yield*/, this.ethereum.getGasPrice()];
case 2:
gasPrice = _b.sent();
if (!new bignumber_js_1.default(nonce).isInteger()) return [3 /*break*/, 3];
_a = nonce;
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, this.ethereum.getNonce(sender)];
case 4:
_a = _b.sent();
_b.label = 5;
case 5:
nonce = _a;
return [4 /*yield*/, this._erc20.callABI("transfer", this.contractAddress, value)];
case 6:
calldata = _b.sent();
tx = this.ethereum.getTx(sender, this._erc20.contractAddress, nonce, 90000, gasPrice, "0", calldata);
return [4 /*yield*/, this.ethereum.signTransaction(tx, secret)];
case 7:
sign = _b.sent();
hash = web3.utils.sha3(sign);
return [4 /*yield*/, _super.prototype.callABI.call(this, "depositToken", jtAddress, this._erc20.contractAddress, value, hash)];
case 8:
calldata1 = _b.sent();
tx1 = this.ethereum.getTx(sender, this.contractAddress, nonce + 1, 450000, gasPrice, "0", calldata1);
return [4 /*yield*/, this.ethereum.signTransaction(tx1, secret)];
case 9:
sign1 = _b.sent();
batch = new promisifyBatchRequest_1.default(web3.BatchRequest);
batch.add(web3.eth.sendSignedTransaction.request, sign);
batch.add(web3.eth.sendSignedTransaction.request, sign1);
return [4 /*yield*/, batch.execute()];
case 10:
receipts = _b.sent();
return [2 /*return*/, receipts];
}
});
});
};
__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);
return Fingate;
}(smartContract_1.default));
exports.default = Fingate;
//# sourceMappingURL=fingate.js.map