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]]);
}

/**
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.
*
* @param _req
* @param res
*/
private dryRun: IPostRequestHandler<IBodyContractMetadata, IContractDryParams> = async (
{ params: { address }, query: { caller, payValue = '0', inputData } },
res,
): Promise<void> => {
const dryRunResult = await this.api.call.reviveApi.call(
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;
inputData: string;
}

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