Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules/
.vscode/
build/
.claude/
68 changes: 68 additions & 0 deletions src/batchBuilder.ts
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,
};
}
Comment thread
jtourkos marked this conversation as resolved.
Comment thread
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;
}
22 changes: 22 additions & 0 deletions src/contracts/caller-abi.ts
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;
57 changes: 57 additions & 0 deletions src/contracts/caller-client.ts
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;
}
Comment thread
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)}`,
);
}
}
9 changes: 9 additions & 0 deletions src/getNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0xd0Dd053392db676D57317CD4fe96Fc2cCf42D0b4',
repoSubAccountDriver: '0xc219395880fa72e3ad9180b8878e0d39d144130b',
caller: '0x60F25ac5F289Dc7F640f948521d486C964A248e5',
},
};
case 11155111:
Expand All @@ -22,6 +23,7 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0x74A32a38D945b9527524900429b083547DeB9bF4',
repoSubAccountDriver: '0x317400fd9dfdad78d53a34455d89beb8f03f90ee',
caller: '0x09e04Cb8168bd0E8773A79Cc2099f19C46776Fee',
},
};
case 314:
Expand All @@ -32,6 +34,7 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0xd320F59F109c618b19707ea5C5F068020eA333B3',
repoSubAccountDriver: '0x925a69f6d07ee4c753df139bcc2a946e1d1ee92a',
caller: '0xd6Ab8e72dE3742d45AdF108fAa112Cd232718828',
},
};
case 1088:
Expand All @@ -42,6 +45,7 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0xd320F59F109c618b19707ea5C5F068020eA333B3',
repoSubAccountDriver: '0x925a69f6d07ee4c753df139bcc2a946e1d1ee92a',
caller: '0xd6Ab8e72dE3742d45AdF108fAa112Cd232718828',
},
};
case 10:
Expand All @@ -52,6 +56,7 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0xd320F59F109c618b19707ea5C5F068020eA333B3',
repoSubAccountDriver: '0x925a69f6d07ee4c753df139bcc2a946e1d1ee92a',
caller: '0xd6Ab8e72dE3742d45AdF108fAa112Cd232718828',
},
};
case 31337:
Expand All @@ -62,6 +67,10 @@ export default function getNetwork(chain: ChainId): Network {
contracts: {
drips: '0x7CBbD3FdF9E5eb359E6D9B12848c5Faa81629944',
repoSubAccountDriver: '0xB8743C2bB8DF7399273aa7EE4cE8d4109Bec327F',
// Override via LOCAL_CALLER_ADDRESS env var when testing batch functionality
caller:
process.env.LOCAL_CALLER_ADDRESS ??
'0x0000000000000000000000000000000000000000',
},
};
}
Expand Down
Loading