Skip to content

Commit

Permalink
refactor: external calls (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
iherger authored Oct 4, 2023
1 parent d1deb18 commit bf77b84
Show file tree
Hide file tree
Showing 25 changed files with 1,258 additions and 1,300 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-tools-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@enzymefinance/sdk": patch
---

Refactor external calls
10 changes: 10 additions & 0 deletions packages/sdk/src/External.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export * as AaveV2 from "@enzymefinance/sdk/internal/External/AaveV2";
export * as BalancerV2 from "@enzymefinance/sdk/internal/External/BalancerV2";
export * as CompoundV2 from "@enzymefinance/sdk/internal/External/CompoundV2";
export * as Convex from "@enzymefinance/sdk/internal/External/Convex";
export * as Curve from "@enzymefinance/sdk/internal/External/Curve";
export * as IdleV4 from "@enzymefinance/sdk/internal/External/IdleV4";
export * as Liquity from "@enzymefinance/sdk/internal/External/AaveV2";
export * as Maple from "@enzymefinance/sdk/internal/External/Maple";
export * as TheGraph from "@enzymefinance/sdk/internal/External/TheGraph";
export * as UniswapV2 from "@enzymefinance/sdk/internal/External/UniswapV2";
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * as Assets from "@enzymefinance/sdk/Assets";
export * as Deposit from "@enzymefinance/sdk/Deposit";
export * as External from "@enzymefinance/sdk/External";
export * as Fees from "@enzymefinance/sdk/Fees";
export * as Migration from "@enzymefinance/sdk/Migration";
export * as Performance from "@enzymefinance/sdk/Performance";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Viem } from "@enzymefinance/sdk/Utils";
import * as ExternalPositionManager from "@enzymefinance/sdk/internal/ExternalPositionManager";
import { type Address, type Hex, type PublicClient, decodeAbiParameters, encodeAbiParameters } from "viem";
import { type Address, type Hex, decodeAbiParameters, encodeAbiParameters } from "viem";

export type Action = typeof Action[keyof typeof Action];
export const Action = {
Expand Down Expand Up @@ -156,58 +155,3 @@ export function repayBorrowDecode(encoded: Hex): RepayBorrowArgs {
amounts,
};
}

//--------------------------------------------------------------------------------------------
// EXTERNAL CONTRACT METHODS
//--------------------------------------------------------------------------------------------

const aaveIncentivesControllerAbi = [
{
inputs: [
{ internalType: "address[]", name: "assets", type: "address[]" },
{ internalType: "address", name: "user", type: "address" },
],
name: "getRewardsBalance",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "address", name: "_user", type: "address" }],
name: "getUserUnclaimedRewards",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
] as const;

export async function getRewardsBalance(
client: PublicClient,
args: Viem.ContractCallParameters<{
aaveIncentivesController: Address;
assets: Address[];
user: Address;
}>,
) {
return Viem.readContract(client, args, {
abi: aaveIncentivesControllerAbi,
functionName: "getRewardsBalance",
address: args.aaveIncentivesController,
args: [args.assets, args.user],
});
}

export async function getUserUnclaimedRewards(
client: PublicClient,
args: Viem.ContractCallParameters<{
aaveIncentivesController: Address;
user: Address;
}>,
) {
return Viem.readContract(client, args, {
abi: aaveIncentivesControllerAbi,
functionName: "getUserUnclaimedRewards",
address: args.aaveIncentivesController,
args: [args.user],
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Viem } from "@enzymefinance/sdk/Utils";
import * as ExternalPositionManager from "@enzymefinance/sdk/internal/ExternalPositionManager";
import { type Address, type Hex, type PublicClient, decodeAbiParameters, encodeAbiParameters } from "viem";
import { type Address, type Hex, decodeAbiParameters, encodeAbiParameters } from "viem";

export type Action = typeof Action[keyof typeof Action];
export const Action = {
Expand Down Expand Up @@ -175,116 +174,3 @@ export function delegateDecode(encoded: Hex): DelegateArgs {
delegate,
};
}

//--------------------------------------------------------------------------------------------
// EXTERNAL CONTRACT METHODS
//--------------------------------------------------------------------------------------------

const voteLockedConvexTokenAbi = [
{
inputs: [
{
internalType: "address",
name: "_user",
type: "address",
},
],
name: "lockedBalances",
outputs: [
{ internalType: "uint256", name: "total", type: "uint256" },
{
internalType: "uint256",
name: "unlockable",
type: "uint256",
},
{
internalType: "uint256",
name: "locked",
type: "uint256",
},
{
components: [
{ internalType: "uint112", name: "amount", type: "uint112" },
{ internalType: "uint112", name: "boosted", type: "uint112" },
{ internalType: "uint32", name: "unlockTime", type: "uint32" },
],
internalType: "struct CvxLockerV2.LockedBalance[]",
name: "lockData",
type: "tuple[]",
},
],
stateMutability: "view",
type: "function",
},
] as const;

type LockData = {
amount: bigint;
boosted: bigint;
unlockTime: number;
};

type LockedBalances = {
total: bigint;
unlockable: bigint;
locked: bigint;
lockedData: LockData[];
};

export async function getVoteLockedBalances(
client: PublicClient,
args: Viem.ContractCallParameters<{
voteLockedConvexToken: Address;
positionAddress: Address;
}>,
) {
const [total, unlockable, locked, balancesData] = await Viem.readContract(client, args, {
abi: voteLockedConvexTokenAbi,
address: args.voteLockedConvexToken,
functionName: "lockedBalances",
args: [args.positionAddress],
});

const lockedData = balancesData.map((data) => {
return {
amount: data.amount,
boosted: data.boosted,
unlockTime: data.unlockTime,
};
});

const lockedBalancesData = {
total,
unlockable,
locked,
lockedData,
};

return lockedBalancesData;
}

export async function getAllVoteLockedBalances(
client: PublicClient,
args: Viem.ContractCallParameters<{
voteLockedConvexToken: Address;
positionAddresses: Address[];
}>,
) {
const allLockedBalances = await Promise.all(
args.positionAddresses.map(async (position) => {
const lockedBalances = await getVoteLockedBalances(client, {
voteLockedConvexToken: args.voteLockedConvexToken,
positionAddress: position,
});

return { position, lockedBalances };
}),
);

const lockedBalancesMap: Record<Address, LockedBalances> = {};
for (const { position, lockedBalances } of allLockedBalances) {
lockedBalancesMap[position] = lockedBalances;
}

return lockedBalancesMap;
}
104 changes: 1 addition & 103 deletions packages/sdk/src/internal/Extensions/ExternalPositions/Liquity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Viem } from "@enzymefinance/sdk/Utils";
import * as ExternalPositionManager from "@enzymefinance/sdk/internal/ExternalPositionManager";
import { type Address, type Hex, type PublicClient, decodeAbiParameters, encodeAbiParameters } from "viem";
import { type Address, type Hex, decodeAbiParameters, encodeAbiParameters } from "viem";

export type Action = typeof Action[keyof typeof Action];
export const Action = {
Expand Down Expand Up @@ -251,104 +250,3 @@ export function repayBorrowDecode(encoded: Hex): RepayBorrowArgs {
lowerHint,
};
}

//--------------------------------------------------------------------------------------------
// EXTERNAL CONTRACT METHODS
//--------------------------------------------------------------------------------------------

const troveManagerAbi = {
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "Troves",
outputs: [
{
internalType: "uint256",
name: "debt",
type: "uint256",
},
{
internalType: "uint256",
name: "coll",
type: "uint256",
},
{
internalType: "uint256",
name: "stake",
type: "uint256",
},
{
internalType: "enum TroveManager.Status",
name: "status",
type: "uint8",
},
{
internalType: "uint128",
name: "arrayIndex",
type: "uint128",
},
],
stateMutability: "view",
type: "function",
} as const;

type Trove = {
debt: bigint;
collateral: bigint;
stake: bigint;
status: number;
arrayIndex: bigint;
};

export async function getTrove(
client: PublicClient,
args: Viem.ContractCallParameters<{
liquityTroveManager: Address;
debtPosition: Address;
}>,
) {
const [debt, collateral, stake, status, arrayIndex] = await Viem.readContract(client, args, {
abi: [troveManagerAbi],
functionName: "Troves",
address: args.liquityTroveManager,
args: [args.debtPosition],
});

return {
debt,
collateral,
stake,
status,
arrayIndex,
};
}

export async function getTroves(
client: PublicClient,
args: Viem.ContractCallParameters<{
liquityTroveManager: Address;
debtPositions: [Address];
}>,
) {
const troves = await Promise.all(
args.debtPositions.map(async (position) => {
const trove = await getTrove(client, {
...args,
debtPosition: position,
});

return { position, trove };
}),
);

const troveMap: Record<Address, Trove> = {};
for (const { position, trove } of troves) {
troveMap[position] = trove;
}

return troveMap;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Viem } from "@enzymefinance/sdk/Utils";
import * as ExternalPositionManager from "@enzymefinance/sdk/internal/ExternalPositionManager";
import { type Address, type Hex, type PublicClient, decodeAbiParameters, encodeAbiParameters } from "viem";
import { type Address, type Hex, decodeAbiParameters, encodeAbiParameters } from "viem";

export type Action = typeof Action[keyof typeof Action];
export const Action = {
Expand Down Expand Up @@ -183,30 +182,3 @@ export function decodeClaimRewardsV1(encoded: Hex): ClaimRewardsV1Args {
rewardsContract,
};
}

//--------------------------------------------------------------------------------------------
// EXTERNAL CONTRACT METHODS
//--------------------------------------------------------------------------------------------

const convertToExitAssetsAbi = {
inputs: [{ internalType: "uint256", name: "shares_", type: "uint256" }],
name: "convertToExitAssets",
outputs: [{ internalType: "uint256", name: "assets_", type: "uint256" }],
stateMutability: "view",
type: "function",
} as const;

export async function convertSharesToExitAssets(
client: PublicClient,
args: Viem.ContractCallParameters<{
pool: Address;
shares: bigint;
}>,
) {
return Viem.readContract(client, args, {
abi: [convertToExitAssetsAbi],
functionName: "convertToExitAssets",
address: args.pool,
args: [args.shares],
});
}
Loading

0 comments on commit bf77b84

Please sign in to comment.