-
Notifications
You must be signed in to change notification settings - Fork 0
feat: batch split calls via Caller contract #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| node_modules/ | ||
| .vscode/ | ||
| build/ | ||
| .claude/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import {Interface} from 'ethers'; | ||
| import {Call} from './contracts/caller-client'; | ||
| import {OxString, SplitsReceiver} from './types'; | ||
| import appSettings from './appSettings'; | ||
| import {dripsAbi} from './contracts/drips-abi'; | ||
|
|
||
| const dripsInterface = new Interface(dripsAbi); | ||
| const dripsAddress = appSettings.network.contracts.drips; | ||
|
|
||
| export function buildReceiveStreamsCall( | ||
| accountId: bigint, | ||
| token: OxString, | ||
| maxCycles: number, | ||
| ): Call { | ||
| return { | ||
| target: dripsAddress, | ||
| data: dripsInterface.encodeFunctionData('receiveStreams', [ | ||
| accountId, | ||
| token, | ||
| maxCycles, | ||
| ]), | ||
| value: 0n, | ||
| }; | ||
| } | ||
|
jtourkos marked this conversation as resolved.
|
||
|
|
||
| export function buildSplitCall( | ||
| accountId: bigint, | ||
| token: OxString, | ||
| receivers: SplitsReceiver[], | ||
| ): Call { | ||
| // Convert SplitsReceiver[] to the format expected by the contract | ||
| const contractReceivers = receivers.map(r => ({ | ||
| accountId: r.accountId, | ||
| weight: r.weight, | ||
| })); | ||
|
|
||
| return { | ||
| target: dripsAddress, | ||
| data: dripsInterface.encodeFunctionData('split', [ | ||
| accountId, | ||
| token, | ||
| contractReceivers, | ||
| ]), | ||
| value: 0n, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Splits an array into chunks of a given size | ||
| * @param array - The array to chunk | ||
| * @param size - The size of each chunk (must be a positive integer) | ||
| * @returns Array of chunks | ||
| */ | ||
| export function chunk<T>(array: T[], size: number): T[][] { | ||
| if (array.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!Number.isInteger(size) || size <= 0) { | ||
| throw new Error(`Chunk size must be a positive integer, got: ${size}`); | ||
| } | ||
|
|
||
| const chunks: T[][] = []; | ||
| for (let i = 0; i < array.length; i += size) { | ||
| chunks.push(array.slice(i, i + size)); | ||
| } | ||
| return chunks; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export const callerAbi = [ | ||
| { | ||
| inputs: [ | ||
| { | ||
| components: [ | ||
| {internalType: 'address', name: 'target', type: 'address'}, | ||
| {internalType: 'bytes', name: 'data', type: 'bytes'}, | ||
| {internalType: 'uint256', name: 'value', type: 'uint256'}, | ||
| ], | ||
| internalType: 'struct Call[]', | ||
| name: 'calls', | ||
| type: 'tuple[]', | ||
| }, | ||
| ], | ||
| name: 'callBatched', | ||
| outputs: [{internalType: 'bytes[]', name: 'returnData', type: 'bytes[]'}], | ||
| stateMutability: 'nonpayable', | ||
| type: 'function', | ||
| }, | ||
| ] as const; | ||
|
|
||
| export type CallerAbi = typeof callerAbi; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import {Contract, TransactionResponse, ZeroAddress} from 'ethers'; | ||
| import appSettings from '../appSettings'; | ||
| import {getContractRunner} from '../getWalletInstance'; | ||
| import {callerAbi} from './caller-abi'; | ||
|
|
||
| const { | ||
| network: { | ||
| contracts: {caller: contractAddress}, | ||
| name: networkName, | ||
| }, | ||
| } = appSettings; | ||
|
|
||
| let contractInstance: Contract | null = null; | ||
|
|
||
| export type Call = { | ||
| target: string; | ||
| data: string; | ||
| value: bigint; | ||
| }; | ||
|
|
||
| async function getCallerContract(): Promise<Contract> { | ||
| if (contractInstance) { | ||
| return contractInstance; | ||
| } | ||
|
jtourkos marked this conversation as resolved.
|
||
|
|
||
| if (!contractAddress || contractAddress === ZeroAddress) { | ||
| throw new Error( | ||
| `No Caller contract address configured for chain: ${networkName}`, | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| contractInstance = new Contract( | ||
| contractAddress, | ||
| callerAbi, | ||
| await getContractRunner(), | ||
| ); | ||
| return contractInstance; | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Failed to initialize Caller contract: ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function callerBatchedCall( | ||
| calls: Call[], | ||
| ): Promise<TransactionResponse> { | ||
| try { | ||
| const caller = await getCallerContract(); | ||
| return await caller.callBatched(calls); | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Caller.callBatched failed: ${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.