Skip to main content

ApprovalController Actions

17 actions registered.

NameDeprecated
acceptRequest
add
addAndShowApprovalRequest
addRequest
clearRequests
endFlow
get
getApprovalCount
getState
getTotalApprovalCount
hasRequest
rejectRequest
setFlowLoadingText
showError
showSuccess
startFlow
updateRequestState

ApprovalController:acceptRequest​

Package: @metamask/approval-controller

Resolves the promise of the approval with the given id, and deletes the approval. Throws an error if no such approval exists.

Parameters:

NameDescription
idThe id of the approval request.
valueThe value to resolve the approval promise with.
optionsOptions bag.

Returns: A promise that either resolves once a result is provided by the creator of the approval request, or immediately if options.waitForResult is false or undefined.

Handler signature:

(id: string, value?: unknown, options?: AcceptOptions) => Promise<AcceptResult>

ApprovalController:add​

Package: @metamask/approval-controller

Adds an approval request per the given arguments and returns the approval promise resolving to an AddResult object.

There can only be one approval per origin and type. An error is thrown if attempting to add an invalid or duplicate request.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval request. A random id will be generated if none is provided.
opts.originThe origin of the approval request.
opts.typeThe type associated with the approval request.
opts.requestDataAdditional data associated with the request, if any.

Returns: The approval promise resolving to an AddResult object.

Handler signature:

(opts: AddApprovalOptions & {
expectsResult: true;
}) => Promise<AddResult>

ApprovalController:addAndShowApprovalRequest​

Package: @metamask/approval-controller

Adds an approval request per the given arguments, calls the show approval request function, and returns the associated approval promise resolving to an AddResult object.

There can only be one approval per origin and type. An error is thrown if attempting to add an invalid or duplicate request.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval request. A random id will be generated if none is provided.
opts.originThe origin of the approval request.
opts.typeThe type associated with the approval request.
opts.requestDataAdditional data associated with the request,
opts.requestStateAdditional state associated with the request, if any.

Returns: The approval promise resolving to an AddResult object.

Handler signature:

(opts: AddApprovalOptions & {
expectsResult: true;
}) => Promise<AddResult>

ApprovalController:addRequest​

Package: @metamask/approval-controller

Adds an approval request per the given arguments, optionally showing the approval request to the user.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval request. A random id will be generated if none is provided.
opts.originThe origin of the approval request.
opts.typeThe type associated with the approval request.
opts.requestDataAdditional data associated with the request, if any.
opts.requestStateAdditional state associated with the request, if any.
shouldShowRequestWhether to show the approval request to the user.

Returns: The approval promise.

Handler signature:

(opts: AddApprovalOptions, shouldShowRequest: boolean) => Promise<unknown>

ApprovalController:clearRequests​

Package: @metamask/approval-controller

Rejects and deletes all approval requests.

Parameters:

NameDescription
rejectionErrorThe JsonRpcError to reject the approval requests with.

Handler signature:

(rejectionError: JsonRpcError<DataWithOptionalCause>) => void

ApprovalController:endFlow​

Package: @metamask/approval-controller

Ends the current approval flow.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval flow that will be finished.

Handler signature:

({ id }: EndFlowOptions) => void

ApprovalController:get​

Package: @metamask/approval-controller

Gets the info for the approval request with the given id.

Parameters:

NameDescription
idThe id of the approval request.

Returns: The approval request data associated with the id.

Handler signature:

(id: string) => ApprovalRequest<ApprovalRequestData> | undefined

ApprovalController:getApprovalCount​

Package: @metamask/approval-controller

Gets the number of pending approvals, by origin and/or type.

If only origin is specified, all approvals for that origin will be counted, regardless of type. If only type is specified, all approvals for that type will be counted, regardless of origin. If both origin and type are specified, 0 or 1 will be returned.

Parameters:

NameDescription
optsThe approval count options.
opts.originAn approval origin.
opts.typeThe type of the approval request.

Returns: The current approval request count for the given origin and/or type.

Handler signature:

(opts?: {
origin?: string;
type?: string;
}) => number

ApprovalController:getState​

Package: @metamask/approval-controller

Handler signature:

() => ApprovalControllerState

ApprovalController:getTotalApprovalCount​

Package: @metamask/approval-controller

Get the total count of all pending approval requests for all origins.

Returns: The total pending approval request count.

Handler signature:

() => number

ApprovalController:hasRequest​

Package: @metamask/approval-controller

Checks if there's a pending approval request per the given parameters. At least one parameter must be specified. An error will be thrown if the parameters are invalid.

If id is specified, all other parameters will be ignored. If id is not specified, the method will check for requests that match all of the specified parameters.

Parameters:

NameDescription
optsOptions bag.
opts.idThe ID to check for.
opts.originThe origin to check for.
opts.typeThe type to check for.

Returns: true if a matching approval is found, and false otherwise.

Handler signature:

(opts?: {
id?: string;
origin?: string;
type?: string;
}) => boolean

ApprovalController:rejectRequest​

Package: @metamask/approval-controller

Rejects the promise of the approval with the given id, and deletes the approval. Throws an error if no such approval exists.

Parameters:

NameDescription
idThe id of the approval request.
errorThe error to reject the approval promise with.

Handler signature:

(id: string, error: unknown) => void

ApprovalController:setFlowLoadingText​

Package: @metamask/approval-controller

Sets the loading text for the approval flow.

Parameters:

NameDescription
optsOptions bag.
opts.idThe approval flow loading text that will be displayed.
opts.loadingTextThe loading text that will be associated to the approval flow.

Handler signature:

({ id, loadingText }: SetFlowLoadingTextOptions) => void

ApprovalController:showError​

Package: @metamask/approval-controller

Show an error page.

Parameters:

NameDescription
optsOptions bag.
opts.messageThe message text or components to display in the page.
opts.headerThe text or components to display in the header of the page.
opts.flowToEndThe ID of the approval flow to end once the error page is approved.
opts.titleThe title to display above the message. Shown by default but can be hidden with null.
opts.iconThe icon to display in the page. Shown by default but can be hidden with null.

Returns: Empty object to support future additions.

Handler signature:

(opts?: ErrorOptions) => Promise<ErrorResult>

ApprovalController:showSuccess​

Package: @metamask/approval-controller

Show a success page.

Parameters:

NameDescription
optsOptions bag.
opts.messageThe message text or components to display in the page.
opts.headerThe text or components to display in the header of the page.
opts.flowToEndThe ID of the approval flow to end once the success page is approved.
opts.titleThe title to display above the message. Shown by default but can be hidden with null.
opts.iconThe icon to display in the page. Shown by default but can be hidden with null.

Returns: Empty object to support future additions.

Handler signature:

(opts?: SuccessOptions) => Promise<SuccessResult>

ApprovalController:startFlow​

Package: @metamask/approval-controller

Starts a new approval flow.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval flow.
opts.loadingTextThe loading text that will be associated to the approval flow.
opts.showA flag to determine whether the approval should show to the user.

Returns: The object containing the approval flow id.

Handler signature:

(opts?: StartFlowOptions) => ApprovalFlowStartResult

ApprovalController:updateRequestState​

Package: @metamask/approval-controller

Updates the request state of the approval with the given id.

Parameters:

NameDescription
optsOptions bag.
opts.idThe id of the approval request.
opts.requestStateAdditional data associated with the request

Handler signature:

(opts: UpdateRequestStateOptions) => void