Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(studio): Throw error for wrapped Multicall request when address …
Browse files Browse the repository at this point in the history
…is invalid
  • Loading branch information
immasandwich committed Nov 16, 2023
1 parent 78adf7d commit 0ae16a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import { MainModule } from '~main.module';

(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

async function bootstrap() {
const app = await NestFactory.create(MainModule);

Expand Down
20 changes: 20 additions & 0 deletions src/multicall/errors/multicall.address.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type MulticallWrappedInvalidAddressErrorType = MulticallWrappedInvalidAddressError & {
name: 'MulticallWrappedInvalidAddressError';
};

type MulticallWrappedInvalidAddressErrorParams = {
signature: string;
args: any[];
stack?: string;
};

export class MulticallWrappedInvalidAddressError extends Error {
override name = 'MulticallWrappedInvalidAddressError';
stack?: string;

constructor({ signature, args, stack }: MulticallWrappedInvalidAddressErrorParams) {
const argsString = `[${args.map(v => JSON.stringify(v)).join(', ')}]`;
super(`Multicall invalid address provided for method ${signature} with args ${argsString}`);
this.stack = stack;
}
}
19 changes: 10 additions & 9 deletions src/multicall/impl/multicall.viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
encodeFunctionData,
getAbiItem,
getFunctionSignature,
isAddress,
} from 'viem';

import { MulticallContract } from '~contract/contracts/viem/Multicall';
import { MulticallWrappedInvalidAddressError } from '~multicall/errors/multicall.address.error';
import { MulticallWrappedReadRequestError } from '~multicall/errors/multicall.request.error';
import { DEFAULT_DATALOADER_OPTIONS } from '~multicall/multicall.constants';
import { MulticallCallStruct } from '~multicall/multicall.types';

import { MulticallWrappedReadDecodeError } from '../errors/multicall.decode.error';
import { MulticallCallStruct } from '~multicall/multicall.types';

declare module 'abitype' {
export interface Config {
Expand Down Expand Up @@ -63,14 +65,13 @@ export class ViemMulticallDataLoader {
}

async doCalls(calls: ContractCall[]): Promise<ReadContractReturnType[]> {
const callRequests = calls.map(call => ({
target: call.address,
callData: encodeFunctionData({
abi: call.abi,
args: call.args,
functionName: call.functionName,
}),
}));
const callRequests = calls.map(({ address, abi, functionName, args, stack }) => {
if (!isAddress(address))
throw new MulticallWrappedInvalidAddressError({ signature: functionName, args: args, stack: stack });

const callData = encodeFunctionData({ abi, functionName, args });
return { target: address, callData };
});

if (this.beforeCallHook) this.beforeCallHook(calls, callRequests);
const { result: res } = await this.multicall.simulate.aggregate([callRequests, false]);
Expand Down

0 comments on commit 0ae16a0

Please sign in to comment.