UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

58 lines 3.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Evm = void 0; const ethers_1 = require("ethers"); const Utils_1 = require("../../../../../Utils/Utils"); const decimal_js_1 = __importDefault(require("decimal.js")); const CGDriver_1 = require("../../../../../CGDriver"); const CurrencyAmount_1 = require("../../CurrencyAmount"); const EvmPreparedTransaction_1 = require("./EvmPreparedTransaction"); const errors_1 = require("../../errors"); const Currency_1 = require("../../Currency"); class Evm extends Currency_1.Currency { constructor(currencyInfo, client, api, currencyProviders) { super(currencyInfo, client, currencyProviders); this.currencyInfo = currencyInfo; this.api = api; } async getAddress() { const publicKey = await (await this.currencyProviders.getPublicKeyProvider(this.currencyInfo))(); return ethers_1.ethers.computeAddress((0, Utils_1.bytesToHex)(publicKey.raw, true)); } async getBalance(address) { if (!address) address = await this.getAddress(); const balance = await (0, CGDriver_1.ConsumeFunction)(this.api, this.api.addressBalance, address); return { confirmed: new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, new decimal_js_1.default(balance.confirmed), this.client), unconfirmed: new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, new decimal_js_1.default(balance.unconfirmed), this.client) }; } async createTransfer(toAddress, amount) { return this.prepareSmartContractTransaction(toAddress, amount, null); } async prepareSmartContractTransaction(smartContractAddress, amount, data) { const privateKeyProvider = await this.currencyProviders.getPrivateKeyProvider(this.currencyInfo); return new EvmPreparedTransaction_1.EvmPreparedTransaction(this.client, this.api, this.currencyInfo, await this.getAddress(), smartContractAddress, amount, data, privateKeyProvider); } async callSmartContractRaw(smartContractAddress, data) { const result = await (0, CGDriver_1.ConsumeFunction)(this.api, this.api.callSmartContractFunction, { contract: smartContractAddress, data }); return result.result; } async amount(amountStr, unit) { try { switch (unit) { case 'wei': return new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, new decimal_js_1.default(amountStr).div('1_000_000_000_000_000_000'), this.client); default: return new CurrencyAmount_1.CurrencyAmount(this.currencyInfo, new decimal_js_1.default(amountStr), this.client); } } catch (_ex) { throw new errors_1.CannotParseAmount(amountStr); } } async networkStatus() { return await (0, CGDriver_1.ConsumeFunction)(this.api, this.api.networkStatus); } } exports.Evm = Evm; //# sourceMappingURL=Evm.js.map