zodor-protocol-web3
Version:
SDK to interact with zodor protocol
111 lines (110 loc) • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Erc20 = void 0;
var _ethers = require("ethers");
var _Erc2 = _interopRequireDefault(require("../abi/Erc20.json"));
var _ContractFactory = require("../factories/ContractFactory");
var _utils = require("../utils");
var _WalletFactory = require("../factories/WalletFactory");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class Erc20 {
constructor(options) {
_defineProperty(this, "contract", void 0);
_defineProperty(this, "network", void 0);
var {
network,
contractRunner,
contractAddress,
privateKey,
customRpcUrl
} = options;
if (!network) throw new Error('network is required');
if (contractRunner) {
var contractFactory = new _ContractFactory.ContractFactory();
this.contract = contractFactory.createContract(contractAddress, _Erc2.default.abi, contractRunner);
} else {
var rpcProvider = (0, _utils.getRpcProvider)(network, customRpcUrl);
var wallet;
if (privateKey) {
var walletFactory = new _WalletFactory.WalletFactory(rpcProvider);
wallet = walletFactory.createWallet(privateKey);
}
var _contractFactory = new _ContractFactory.ContractFactory();
this.contract = _contractFactory.createContract(contractAddress, _Erc2.default.abi, wallet || rpcProvider);
}
this.network = network;
}
name() {
var _this = this;
return _asyncToGenerator(function* () {
return yield _this.contract.name();
})();
}
symbol() {
var _this2 = this;
return _asyncToGenerator(function* () {
return yield _this2.contract.symbol();
})();
}
decimals() {
var _this3 = this;
return _asyncToGenerator(function* () {
var decimalValue = yield _this3.contract.decimals();
return Number(decimalValue);
})();
}
totalSupply() {
var _this4 = this;
return _asyncToGenerator(function* () {
var totalSupply = yield _this4.contract.totalSupply();
var decimals = yield _this4.decimals();
return Number((0, _ethers.formatEther)((0, _ethers.formatUnits)(totalSupply.toString(), Number(decimals))));
})();
}
balanceOf(walletAddress) {
var _this5 = this;
return _asyncToGenerator(function* () {
var balance = yield _this5.contract.balanceOf(walletAddress);
var decimals = yield _this5.decimals();
return Number((0, _ethers.formatUnits)(balance.toString(), Number(decimals)));
})();
}
allowance(walletAdress, spender) {
var _this6 = this;
return _asyncToGenerator(function* () {
return yield _this6.contract.allowance(walletAdress, spender);
})();
}
approve(spender, amount) {
var _this7 = this;
return _asyncToGenerator(function* () {
var decimals = yield _this7.decimals();
var amountInWei = (0, _ethers.formatUnits)(amount.toString(), Number(decimals));
return yield _this7.contract.approve(spender, amountInWei);
})();
}
transfer(to, amount) {
var _this8 = this;
return _asyncToGenerator(function* () {
var decimals = yield _this8.decimals();
var amountInWei = (0, _ethers.formatUnits)(amount.toString(), Number(decimals));
return yield _this8.contract.transfer(to, amountInWei);
})();
}
transferFrom(from, to, amount) {
var _this9 = this;
return _asyncToGenerator(function* () {
var decimals = yield _this9.decimals();
var amountInWei = (0, _ethers.formatUnits)(amount.toString(), Number(decimals));
return yield _this9.contract.transfer(from, to, amountInWei);
})();
}
}
exports.Erc20 = Erc20;