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

feat: logics #19

Merged
merged 2 commits into from
May 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/perfect-pigs-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@mangrovedao/mgv": minor
---

Added routing logics
Added midPrice and spread calculation to getBook
43 changes: 31 additions & 12 deletions src/actions/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,40 @@ export async function getBook(
allowFailure: false,
})

const asks = parseBookResult({
result: rpcAsks,
ba: BA.asks,
baseDecimals: base.decimals,
quoteDecimals: quote.decimals,
})

const bids = parseBookResult({
result: rpcBids,
ba: BA.bids,
baseDecimals: base.decimals,
quoteDecimals: quote.decimals,
})

const midPrice = !asks[0]?.price
? !bids[0]?.price
? 0
: bids[0].price
: (asks[0].price + bids[0].price) / 2

const spread =
!asks[0]?.price || !bids[0]?.price ? 0 : asks[0].price - bids[0].price

const spreadPercent =
!asks[0]?.price || !bids[0]?.price ? 0 : spread / midPrice

return {
asks: parseBookResult({
result: rpcAsks,
ba: BA.asks,
baseDecimals: base.decimals,
quoteDecimals: quote.decimals,
}),
bids: parseBookResult({
result: rpcBids,
ba: BA.bids,
baseDecimals: base.decimals,
quoteDecimals: quote.decimals,
}),
asks,
bids,
asksConfig: unpackLocalConfig(rpcAsksConfig),
bidsConfig: unpackLocalConfig(rpcBaseConfig),
marketConfig: unpackGlobalConfig(rpcMarketConfig),
midPrice,
spread,
spreadPercent,
}
}
23 changes: 23 additions & 0 deletions src/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,26 @@ export {
// --- mangrove ---

export { blastMangrove } from './mangrove/index.js'

// --- logics ---

export type {
OverlyingParams,
OverlyingResponse,
RoutingLogicOverlying,
LogicBalanceParams,
LogicBalanceResponse,
RoutingLogicBalance,
} from './logics/index.js'

export {
blastOrbitLogic,
blastZeroLendLogic,
blastPacFinanceLogic,
balanceLogicABI,
baseBalance,
aaveLogicABI,
aaveBalance,
aaveOverLying,
buildLogic,
} from './logics/index.js'
23 changes: 23 additions & 0 deletions src/addresses/logics/chains/blast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { aaveBalance, aaveOverLying } from '../strategies/aave.js'
import { buildLogic } from '../utils.js'

export const blastOrbitLogic = buildLogic(
'Orbit',
'0x3870DAFB80713cad59Dd999c85b1E46314b41e9c',
aaveOverLying,
aaveBalance,
)

export const blastZeroLendLogic = buildLogic(
'ZeroLend',
'0x5126d161210654148445AdB3053e6DE2bbeaeefB',
aaveOverLying,
aaveBalance,
)

export const blastPacFinanceLogic = buildLogic(
'PacFinance',
'0x982A72Afe26C72F7bef644164942BFc1d5D025F8',
aaveOverLying,
aaveBalance,
)
5 changes: 5 additions & 0 deletions src/addresses/logics/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
blastOrbitLogic,
blastZeroLendLogic,
blastPacFinanceLogic,
} from './blast.js'
24 changes: 24 additions & 0 deletions src/addresses/logics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export {
blastOrbitLogic,
blastZeroLendLogic,
blastPacFinanceLogic,
} from './chains/index.js'

export {
balanceLogicABI,
baseBalance,
aaveLogicABI,
aaveBalance,
aaveOverLying,
} from './strategies/index.js'

export type {
OverlyingParams,
OverlyingResponse,
RoutingLogicOverlying,
LogicBalanceParams,
LogicBalanceResponse,
RoutingLogicBalance,
} from './utils.js'

export { buildLogic } from './utils.js'
31 changes: 31 additions & 0 deletions src/addresses/logics/strategies/aave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isAddressEqual, parseAbi, zeroAddress } from 'viem'
import type { RoutingLogicOverlying } from '../utils.js'
import { baseBalance } from './base.js'

export const aaveLogicABI = parseAbi([
'function overlying(address asset) public view returns (address aToken)',
])

export const aaveOverLying: RoutingLogicOverlying<
typeof aaveLogicABI,
'view',
'overlying'
> = {
getOverlyingContractParams(params) {
return {
address: params.token,
abi: aaveLogicABI,
functionName: 'overlying',
args: [params.token],
}
},
parseOverlyingContractResponse(response) {
return {
type: 'erc20',
token: response,
available: !isAddressEqual(response, zeroAddress),
}
},
}

export const aaveBalance = baseBalance
24 changes: 24 additions & 0 deletions src/addresses/logics/strategies/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parseAbi } from 'viem'
import type { RoutingLogicBalance } from '../utils.js'

export const balanceLogicABI = parseAbi([
'function balanceLogic(address token, address fundOwner) external view returns (uint balance)',
])

export const baseBalance: RoutingLogicBalance<
typeof balanceLogicABI,
'view',
'balanceLogic'
> = {
getRoutingLogicBalanceParams(params) {
return {
address: params.logic,
abi: balanceLogicABI,
functionName: 'balanceLogic',
args: [params.token, params.user],
}
},
parseRoutingLogicBalanceResponse(response) {
return response
},
}
10 changes: 10 additions & 0 deletions src/addresses/logics/strategies/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {
balanceLogicABI,
baseBalance,
} from './base.js'

export {
aaveLogicABI,
aaveOverLying,
aaveBalance,
} from './aave.js'
98 changes: 98 additions & 0 deletions src/addresses/logics/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type {
Abi,
AbiStateMutability,
Address,
ContractFunctionName,
ContractFunctionParameters,
ContractFunctionReturnType,
} from 'viem'

export type OverlyingParams = {
token: Address
logic: Address
name: string
}

export type OverlyingResponse = {
type: 'erc20' | 'erc721'
token: Address
available: boolean
}

export type RoutingLogicOverlying<
abi extends Abi | readonly unknown[] = Abi,
mutability extends AbiStateMutability = AbiStateMutability,
functionName extends ContractFunctionName<
abi,
mutability
> = ContractFunctionName<abi, mutability>,
> = {
getOverlyingContractParams: (
params: OverlyingParams,
) => ContractFunctionParameters<abi, mutability, functionName>
parseOverlyingContractResponse: (
response: ContractFunctionReturnType<abi, mutability, functionName>,
) => OverlyingResponse
}

export type LogicBalanceParams = {
token: Address
logic: Address
name: string
user: Address
}

export type LogicBalanceResponse = bigint

export type RoutingLogicBalance<
abi extends Abi | readonly unknown[] = Abi,
mutability extends AbiStateMutability = AbiStateMutability,
functionName extends ContractFunctionName<
abi,
mutability
> = ContractFunctionName<abi, mutability>,
> = {
getRoutingLogicBalanceParams: (
params: LogicBalanceParams,
) => ContractFunctionParameters<abi, mutability, functionName>
parseRoutingLogicBalanceResponse: (
response: ContractFunctionReturnType<abi, mutability, functionName>,
) => LogicBalanceResponse
}

export function buildLogic<
TName extends string = string,
TLogicAddress extends Address = Address,
abiOverlying extends Abi | readonly unknown[] = Abi,
mutabilityOverlying extends AbiStateMutability = AbiStateMutability,
functionNameOverlying extends ContractFunctionName<
abiOverlying,
mutabilityOverlying
> = ContractFunctionName<abiOverlying, mutabilityOverlying>,
abiBalance extends Abi | readonly unknown[] = Abi,
mutabilityBalance extends AbiStateMutability = AbiStateMutability,
functionNameBalance extends ContractFunctionName<
abiBalance,
mutabilityBalance
> = ContractFunctionName<abiBalance, mutabilityBalance>,
>(
name: TName,
logic: TLogicAddress,
logicOverlying: RoutingLogicOverlying<
abiOverlying,
mutabilityOverlying,
functionNameOverlying
>,
logicBalance: RoutingLogicBalance<
abiBalance,
mutabilityBalance,
functionNameBalance
>,
) {
return {
name,
logic,
logicOverlying,
logicBalance,
}
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ export { publicMarketActions } from './bundle/index.js'
export type {
Token,
BuildTokenParms,
OverlyingParams,
OverlyingResponse,
RoutingLogicOverlying,
LogicBalanceParams,
LogicBalanceResponse,
RoutingLogicBalance,
} from './addresses/index.js'
6 changes: 6 additions & 0 deletions src/types/actions/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ export type BookParams = {
* @param asksConfig the asks semibook configuration
* @param bidsConfig the bids semibook configuration
* @param marketConfig the global mangrove configuration
* @param midPrice the mid price
* @param spread the spread
* @param spreadPercent the spread percent
*/
export type Book = {
asks: CompleteOffer[]
bids: CompleteOffer[]
asksConfig: LocalConfig
bidsConfig: LocalConfig
marketConfig: GlobalConfig
midPrice: number
spread: number
spreadPercent: number
}