Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb committed May 5, 2024
1 parent 0ea39e9 commit 95768de
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 66 deletions.
102 changes: 51 additions & 51 deletions src/actions/balances.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
type Address,
type Client,
erc20Abi,
isAddressEqual,
zeroAddress,
type Address,
type Client,
} from "viem";
import type { Logic, OverlyingResponse } from "../addresses/logics/utils.js";
import type { MarketParams } from "../types/actions/index.js";
import type { ContractFunctionParameters, MulticallParameters } from "viem";
import { getAction } from "../utils/getAction.js";
import { multicall } from "viem/actions";
import type { Token } from "../addresses/tokens/utils.js";
import type { Prettify } from "../types/lib.js";
} from 'viem'
import type { ContractFunctionParameters, MulticallParameters } from 'viem'
import { multicall } from 'viem/actions'
import type { Logic, OverlyingResponse } from '../addresses/logics/utils.js'
import type { Token } from '../addresses/tokens/utils.js'
import type { MarketParams } from '../types/actions/index.js'
import type { Prettify } from '../types/lib.js'
import { getAction } from '../utils/getAction.js'

/**
* Get the balances for a user
Expand All @@ -20,54 +20,54 @@ import type { Prettify } from "../types/lib.js";
* @param user the user to get balances for
*/
export type GetBalancesParams<TLogics extends Logic[] = Logic[]> = {
logics: TLogics;
markets: MarketParams[];
user: Address;
};
logics: TLogics
markets: MarketParams[]
user: Address
}

export type GetBalancesArgs<TLogics extends Logic[] = Logic[]> =
GetBalancesParams<TLogics> &
Omit<MulticallParameters, "allowFailure" | "contracts">;
Omit<MulticallParameters, 'allowFailure' | 'contracts'>

type ExtendedOverlyingResponse = Prettify<OverlyingResponse & { token: Token }>;
type ExtendedOverlyingResponse = Prettify<OverlyingResponse & { token: Token }>

export type GetBalanceResult<TLogics extends Logic[] = Logic[]> = {
tokens: {
token: Token;
balance: bigint;
}[];
overlying: ExtendedOverlyingResponse[];
token: Token
balance: bigint
}[]
overlying: ExtendedOverlyingResponse[]
logicBalances: {
token: Token;
logic: TLogics[number];
balance: bigint;
}[];
};
token: Token
logic: TLogics[number]
balance: bigint
}[]
}

export async function getBalances<TLogics extends Logic[] = Logic[]>(
client: Client,
args: GetBalancesArgs<TLogics>,
): Promise<GetBalanceResult<TLogics>> {
const { logics, markets, ...multicallArgs } = args;
const { logics, markets, ...multicallArgs } = args
const tokens = markets.reduce((acc, market) => {
if (!acc.find((t) => isAddressEqual(t.address, market.base.address))) {
acc.push(market.base);
acc.push(market.base)
}
if (!acc.find((t) => isAddressEqual(t.address, market.quote.address))) {
acc.push(market.quote);
acc.push(market.quote)
}
return acc;
}, [] as Token[]);
return acc
}, [] as Token[])

const tokenBalanceCalls = tokens.map(
(token) =>
({
address: token.address,
abi: erc20Abi,
functionName: "balanceOf",
functionName: 'balanceOf',
args: [args.user],
}) as const,
);
)

const overlyingCalls = tokens.flatMap((token) =>
logics.map((logic) =>
Expand All @@ -77,7 +77,7 @@ export async function getBalances<TLogics extends Logic[] = Logic[]>(
name: logic.name,
}),
),
) as ContractFunctionParameters[];
) as ContractFunctionParameters[]

const logicBalancesCalls = tokens.flatMap((token) =>
logics.map((logic) =>
Expand All @@ -88,50 +88,50 @@ export async function getBalances<TLogics extends Logic[] = Logic[]>(
user: args.user,
}),
),
) as ContractFunctionParameters[];
) as ContractFunctionParameters[]

const result = await getAction(
client,
multicall,
"multicall",
'multicall',
)({
...multicallArgs,
contracts: [...tokenBalanceCalls, ...overlyingCalls, ...logicBalancesCalls],
allowFailure: true,
});
})

const tokenBalances = tokens.map((token, i) => {
const res = result[i];
const balance = res.status === "success" ? (res.result as bigint) : 0n;
return { token, balance };
});
const res = result[i]
const balance = res.status === 'success' ? (res.result as bigint) : 0n
return { token, balance }
})

const overlying = tokens.flatMap((token, i) =>
logics.map((logic, j) => {
const res = result[tokens.length * (i + 1) + j];
const res = result[tokens.length * (i + 1) + j]
const overlying: OverlyingResponse =
res.status === "success"
res.status === 'success'
? logic.logicOverlying.parseOverlyingContractResponse(res.result)
: {
type: "erc20",
type: 'erc20',
overlying: zeroAddress,
available: false,
};
return { token, ...overlying };
}
return { token, ...overlying }
}),
);
)

const logicBalances = tokens.flatMap((token, i) =>
logics.map((logic, j) => {
const res = result[overlying.length + tokens.length * (i + 1) + j];
const balance = res.status === "success" ? (res.result as bigint) : 0n;
return { token, logic, balance };
const res = result[overlying.length + tokens.length * (i + 1) + j]
const balance = res.status === 'success' ? (res.result as bigint) : 0n
return { token, logic, balance }
}),
);
)

return {
tokens: tokenBalances,
overlying,
logicBalances,
};
}
}
2 changes: 1 addition & 1 deletion src/addresses/logics/chains/blast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export const blastLogics = [
blastOrbitLogic,
blastZeroLendLogic,
blastPacFinanceLogic,
] as const
] as const
2 changes: 1 addition & 1 deletion src/addresses/logics/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export {
blastOrbitLogic,
blastZeroLendLogic,
blastPacFinanceLogic,
blastLogics
blastLogics,
} from './blast.js'
2 changes: 1 addition & 1 deletion src/addresses/logics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ export function buildLogic<
}
}

export type Logic = ReturnType<typeof buildLogic>
export type Logic = ReturnType<typeof buildLogic>
14 changes: 7 additions & 7 deletions src/bundle/public/general-actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Client } from "viem";
import type { Client } from 'viem'
import {
getBalances,
type GetBalanceResult,
type GetBalancesArgs,
} from "../../actions/balances.js";
import type { Logic } from "../../addresses/logics/utils.js";
getBalances,
} from '../../actions/balances.js'
import type { Logic } from '../../addresses/logics/utils.js'

export type GeneralActions = {
/**
Expand All @@ -16,9 +16,9 @@ export type GeneralActions = {
*/
getBalances: <TLogics extends Logic[] = Logic[]>(
args: GetBalancesArgs<TLogics>,
) => Promise<GetBalanceResult<TLogics>>;
};
) => Promise<GetBalanceResult<TLogics>>
}

export const generalActions = (client: Client): GeneralActions => ({
getBalances: (args) => getBalances(client, args),
});
})
4 changes: 2 additions & 2 deletions src/bundle/public/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { publicMarketActions } from "./market-actions.js";
export { generalActions } from "./general-actions.js";
export { publicMarketActions } from './market-actions.js'
export { generalActions } from './general-actions.js'
13 changes: 10 additions & 3 deletions src/lib/human-readable.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { formatUnits } from 'viem'
import type { MarketParams } from '../types/index.js'
import type { BA } from './enums.js'
import {
inboundFromOutbound,
outboundFromInbound,
tickFromPrice,
tickFromVolumes,
} from './tick.js'
import type { MarketParams } from '../types/index.js'

// if ask, then outbound is the base, inbound is the quote
// if bid, then outbound is the quote, inbound is the base
Expand Down Expand Up @@ -107,8 +107,15 @@ export function amounts(
// quote is inbound, base is outbound
const tick =
'humanPrice' in params
? tickFromPrice(humanPriceToRawPrice(params.humanPrice, market), market.tickSpacing)
: tickFromVolumes(params.quoteAmount, params.baseAmount, market.tickSpacing)
? tickFromPrice(
humanPriceToRawPrice(params.humanPrice, market),
market.tickSpacing,
)
: tickFromVolumes(
params.quoteAmount,
params.baseAmount,
market.tickSpacing,
)
const baseAmount =
'baseAmount' in params
? params.baseAmount
Expand Down

0 comments on commit 95768de

Please sign in to comment.