Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring / Portfolio Typescript Issues - Remove any types #1287

Draft
wants to merge 3 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/controllers/portfolio/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
// eslint-disable-next-line import/no-cycle
import {
AccountState,
AdditionalPortfolioGetResult,
GetOptions,
NetworkState,
PortfolioControllerState,
Expand Down Expand Up @@ -427,7 +428,7 @@ export class PortfolioController extends EventEmitter {
this.#setNetworkLoading(accountId, 'latest', 'rewards', true)
this.emitUpdate()

let res: any
let res: AdditionalPortfolioGetResult
try {
res = await this.#callRelayer(`/v2/identity/${accountId}/portfolio-additional`)
} catch (e: any) {
Expand All @@ -445,7 +446,7 @@ export class PortfolioController extends EventEmitter {
res.data.rewards.walletClaimableBalance || []
]
.flat()
.map((t: any) => ({
.map((t) => ({
...t,
symbol: t.address === '0x47Cd7E91C3CBaAF266369fe8518345fc4FC12935' ? 'xWALLET' : t.symbol,
flags: getFlags(res.data.rewards, 'rewards', t.networkId, t.address)
Expand All @@ -464,7 +465,7 @@ export class PortfolioController extends EventEmitter {
}
}

const gasTankTokens = res.data.gasTank.balance.map((t: any) => ({
const gasTankTokens = res.data.gasTank.balance.map((t) => ({
...t,
flags: getFlags(res.data, 'gasTank', t.networkId, t.address)
}))
Expand Down
14 changes: 14 additions & 0 deletions src/libs/deployless/deployless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert'
import { AbiCoder, concat, getBytes, Interface, JsonRpcProvider, Provider } from 'ethers'

import DeploylessCompiled from '../../../contracts/compiled/Deployless.json'
import { TokenError } from '../portfolio/interfaces'

// this is a magic contract that is constructed like `constructor(bytes memory contractBytecode, bytes memory data)` and returns the result from the call
// compiled from relayer:a7ea373559d8c419577ac05527bd37fbee8856ae/src/velcro-v3/contracts/Deployless.sol with solc 0.8.17
Expand Down Expand Up @@ -35,6 +36,19 @@ export enum DeploylessMode {
ProxyContract,
StateOverride
}

export type DeploylessResult = {
nfts?: bigint[]
name?: string
amount: bigint
decimals: number
symbol: string
error: TokenError
}

export interface SimulationResult extends DeploylessResult {
addr: string
}
export type CallOptions = {
mode: DeploylessMode
// Note: some RPCs don't seem to like numbers, we can use hex strings for them
Expand Down
2 changes: 1 addition & 1 deletion src/libs/portfolio/gecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function geckoResponseIdentifier(tokenAddr: string, network: Network): st
}

export function geckoRequestBatcher(queue: QueueElement[]): Request[] {
const segments: { [key: string]: any[] } = {}
const segments: { [key: string]: QueueElement[] } = {}
// eslint-disable-next-line no-restricted-syntax
for (const queueItem of queue) {
let segmentId: string = queueItem.data.baseCurrency
Expand Down
41 changes: 26 additions & 15 deletions src/libs/portfolio/getOnchainBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { Network } from '../../interfaces/network'
import { getEoaSimulationStateOverride } from '../../utils/simulationStateOverride'
import { getAccountDeployParams, isSmartAccount } from '../account/account'
import { callToTuple, toSingletonCall } from '../accountOp/accountOp'
import { Deployless, DeploylessMode, parseErr } from '../deployless/deployless'
import {
Deployless,
DeploylessMode,
DeploylessResult,
parseErr,
SimulationResult
} from '../deployless/deployless'
import { getFlags, overrideSymbol } from './helpers'
import {
CollectionResult,
Expand Down Expand Up @@ -94,18 +100,18 @@ export async function getNFTs(
deployless: Deployless,
opts: Partial<GetOptions>,
accountAddr: string,
tokenAddrs: [string, any][],
tokenAddrs: [string, { isKnown: boolean; tokens: string[]; enumerable: boolean }][],
limits: LimitsOptions
): Promise<[[TokenError, CollectionResult][], {}][]> {
const deploylessOpts = getDeploylessOpts(accountAddr, !network.rpcNoStateOverride, opts)
const mapToken = (token: any) => {
const mapToken = (token: DeploylessResult) => {
return {
name: token.name,
networkId: network.id,
symbol: token.symbol,
amount: BigInt(token.nfts.length),
amount: BigInt(token.nfts?.length ?? 0),
decimals: 1,
collectibles: [...token.nfts]
collectibles: token.nfts ? [...token.nfts] : []
} as CollectionResult
}

Expand All @@ -125,7 +131,7 @@ export async function getNFTs(
)
)[0]

return [collections.map((token: any) => [token.error, mapToken(token)]), {}]
return [collections.map((token: DeploylessResult) => [token.error, mapToken(token)]), {}]
}

const { accountOps, account } = opts.simulation
Expand Down Expand Up @@ -158,18 +164,18 @@ export async function getNFTs(
// simulation was performed if the nonce is changed
const hasSimulation = afterNonce !== beforeNonce

const simulationTokens: (CollectionResult & { addr: any })[] | null = hasSimulation
? after[0].map((simulationToken: any, tokenIndex: number) => ({
const simulationTokens: (SimulationResult & { addr: string })[] | null = hasSimulation
? after[0].map((simulationToken: SimulationResult, tokenIndex: number) => ({
...mapToken(simulationToken),
addr: deltaAddressesMapping[tokenIndex]
}))
: null

return [
before[0].map((beforeToken: any, i: number) => {
before[0].map((beforeToken: DeploylessResult, i: number) => {
const simulationToken = simulationTokens
? simulationTokens.find(
(token: any) => token.addr.toLowerCase() === tokenAddrs[i][0].toLowerCase()
(token: SimulationResult) => token.addr.toLowerCase() === tokenAddrs[i][0].toLowerCase()
)
: null

Expand Down Expand Up @@ -211,7 +217,7 @@ export async function getTokens(
accountAddr: string,
tokenAddrs: string[]
): Promise<[[TokenError, TokenResult][], MetaData][]> {
const mapToken = (token: any, address: string) => {
const mapToken = (token: DeploylessResult, address: string) => {
return {
amount: token.amount,
networkId: network.id,
Expand All @@ -233,7 +239,10 @@ export async function getTokens(
)

return [
results.map((token: any, i: number) => [token.error, mapToken(token, tokenAddrs[i])]),
results.map((token: DeploylessResult, i: number) => [
token.error,
mapToken(token, tokenAddrs[i])
]),
{
blockNumber
}
Expand Down Expand Up @@ -268,16 +277,18 @@ export async function getTokens(
const hasSimulation = afterNonce !== beforeNonce

const simulationTokens = hasSimulation
? after[0].map((simulationToken: any, tokenIndex: number) => ({
? after[0].map((simulationToken: SimulationResult, tokenIndex: number) => ({
...simulationToken,
amount: simulationToken.amount,
addr: deltaAddressesMapping[tokenIndex]
}))
: null
return [
before[0].map((token: any, i: number) => {
before[0].map((token: DeploylessResult, i: number) => {
const simulation = simulationTokens
? simulationTokens.find((simulationToken: any) => simulationToken.addr === tokenAddrs[i])
? simulationTokens.find(
(simulationToken: SimulationResult) => simulationToken.addr === tokenAddrs[i]
)
: null

// Here's the math before `simulationAmount` and `amountPostSimulation`.
Expand Down
5 changes: 3 additions & 2 deletions src/libs/portfolio/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getFlags(
const isRewardsOrGasTank = ['gasTank', 'rewards'].includes(networkId)
const onGasTank = networkId === 'gasTank'

let rewardsType = null
let rewardsType: 'wallet-vesting' | 'wallet-rewards' | null = null
if (networkData?.xWalletClaimableBalance?.address.toLowerCase() === address.toLowerCase())
rewardsType = 'wallet-rewards'
if (networkData?.walletClaimableBalance?.address.toLowerCase() === address.toLowerCase())
Expand All @@ -54,7 +54,8 @@ export function getFlags(
(isRewardsOrGasTank ? t.networkId === tokenNetwork : t.networkId === networkId)
)

const canTopUpGasTank = foundFeeToken && !foundFeeToken?.disableGasTankDeposit && !rewardsType
const canTopUpGasTank =
(foundFeeToken && !foundFeeToken?.disableGasTankDeposit && !rewardsType) || false
const isFeeToken =
address === ZeroAddress ||
// disable if not in gas tank
Expand Down
22 changes: 22 additions & 0 deletions src/libs/portfolio/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ export interface PortfolioLibGetResult {
afterNonce: bigint
}

type GasTankOrRewardsToken = Omit<TokenResult, 'simulationAmount' | 'amountPostSimulation'>
export interface AdditionalPortfolioGetResult {
data: {
rewards: {
supplyControllerAddr: string
claimableRewardsData: ClaimableRewardsData
walletClaimableBalance: GasTankOrRewardsToken
xWalletClaimableBalance: GasTankOrRewardsToken
}
gasTank: {
availableGasTankAssets: (GasTankOrRewardsToken & {
icon: string
})[]
balance: (GasTankOrRewardsToken & {
availableAmount: string
cashback: string
saved: string
})[]
}
}
}

interface Total {
[currency: string]: number
}
Expand Down
3 changes: 2 additions & 1 deletion src/libs/portfolio/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QueueElement } from './batcher'
import {
CollectionResult,
ERC721Enumerable,
Expand All @@ -8,7 +9,7 @@ import {
} from './interfaces'

export function paginate(
input: string[] | [string, ERC721Enumerable | ERC721Innumerable][],
input: string[] | QueueElement[] | [string, ERC721Enumerable | ERC721Innumerable][],
limit: number
): any[][] {
const pages = []
Expand Down
2 changes: 1 addition & 1 deletion src/libs/portfolio/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class Portfolio {
.filter((_tokensWithErrResult: [TokenError, TokenResult]) =>
tokenFilter(_tokensWithErrResult)
)
.map(([, result]: [any, TokenResult]) => result)
.map(([, result]) => result)

const unfilteredCollections = collectionsWithErrResult.map(([error, x], i) => {
const address = collectionsHints[i][0] as unknown as string
Expand Down