Skip to content

Commit

Permalink
Fix osmosis lp connector
Browse files Browse the repository at this point in the history
  • Loading branch information
mlguys committed Apr 26, 2024
1 parent 83613ad commit b8ba322
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
40 changes: 20 additions & 20 deletions src/chains/osmosis/osmosis.controllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import {
HttpException,
import { HttpException,
LOAD_WALLET_ERROR_CODE,
LOAD_WALLET_ERROR_MESSAGE,
PRICE_FAILED_ERROR_CODE,
Expand Down Expand Up @@ -673,29 +671,31 @@ export class OsmosisController {
if (req.txHash){
const transaction = await osmosis.getTransaction(req.txHash);
const currentBlock = await osmosis.getCurrentBlockNumber();

const decoder = new TextDecoder();
//@ts-ignore cosmojs models again
var pool_id = undefined;
var position_id = undefined;
//@ts-ignore cosmojs models again
if (transaction.txResponse.logs){
//@ts-ignore cosmojs models again
transaction.txResponse.logs.forEach((log) => {
if (transaction.txResponse.events){
//@ts-ignore cosmojs models again
const create_position_event = log.events.find(({ type }) => type === 'create_position');
const create_position_event = transaction.txResponse.events.find(({ type }) => type === 'create_position');
if (create_position_event){
//@ts-ignore cosmojs models again
const pool_id_attribute = create_position_event.attributes.find(({ key }) => key === 'pool_id');
if (pool_id_attribute){
pool_id = pool_id_attribute.value
}
//@ts-ignore cosmojs models again
const position_id_attribute = create_position_event.attributes.find(({ key }) => key === 'position_id');
if (position_id_attribute){
position_id = position_id_attribute.value
}
}
})
//@ts-ignore cosmojs models again
create_position_event.attributes.forEach(attribute => {
const key = decoder.decode(attribute.key);
const value = decoder.decode(attribute.value);

if (key === 'pool_id'){
pool_id = value;
}

if (key === 'position_id'){
position_id = value;
}

console.log(`Key: ${key}, Value: ${value}`);
});
}
}
var tokenId = position_id;
if (position_id == undefined){
Expand Down
29 changes: 17 additions & 12 deletions src/chains/osmosis/osmosis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// OSMO message composer classes don't quite match up with what the RPC/Go backend actually accepts.

import { CosmosWallet, CosmosAsset, CosmosTokenValue, CosmosBase } from '../../chains/cosmos/cosmos-base';
import { OsmosisController } from './osmosis.controllers';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -1361,8 +1360,9 @@ export class Osmosis extends CosmosBase implements Cosmosish{
tokenMinAmount1 = token1_bignumber.shiftedBy(this.getExponentByBase(token1.base)).multipliedBy(100-slippage).dividedBy(100).integerValue(BigNumber.ROUND_CEIL)
}

const lowerTick = findTickForPrice(req.lowerPrice!, pool.exponentAtPriceOne, pool.tickSpacing, true) // pool.currentTick,
const upperTick = findTickForPrice(req.upperPrice!, pool.exponentAtPriceOne, pool.tickSpacing, false)
// FIXME: ticks are less than 1 exponent, need to check tick calculation
const lowerTick = findTickForPrice(req.lowerPrice!, pool.exponentAtPriceOne, pool.tickSpacing, true) + '0' // pool.currentTick,
const upperTick = findTickForPrice(req.upperPrice!, pool.exponentAtPriceOne, pool.tickSpacing, false) + '0'

var tokenMinAmount0_final = tokenMinAmount0.toString()
var tokenMinAmount1_final = tokenMinAmount1.toString()
Expand Down Expand Up @@ -2266,7 +2266,7 @@ export class Osmosis extends CosmosBase implements Cosmosish{
countTotal: false,
reverse: false,
},
poolId: final_poolId.toString()
poolId: BigInt(final_poolId)
})
clPositions = clPositionsContainer.positions
} catch (error) {
Expand All @@ -2290,7 +2290,6 @@ export class Osmosis extends CosmosBase implements Cosmosish{
owner: address,
});
const lockedCoins: Coin[] = lockedCoinsContainer.lockedCoins ? lockedCoinsContainer.lockedCoins : []

// RETURN TYPES:
// concentrated-liquidity/pool || cosmwasmpool/v1beta1/model/pool || gamm/pool-models/balancer/balancerPool || gamm/pool-models/stableswap/stableswap_pool
const poolsContainer = await this._provider.osmosis.poolmanager.v1beta1.allPools({});
Expand Down Expand Up @@ -2351,17 +2350,23 @@ export class Osmosis extends CosmosBase implements Cosmosish{
}
}
});
const clPositionPool = extendedPools.find((pl) => pl.id.toString() === clPosition.position.poolId.toString());
const exponentToken0 = this.getExponentByBase(clPosition.asset0.denom)
const exponentToken1 = this.getExponentByBase(clPosition.asset1.denom)
// @ts-ignore
const lowerPrice = tickToPrice(exponentToken0, exponentToken1, clPosition.position.lowerTick.toString(), clPositionPool.exponentAtPriceOne.toString())
// @ts-ignore
const upperPrice = tickToPrice(exponentToken0, exponentToken1, clPosition.position.upperTick.toString(), clPositionPool.exponentAtPriceOne.toString())

returnObj.token0 = clPosition.asset0.denom;
returnObj.token1 = clPosition.asset1.denom;
returnObj.amount0 = clPosition.asset0.amount;
returnObj.amount1 = clPosition.asset1.amount;
returnObj.lowerPrice = clPosition.position.lowerTick.toString();
returnObj.upperPrice = clPosition.position.upperTick.toString();
returnObj.amount0 = (parseFloat(clPosition.asset0.amount) / Math.pow(10, exponentToken0)).toString();
returnObj.amount1 = (parseFloat(clPosition.asset1.amount) / Math.pow(10, exponentToken1)).toString();
returnObj.lowerPrice = lowerPrice.toString();
returnObj.upperPrice = upperPrice.toString();
returnObj.poolShares = clPosition.position.liquidity
returnObj.unclaimedToken0 = unclaimedToken0.toString()
returnObj.unclaimedToken1 = unclaimedToken1.toString()
const clPositionPool = extendedPools.find((pl) => pl.id.toString() === clPosition.position.poolId.toString());
returnObj.unclaimedToken0 = (parseFloat(unclaimedToken0.toString()) / Math.pow(10, exponentToken0)).toString();
returnObj.unclaimedToken1 = (parseFloat(unclaimedToken1.toString()) / Math.pow(10, exponentToken1)).toString();
returnObj.fee = clPositionPool?.fees7D.toString()
}
// not returning GAMM positons here; problematic for strat and apparently this is amm-liquidity route only
Expand Down

0 comments on commit b8ba322

Please sign in to comment.