File tree Expand file tree Collapse file tree 2 files changed +43
-4
lines changed
src/server/routes/contract/read Expand file tree Collapse file tree 2 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { Type } from "@sinclair/typebox";
2
2
import type { FastifyInstance } from "fastify" ;
3
3
import { StatusCodes } from "http-status-codes" ;
4
4
import { getContract } from "../../../../utils/cache/getContract" ;
5
+ import { createCustomError } from "../../../middleware/error" ;
5
6
import {
6
7
readRequestQuerySchema ,
7
8
type readSchema ,
@@ -62,11 +63,28 @@ export async function readContract(fastify: FastifyInstance) {
62
63
return arg ;
63
64
} ) ;
64
65
65
- let returnData = await contract . call ( functionName , parsedArgs ?? [ ] ) ;
66
+ let returnData : unknown ;
67
+
68
+ try {
69
+ returnData = await contract . call ( functionName , parsedArgs ?? [ ] ) ;
70
+ } catch ( e ) {
71
+ if (
72
+ e instanceof Error &&
73
+ ( e . message . includes ( "is not a function" ) ||
74
+ e . message . includes ( "arguments, but" ) )
75
+ ) {
76
+ throw createCustomError (
77
+ e . message ,
78
+ StatusCodes . BAD_REQUEST ,
79
+ "BAD_REQUEST" ,
80
+ ) ;
81
+ }
82
+ }
66
83
returnData = bigNumberReplacer ( returnData ) ;
67
84
68
85
reply . status ( StatusCodes . OK ) . send ( {
69
- result : returnData ,
86
+ // biome-ignore lint/suspicious/noExplicitAny: data from chain
87
+ result : returnData as any ,
70
88
} ) ;
71
89
} ,
72
90
} ) ;
Original file line number Diff line number Diff line change 1
- import { beforeAll , describe , test } from "bun:test" ;
1
+ import { beforeAll , describe , expect , test } from "bun:test" ;
2
2
import { sepolia } from "thirdweb/chains" ;
3
- import { expect } from "vitest " ;
3
+ import type { ApiError } from "../../../sdk/dist/thirdweb-dev-engine.cjs " ;
4
4
import type { setupEngine } from "../utils/engine" ;
5
5
import { setup } from "./setup" ;
6
6
@@ -35,4 +35,25 @@ describe("Read Tests", () => {
35
35
expect ( result [ 2 ] ) . toEqual ( "1" ) ;
36
36
expect ( result [ 3 ] ) . toEqual ( "2" ) ;
37
37
} ) ;
38
+
39
+ test ( "Incorrectly read a contract should 400 (incorrect arity)" , async ( ) => {
40
+ const structValues = {
41
+ name : "test" ,
42
+ value : 123 ,
43
+ } ;
44
+
45
+ const structString = JSON . stringify ( structValues ) ;
46
+
47
+ try {
48
+ await engine . contract . read (
49
+ "readStructAndInts" ,
50
+ chainIdString ,
51
+ structContractAddress ,
52
+ [ structString , 1 ] . join ( "," ) ,
53
+ ) ;
54
+ throw new Error ( "Expected method to throw" ) ;
55
+ } catch ( error ) {
56
+ expect ( ( error as ApiError ) . status ) . toBe ( 400 ) ;
57
+ }
58
+ } ) ;
38
59
} ) ;
You can’t perform that action at this time.
0 commit comments