diff --git a/lib/moment/collectMoment.ts b/lib/moment/collectMoment.ts index 733b5f8c8..668624ea7 100644 --- a/lib/moment/collectMoment.ts +++ b/lib/moment/collectMoment.ts @@ -4,7 +4,7 @@ import { CHAIN_ID, IS_TESTNET, USDC_ADDRESS } from "@/lib/consts"; import { sendUserOperation } from "@/lib/coinbase/sendUserOperation"; import { getOrCreateSmartWallet } from "../coinbase/getOrCreateSmartWallet"; import { collectSchema } from "../schema/collectSchema"; -import { distributeSplitFunds } from "../splits/distributeSplitFunds"; +import { distributeCall } from "../splits/distributeCall"; import isSplitContract from "../splits/isSplitContract"; import { MomentType } from "@/types/moment"; import getCollectCall from "../viem/getCollectCall"; @@ -53,28 +53,27 @@ export async function collectMoment({ amount ); - const calls = [...approveCall, collectCall] as readonly OneOf< - Call - >[]; - // Send the transaction and wait for receipt using the helper - const transaction = await sendUserOperation({ - smartAccount, - network: IS_TESTNET ? "base-sepolia" : "base", - calls, - }); + const calls = [...approveCall, collectCall] as OneOf>[]; - // Distribute funds from split contract if fundsRecipient is a split if (saleConfig.fundsRecipient) { const isSplit = await isSplitContract(saleConfig.fundsRecipient as Address, CHAIN_ID); if (isSplit) { - await distributeSplitFunds({ + const splitCall = await distributeCall({ splitAddress: saleConfig.fundsRecipient, tokenAddress: saleConfig.type === MomentType.Erc20Mint ? USDC_ADDRESS : zeroAddress, // zeroAddress for native ETH smartAccount, }); + calls.push(splitCall); } } + // Send the transaction and wait for receipt using the helper + const transaction = await sendUserOperation({ + smartAccount, + network: IS_TESTNET ? "base-sepolia" : "base", + calls, + }); + return { hash: transaction.transactionHash as Hash, chainId: CHAIN_ID, diff --git a/lib/splits/distributeCall.ts b/lib/splits/distributeCall.ts new file mode 100644 index 000000000..dd7e727b3 --- /dev/null +++ b/lib/splits/distributeCall.ts @@ -0,0 +1,38 @@ +import { Address, Hash } from "viem"; +import { CHAIN_ID, USDC_ADDRESS } from "@/lib/consts"; +import { getSplitsClient } from "./getSplitsClient"; +import { getPublicClient } from "@/lib/viem/publicClient"; +import { EvmSmartAccount } from "@coinbase/cdp-sdk"; +import { Call } from "@coinbase/coinbase-sdk/dist/types/calls"; + +/** + * Generates call data to distribute funds from a split contract to its recipients. + * Uses 0xSplits SDK to generate the distribution call data. + * Returns a Call object that can be executed via smart wallet. + */ +export async function distributeCall({ + splitAddress, + tokenAddress = USDC_ADDRESS, + smartAccount, +}: { + splitAddress: Address; + tokenAddress?: Address; + smartAccount: EvmSmartAccount; +}): Promise> { + const publicClient = getPublicClient(CHAIN_ID); + const splitsClient = getSplitsClient({ + chainId: CHAIN_ID, + publicClient, + }); + + const distributeCallData = await splitsClient.callData.distribute({ + splitAddress, + tokenAddress, + distributorAddress: smartAccount.address, + }); + + return { + to: distributeCallData.address as Address, + data: distributeCallData.data, + }; +} diff --git a/lib/splits/distributeSplitFunds.ts b/lib/splits/distributeSplitFunds.ts deleted file mode 100644 index b544423cd..000000000 --- a/lib/splits/distributeSplitFunds.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { Address, Hash } from "viem"; -import { CHAIN_ID, IS_TESTNET, USDC_ADDRESS } from "@/lib/consts"; -import { getSplitsClient } from "./getSplitsClient"; -import { getPublicClient } from "@/lib/viem/publicClient"; -import { sendUserOperation } from "@/lib/coinbase/sendUserOperation"; -import { EvmSmartAccount } from "@coinbase/cdp-sdk"; - -/** - * Distributes funds from a split contract to its recipients. - * Uses 0xSplits SDK to generate the distribution call data and executes it via smart wallet. - */ -export async function distributeSplitFunds({ - splitAddress, - tokenAddress = USDC_ADDRESS, - smartAccount, -}: { - splitAddress: Address; - tokenAddress?: Address; - smartAccount: EvmSmartAccount; -}): Promise { - const publicClient = getPublicClient(CHAIN_ID); - const splitsClient = getSplitsClient({ - chainId: CHAIN_ID, - publicClient, - }); - - const distributeCallData = await splitsClient.callData.distribute({ - splitAddress, - tokenAddress, - distributorAddress: smartAccount.address, - }); - - const distributeTransaction = await sendUserOperation({ - smartAccount, - network: IS_TESTNET ? "base-sepolia" : "base", - calls: [ - { - to: distributeCallData.address as Address, - data: distributeCallData.data, - }, - ], - }); - - return distributeTransaction.transactionHash as Hash; -}