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: add simulation/order flow for Armada keepers #494

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IArmadaPoolId } from './IArmadaPoolId'
import { IArmadaPoolInfo } from './IArmadaPoolInfo'
import { IArmadaPosition } from './IArmadaPosition'
import { IArmadaPositionId } from './IArmadaPositionId'
import { IRebalanceData } from './IRebalanceData'
import { IArmadaRebalanceData } from './IArmadaRebalanceData'

/**
* @name IArmadaManager
Expand Down Expand Up @@ -132,7 +132,7 @@ export interface IArmadaManager {
*/
rebalance(params: {
poolId: IArmadaPoolId
rebalanceData: IRebalanceData[]
rebalanceData: IArmadaRebalanceData[]
}): Promise<TransactionInfo>

/**
Expand All @@ -145,7 +145,7 @@ export interface IArmadaManager {
*/
adjustBuffer(params: {
poolId: IArmadaPoolId
rebalanceData: IRebalanceData[]
rebalanceData: IArmadaRebalanceData[]
}): Promise<TransactionInfo>

/** GOVERNANCE TRANSACTIONS */
Expand Down Expand Up @@ -300,7 +300,7 @@ export interface IArmadaManager {
*/
forceRebalance(params: {
poolId: IArmadaPoolId
rebalanceData: IRebalanceData[]
rebalanceData: IArmadaRebalanceData[]
}): Promise<TransactionInfo>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { z } from 'zod'
export const __signature__: unique symbol = Symbol()

/**
* @name IRebalanceData
* @name IArmadaRebalanceData
* @description Data structure for rebalancing assets, used by Keepers of a fleet
*/
export interface IRebalanceData {
export interface IArmadaRebalanceData {
/** Signature to differentiate from similar interfaces */
readonly [__signature__]: symbol
/** Ark where the tokens are taken from */
Expand All @@ -27,26 +27,28 @@ export interface IRebalanceData {
}
robercano marked this conversation as resolved.
Show resolved Hide resolved

/**
* @description Zod schema for IRebalanceData
* @description Zod schema for IArmadaRebalanceData
*/
export const RebalanceDataSchema = z.object({
export const ArmadaRebalanceDataSchema = z.object({
fromArk: AddressDataSchema,
toArk: AddressDataSchema,
amount: TokenAmountDataSchema,
})
robercano marked this conversation as resolved.
Show resolved Hide resolved

/**
* Type definition for the RebalanceData data
* Type definition for the ArmadaRebalanceData data
*/
export type IRebalanceDataData = Readonly<z.infer<typeof RebalanceDataSchema>>
export type IArmadaRebalanceDataData = Readonly<z.infer<typeof ArmadaRebalanceDataSchema>>

/**
* @description Type guard for IRebalanceData
* @description Type guard for IArmadaRebalanceData
* @param maybeRebalanceData
* @returns true if the object is an IRebalanceData
* @returns true if the object is an IArmadaRebalanceData
*/
export function isRebalanceData(maybeRebalanceData: unknown): maybeRebalanceData is IRebalanceData {
const zodReturn = RebalanceDataSchema.safeParse(maybeRebalanceData)
export function isArmadaRebalanceData(
maybeRebalanceData: unknown,
): maybeRebalanceData is IArmadaRebalanceData {
const zodReturn = ArmadaRebalanceDataSchema.safeParse(maybeRebalanceData)

return zodReturn.success
}
8 changes: 4 additions & 4 deletions sdk/armada-protocol-common/src/common/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export {
} from './IArmadaProtocol'
export type { IArmadaProtocol, IArmadaProtocolData } from './IArmadaProtocol'
export {
__signature__ as __irebalancedata__,
isRebalanceData,
type IRebalanceData,
} from './IRebalanceData'
__signature__ as __iarmadarebalancedata__,
isArmadaRebalanceData,
type IArmadaRebalanceData,
} from './IArmadaRebalanceData'
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { IUser, isUser } from '@summerfi/sdk-common'
import { z } from 'zod'
import { IArmadaPoolId, isArmadaPoolId } from '../../common/interfaces/IArmadaPoolId'
import {
IArmadaRebalanceData,
isArmadaRebalanceData,
} from '../../common/interfaces/IArmadaRebalanceData'

/**
* Unique signature to provide branded types to the interface
*/
export const __signature__: unique symbol = Symbol()

/**
* Parameters for an Armada Protocol simulation
*/
export interface IArmadaKeepersParameters extends IArmadaKeepersParametersData {
/** Signature used to differentiate it from similar interfaces */
readonly [__signature__]: symbol
/** Keeper that triggered the simulation */
readonly keeper: IUser
/** ID of the pool where the operation is taking place */
readonly poolId: IArmadaPoolId
/** Rebalance data */
readonly rebalanceData: IArmadaRebalanceData[]
}

/**
* Zod schema for the Armada parameters
*/
export const ArmadaKeepersParametersDataSchema = z.object({
keeper: z.custom<IUser>((val) => isUser(val)),
poolId: z.custom<IArmadaPoolId>((val) => isArmadaPoolId(val)),
rebalanceData: z.custom<IArmadaRebalanceData[]>((val) =>
(val as unknown[]).every((val) => isArmadaRebalanceData(val)),
),
})

/**
* Type for the data part of the IArmadaParameters interface
*/
export type IArmadaKeepersParametersData = Readonly<
z.infer<typeof ArmadaKeepersParametersDataSchema>
>

/**
* Type guard for the Armada Protocol simulation parameters
*
* @param maybeArmadaParameters Parameters to check
*
* @returns True if the parameters are valid
*/
export function isArmadaKeepersParameters(
maybeArmadaParameters: unknown,
): maybeArmadaParameters is IArmadaKeepersParameters {
return ArmadaKeepersParametersDataSchema.safeParse(maybeArmadaParameters).success
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const __signature__: unique symbol = Symbol()
/**
* Parameters for an Armada Protocol simulation
*/
export interface IArmadaParameters extends IArmadaParametersData {
export interface IArmadaUsersParameters extends IArmadaUsersParametersData {
/** Signature used to differentiate it from similar interfaces */
readonly [__signature__]: symbol
/** User that triggered the simulation */
Expand All @@ -27,17 +27,17 @@ export interface IArmadaParameters extends IArmadaParametersData {
/**
* Zod schema for the Armada parameters
*/
export const ArmadaParametersDataSchema = z.object({
user: z.custom<IUser>((val) => isUser(val)),
poolId: z.custom<IArmadaPoolId>((val) => isArmadaPoolId(val)),
export const ArmadaUsersParametersDataSchema = z.object({
user: z.custom<IUser>(isUser),
poolId: z.custom<IArmadaPoolId>(isArmadaPoolId),
operation: z.nativeEnum(ArmadaOperationType),
amount: z.custom<ITokenAmount>((val) => isTokenAmount(val)),
amount: z.custom<ITokenAmount>(isTokenAmount),
})

/**
* Type for the data part of the IArmadaParameters interface
*/
export type IArmadaParametersData = Readonly<z.infer<typeof ArmadaParametersDataSchema>>
export type IArmadaUsersParametersData = Readonly<z.infer<typeof ArmadaUsersParametersDataSchema>>

/**
* Type guard for the Armada Protocol simulation parameters
Expand All @@ -46,8 +46,8 @@ export type IArmadaParametersData = Readonly<z.infer<typeof ArmadaParametersData
*
* @returns True if the parameters are valid
*/
export function isArmadaParameters(
export function isArmadaUsersParameters(
maybeArmadaParameters: unknown,
): maybeArmadaParameters is IArmadaParameters {
return ArmadaParametersDataSchema.safeParse(maybeArmadaParameters).success
): maybeArmadaParameters is IArmadaUsersParameters {
return ArmadaUsersParametersDataSchema.safeParse(maybeArmadaParameters).success
}
18 changes: 13 additions & 5 deletions sdk/armada-protocol-common/src/orders/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export {
ArmadaParametersDataSchema,
__signature__ as __iarmadaparameters__,
isArmadaParameters,
} from './IArmadaParameters'
export type { IArmadaParameters, IArmadaParametersData } from './IArmadaParameters'
ArmadaKeepersParametersDataSchema,
__signature__ as __iarmadakeepersparameters__,
isArmadaKeepersParameters,
type IArmadaKeepersParameters,
type IArmadaKeepersParametersData,
} from './IArmadaKeepersParameters'
export {
ArmadaUsersParametersDataSchema,
__signature__ as __iarmadausersparameters__,
isArmadaUsersParameters,
type IArmadaUsersParameters,
type IArmadaUsersParametersData,
} from './IArmadaUsersParameters'
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ISimulation, SimulationSchema, SimulationType } from '@summerfi/sdk-common/simulation'
import { IUser, isUser } from '@summerfi/sdk-common/user'
import { z } from 'zod'
import { IArmadaPoolId, isArmadaPoolId } from '../../common'
import {
IArmadaRebalanceData,
isArmadaRebalanceData,
} from '../../common/interfaces/IArmadaRebalanceData'

/**
* Unique signature to provide branded types to the interface
*/
export const __signature__: unique symbol = Symbol()

/**
* @interface IArmadaKeepersSimulation
* @description Simulation result of an Armada Protocol Keepers operation
*/
export interface IArmadaKeepersSimulation extends ISimulation {
/** Signature used to differentiate it from similar interfaces */
readonly [__signature__]: symbol
/** User for which the simulation was performed */
readonly keeper: IUser
/** ID of the pool where the operation is taking place */
readonly poolId: IArmadaPoolId
/** Rebalance data */
readonly rebalanceData: IArmadaRebalanceData[]

// Re-declaring the properties to narrow the types
readonly type: SimulationType.ArmadaUsers
}

/**
* @description Zod schema for IArmadaKeepersSimulation
*/
export const ArmadaKeepersSimulationSchema = z.object({
...SimulationSchema.shape,
keeper: z.custom<IUser>((val) => isUser(val)),
poolId: z.custom<IArmadaPoolId>((val) => isArmadaPoolId(val)),
rebalanceData: z.custom<IArmadaRebalanceData[]>((val) =>
(val as unknown[]).every((val) => isArmadaRebalanceData(val)),
),
type: z.literal(SimulationType.ArmadaUsers),
})

/**
* Type for the data part of the IArmadaKeepersSimulation interface
*/
export type IArmadaKeepersSimulationData = Readonly<z.infer<typeof ArmadaKeepersSimulationSchema>>

/**
* @description Type guard for IArmadaKeepersSimulation
* @param maybeArmadaKeepersSimulationData
* @returns true if the object is an IArmadaKeepersSimulation
*/
export function isArmadaKeepersSimulation(
maybeArmadaKeepersSimulationData: unknown,
): maybeArmadaKeepersSimulationData is IArmadaKeepersSimulation {
return ArmadaKeepersSimulationSchema.safeParse(maybeArmadaKeepersSimulationData).success
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { IArmadaPosition, isArmadaPosition } from '../../common/interfaces/IArma
export const __signature__: unique symbol = Symbol()

/**
* @interface IArmadaSimulation
* @description Simulation result of an Armada Protocol operation
* @interface IArmadaUsersSimulation
* @description Simulation result of an Armada Protocol Users operation
*/
export interface IArmadaSimulation extends ISimulation {
export interface IArmadaUsersSimulation extends ISimulation {
/** Signature used to differentiate it from similar interfaces */
readonly [__signature__]: symbol
/** User for which the simulation was performed */
Expand All @@ -23,32 +23,32 @@ export interface IArmadaSimulation extends ISimulation {
readonly newPosition: IArmadaPosition

// Re-declaring the properties to narrow the types
readonly type: SimulationType.Armada
readonly type: SimulationType.ArmadaUsers
}

/**
* @description Zod schema for IArmadaSimulation
*/
export const ArmadaSimulationSchema = z.object({
export const ArmadaUsersSimulationSchema = z.object({
...SimulationSchema.shape,
user: z.custom<IUser>((val) => isUser(val)),
previousPosition: z.custom<IArmadaPosition>((val) => isArmadaPosition(val)),
newPosition: z.custom<IArmadaPosition>((val) => isArmadaPosition(val)),
type: z.literal(SimulationType.Armada),
type: z.literal(SimulationType.ArmadaUsers),
})

/**
* Type for the data part of the IArmadaSimulation interface
*/
export type IArmadaSimulationData = Readonly<z.infer<typeof ArmadaSimulationSchema>>
export type IArmadaUsersSimulationData = Readonly<z.infer<typeof ArmadaUsersSimulationSchema>>

/**
* @description Type guard for IRefinanceSimulation
* @param maybeArmadaSimulationData
* @returns true if the object is an IArmadaSimulation
* @description Type guard for IArmadaUsersSimulation
* @param maybeArmadaUsersSimulationData
* @returns true if the object is an IArmadaUsersSimulation
*/
export function isArmadaSimulation(
maybeArmadaSimulationData: unknown,
): maybeArmadaSimulationData is IArmadaSimulation {
return ArmadaSimulationSchema.safeParse(maybeArmadaSimulationData).success
export function isArmadaUsersSimulation(
maybeArmadaUsersSimulationData: unknown,
): maybeArmadaUsersSimulationData is IArmadaUsersSimulation {
return ArmadaUsersSimulationSchema.safeParse(maybeArmadaUsersSimulationData).success
}
24 changes: 15 additions & 9 deletions sdk/armada-protocol-common/src/simulator/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
export {
ArmadaKeepersSimulationSchema,
__signature__ as __iarmadakeeperssimulation__,
isArmadaKeepersSimulation,
type IArmadaKeepersSimulation,
type IArmadaKeepersSimulationData,
} from './IArmadaKeepersSimulation'
export {
ArmadaSimulatedPositionDataSchema,
__signature__ as __iarmadasimulatedposition__,
isArmadaSimulatedPosition,
} from './IArmadaSimulatedPosition'
export type {
IArmadaSimulatedPosition,
IArmadaSimulatedPositionData,
type IArmadaSimulatedPosition,
type IArmadaSimulatedPositionData,
} from './IArmadaSimulatedPosition'
export {
ArmadaSimulationSchema,
__signature__ as __iarmadasimulation__,
isArmadaSimulation,
} from './IArmadaSimulation'
export type { IArmadaSimulation, IArmadaSimulationData } from './IArmadaSimulation'
ArmadaUsersSimulationSchema,
__signature__ as __iarmadauserssimulation__,
isArmadaUsersSimulation,
type IArmadaUsersSimulation,
type IArmadaUsersSimulationData,
} from './IArmadaUsersSimulation'
Loading
Loading