UNPKG

@augustdigital/vaults

Version:

JS SDK for web3 interactions with the August Digital Lending Pools

172 lines 6.06 kB
"use strict"; 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"); async function allowance(signer, options) { 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] = await Promise.all([ poolContract.asset(), poolContract.decimals(), ]); const depositTokenContract = (0, utils_1.createContract)({ address: asset, abi: abis_1.ABI_ERC20, provider: signer, }); const _allowance = (await depositTokenContract.allowance(wallet, pool)); const normalized = (0, utils_1.toNormalizedBn)(_allowance, decimals); console.log('#allowance:', normalized); return normalized; } catch (e) { console.error('#allowance', e); } } async function approve(signer, options) { 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 = await allowance(signer, { pool, wallet }); if (!_allowance || _allowance?.raw === '0') { try { const poolContract = (0, utils_1.createContract)({ address: pool, provider: signer, abi: abis_2.ABI_LENDING_POOLS, }); const [asset, decimals] = await 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 = await depositTokenContract .connect(signer) .approve(pool, normalizedAmt.raw); if (wait) await approvalTx.wait(); console.log('#approve::tx_hash:', approvalTx?.hash); return approvalTx?.hash; } catch (e) { console.error('#approve:', e); } } else { console.log('#approve::allowance:', _allowance.normalized, '\n#approve::amount:', amount); } } async function deposit(signer, options) { 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; await 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] = await Promise.all([ poolContract.decimals(), ]); const normalizedAmt = (0, utils_1.toNormalizedBn)(amount, decimals); const depositTx = await poolContract .connect(signer) .deposit(normalizedAmt.raw, wallet); if (wait) await depositTx.wait(); console.log('#deposit::tx_hash:', depositTx?.hash); return depositTx?.hash; } catch (e) { console.error('#deposit:', e); } } async function requestRedeem(signer, options) { 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] = await Promise.all([ poolContract.decimals(), ]); const normalizedAmt = (0, utils_1.toNormalizedBn)(amount, decimals); const requestRedeemTx = await poolContract .connect(signer) .requestRedeem(normalizedAmt.raw, wallet, wallet); if (wait) await requestRedeemTx.wait(); console.log('#requestRedeem::tx_hash:', requestRedeemTx?.hash); return requestRedeemTx?.hash; } catch (e) { console.error('#requestRedeem:', e); } } async function redeem(signer, options) { 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 = await poolContract .connect(signer) .redeem(year, month, day, receiverIndex, wallet); if (wait) await redeemTx.wait(); console.log('#redeem::tx_hash:', redeemTx?.hash); return redeemTx?.hash; } catch (e) { console.error('#redeem:', e); } } //# sourceMappingURL=user-actions.js.map