Skip to content

Commit

Permalink
Merge pull request #12 from KowalewskiPawel/add-ver-0.4
Browse files Browse the repository at this point in the history
Ver. 0.4; Add tx write execution result
  • Loading branch information
KowalewskiPawel authored Apr 3, 2023
2 parents a629f91 + dae1fce commit 51ad960
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 41 deletions.
29 changes: 26 additions & 3 deletions contractAPI/contractCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { writeTransactionUrl } from "../utils/fs";

export const writeContractCall = async (
methodName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any[],
transactionId?: string
) => {
Expand All @@ -25,7 +26,7 @@ export const writeContractCall = async (

// Execute dry-run query call to fetch required gas value

const { gasRequired } = await contractApi.query[methodName](
const { gasRequired, result, output } = await contractApi.query[methodName](
accountKeypair.address,
{
gasLimit: providerApi?.registry.createType("WeightV2", {
Expand All @@ -45,14 +46,33 @@ export const writeContractCall = async (
) as WeightV2;

// actual smart contract transaction call
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
/* @ts-ignore */
const isReverted = result.asOk.toJSON().flags?.includes("Revert");

await contractApi.tx[methodName](
// check if the call was successful
if (result.isOk && isReverted) {
// output the return value
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
/* @ts-ignore */
return output?.toJSON()?.ok?.err;

// return output?.toHuman();
} else if (result.isErr) {
console.error("Error", result.asErr);

return result.asErr.toString();
}

const queryTx = await contractApi.tx[methodName](
{
gasLimit,
storageDepositLimit,
},
...args
).signAndSend(accountKeypair, async (res) => {
);

await queryTx.signAndSend(accountKeypair, async (res) => {
if (res.status.isInBlock) {
console.log("in a block");
const { number } = await contractApi.api.rpc.chain.getHeader();
Expand All @@ -64,8 +84,11 @@ export const writeContractCall = async (
console.log("finalized");
}
});

return "OK";
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const readContractCall = async (methodName: string, args: any[]) => {
// Read only query example

Expand Down
83 changes: 46 additions & 37 deletions controllers/contract.controller.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
import { randomUUID } from "crypto";
import { Request, Response } from "express";
import { readContractCall, writeContractCall } from "../contractAPI/contractCall";
import {
readContractCall,
writeContractCall,
} from "../contractAPI/contractCall";
import { getTransactionUrl } from "../utils/fs";

export const readContract = async (req: Request, res: Response) => {
try {
const { methodName, args } = req.body;
try {
const { methodName, args } = req.body;

const readResult = await readContractCall(methodName, args);
const readResult = await readContractCall(methodName, args);

return res.status(200).json({ result: readResult });
} catch (error) {
return res.status(500).json({
error: true,
message: "Something went wrong",
});
}
};

export const writeContract = async (req: Request, res: Response) => {
try {
const { methodName, args } = req.body;

const transactionId = randomUUID();
return res.status(200).json({ result: readResult });
} catch (error) {
return res.status(500).json({
error: true,
message: "Something went wrong",
});
}
};

await writeContractCall(methodName, args, transactionId);

return res.status(200).json({ transactionId });
} catch (error) {
return res.status(500).json({
error: true,
message: "Something went wrong",
});
}
};
export const writeContract = async (req: Request, res: Response) => {
try {
const { methodName, args } = req.body;

export const txUrl = async (req: Request, res: Response) => {
try {
const { txId } = req.body;
const transactionId = randomUUID();

const txUrl = getTransactionUrl(txId);
const callResult = await writeContractCall(methodName, args, transactionId);

return res.status(200).json({ transactionBlockUrl: txUrl });
} catch (error) {
if (callResult !== "OK") {
return res.status(500).json({
error: true,
message: "Something went wrong",
message: JSON.stringify(callResult) || "Something went wrong",
});
}
};


return res.status(200).json({ transactionId });
} catch (err) {
return res.status(500).json({
error: true,
message: JSON.stringify(err) || "Something went wrong",
});
}
};

export const txUrl = async (req: Request, res: Response) => {
try {
const { txId } = req.body;

const txUrl = getTransactionUrl(txId);

return res.status(200).json({ transactionBlockUrl: txUrl });
} catch (error) {
return res.status(500).json({
error: true,
message: "Something went wrong",
});
}
};
2 changes: 1 addition & 1 deletion routes/contract.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export const contractRouter = Router();

contractRouter
.get("/read", readContract)
.get("/txUrl", txUrl)
.post("/txUrl", txUrl)
.post("/write", writeContract);

0 comments on commit 51ad960

Please sign in to comment.