-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from mangrovedao/feat/humanPrices
Feat/human prices
- Loading branch information
Showing
18 changed files
with
315 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mangrovedao/mgv": minor | ||
--- | ||
|
||
Added general actions for balances and logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mangrovedao/mgv": patch | ||
--- | ||
|
||
Added tickSpacing to tick lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mangrovedao/mgv": minor | ||
--- | ||
|
||
Add human readable prices conversions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"formatter": { | ||
"external": { | ||
"command": "bun", | ||
"arguments": ["run", "format", "--stdin-file-path={buffer_path}"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
type Address, | ||
type Client, | ||
erc20Abi, | ||
isAddressEqual, | ||
zeroAddress, | ||
} 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 | ||
* @param logics the logics to get balances for | ||
* @param markets the markets to get balances for | ||
* @param user the user to get balances for | ||
*/ | ||
export type GetBalancesParams<TLogics extends Logic[] = Logic[]> = { | ||
logics: TLogics | ||
markets: MarketParams[] | ||
user: Address | ||
} | ||
|
||
export type GetBalancesArgs<TLogics extends Logic[] = Logic[]> = | ||
GetBalancesParams<TLogics> & | ||
Omit<MulticallParameters, 'allowFailure' | 'contracts'> | ||
|
||
type ExtendedOverlyingResponse = Prettify<OverlyingResponse & { token: Token }> | ||
|
||
export type GetBalanceResult<TLogics extends Logic[] = Logic[]> = { | ||
tokens: { | ||
token: Token | ||
balance: bigint | ||
}[] | ||
overlying: ExtendedOverlyingResponse[] | ||
logicBalances: { | ||
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 tokens = markets.reduce((acc, market) => { | ||
if (!acc.find((t) => isAddressEqual(t.address, market.base.address))) { | ||
acc.push(market.base) | ||
} | ||
if (!acc.find((t) => isAddressEqual(t.address, market.quote.address))) { | ||
acc.push(market.quote) | ||
} | ||
return acc | ||
}, [] as Token[]) | ||
|
||
const tokenBalanceCalls = tokens.map( | ||
(token) => | ||
({ | ||
address: token.address, | ||
abi: erc20Abi, | ||
functionName: 'balanceOf', | ||
args: [args.user], | ||
}) as const, | ||
) | ||
|
||
const overlyingCalls = tokens.flatMap((token) => | ||
logics.map((logic) => | ||
logic.logicOverlying.getOverlyingContractParams({ | ||
token: token.address, | ||
logic: logic.logic, | ||
name: logic.name, | ||
}), | ||
), | ||
) as ContractFunctionParameters[] | ||
|
||
const logicBalancesCalls = tokens.flatMap((token) => | ||
logics.map((logic) => | ||
logic.logicBalance.getRoutingLogicBalanceParams({ | ||
token: token.address, | ||
logic: logic.logic, | ||
name: logic.name, | ||
user: args.user, | ||
}), | ||
), | ||
) as ContractFunctionParameters[] | ||
|
||
const result = await getAction( | ||
client, | ||
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 overlying = tokens.flatMap((token, i) => | ||
logics.map((logic, j) => { | ||
const res = result[tokens.length * (i + 1) + j] | ||
const overlying: OverlyingResponse = | ||
res.status === 'success' | ||
? logic.logicOverlying.parseOverlyingContractResponse(res.result) | ||
: { | ||
type: 'erc20', | ||
overlying: zeroAddress, | ||
available: false, | ||
} | ||
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 } | ||
}), | ||
) | ||
|
||
return { | ||
tokens: tokenBalances, | ||
overlying, | ||
logicBalances, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ export { | |
blastOrbitLogic, | ||
blastZeroLendLogic, | ||
blastPacFinanceLogic, | ||
blastLogics, | ||
} from './blast.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { publicMarketActions } from './public/index.js' | ||
export { publicMarketActions, generalActions } from './public/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Client } from 'viem' | ||
import { | ||
type GetBalanceResult, | ||
type GetBalancesArgs, | ||
getBalances, | ||
} from '../../actions/balances.js' | ||
import type { Logic } from '../../addresses/logics/utils.js' | ||
|
||
export type GeneralActions = { | ||
/** | ||
* | ||
* @param args.markets the markets to get balances for | ||
* @param args.logics the logics to get balances for | ||
* @param args.user the user to get balances for | ||
* @returns balances of user for each token in markets, overlying tokens, and logic balances | ||
*/ | ||
getBalances: <TLogics extends Logic[] = Logic[]>( | ||
args: GetBalancesArgs<TLogics>, | ||
) => Promise<GetBalanceResult<TLogics>> | ||
} | ||
|
||
export const generalActions = (client: Client): GeneralActions => ({ | ||
getBalances: (args) => getBalances(client, args), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { publicMarketActions } from './market-actions.js' | ||
export { generalActions } from './general-actions.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.