Skip to content

Commit a8dc428

Browse files
Fr/openapi spec fix (#676)
* openapi fixes * fix for read type * fix: openapi spec for write and update contract schemas * reuse abi type * fix return type on read --------- Co-authored-by: Phillip Ho <arcoraven@gmail.com>
1 parent 0a1fd55 commit a8dc428

File tree

10 files changed

+146
-76
lines changed

10 files changed

+146
-76
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,5 @@ credentials
150150
https
151151

152152
# Auto generated OpenAPI file
153-
openapi.json
153+
openapi.json
154+
.aider*

biome.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.9.2/schema.json",
3+
"vcs": {
4+
"enabled": true,
5+
"clientKind": "git",
6+
"useIgnoreFile": true,
7+
"defaultBranch": "dev"
8+
},
9+
"formatter": {
10+
"enabled": true,
11+
"indentStyle": "space"
12+
},
13+
"organizeImports": {
14+
"enabled": true
15+
},
16+
"linter": {
17+
"enabled": true,
18+
"rules": {
19+
"correctness": {
20+
"noNewSymbol": "error",
21+
"noUnusedImports": "error",
22+
"noUnusedVariables": "error",
23+
"useArrayLiterals": "error"
24+
},
25+
"complexity": {
26+
"noStaticOnlyClass": "off"
27+
}
28+
}
29+
}
30+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"zod": "^3.23.8"
7676
},
7777
"devDependencies": {
78+
"@biomejs/biome": "^1.9.2",
7879
"@types/cli-progress": "^3.11.3",
7980
"@types/cookie": "^0.5.1",
8081
"@types/crypto-js": "^4.2.2",

src/server/middleware/open-api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import swagger from "@fastify/swagger";
2-
import { FastifyInstance } from "fastify";
2+
import type { FastifyInstance } from "fastify";
33

44
export const withOpenApi = async (server: FastifyInstance) => {
55
await server.register(swagger, {
6-
// mode: ,
76
openapi: {
87
info: {
98
title: "thirdweb Engine",
@@ -30,10 +29,15 @@ export const withOpenApi = async (server: FastifyInstance) => {
3029
},
3130
],
3231
},
32+
refResolver: {
33+
buildLocalReference(json, baseUri, fragment, i) {
34+
return json.$id || `def-${i}`;
35+
},
36+
},
3337
});
3438

3539
// Exports the /json endpoint without the Swagger UI.
36-
await server.get("/json", {}, async (req, res) => {
40+
server.get("/json", {}, async (_, res) => {
3741
res.send(server.swagger());
3842
});
3943
};

src/server/routes/backend-wallet/simulateTransaction.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { randomUUID } from "crypto";
3-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
43
import { StatusCodes } from "http-status-codes";
5-
import { Address, Hex } from "thirdweb";
4+
import { randomUUID } from "node:crypto";
5+
import type { Address, Hex } from "thirdweb";
66
import { doSimulateTransaction } from "../../../utils/transaction/simulateQueuedTransaction";
7-
import { QueuedTransaction } from "../../../utils/transaction/types";
7+
import type { QueuedTransaction } from "../../../utils/transaction/types";
88
import { createCustomError } from "../../middleware/error";
99
import { AddressSchema } from "../../schemas/address";
1010
import {
@@ -17,6 +17,14 @@ import {
1717
} from "../../schemas/wallet";
1818
import { getChainIdFromChain } from "../../utils/chain";
1919

20+
const FunctionArgumentSchema = Type.Union([
21+
Type.String({ description: "String argument" }),
22+
Type.Number({ description: "Numeric argument" }),
23+
Type.Boolean({ description: "Boolean argument" }),
24+
Type.Array(Type.Any(), { description: "Array argument" }),
25+
Type.Object({}, { description: "Object argument" }),
26+
]);
27+
2028
const simulateRequestBodySchema = Type.Object({
2129
toAddress: {
2230
...AddressSchema,
@@ -35,17 +43,9 @@ const simulateRequestBodySchema = Type.Object({
3543
}),
3644
),
3745
args: Type.Optional(
38-
Type.Array(
39-
Type.Union([
40-
Type.String({
41-
description: "The arguments to call for this function",
42-
}),
43-
Type.Tuple([Type.String(), Type.String()]),
44-
Type.Object({}),
45-
Type.Array(Type.Any()),
46-
Type.Any(),
47-
]),
48-
),
46+
Type.Array(FunctionArgumentSchema, {
47+
description: "The arguments to call for this function",
48+
}),
4949
),
5050
// Raw transaction args
5151
data: Type.Optional(

src/server/routes/contract/metadata/abi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { getContract } from "../../../../utils/cache/getContract";
55
import { abiSchema } from "../../../schemas/contract";

src/server/routes/contract/read/read.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
import { FastifyInstance } from "fastify";
1+
import { Type } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
23
import { StatusCodes } from "http-status-codes";
34
import { getContract } from "../../../../utils/cache/getContract";
4-
import { readRequestQuerySchema, readSchema } from "../../../schemas/contract";
5-
import { partialRouteSchema } from "../../../schemas/sharedApiSchemas";
5+
import {
6+
readRequestQuerySchema,
7+
type readSchema,
8+
} from "../../../schemas/contract";
9+
import {
10+
partialRouteSchema,
11+
standardResponseSchema,
12+
} from "../../../schemas/sharedApiSchemas";
613
import { getChainIdFromChain } from "../../../utils/chain";
714
import { bigNumberReplacer } from "../../../utils/convertor";
815

16+
const responseSchema = Type.Object({
17+
result: Type.Any(),
18+
});
19+
920
export async function readContract(fastify: FastifyInstance) {
1021
fastify.route<readSchema>({
1122
method: "GET",
@@ -17,6 +28,10 @@ export async function readContract(fastify: FastifyInstance) {
1728
operationId: "read",
1829
...partialRouteSchema,
1930
querystring: readRequestQuerySchema,
31+
response: {
32+
...standardResponseSchema,
33+
[StatusCodes.OK]: responseSchema,
34+
},
2035
},
2136
handler: async (request, reply) => {
2237
const { chain, contractAddress } = request.params;
@@ -31,11 +46,11 @@ export async function readContract(fastify: FastifyInstance) {
3146
const parsedArgs = args?.split(",").map((arg) => {
3247
if (arg === "true") {
3348
return true;
34-
} else if (arg === "false") {
49+
}
50+
if (arg === "false") {
3551
return false;
36-
} else {
37-
return arg;
3852
}
53+
return arg;
3954
});
4055

4156
let returnData = await contract.call(functionName, parsedArgs ?? []);

src/server/routes/contract/write/write.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { prepareContractCall, resolveMethod } from "thirdweb";
55
import { getContractV5 } from "../../../../utils/cache/getContractv5";
@@ -10,7 +10,6 @@ import { abiSchema } from "../../../schemas/contract";
1010
import {
1111
contractParamSchema,
1212
requestQuerystringSchema,
13-
standardResponseSchema,
1413
transactionWritesResponseSchema,
1514
} from "../../../schemas/sharedApiSchemas";
1615
import { txOverridesWithValueSchema } from "../../../schemas/txOverrides";
@@ -26,17 +25,9 @@ const writeRequestBodySchema = Type.Object({
2625
functionName: Type.String({
2726
description: "The function to call on the contract",
2827
}),
29-
args: Type.Array(
30-
Type.Union([
31-
Type.String({
32-
description: "The arguments to call on the function",
33-
}),
34-
Type.Tuple([Type.String(), Type.String()]),
35-
Type.Object({}),
36-
Type.Array(Type.Any()),
37-
Type.Any(),
38-
]),
39-
),
28+
args: Type.Array(Type.Any(), {
29+
description: "The arguments to call on the function",
30+
}),
4031
...txOverridesWithValueSchema.properties,
4132
abi: Type.Optional(Type.Array(abiSchema)),
4233
});
@@ -59,11 +50,10 @@ export async function writeToContract(fastify: FastifyInstance) {
5950
params: contractParamSchema,
6051
headers: walletWithAAHeaderSchema,
6152
querystring: requestQuerystringSchema,
53+
body: writeRequestBodySchema,
6254
response: {
63-
...standardResponseSchema,
6455
[StatusCodes.OK]: transactionWritesResponseSchema,
6556
},
66-
body: writeRequestBodySchema,
6757
},
6858
handler: async (request, reply) => {
6959
const { chain, contractAddress } = request.params;

src/server/schemas/contract/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { contractSchemaTypes } from "../sharedApiSchemas";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { contractSchemaTypes } from "../sharedApiSchemas";
33

44
/**
55
* Basic schema for all Request Query String
@@ -56,8 +56,8 @@ export const abiEventSchema = Type.Object({
5656
export const abiSchema = Type.Object({
5757
type: Type.String(),
5858
name: Type.Optional(Type.String()),
59-
inputs: Type.Array(abiTypeSchema),
60-
outputs: Type.Array(abiTypeSchema),
59+
inputs: Type.Optional(Type.Array(abiTypeSchema)),
60+
outputs: Type.Optional(Type.Array(abiTypeSchema)),
6161
stateMutability: Type.Optional(Type.String()),
6262
});
6363

yarn.lock

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,60 @@
603603
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
604604
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
605605

606+
"@biomejs/biome@^1.9.2":
607+
version "1.9.2"
608+
resolved "https://registry.yarnpkg.com/@biomejs/biome/-/biome-1.9.2.tgz#297a6a0172e46124b9b6058ec3875091d352a0be"
609+
integrity sha512-4j2Gfwft8Jqp1X0qLYvK4TEy4xhTo4o6rlvJPsjPeEame8gsmbGQfOPBkw7ur+7/Z/f0HZmCZKqbMvR7vTXQYQ==
610+
optionalDependencies:
611+
"@biomejs/cli-darwin-arm64" "1.9.2"
612+
"@biomejs/cli-darwin-x64" "1.9.2"
613+
"@biomejs/cli-linux-arm64" "1.9.2"
614+
"@biomejs/cli-linux-arm64-musl" "1.9.2"
615+
"@biomejs/cli-linux-x64" "1.9.2"
616+
"@biomejs/cli-linux-x64-musl" "1.9.2"
617+
"@biomejs/cli-win32-arm64" "1.9.2"
618+
"@biomejs/cli-win32-x64" "1.9.2"
619+
620+
"@biomejs/cli-darwin-arm64@1.9.2":
621+
version "1.9.2"
622+
resolved "https://registry.yarnpkg.com/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-1.9.2.tgz#9303e426156189b2ab469154f7cd94ecfbf8bd5e"
623+
integrity sha512-rbs9uJHFmhqB3Td0Ro+1wmeZOHhAPTL3WHr8NtaVczUmDhXkRDWScaxicG9+vhSLj1iLrW47itiK6xiIJy6vaA==
624+
625+
"@biomejs/cli-darwin-x64@1.9.2":
626+
version "1.9.2"
627+
resolved "https://registry.yarnpkg.com/@biomejs/cli-darwin-x64/-/cli-darwin-x64-1.9.2.tgz#a674e61c04b5aeca31b5b1e7f7b678937a6e90ac"
628+
integrity sha512-BlfULKijNaMigQ9GH9fqJVt+3JTDOSiZeWOQtG/1S1sa8Lp046JHG3wRJVOvekTPL9q/CNFW1NVG8J0JN+L1OA==
629+
630+
"@biomejs/cli-linux-arm64-musl@1.9.2":
631+
version "1.9.2"
632+
resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-arm64-musl/-/cli-linux-arm64-musl-1.9.2.tgz#b126b0093a7b7632c01a948bf7a0feaf5040ea76"
633+
integrity sha512-ZATvbUWhNxegSALUnCKWqetTZqrK72r2RsFD19OK5jXDj/7o1hzI1KzDNG78LloZxftrwr3uI9SqCLh06shSZw==
634+
635+
"@biomejs/cli-linux-arm64@1.9.2":
636+
version "1.9.2"
637+
resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-arm64/-/cli-linux-arm64-1.9.2.tgz#c71b4aa374fcd32d3f1a9e7c8a6ce3090e9b6be4"
638+
integrity sha512-T8TJuSxuBDeQCQzxZu2o3OU4eyLumTofhCxxFd3+aH2AEWVMnH7Z/c3QP1lHI5RRMBP9xIJeMORqDQ5j+gVZzw==
639+
640+
"@biomejs/cli-linux-x64-musl@1.9.2":
641+
version "1.9.2"
642+
resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-x64-musl/-/cli-linux-x64-musl-1.9.2.tgz#5fce02295b435362aa75044ddc388547fbbbbb19"
643+
integrity sha512-CjPM6jT1miV5pry9C7qv8YJk0FIZvZd86QRD3atvDgfgeh9WQU0k2Aoo0xUcPdTnoz0WNwRtDicHxwik63MmSg==
644+
645+
"@biomejs/cli-linux-x64@1.9.2":
646+
version "1.9.2"
647+
resolved "https://registry.yarnpkg.com/@biomejs/cli-linux-x64/-/cli-linux-x64-1.9.2.tgz#17d7bac0b6ebb20b9fb18812d4ef390f5855858f"
648+
integrity sha512-T0cPk3C3Jr2pVlsuQVTBqk2qPjTm8cYcTD9p/wmR9MeVqui1C/xTVfOIwd3miRODFMrJaVQ8MYSXnVIhV9jTjg==
649+
650+
"@biomejs/cli-win32-arm64@1.9.2":
651+
version "1.9.2"
652+
resolved "https://registry.yarnpkg.com/@biomejs/cli-win32-arm64/-/cli-win32-arm64-1.9.2.tgz#725734c4dc35cdcad3ef5cbcd85c6ff1238afa46"
653+
integrity sha512-2x7gSty75bNIeD23ZRPXyox6Z/V0M71ObeJtvQBhi1fgrvPdtkEuw7/0wEHg6buNCubzOFuN9WYJm6FKoUHfhg==
654+
655+
"@biomejs/cli-win32-x64@1.9.2":
656+
version "1.9.2"
657+
resolved "https://registry.yarnpkg.com/@biomejs/cli-win32-x64/-/cli-win32-x64-1.9.2.tgz#6f506d604d3349c1ba674d46cb96540c00a2ba83"
658+
integrity sha512-JC3XvdYcjmu1FmAehVwVV0SebLpeNTnO2ZaMdGCSOdS7f8O9Fq14T2P1gTG1Q29Q8Dt1S03hh0IdVpIZykOL8g==
659+
606660
"@blocto/sdk@0.10.2":
607661
version "0.10.2"
608662
resolved "https://registry.yarnpkg.com/@blocto/sdk/-/sdk-0.10.2.tgz#d29c652d25ac367a3c8d5fabdcb770b5cbb07c1c"
@@ -10517,16 +10571,7 @@ strict-uri-encode@^2.0.0:
1051710571
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546"
1051810572
integrity sha512-QwiXZgpRcKkhTj2Scnn++4PKtWsH0kpzZ62L2R6c/LUVYv7hVnZqcg2+sMuT6R7Jusu1vviK/MFsu6kNJfWlEQ==
1051910573

10520-
"string-width-cjs@npm:string-width@^4.2.0":
10521-
version "4.2.3"
10522-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
10523-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
10524-
dependencies:
10525-
emoji-regex "^8.0.0"
10526-
is-fullwidth-code-point "^3.0.0"
10527-
strip-ansi "^6.0.1"
10528-
10529-
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
10574+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
1053010575
version "4.2.3"
1053110576
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
1053210577
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -10563,14 +10608,7 @@ string_decoder@~1.1.1:
1056310608
dependencies:
1056410609
safe-buffer "~5.1.0"
1056510610

10566-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
10567-
version "6.0.1"
10568-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
10569-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
10570-
dependencies:
10571-
ansi-regex "^5.0.1"
10572-
10573-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
10611+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
1057410612
version "6.0.1"
1057510613
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
1057610614
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -11599,7 +11637,7 @@ wordwrap@^1.0.0:
1159911637
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
1160011638
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==
1160111639

11602-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
11640+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
1160311641
version "7.0.0"
1160411642
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1160511643
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -11617,15 +11655,6 @@ wrap-ansi@^6.2.0:
1161711655
string-width "^4.1.0"
1161811656
strip-ansi "^6.0.0"
1161911657

11620-
wrap-ansi@^7.0.0:
11621-
version "7.0.0"
11622-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
11623-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
11624-
dependencies:
11625-
ansi-styles "^4.0.0"
11626-
string-width "^4.1.0"
11627-
strip-ansi "^6.0.0"
11628-
1162911658
wrap-ansi@^8.1.0:
1163011659
version "8.1.0"
1163111660
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)