UNPKG

@augustdigital/pools

Version:

JS SDK for web3 interactions with the August Digital Lending Pools

191 lines 8.15 kB
"use strict"; 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 }); exports.allowance = allowance; exports.approve = approve; exports.deposit = deposit; exports.requestRedeem = requestRedeem; exports.redeem = redeem; const utils_1 = require("@augustdigital/utils"); const abis_1 = require("@augustdigital/abis"); const abis_2 = require("@augustdigital/abis"); function allowance(signer, options) { return __awaiter(this, void 0, void 0, function* () { const { wallet, pool } = options; const [goodWallet, goodPool] = [ (0, utils_1.checkAddress)(wallet, console, 'wallet'), (0, utils_1.checkAddress)(pool, console), ]; if (!goodWallet || !goodPool) return; try { const poolContract = (0, utils_1.createContract)({ address: pool, abi: abis_2.ABI_LENDING_POOLS, provider: signer, }); const [asset, decimals] = yield Promise.all([ poolContract.asset(), poolContract.decimals(), ]); const depositTokenContract = (0, utils_1.createContract)({ address: asset, abi: abis_1.ABI_ERC20, provider: signer, }); const _allowance = (yield depositTokenContract.allowance(wallet, pool)); const normalized = (0, utils_1.toNormalizedBn)(_allowance, decimals); console.log('#allowance:', normalized); return normalized; } catch (e) { console.error('#allowance', e); } }); } function approve(signer, options) { return __awaiter(this, void 0, void 0, function* () { const { wallet, pool, wait, amount } = options; const [goodWallet, goodPool] = [ (0, utils_1.checkAddress)(wallet, console, 'wallet'), (0, utils_1.checkAddress)(pool, console), ]; if (!goodWallet || !goodPool || !amount) return; const _allowance = yield allowance(signer, { pool, wallet }); if (!_allowance || (_allowance === null || _allowance === void 0 ? void 0 : _allowance.raw) === '0') { try { const poolContract = (0, utils_1.createContract)({ address: pool, provider: signer, abi: abis_2.ABI_LENDING_POOLS, }); const [asset, decimals] = yield Promise.all([ poolContract.asset(), poolContract.decimals(), ]); const normalizedAmt = (0, utils_1.toNormalizedBn)(amount, decimals); const depositTokenContract = (0, utils_1.createContract)({ address: asset, abi: abis_1.ABI_ERC20, provider: signer, }); const approvalTx = yield depositTokenContract .connect(signer) .approve(pool, normalizedAmt.raw); if (wait) yield approvalTx.wait(); console.log('#approve::tx_hash:', approvalTx === null || approvalTx === void 0 ? void 0 : approvalTx.hash); return approvalTx === null || approvalTx === void 0 ? void 0 : approvalTx.hash; } catch (e) { console.error('#approve:', e); } } else { console.log('#approve::allowance:', _allowance.normalized, '\n#approve::amount:', amount); } }); } function deposit(signer, options) { return __awaiter(this, void 0, void 0, function* () { const { wallet, pool, wait, amount } = options; const [goodWallet, goodPool] = [ (0, utils_1.checkAddress)(wallet, console, 'wallet'), (0, utils_1.checkAddress)(pool, console), ]; if (!goodWallet || !goodPool) return; yield approve(signer, { pool, wallet, amount, wait: true }); try { const poolContract = (0, utils_1.createContract)({ address: pool, provider: signer, abi: abis_2.ABI_LENDING_POOLS, }); const [decimals] = yield Promise.all([ poolContract.decimals(), ]); const normalizedAmt = (0, utils_1.toNormalizedBn)(amount, decimals); const depositTx = yield poolContract .connect(signer) .deposit(normalizedAmt.raw, wallet); if (wait) yield depositTx.wait(); console.log('#deposit::tx_hash:', depositTx === null || depositTx === void 0 ? void 0 : depositTx.hash); return depositTx === null || depositTx === void 0 ? void 0 : depositTx.hash; } catch (e) { console.error('#deposit:', e); } }); } function requestRedeem(signer, options) { return __awaiter(this, void 0, void 0, function* () { const { wallet, pool, wait, amount } = options; const [goodWallet, goodPool] = [ (0, utils_1.checkAddress)(wallet, console, 'wallet'), (0, utils_1.checkAddress)(pool, console), ]; if (!goodWallet || !goodPool) return; try { const poolContract = (0, utils_1.createContract)({ address: pool, provider: signer, abi: abis_2.ABI_LENDING_POOLS, }); const [decimals] = yield Promise.all([ poolContract.decimals(), ]); const normalizedAmt = (0, utils_1.toNormalizedBn)(amount, decimals); const requestRedeemTx = yield poolContract .connect(signer) .requestRedeem(normalizedAmt.raw, wallet, wallet); if (wait) yield requestRedeemTx.wait(); console.log('#requestRedeem::tx_hash:', requestRedeemTx === null || requestRedeemTx === void 0 ? void 0 : requestRedeemTx.hash); return requestRedeemTx === null || requestRedeemTx === void 0 ? void 0 : requestRedeemTx.hash; } catch (e) { console.error('#requestRedeem:', e); } }); } function redeem(signer, options) { return __awaiter(this, void 0, void 0, function* () { const { wallet, pool, wait, year, day, month, receiverIndex } = options; const [goodWallet, goodPool] = [ (0, utils_1.checkAddress)(wallet, console, 'wallet'), (0, utils_1.checkAddress)(pool, console), ]; if (!(goodWallet && goodPool && year && day && month && receiverIndex)) return; try { const poolContract = (0, utils_1.createContract)({ address: pool, provider: signer, abi: abis_2.ABI_LENDING_POOLS, }); const redeemTx = yield poolContract .connect(signer) .redeem(year, month, day, receiverIndex, wallet); if (wait) yield redeemTx.wait(); console.log('#redeem::tx_hash:', redeemTx === null || redeemTx === void 0 ? void 0 : redeemTx.hash); return redeemTx === null || redeemTx === void 0 ? void 0 : redeemTx.hash; } catch (e) { console.error('#redeem:', e); } }); } //# sourceMappingURL=user-actions.js.map