diff --git a/.changeset/spotty-worms-matter.md b/.changeset/spotty-worms-matter.md new file mode 100644 index 00000000..b1ef6434 --- /dev/null +++ b/.changeset/spotty-worms-matter.md @@ -0,0 +1,5 @@ +--- +"@enzymefinance/sdk": patch +--- + +Deposit transactions for backend diff --git a/packages/sdk/src/Deposit.ts b/packages/sdk/src/Deposit.ts index 68a5de1b..7d1b6cba 100644 --- a/packages/sdk/src/Deposit.ts +++ b/packages/sdk/src/Deposit.ts @@ -4,12 +4,10 @@ import { Viem } from "@enzymefinance/sdk/Utils"; import { Assertion } from "@enzymefinance/sdk/Utils"; import { type Address, type Hex, type PublicClient } from "viem"; -/** - * Get the shares action timelock. - * - * @param client The public client to use to read the contract. - * @returns The shares action timelock in seconds. - */ +//-------------------------------------------------------------------------------------------- +// DEPOSIT +//-------------------------------------------------------------------------------------------- + export function getSharesActionTimelock( client: PublicClient, args: Viem.ContractCallParameters<{ @@ -42,8 +40,8 @@ export async function getExpectedSharesForDeposit( client: PublicClient, args: Viem.ContractCallParameters<{ comptrollerProxy: Address; - depositor: Address; amount: bigint; + depositor: Address; }>, ) { const { result } = await Viem.simulateContract(client, args, { @@ -57,19 +55,74 @@ export async function getExpectedSharesForDeposit( return result; } -export async function getExpectedSharesForNativeTokenDeposit( - client: PublicClient, +export async function deposit( args: Viem.ContractCallParameters<{ - depositWrapper: Address; comptrollerProxy: Address; - minSharesQuantity: bigint; - exchange: Address; - exchangeApproveTarget: Address; - exchangeData: Hex; - minInvestmentAmount: bigint; - depositor: Address; amount: bigint; + depositor: Address; + minSharesQuantity: bigint; }>, +) { + return new Viem.PopulatedTransaction({ + abi: Abis.IComptrollerLib, + functionName: "buyShares", + address: args.comptrollerProxy, + args: [args.amount, args.minSharesQuantity], + }); +} + +//-------------------------------------------------------------------------------------------- +// REDEMPTION +//-------------------------------------------------------------------------------------------- + +export async function getSpecificAssetsRedemptionExpectedAmounts( + client: PublicClient, + args: Viem.ContractCallParameters<{ + signer: Address; + recipient: Address; + sharesQuantity: bigint; + payoutAssets: Address[]; + payoutPercentages: bigint[]; + }>, +) { + const { result: payoutAmounts } = await Viem.simulateContract(client, args, { + abi: Abis.IComptrollerLib, + functionName: "redeemSharesForSpecificAssets", + address: args.signer, + args: [args.recipient, args.sharesQuantity, args.payoutAssets, args.payoutPercentages], + }); + + const output: Record = {}; + + for (let i = 0; i < args.payoutAssets.length; i++) { + const payoutAsset = args.payoutAssets[i]; + const payoutAmount = payoutAmounts[i]; + Assertion.invariant(payoutAmount !== undefined, "Expected payout amount to be defined."); + Assertion.invariant(payoutAsset !== undefined, "Expected payout asset to be defined."); + + output[payoutAsset] = payoutAmount; + } + + return output; +} + +//-------------------------------------------------------------------------------------------- +// DEPOSIT WRAPPER +//-------------------------------------------------------------------------------------------- + +interface NativeDepositArgs { + depositWrapper: Address; + comptrollerProxy: Address; + exchange: Address; + exchangeApproveTarget: Address; + exchangeData: Hex; + minInvestmentAmount: bigint; + amount: bigint; +} + +export async function getExpectedSharesForNativeTokenDeposit( + client: PublicClient, + args: Viem.ContractCallParameters, ) { const { result } = await Viem.simulateContract(client, args, { abi: Abis.IDepositWrapper, @@ -77,7 +130,7 @@ export async function getExpectedSharesForNativeTokenDeposit( functionName: "exchangeEthAndBuyShares", args: [ args.comptrollerProxy, - args.minSharesQuantity, + 1n, args.exchange, args.exchangeApproveTarget, args.exchangeData, @@ -90,6 +143,29 @@ export async function getExpectedSharesForNativeTokenDeposit( return result; } +export async function depositNativeToken( + args: Viem.ContractCallParameters, +) { + return new Viem.PopulatedTransaction({ + abi: Abis.IDepositWrapper, + address: args.depositWrapper, + functionName: "exchangeEthAndBuyShares", + args: [ + args.comptrollerProxy, + args.minSharesQuantity, + args.exchange, + args.exchangeApproveTarget, + args.exchangeData, + args.minInvestmentAmount, + ], + value: args.amount, + }); +} + +//-------------------------------------------------------------------------------------------- +// SHARES WRAPPER DEPOSIT +//-------------------------------------------------------------------------------------------- + export async function getExpectedSharesForSharesWrapperDeposit( client: PublicClient, args: Viem.ContractCallParameters<{ @@ -110,6 +186,22 @@ export async function getExpectedSharesForSharesWrapperDeposit( return result; } +export async function sharesWrapperDeposit( + args: Viem.ContractCallParameters<{ + sharesWrapper: Address; + denominationAsset: Address; + amount: bigint; + minSharesAmount: bigint; + }>, +) { + return new Viem.PopulatedTransaction({ + abi: Abis.IGatedRedemptionQueueSharesWrapperLib, + functionName: "deposit", + address: args.sharesWrapper, + args: [args.denominationAsset, args.amount, args.minSharesAmount], + }); +} + export async function isAllowedDepositor( client: PublicClient, args: Viem.ContractCallParameters<{ @@ -135,34 +227,3 @@ export async function isAllowedDepositor( args: [args.comptrollerProxy, args.depositor], }); } - -export async function getSpecificAssetsRedemptionExpectedAmounts( - client: PublicClient, - args: Viem.ContractCallParameters<{ - signer: Address; - recipient: Address; - sharesQuantity: bigint; - payoutAssets: Address[]; - payoutPercentages: bigint[]; - }>, -) { - const { result: payoutAmounts } = await Viem.simulateContract(client, args, { - abi: Abis.IComptrollerLib, - functionName: "redeemSharesForSpecificAssets", - address: args.signer, - args: [args.recipient, args.sharesQuantity, args.payoutAssets, args.payoutPercentages], - }); - - const output: Record = {}; - - for (let i = 0; i < args.payoutAssets.length; i++) { - const payoutAsset = args.payoutAssets[i]; - const payoutAmount = payoutAmounts[i]; - Assertion.invariant(payoutAmount !== undefined, "Expected payout amount to be defined."); - Assertion.invariant(payoutAsset !== undefined, "Expected payout asset to be defined."); - - output[payoutAsset] = payoutAmount; - } - - return output; -}