Skip to content

Commit

Permalink
feat: uniswap test
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsains committed Oct 5, 2023
1 parent 0cca68f commit e90f552
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 51 deletions.
87 changes: 43 additions & 44 deletions src/adapters/uniswap-v3/products/pool/uniswapV3PoolAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,30 @@ import {
import { getThinTokenMetadata } from '../../../../core/utils/getTokenMetadata'
import { filterMap } from '../../../../core/utils/filters'

const EXAMPLE_USER_WITH_POSITION = '0x30cb2c51fc4f031fa5f326d334e1f5da00e19ab5'

const deadline = Math.floor(Date.now() - 1000) + 60 * 10

const positionManagerCommonAddress = '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
const positionManagerCommonAddress =
'0xC36442b4a4522E871399CD717aBDD847Ab11FE88'

const contractAddresses : Partial<Record<Chain, {positionManager: string}>> = {
const contractAddresses: Partial<Record<Chain, { positionManager: string }>> = {
[Chain.Ethereum]: {
positionManager: positionManagerCommonAddress
positionManager: positionManagerCommonAddress,
},
[Chain.Arbitrum]: {
positionManager: positionManagerCommonAddress
positionManager: positionManagerCommonAddress,
},
[Chain.Optimism]: {
positionManager: positionManagerCommonAddress
positionManager: positionManagerCommonAddress,
},
[Chain.Polygon]: {
positionManager: positionManagerCommonAddress
positionManager: positionManagerCommonAddress,
},
[Chain.Bsc]: {
positionManager: '0x7b8A01B39D58278b5DE7e48c8449c9f4F5170613'
positionManager: '0x7b8A01B39D58278b5DE7e48c8449c9f4F5170613',
},
[Chain.Base]: {
positionManager: '0x03a520b32C04BF3bEEf7BEb72E919cf822Ed34f1'
positionManager: '0x03a520b32C04BF3bEEf7BEb72E919cf822Ed34f1',
},
}

Expand All @@ -87,7 +88,8 @@ export class UniswapV3PoolAdapter
name: 'UniswapV3',
description: 'UniswapV3 defi adapter',
siteUrl: 'https://uniswap.org/',
iconUrl: 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984/logo.png',
iconUrl:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984/logo.png',
positionType: PositionType.Supply,
chainId: this.chainId,
}
Expand Down Expand Up @@ -159,8 +161,6 @@ export class UniswapV3PoolAdapter

if (nonZeroPositions.length == 0) return []



const tokenBalancesPromises = nonZeroPositions.map(
({ liquidity, tokenId }) =>
positionsManagerContract.decreaseLiquidity.staticCallResult(
Expand All @@ -172,25 +172,30 @@ export class UniswapV3PoolAdapter
deadline,
},
{ from: userAddress, blockTag: blockNumber },
),
) as unknown as Promise<
[bigint, bigint] & { amount0: bigint; amount1: bigint }
>, // IMO typechain has an bug here, incorrect type being returned
)

const claimableFeesPromises = nonZeroPositions.map(({ tokenId }) =>
positionsManagerContract.collect.staticCallResult(
{
tokenId: tokenId as bigint,
recipient: userAddress,
amount0Max: maxUint128,
amount1Max: maxUint128,
},
{ from: userAddress, blockTag: blockNumber },
),
const claimableFeesPromises = nonZeroPositions.map(
({ tokenId }) =>
positionsManagerContract.collect.staticCallResult(
{
tokenId: tokenId as bigint,
recipient: userAddress,
amount0Max: maxUint128,
amount1Max: maxUint128,
},
{ from: userAddress, blockTag: blockNumber },
) as unknown as Promise<
[bigint, bigint] & { amount0: bigint; amount1: bigint }
>, // IMO typechain has an bug here, incorrect type being returned
)

const erc20MetadataPromises = nonZeroPositions.map(({ token0, token1 }) =>
Promise.all([
getThinTokenMetadata(token0, 1),
getThinTokenMetadata(token1, 1),
getThinTokenMetadata(token0, this.chainId),
getThinTokenMetadata(token1, this.chainId),
]),
)

Expand All @@ -201,25 +206,19 @@ export class UniswapV3PoolAdapter
])

return nonZeroPositions.map((pos, index) => {


const token0 = 0
const token1 = 1
const token0RawBalance = tokenBalances[index]!.amount0

const token1RawBalance = tokenBalances[index]!.amount1

const token0RawBalance = tokenBalances[index]?.[token0]
// @ts-ignore // Type issue - ethers or typechain has an issue imo here
const token1RawBalance = tokenBalances[index]?.[token1]
const token0FeeRawBalance = claimableFees[index]!.amount0

const token0FeeRawBalance = claimableFees[index]?.[token0]
// @ts-ignore // Type issue - ethers or typechain has an issue imo here
const token1FeeRawBalance = claimableFees[index]?.[token1]
const token1FeeRawBalance = claimableFees[index]!.amount1

const nftName = `${erc20Metadata[index]?.[token0]
?.symbol} / ${erc20Metadata[index]?.[token1]?.symbol} - ${formatUnits(
pos.fee,
4,
)}%`
const token0Index = 0
const token1Index = 1
const nftName = `${erc20Metadata[index]![token0Index].symbol} / ${
erc20Metadata[index]![token1Index].symbol
} - ${formatUnits(pos.fee, 4)}%`
return {
address: positionManagerCommonAddress,
name: nftName,
Expand All @@ -231,25 +230,25 @@ export class UniswapV3PoolAdapter
tokens: [
this.createUnderlyingToken(
pos.token0,
erc20Metadata[index]?.[token0],
erc20Metadata[index]?.[token0Index],
token0RawBalance,
TokenType.Underlying,
),
this.createUnderlyingToken(
pos.token0,
erc20Metadata[index]?.[token0],
erc20Metadata[index]?.[token0Index],
token0FeeRawBalance,
TokenType.UnderlyingClaimableFee,
),
this.createUnderlyingToken(
pos.token1,
erc20Metadata[index]?.[token1],
erc20Metadata[index]?.[token1Index],
token1RawBalance,
TokenType.Underlying,
),
this.createUnderlyingToken(
pos.token1,
erc20Metadata[index]?.[token1],
erc20Metadata[index]?.[token1Index],
token1FeeRawBalance,
TokenType.UnderlyingClaimableFee,
),
Expand Down
119 changes: 119 additions & 0 deletions src/adapters/uniswap-v3/tests/snapshots/ethereum.positions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"blockNumber": 18283951,
"snapshot": [
{
"protocolId": "uniswap-v3",
"name": "UniswapV3",
"description": "UniswapV3 defi adapter",
"siteUrl": "https://uniswap.org/",
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984/logo.png",
"positionType": "supply",
"chainId": 1,
"success": true,
"tokens": [
{
"address": "0xC36442b4a4522E871399CD717aBDD847Ab11FE88",
"name": "WAGMI / WETH - 1.0%",
"symbol": "WAGMI / WETH - 1.0%",
"decimals": 18,
"balanceRaw": "26541672142114225144722n",
"balance": "-1",
"type": "protocol",
"tokens": [
{
"address": "0x92CC36D66e9d739D50673d1f27929a371FB83a67",
"iconUrl": "",
"balance": "-1",
"name": "Wagmi",
"symbol": "WAGMI",
"decimals": 18,
"balanceRaw": "137879720104358891618656n",
"type": "underlying"
},
{
"address": "0x92CC36D66e9d739D50673d1f27929a371FB83a67",
"iconUrl": "",
"balance": "-1",
"name": "Wagmi",
"symbol": "WAGMI",
"decimals": 18,
"balanceRaw": "6257767925451742748908n",
"type": "underlying-claimable-fee"
},
{
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"iconUrl": "",
"balance": "-1",
"name": "Wrapped Ether",
"symbol": "WETH",
"decimals": 18,
"balanceRaw": "0n",
"type": "underlying"
},
{
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"iconUrl": "",
"balance": "-1",
"name": "Wrapped Ether",
"symbol": "WETH",
"decimals": 18,
"balanceRaw": "92742886944754249n",
"type": "underlying-claimable-fee"
}
]
},
{
"address": "0xC36442b4a4522E871399CD717aBDD847Ab11FE88",
"name": "ARB / WETH - 1.0%",
"symbol": "ARB / WETH - 1.0%",
"decimals": 18,
"balanceRaw": "17521394682890000402966n",
"balance": "-1",
"type": "protocol",
"tokens": [
{
"address": "0xB50721BCf8d664c30412Cfbc6cf7a15145234ad1",
"iconUrl": "",
"balance": "-1",
"name": "Arbitrum",
"symbol": "ARB",
"decimals": 18,
"balanceRaw": "21577833278514999999999n",
"type": "underlying"
},
{
"address": "0xB50721BCf8d664c30412Cfbc6cf7a15145234ad1",
"iconUrl": "",
"balance": "-1",
"name": "Arbitrum",
"symbol": "ARB",
"decimals": 18,
"balanceRaw": "0n",
"type": "underlying-claimable-fee"
},
{
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"iconUrl": "",
"balance": "-1",
"name": "Wrapped Ether",
"symbol": "WETH",
"decimals": 18,
"balanceRaw": "0n",
"type": "underlying"
},
{
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"iconUrl": "",
"balance": "-1",
"name": "Wrapped Ether",
"symbol": "WETH",
"decimals": 18,
"balanceRaw": "0n",
"type": "underlying-claimable-fee"
}
]
}
]
}
]
}
14 changes: 7 additions & 7 deletions src/adapters/uniswap-v3/tests/testCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { TimePeriod } from '../../../core/constants/timePeriod'
import type { TestCase } from '../../../types/testCase'

export const testCases: TestCase[] = [
// {
// chainId: Chain.Ethereum,
// method: 'positions',
// input: {
// userAddress: '0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255',
// },
// },
{
chainId: Chain.Ethereum,
method: 'positions',
input: {
userAddress: '0x30cb2c51fc4f031fa5f326d334e1f5da00e19ab5',
},
},
// {
// chainId: Chain.Ethereum,
// method: 'profits',
Expand Down
10 changes: 10 additions & 0 deletions src/types/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ export interface TokenBalance extends Erc20Metadata {
balance: string
}

/**
* Underlying token balances of a position
* The underlying token may be a simple erc20 such as Dai.
* Should the underlying token be another protocol token then we expect that to be resolved down into the underlying simple erc20 tokens
*/
export interface Underlying extends TokenBalance {
type: typeof TokenType.Underlying | typeof TokenType.UnderlyingClaimableFee
iconUrl: string
tokens?: Underlying[]
}
/**
* Underlying token balances of a position
* The underlying token may be a simple erc20 such as Dai.
Expand Down

0 comments on commit e90f552

Please sign in to comment.