Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/controllers/contracts/ContractsInkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ContractPromise } from '@polkadot/api-contract';
import { hexToU8a } from '@polkadot/util';
import { RequestHandler } from 'express';
import { BadRequest } from 'http-errors';

import { validateAddress } from '../../middleware';
import { ContractsInkService } from '../../services';
import { IBodyContractMetadata, IContractQueryParam, IPostRequestHandler } from '../../types/requests';
import {
IBodyContractMetadata,
IContractDryParams,
IContractQueryParam,
IPostRequestHandler,
} from '../../types/requests';
import AbstractController from '../AbstractController';

export default class ContractsInkController extends AbstractController<ContractsInkService> {
Expand All @@ -34,6 +40,7 @@ export default class ContractsInkController extends AbstractController<Contracts
protected initRoutes(): void {
this.router.use(this.path, validateAddress);
this.safeMountAsyncPostHandlers([['/query', this.callContractQuery as RequestHandler]]);
this.safeMountAsyncGetHandlers([['/dry-run', this.dryRun as RequestHandler]]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is returning IPostRequestHandler while being mounted as a GET handler.

Which one is it?

}

/**
Expand Down Expand Up @@ -62,4 +69,26 @@ export default class ContractsInkController extends AbstractController<Contracts
await this.service.fetchContractCall(contract, address, method, argsArray, gasLimit, storageDepositLimit),
);
};

/**
* Send a message call to a dry run contract. It defaults to get if nothing is inputted.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It defaults to get if nothing is inputted.

Personally this is a bit hard to understand on a first read through. Maybe something like

Execute a dry run contract call without requiring the full ABI

is better?

*
* @param _req
* @param res
*/
private dryRun: IPostRequestHandler<IBodyContractMetadata, IContractDryParams> = async (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we have IPostRequestHandler but it is mounted as a GET handler with

this.safeMountAsyncGetHandlers([['/dry-run', this.dryRun as RequestHandler]]);

{ params: { address }, query: { caller, payValue = '0', inputData } },
res,
): Promise<void> => {
const dryRunResult = await this.api.call.reviveApi.call(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a check if reviveApi is available as well as graceful handling if it is not.

caller,
address,
this.api.registry.createType('Balance', BigInt(payValue)),
null,
null,
hexToU8a(inputData) ?? '',
);

ContractsInkController.sanitizedSend(res, dryRunResult.toHuman());
};
}
6 changes: 6 additions & 0 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export interface IContractQueryParam extends Query {
storageDepositLimit: string;
}

export interface IContractDryParams extends Query {
caller: string;
payValue: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is meant to be optional, based on the default values that is has above.

inputData: string;
Comment on lines +74 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any of these values being validated anywhere which can lead to some bugs and poor UX / hard to debug issues.

}

export interface IPalletsConstantsParam extends ParamsDictionary {
palletId: string;
constantItemId: string;
Expand Down