Skip to content

Commit 037c194

Browse files
authored
Update: Contract-Write Accepts Contract ABI (#534)
* Update: Contract-Write Accepts Contract ABI * updated abiSchema
1 parent 066c7e5 commit 037c194

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { queueTx } from "../../../../db/transactions/queueTx";
55
import { getContract } from "../../../../utils/cache/getContract";
6+
import { abiSchema } from "../../../schemas/contract";
67
import {
78
contractParamSchema,
89
requestQuerystringSchema,
@@ -30,20 +31,9 @@ const writeRequestBodySchema = Type.Object({
3031
]),
3132
),
3233
...txOverrides.properties,
34+
abi: Type.Array(abiSchema),
3335
});
3436

35-
// Adding example for Swagger File
36-
writeRequestBodySchema.examples = [
37-
{
38-
functionName: "transferFrom",
39-
args: [
40-
"0x1946267d81Fb8aDeeEa28e6B98bcD446c8248473",
41-
"0x3EcDBF3B911d0e9052b64850693888b008e18373",
42-
"0",
43-
],
44-
},
45-
];
46-
4737
// LOGIC
4838
export async function writeToContract(fastify: FastifyInstance) {
4939
fastify.route<{
@@ -71,7 +61,7 @@ export async function writeToContract(fastify: FastifyInstance) {
7161
handler: async (request, reply) => {
7262
const { chain, contractAddress } = request.params;
7363
const { simulateTx } = request.query;
74-
const { functionName, args, txOverrides } = request.body;
64+
const { functionName, args, txOverrides, abi } = request.body;
7565
const {
7666
"x-backend-wallet-address": walletAddress,
7767
"x-account-address": accountAddress,
@@ -84,6 +74,7 @@ export async function writeToContract(fastify: FastifyInstance) {
8474
contractAddress,
8575
walletAddress,
8676
accountAddress,
77+
abi,
8778
});
8879
const tx = contract.prepare(functionName, args, {
8980
value: txOverrides?.value,

src/server/schemas/contract/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ export const abiEventSchema = Type.Object({
5454

5555
export const abiSchema = Type.Object({
5656
type: Type.String(),
57-
name: Type.String(),
57+
name: Type.Optional(Type.String()),
5858
inputs: Type.Array(abiTypeSchema),
59+
stateMutability: Type.Optional(Type.String()),
5960
});
6061

6162
export const contractEventSchema = Type.Record(Type.String(), Type.Any());

src/utils/cache/getContract.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1+
import { Static, Type } from "@sinclair/typebox";
12
import { StatusCodes } from "http-status-codes";
23
import { createCustomError } from "../../server/middleware/error";
4+
import { abiSchema } from "../../server/schemas/contract";
35
import { getSdk } from "./getSdk";
46

7+
const abiArraySchema = Type.Array(abiSchema);
8+
59
interface GetContractParams {
610
chainId: number;
711
walletAddress?: string;
812
accountAddress?: string;
913
contractAddress: string;
14+
abi?: Static<typeof abiArraySchema>;
1015
}
1116

1217
export const getContract = async ({
1318
chainId,
1419
walletAddress,
1520
contractAddress,
1621
accountAddress,
22+
abi,
1723
}: GetContractParams) => {
1824
const sdk = await getSdk({ chainId, walletAddress, accountAddress });
1925

2026
try {
27+
if (abi) {
28+
return sdk.getContractFromAbi(contractAddress, abi);
29+
}
2130
// SDK already handles caching.
2231
return await sdk.getContract(contractAddress);
2332
} catch (e) {

0 commit comments

Comments
 (0)