From e39f41403d07a56ebb442414033474e7452f291d Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 16 Oct 2020 05:31:45 -0700 Subject: [PATCH] merge dtUtils --- src/utils/dtUtils.ts | 113 ++++++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 27 deletions(-) diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index cbb8621..9c8b638 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -44,33 +44,6 @@ export async function getCheapestPool( } } -export async function getFirstPool( - ocean: Ocean, - dataTokenAddress: string -): Promise { - if (!ocean || !dataTokenAddress) return null - - const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) - - if (tokenPools === undefined || tokenPools.length === 0) { - return { - address: '', - price: 0 - } - } - const firstPoolAddress = tokenPools[0] - const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') - const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress) - const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress) - - return { - address: firstPoolAddress, - price: Number(firstPoolPrice), - ocean: Number(oceanReserve), - datatoken: Number(dtReserve) - } -} - export async function getCheapestExchange( ocean: Ocean, dataTokenAddress: string @@ -113,6 +86,33 @@ export async function getCheapestExchange( } } +export async function getFirstPool( + ocean: Ocean, + dataTokenAddress: string +): Promise { + if (!ocean || !dataTokenAddress) return null + + const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) + + if (tokenPools === undefined || tokenPools.length === 0) { + return { + address: '', + price: 0 + } + } + const firstPoolAddress = tokenPools[0] + const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') + const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress) + const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress) + + return { + address: firstPoolAddress, + price: Number(firstPoolPrice), + ocean: Number(oceanReserve), + datatoken: Number(dtReserve) + } +} + export async function getBestDataTokenPrice( ocean: Ocean, dataTokenAddress: string @@ -146,3 +146,62 @@ export async function getBestDataTokenPrice( } as BestPrice } } + +export async function checkAndBuyDT( + ocean: Ocean, + dataTokenAddress: string, + account: Account, + config: Config +): Promise { + const userOwnedTokens = await ocean.accounts.getTokenBalance( + dataTokenAddress, + account + ) + Logger.log(`User has ${userOwnedTokens} tokens`) + if (userOwnedTokens === '0') { + const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress) + + switch (bestPrice?.type) { + case 'pool': { + const price = new Decimal(bestPrice.value).times(1.05).toString() + const maxPrice = new Decimal(bestPrice.value).times(2).toString() + Logger.log('Buying token from pool', bestPrice, account.getId(), price) + const buyResponse = await ocean.pool.buyDT( + account.getId(), + bestPrice.address, + '1', + price, + maxPrice + ) + Logger.log('DT buy response', buyResponse) + return buyResponse + } + case 'exchange': { + if (!config.oceanTokenAddress) { + Logger.error(`'oceanTokenAddress' not set in config`) + return + } + + if (!config.fixedRateExchangeAddress) { + Logger.error(`'fixedRateExchangeAddress' not set in config`) + return + } + + Logger.log('Buying token from exchange', bestPrice, account.getId()) + await ocean.datatokens.approve( + config.oceanTokenAddress, + config.fixedRateExchangeAddress, + bestPrice.value.toString(), + account.getId() + ) + const exchange = await ocean.fixedRateExchange.buyDT( + bestPrice.address, + '1', + account.getId() + ) + Logger.log('DT exchange buy response', exchange) + return exchange + } + } + } +}