jcc-ethereum-utils
Version:
Toolkit of crossing chain from Ethereum to SWTC chain
198 lines • 9.28 kB
JavaScript
;
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 erc20ABI_1 = require("./abi/erc20ABI");
var ethereum_1 = require("./ethereum");
var smartContract_1 = require("./smartContract");
var validator_1 = require("./validator");
/**
* toolkit of erc20
*
* @class ERC20
*/
var ERC20 = /** @class */ (function (_super) {
__extends(ERC20, _super);
/**
* Creates an instance of ERC20
* @memberof ERC20
*/
function ERC20() {
return _super.call(this) || this;
}
/**
* init instance of erc20 contract
*
* @param {string} contractAddress contract address of erc20 token
* @memberof ERC20
*/
ERC20.prototype.init = function (contractAddress, ethereum) {
_super.prototype.init.call(this, contractAddress, ethereum, erc20ABI_1.default);
};
/**
* destroy instance of erc20 contract
*
* @memberof ERC20
*/
ERC20.prototype.destroy = function () {
_super.prototype.destroy.call(this);
};
/**
* request decimals of erc20 token
*
* @returns {Promise<number>}
* @memberof ERC20
*/
ERC20.prototype.decimals = function () {
return __awaiter(this, void 0, void 0, function () {
var decimals;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _super.prototype.callABI.call(this, "decimals")];
case 1:
decimals = _a.sent();
return [2 /*return*/, parseInt(decimals, 10)];
}
});
});
};
/**
* request balance of erc20 token
*
* @param {string} address ethereum address
* @returns {Promise<string>} resolve "0" if request failed
* @memberof ERC20
*/
ERC20.prototype.balanceOf = function (address) {
return __awaiter(this, void 0, void 0, function () {
var bnBalance, decimals, balance;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _super.prototype.callABI.call(this, "balanceOf", address)];
case 1:
bnBalance = _a.sent();
return [4 /*yield*/, this.decimals()];
case 2:
decimals = _a.sent();
balance = new bignumber_js_1.default(bnBalance).dividedBy(Math.pow(10, decimals)).toString(10);
return [2 /*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
*/
ERC20.prototype.transfer = function (secret, to, amount, nonce) {
return __awaiter(this, void 0, void 0, function () {
var decimals, sender, value, gasPrice, _a, calldata, tx, sign, hash;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.decimals()];
case 1:
decimals = _b.sent();
sender = ethereum_1.default.getAddress(secret);
value = this.ethereum.getWeb3().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*/, _super.prototype.callABI.call(this, "transfer", to, value)];
case 6:
calldata = _b.sent();
tx = this.ethereum.getTx(sender, this.contractAddress, nonce, 90000, gasPrice, "0", calldata);
return [4 /*yield*/, this.ethereum.signTransaction(tx, secret)];
case 7:
sign = _b.sent();
return [4 /*yield*/, this.ethereum.sendSignedTransaction(sign)];
case 8:
hash = _b.sent();
return [2 /*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);
return ERC20;
}(smartContract_1.default));
exports.default = ERC20;
//# sourceMappingURL=erc20.js.map