diff --git a/packages/trails-api/src/api.gen.ts b/packages/trails-api/src/api.gen.ts new file mode 100644 index 000000000..12866ef4a --- /dev/null +++ b/packages/trails-api/src/api.gen.ts @@ -0,0 +1,1341 @@ +/* eslint-disable */ +// trails-api v0.4.0 28a9163b0bcbdf5cecfc8f2b668550cc1ccadaf3 +// -- +// Code generated by webrpc-gen@v0.25.3 with typescript generator. DO NOT EDIT. +// +// webrpc-gen -schema=api.ridl -target=typescript -client -out=./clients/api.gen.ts + +export const WebrpcHeader = 'Webrpc' + +export const WebrpcHeaderValue = 'webrpc@v0.25.3;gen-typescript@v0.17.0;trails-api@v0.4.0' + +// WebRPC description and code-gen version +export const WebRPCVersion = 'v1' + +// Schema version of your RIDL schema +export const WebRPCSchemaVersion = 'v0.4.0' + +// Schema hash generated from your RIDL schema +export const WebRPCSchemaHash = '28a9163b0bcbdf5cecfc8f2b668550cc1ccadaf3' + +type WebrpcGenVersions = { + webrpcGenVersion: string + codeGenName: string + codeGenVersion: string + schemaName: string + schemaVersion: string +} + +export function VersionFromHeader(headers: Headers): WebrpcGenVersions { + const headerValue = headers.get(WebrpcHeader) + if (!headerValue) { + return { + webrpcGenVersion: '', + codeGenName: '', + codeGenVersion: '', + schemaName: '', + schemaVersion: '' + } + } + + return parseWebrpcGenVersions(headerValue) +} + +function parseWebrpcGenVersions(header: string): WebrpcGenVersions { + const versions = header.split(';') + if (versions.length < 3) { + return { + webrpcGenVersion: '', + codeGenName: '', + codeGenVersion: '', + schemaName: '', + schemaVersion: '' + } + } + + const [_, webrpcGenVersion] = versions[0]!.split('@') + const [codeGenName, codeGenVersion] = versions[1]!.split('@') + const [schemaName, schemaVersion] = versions[2]!.split('@') + + return { + webrpcGenVersion: webrpcGenVersion ?? '', + codeGenName: codeGenName ?? '', + codeGenVersion: codeGenVersion ?? '', + schemaName: schemaName ?? '', + schemaVersion: schemaVersion ?? '' + } +} + +// +// Types +// + +export enum TradeType { + EXACT_INPUT = 'EXACT_INPUT', + EXACT_OUTPUT = 'EXACT_OUTPUT' +} + +export enum SortOrder { + DESC = 'DESC', + ASC = 'ASC' +} + +export interface Version { + webrpcVersion: string + schemaVersion: string + schemaHash: string + appVersion: string +} + +export interface RuntimeStatus { + healthOK: boolean + startTime: string + uptime: number + ver: string + branch: string + commitHash: string + checks: RuntimeChecks + numTxnsRelayed: { [key: string]: NumTxnsRelayed } +} + +export interface NumTxnsRelayed { + chainID: number + prev: number + current: number + period: number +} + +export interface RuntimeChecks {} + +export interface MetaTxn { + id: string + chainId: string + walletAddress: string + contract: string + input: string +} + +export interface Call { + to: string + value?: string + data?: string + gasLimit?: string + delegateCall?: boolean + onlyFallback?: boolean + behaviorOnError?: number +} + +export interface IntentCallsPayload { + chainId: string + space?: string + nonce?: string + calls: Array +} + +export interface IntentConfig { + id: number + configHash: string + originIntentAddress: string + destinationIntentAddress: string + mainSigner: string + calls: Array + preconditions: Array + executionStatus?: string + metaTxnId?: string + txnHash?: string + updatedAt?: string + createdAt?: string +} + +export interface AddressOverrides { + trailsLiFiSapientSignerAddress?: string + trailsRelaySapientSignerAddress?: string + trailsCCTPV2SapientSignerAddress?: string +} + +export interface TakerFee { + address: string + bps: number +} + +export interface IntentPrecondition { + type: string + chainId: string + data: any +} + +export interface Token { + chainId: number + contractAddress: string + tokenId?: string +} + +export interface Price { + value: number + currency: string +} + +export interface TokenPrice { + token: Token + price?: Price + price24hChange?: Price + price24hVol?: Price + floorPrice: Price + buyPrice: Price + sellPrice: Price + updatedAt: string +} + +export interface ExchangeRate { + name: string + symbol: string + value: number + vsCurrency: string + currencyType: string +} + +export interface Page { + pageSize?: number + page?: number + totalRecords?: number + column?: string + before?: any + after?: any + sort?: Array + more?: boolean +} + +export interface SortBy { + column: string + order: SortOrder +} + +export interface CCTPTransfer { + id: string + sourceTxHash: string + sourceChainId: number + destinationChainId: number + message: string + attestation: string + status: string + createdAt: string + updatedAt: string +} + +export interface CrossChainFee { + providerFee: string + trailsSwapFee: string + providerFeeUSD: number + trailsSwapFeeUSD: number + totalFeeAmount: string + totalFeeUSD: number +} + +export interface MetaTxnFeeDetail { + metaTxnID: string + estimatedGasLimit: string + feeNative: string +} + +export interface ChainExecuteQuote { + chainId: string + totalGasLimit: string + gasPrice: string + totalFeeAmount: string + nativeTokenSymbol: string + nativeTokenPrice?: string + metaTxnFeeDetails: Array + totalFeeUSD?: string +} + +export interface ExecuteQuote { + chainQuotes: Array +} + +export interface TrailsFee { + executeQuote: ExecuteQuote + crossChainFee?: CrossChainFee + takerFeeAmount?: string + takerFeeUSD?: number + trailsFixedFeeUSD: number + feeToken?: string + originTokenTotalAmount?: string + totalFeeAmount?: string + totalFeeUSD?: string + quoteProvider?: string +} + +export interface IntentQuote { + fromAmount: string + fromAmountMin: string + toAmount: string + toAmountMin: string + priceImpact: number + priceImpactUsd: string + maxSlippage: number + quoteProvider: string + quoteProviderRequestId: string + quoteProviderFeeUsd: string + feeQuotes: { [key: string]: string } +} + +export interface API { + /** + * + * Runtime + * + */ + ping(headers?: object, signal?: AbortSignal): Promise + version(headers?: object, signal?: AbortSignal): Promise + runtimeStatus(headers?: object, signal?: AbortSignal): Promise + clock(headers?: object, signal?: AbortSignal): Promise + /** + * + * Chain abstraction + * + */ + getIntentCallsPayloads( + args: GetIntentCallsPayloadsArgs, + headers?: object, + signal?: AbortSignal + ): Promise + commitIntentConfig(args: CommitIntentConfigArgs, headers?: object, signal?: AbortSignal): Promise + getIntentConfig(args: GetIntentConfigArgs, headers?: object, signal?: AbortSignal): Promise + /** + * + * CCTP + * + */ + getCCTPTransfer(args: GetCCTPTransferArgs, headers?: object, signal?: AbortSignal): Promise + queueCCTPTransfer(args: QueueCCTPTransferArgs, headers?: object, signal?: AbortSignal): Promise + /** + * + * Intent Machine Worker + * + */ + queueIntentConfigExecution( + args: QueueIntentConfigExecutionArgs, + headers?: object, + signal?: AbortSignal + ): Promise + getIntentConfigExecutionStatus( + args: GetIntentConfigExecutionStatusArgs, + headers?: object, + signal?: AbortSignal + ): Promise + listIntentConfigs(args: ListIntentConfigsArgs, headers?: object, signal?: AbortSignal): Promise + queueMetaTxnReceipt(args: QueueMetaTxnReceiptArgs, headers?: object, signal?: AbortSignal): Promise +} + +export interface PingArgs {} + +export interface PingReturn { + status: boolean +} +export interface VersionArgs {} + +export interface VersionReturn { + version: Version +} +export interface RuntimeStatusArgs {} + +export interface RuntimeStatusReturn { + status: RuntimeStatus +} +export interface ClockArgs {} + +export interface ClockReturn { + serverTime: string +} +export interface GetIntentCallsPayloadsArgs { + userAddress: string + destinationChainId: number + destinationTokenAddress: string + destinationTokenAmount: string + destinationToAddress: string + originChainId: number + originTokenAddress: string + originTokenAmount: string + destinationCallData?: string + destinationCallValue?: string + provider?: string + addressOverrides?: AddressOverrides + destinationSalt?: string + takerFee?: TakerFee + slippageTolerance?: number + tradeType?: TradeType +} + +export interface GetIntentCallsPayloadsReturn { + calls: Array + preconditions: Array + metaTxns: Array + trailsFee: TrailsFee + quote: IntentQuote + feeQuotes: { [key: string]: string } + originIntentAddress: string + destinationIntentAddress: string +} +export interface CommitIntentConfigArgs { + originIntentAddress: string + destinationIntentAddress: string + mainSigner: string + calls: Array + preconditions: Array + addressOverrides?: AddressOverrides +} + +export interface CommitIntentConfigReturn { + config: IntentConfig +} +export interface GetIntentConfigArgs { + intentAddress: string +} + +export interface GetIntentConfigReturn { + config: IntentConfig +} +export interface GetCCTPTransferArgs { + id: string +} + +export interface GetCCTPTransferReturn { + transfer: CCTPTransfer +} +export interface QueueCCTPTransferArgs { + sourceTxHash?: string + metaTxHash?: string + sourceChainId: number + destinationChainId: number +} + +export interface QueueCCTPTransferReturn { + transfer: CCTPTransfer +} +export interface QueueIntentConfigExecutionArgs { + intentConfigId: number +} + +export interface QueueIntentConfigExecutionReturn { + status: boolean +} +export interface GetIntentConfigExecutionStatusArgs { + intentConfigId: number +} + +export interface GetIntentConfigExecutionStatusReturn { + executionStatus: string +} +export interface ListIntentConfigsArgs { + page?: Page + executionStatus?: string +} + +export interface ListIntentConfigsReturn { + page: Page + intentConfigs: Array +} +export interface QueueMetaTxnReceiptArgs { + metaTxID: string +} + +export interface QueueMetaTxnReceiptReturn { + status: boolean +} + +// +// Client +// +export class API implements API { + protected hostname: string + protected fetch: Fetch + protected path = '/rpc/API/' + + constructor(hostname: string, fetch: Fetch) { + this.hostname = hostname.replace(/\/*$/, '') + this.fetch = (input: RequestInfo, init?: RequestInit) => fetch(input, init) + } + + private url(name: string): string { + return this.hostname + this.path + name + } + + ping = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Ping'), createHTTPRequest({}, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + status: _data.status + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + version = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Version'), createHTTPRequest({}, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + version: _data.version + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + runtimeStatus = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + status: _data.status + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + clock = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Clock'), createHTTPRequest({}, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + serverTime: _data.serverTime + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + getIntentCallsPayloads = ( + args: GetIntentCallsPayloadsArgs, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch(this.url('GetIntentCallsPayloads'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + calls: >_data.calls, + preconditions: >_data.preconditions, + metaTxns: >_data.metaTxns, + trailsFee: _data.trailsFee, + quote: _data.quote, + feeQuotes: <{ [key: string]: string }>_data.feeQuotes, + originIntentAddress: _data.originIntentAddress, + destinationIntentAddress: _data.destinationIntentAddress + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + commitIntentConfig = ( + args: CommitIntentConfigArgs, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch(this.url('CommitIntentConfig'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + config: _data.config + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + getIntentConfig = (args: GetIntentConfigArgs, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('GetIntentConfig'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + config: _data.config + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + getCCTPTransfer = (args: GetCCTPTransferArgs, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('GetCCTPTransfer'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + transfer: _data.transfer + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + queueCCTPTransfer = (args: QueueCCTPTransferArgs, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('QueueCCTPTransfer'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + transfer: _data.transfer + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + queueIntentConfigExecution = ( + args: QueueIntentConfigExecutionArgs, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch(this.url('QueueIntentConfigExecution'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + status: _data.status + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + getIntentConfigExecutionStatus = ( + args: GetIntentConfigExecutionStatusArgs, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch(this.url('GetIntentConfigExecutionStatus'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + executionStatus: _data.executionStatus + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + listIntentConfigs = (args: ListIntentConfigsArgs, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('ListIntentConfigs'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + page: _data.page, + intentConfigs: >_data.intentConfigs + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } + + queueMetaTxnReceipt = ( + args: QueueMetaTxnReceiptArgs, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch(this.url('QueueMetaTxnReceipt'), createHTTPRequest(args, headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return { + status: _data.status + } + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` }) + } + ) + } +} + +const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => { + const reqHeaders: { [key: string]: string } = { ...headers, 'Content-Type': 'application/json' } + reqHeaders[WebrpcHeader] = WebrpcHeaderValue + + return { + method: 'POST', + headers: reqHeaders, + body: JSON.stringify(body || {}), + signal + } +} + +const buildResponse = (res: Response): Promise => { + return res.text().then(text => { + let data + try { + data = JSON.parse(text) + } catch (error) { + let message = '' + if (error instanceof Error) { + message = error.message + } + throw WebrpcBadResponseError.new({ + status: res.status, + cause: `JSON.parse(): ${message}: response text: ${text}` + }) + } + if (!res.ok) { + const code: number = typeof data.code === 'number' ? data.code : 0 + throw (webrpcErrorByCode[code] || WebrpcError).new(data) + } + return data + }) +} + +// +// Errors +// + +export class WebrpcError extends Error { + name: string + code: number + message: string + status: number + cause?: string + + /** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */ + msg: string + + constructor(name: string, code: number, message: string, status: number, cause?: string) { + super(message) + this.name = name || 'WebrpcError' + this.code = typeof code === 'number' ? code : 0 + this.message = message || `endpoint error ${this.code}` + this.msg = this.message + this.status = typeof status === 'number' ? status : 0 + this.cause = cause + Object.setPrototypeOf(this, WebrpcError.prototype) + } + + static new(payload: any): WebrpcError { + return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause) + } +} + +// Webrpc errors + +export class WebrpcEndpointError extends WebrpcError { + constructor( + name: string = 'WebrpcEndpoint', + code: number = 0, + message: string = `endpoint error`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcEndpointError.prototype) + } +} + +export class WebrpcRequestFailedError extends WebrpcError { + constructor( + name: string = 'WebrpcRequestFailed', + code: number = -1, + message: string = `request failed`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype) + } +} + +export class WebrpcBadRouteError extends WebrpcError { + constructor( + name: string = 'WebrpcBadRoute', + code: number = -2, + message: string = `bad route`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcBadRouteError.prototype) + } +} + +export class WebrpcBadMethodError extends WebrpcError { + constructor( + name: string = 'WebrpcBadMethod', + code: number = -3, + message: string = `bad method`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcBadMethodError.prototype) + } +} + +export class WebrpcBadRequestError extends WebrpcError { + constructor( + name: string = 'WebrpcBadRequest', + code: number = -4, + message: string = `bad request`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcBadRequestError.prototype) + } +} + +export class WebrpcBadResponseError extends WebrpcError { + constructor( + name: string = 'WebrpcBadResponse', + code: number = -5, + message: string = `bad response`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcBadResponseError.prototype) + } +} + +export class WebrpcServerPanicError extends WebrpcError { + constructor( + name: string = 'WebrpcServerPanic', + code: number = -6, + message: string = `server panic`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcServerPanicError.prototype) + } +} + +export class WebrpcInternalErrorError extends WebrpcError { + constructor( + name: string = 'WebrpcInternalError', + code: number = -7, + message: string = `internal error`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype) + } +} + +export class WebrpcClientDisconnectedError extends WebrpcError { + constructor( + name: string = 'WebrpcClientDisconnected', + code: number = -8, + message: string = `client disconnected`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype) + } +} + +export class WebrpcStreamLostError extends WebrpcError { + constructor( + name: string = 'WebrpcStreamLost', + code: number = -9, + message: string = `stream lost`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcStreamLostError.prototype) + } +} + +export class WebrpcStreamFinishedError extends WebrpcError { + constructor( + name: string = 'WebrpcStreamFinished', + code: number = -10, + message: string = `stream finished`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype) + } +} + +// Schema errors + +export class UnauthorizedError extends WebrpcError { + constructor( + name: string = 'Unauthorized', + code: number = 1000, + message: string = `Unauthorized access`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, UnauthorizedError.prototype) + } +} + +export class PermissionDeniedError extends WebrpcError { + constructor( + name: string = 'PermissionDenied', + code: number = 1001, + message: string = `Permission denied`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, PermissionDeniedError.prototype) + } +} + +export class SessionExpiredError extends WebrpcError { + constructor( + name: string = 'SessionExpired', + code: number = 1002, + message: string = `Session expired`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, SessionExpiredError.prototype) + } +} + +export class MethodNotFoundError extends WebrpcError { + constructor( + name: string = 'MethodNotFound', + code: number = 1003, + message: string = `Method not found`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, MethodNotFoundError.prototype) + } +} + +export class RequestConflictError extends WebrpcError { + constructor( + name: string = 'RequestConflict', + code: number = 1004, + message: string = `Conflict with target resource`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, RequestConflictError.prototype) + } +} + +export class AbortedError extends WebrpcError { + constructor( + name: string = 'Aborted', + code: number = 1005, + message: string = `Request aborted`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, AbortedError.prototype) + } +} + +export class GeoblockedError extends WebrpcError { + constructor( + name: string = 'Geoblocked', + code: number = 1006, + message: string = `Geoblocked region`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, GeoblockedError.prototype) + } +} + +export class RateLimitedError extends WebrpcError { + constructor( + name: string = 'RateLimited', + code: number = 1007, + message: string = `Rate-limited. Please slow down.`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, RateLimitedError.prototype) + } +} + +export class ProjectNotFoundError extends WebrpcError { + constructor( + name: string = 'ProjectNotFound', + code: number = 1008, + message: string = `Project not found`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, ProjectNotFoundError.prototype) + } +} + +export class AccessKeyNotFoundError extends WebrpcError { + constructor( + name: string = 'AccessKeyNotFound', + code: number = 1101, + message: string = `Access key not found`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, AccessKeyNotFoundError.prototype) + } +} + +export class AccessKeyMismatchError extends WebrpcError { + constructor( + name: string = 'AccessKeyMismatch', + code: number = 1102, + message: string = `Access key mismatch`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, AccessKeyMismatchError.prototype) + } +} + +export class InvalidOriginError extends WebrpcError { + constructor( + name: string = 'InvalidOrigin', + code: number = 1103, + message: string = `Invalid origin for Access Key`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, InvalidOriginError.prototype) + } +} + +export class InvalidServiceError extends WebrpcError { + constructor( + name: string = 'InvalidService', + code: number = 1104, + message: string = `Service not enabled for Access key`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, InvalidServiceError.prototype) + } +} + +export class UnauthorizedUserError extends WebrpcError { + constructor( + name: string = 'UnauthorizedUser', + code: number = 1105, + message: string = `Unauthorized user`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, UnauthorizedUserError.prototype) + } +} + +export class QuotaExceededError extends WebrpcError { + constructor( + name: string = 'QuotaExceeded', + code: number = 1200, + message: string = `Quota request exceeded`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, QuotaExceededError.prototype) + } +} + +export class QuotaRateLimitError extends WebrpcError { + constructor( + name: string = 'QuotaRateLimit', + code: number = 1201, + message: string = `Quota rate limit exceeded`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, QuotaRateLimitError.prototype) + } +} + +export class NoDefaultKeyError extends WebrpcError { + constructor( + name: string = 'NoDefaultKey', + code: number = 1300, + message: string = `No default access key found`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, NoDefaultKeyError.prototype) + } +} + +export class MaxAccessKeysError extends WebrpcError { + constructor( + name: string = 'MaxAccessKeys', + code: number = 1301, + message: string = `Access keys limit reached`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, MaxAccessKeysError.prototype) + } +} + +export class AtLeastOneKeyError extends WebrpcError { + constructor( + name: string = 'AtLeastOneKey', + code: number = 1302, + message: string = `You need at least one Access Key`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, AtLeastOneKeyError.prototype) + } +} + +export class TimeoutError extends WebrpcError { + constructor( + name: string = 'Timeout', + code: number = 1900, + message: string = `Request timed out`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, TimeoutError.prototype) + } +} + +export class InvalidArgumentError extends WebrpcError { + constructor( + name: string = 'InvalidArgument', + code: number = 2000, + message: string = `Invalid argument`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, InvalidArgumentError.prototype) + } +} + +export class UnavailableError extends WebrpcError { + constructor( + name: string = 'Unavailable', + code: number = 2002, + message: string = `Unavailable resource`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, UnavailableError.prototype) + } +} + +export class QueryFailedError extends WebrpcError { + constructor( + name: string = 'QueryFailed', + code: number = 2003, + message: string = `Query failed`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, QueryFailedError.prototype) + } +} + +export class NotFoundError extends WebrpcError { + constructor( + name: string = 'NotFound', + code: number = 8000, + message: string = `Resource not found`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, NotFoundError.prototype) + } +} + +export class UnsupportedNetworkError extends WebrpcError { + constructor( + name: string = 'UnsupportedNetwork', + code: number = 8008, + message: string = `Unsupported network`, + status: number = 0, + cause?: string + ) { + super(name, code, message, status, cause) + Object.setPrototypeOf(this, UnsupportedNetworkError.prototype) + } +} + +export enum errors { + WebrpcEndpoint = 'WebrpcEndpoint', + WebrpcRequestFailed = 'WebrpcRequestFailed', + WebrpcBadRoute = 'WebrpcBadRoute', + WebrpcBadMethod = 'WebrpcBadMethod', + WebrpcBadRequest = 'WebrpcBadRequest', + WebrpcBadResponse = 'WebrpcBadResponse', + WebrpcServerPanic = 'WebrpcServerPanic', + WebrpcInternalError = 'WebrpcInternalError', + WebrpcClientDisconnected = 'WebrpcClientDisconnected', + WebrpcStreamLost = 'WebrpcStreamLost', + WebrpcStreamFinished = 'WebrpcStreamFinished', + Unauthorized = 'Unauthorized', + PermissionDenied = 'PermissionDenied', + SessionExpired = 'SessionExpired', + MethodNotFound = 'MethodNotFound', + RequestConflict = 'RequestConflict', + Aborted = 'Aborted', + Geoblocked = 'Geoblocked', + RateLimited = 'RateLimited', + ProjectNotFound = 'ProjectNotFound', + AccessKeyNotFound = 'AccessKeyNotFound', + AccessKeyMismatch = 'AccessKeyMismatch', + InvalidOrigin = 'InvalidOrigin', + InvalidService = 'InvalidService', + UnauthorizedUser = 'UnauthorizedUser', + QuotaExceeded = 'QuotaExceeded', + QuotaRateLimit = 'QuotaRateLimit', + NoDefaultKey = 'NoDefaultKey', + MaxAccessKeys = 'MaxAccessKeys', + AtLeastOneKey = 'AtLeastOneKey', + Timeout = 'Timeout', + InvalidArgument = 'InvalidArgument', + Unavailable = 'Unavailable', + QueryFailed = 'QueryFailed', + NotFound = 'NotFound', + UnsupportedNetwork = 'UnsupportedNetwork' +} + +export enum WebrpcErrorCodes { + WebrpcEndpoint = 0, + WebrpcRequestFailed = -1, + WebrpcBadRoute = -2, + WebrpcBadMethod = -3, + WebrpcBadRequest = -4, + WebrpcBadResponse = -5, + WebrpcServerPanic = -6, + WebrpcInternalError = -7, + WebrpcClientDisconnected = -8, + WebrpcStreamLost = -9, + WebrpcStreamFinished = -10, + Unauthorized = 1000, + PermissionDenied = 1001, + SessionExpired = 1002, + MethodNotFound = 1003, + RequestConflict = 1004, + Aborted = 1005, + Geoblocked = 1006, + RateLimited = 1007, + ProjectNotFound = 1008, + AccessKeyNotFound = 1101, + AccessKeyMismatch = 1102, + InvalidOrigin = 1103, + InvalidService = 1104, + UnauthorizedUser = 1105, + QuotaExceeded = 1200, + QuotaRateLimit = 1201, + NoDefaultKey = 1300, + MaxAccessKeys = 1301, + AtLeastOneKey = 1302, + Timeout = 1900, + InvalidArgument = 2000, + Unavailable = 2002, + QueryFailed = 2003, + NotFound = 8000, + UnsupportedNetwork = 8008 +} + +export const webrpcErrorByCode: { [code: number]: any } = { + [0]: WebrpcEndpointError, + [-1]: WebrpcRequestFailedError, + [-2]: WebrpcBadRouteError, + [-3]: WebrpcBadMethodError, + [-4]: WebrpcBadRequestError, + [-5]: WebrpcBadResponseError, + [-6]: WebrpcServerPanicError, + [-7]: WebrpcInternalErrorError, + [-8]: WebrpcClientDisconnectedError, + [-9]: WebrpcStreamLostError, + [-10]: WebrpcStreamFinishedError, + [1000]: UnauthorizedError, + [1001]: PermissionDeniedError, + [1002]: SessionExpiredError, + [1003]: MethodNotFoundError, + [1004]: RequestConflictError, + [1005]: AbortedError, + [1006]: GeoblockedError, + [1007]: RateLimitedError, + [1008]: ProjectNotFoundError, + [1101]: AccessKeyNotFoundError, + [1102]: AccessKeyMismatchError, + [1103]: InvalidOriginError, + [1104]: InvalidServiceError, + [1105]: UnauthorizedUserError, + [1200]: QuotaExceededError, + [1201]: QuotaRateLimitError, + [1300]: NoDefaultKeyError, + [1301]: MaxAccessKeysError, + [1302]: AtLeastOneKeyError, + [1900]: TimeoutError, + [2000]: InvalidArgumentError, + [2002]: UnavailableError, + [2003]: QueryFailedError, + [8000]: NotFoundError, + [8008]: UnsupportedNetworkError +} + +export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise diff --git a/packages/trails-api/src/trails-api.gen.ts b/packages/trails-api/src/trails-api.gen.ts new file mode 100644 index 000000000..3067874d3 --- /dev/null +++ b/packages/trails-api/src/trails-api.gen.ts @@ -0,0 +1,1584 @@ +/* eslint-disable */ +// trails-api v1.0.0 d77c84a0d98b34c7e6d3a9810cc8819aee40b978 +// -- +// Code generated by Webrpc-gen@v0.30.2 with typescript generator. DO NOT EDIT. +// +// webrpc-gen -schema=trails-api.ridl -target=typescript -client -out=./clients/trails-api.gen.ts + +// Webrpc description and code-gen version +export const WebrpcVersion = 'v1' + +// Schema version of your RIDL schema +export const WebrpcSchemaVersion = 'v1.0.0' + +// Schema hash generated from your RIDL schema +export const WebrpcSchemaHash = 'd77c84a0d98b34c7e6d3a9810cc8819aee40b978' + +// +// Client interface +// + +export interface TrailsClient { + ping(headers?: object, signal?: AbortSignal): Promise + + version(headers?: object, signal?: AbortSignal): Promise + + runtimeStatus(headers?: object, signal?: AbortSignal): Promise + + clock(headers?: object, signal?: AbortSignal): Promise + + quoteIntent(req: QuoteIntentRequest, headers?: object, signal?: AbortSignal): Promise + + commitIntent(req: CommitIntentRequest, headers?: object, signal?: AbortSignal): Promise + + executeIntent(req: ExecuteIntentRequest, headers?: object, signal?: AbortSignal): Promise + + waitIntentReceipt(req: WaitIntentReceiptRequest, headers?: object, signal?: AbortSignal): Promise + + getIntentReceipt(req: GetIntentReceiptRequest, headers?: object, signal?: AbortSignal): Promise + + getIntent(req: GetIntentRequest, headers?: object, signal?: AbortSignal): Promise + + searchIntents(req: SearchIntentsRequest, headers?: object, signal?: AbortSignal): Promise + + getIntentTransactionHistory( + req: GetIntentTransactionHistoryRequest, + headers?: object, + signal?: AbortSignal + ): Promise + + getIntentGasFeeOptions( + req: GetIntentGasFeeOptionsRequest, + headers?: object, + signal?: AbortSignal + ): Promise + + getIntentEntryTransaction( + req: GetIntentEntryTransactionRequest, + headers?: object, + signal?: AbortSignal + ): Promise +} + +// +// Schema types +// + +export enum TradeType { + EXACT_INPUT = 'EXACT_INPUT', + EXACT_OUTPUT = 'EXACT_OUTPUT' +} + +export enum QuoteProviderType { + RELAY = 'RELAY', + CCTPV2 = 'CCTPV2', + LIFI = 'LIFI' +} + +export enum TransactionStatus { + PENDING = 'PENDING', + PROCESSING = 'PROCESSING', + FAILED = 'FAILED', + SUCCEEDED = 'SUCCEEDED' +} + +export enum IntentStatus { + SOLVED = 'SOLVED', + COMMITTED = 'COMMITTED', + EXECUTING = 'EXECUTING', + FAILED = 'FAILED', + SUCCEEDED = 'SUCCEEDED' +} + +export enum ChainGasUsageStatus { + NORMAL = 'NORMAL', + BUSY = 'BUSY', + VERY_BUSY = 'VERY_BUSY' +} + +export interface QuoteIntentRequest { + ownerAddress: string + originChainId: number + originTokenAddress: string + originTokenAmount?: bigint + destinationChainId: number + destinationTokenAddress: string + destinationTokenAmount: bigint + destinationToAddress: string + destinationCallData?: string + destinationCallValue?: string + tradeType?: TradeType + options?: QuoteIntentRequestOptions +} + +export interface QuoteIntentRequestOptions { + quoteProvider?: QuoteProviderType + slippageTolerance?: number + trailsAddressOverrides?: TrailsAddressOverrides +} + +export interface Intent { + intentId: string + status: IntentStatus + quoteRequest: QuoteIntentRequest + ownerAddress: string + originIntentAddress: string + destinationIntentAddress: string + destinationSalt: bigint + calls: Array + preconditions: Array + metaTxns: Array + quote: IntentProviderQuote + fees: IntentFees + executedAt?: string + expiresAt: string + updatedAt?: string + createdAt?: string +} + +export interface IntentCalls { + chainId: number + space?: bigint + nonce?: bigint + calls: Array +} + +export interface TransactionCall { + to: string + value?: bigint + data?: string + gasLimit?: bigint + delegateCall?: boolean + onlyFallback?: boolean + behaviorOnError?: number +} + +export interface IntentPrecondition { + type: string + chainId: number + data: IntentPreconditionData +} + +export interface IntentPreconditionData { + address: string + token: string + min: number +} + +export interface MetaTxn { + id: string + chainId: number + walletAddress: string + contract: string + input: string +} + +export interface IntentReceipt { + intentId: string + status: IntentStatus + ownerAddress: string + originChainId: number + destinationChainId: number + transferTransactionHash: string + transferTransactionStatus: TransactionStatus + originTransaction: IntentTransaction + destinationTransaction: IntentTransaction + processedAt?: string + retryCount?: number + updatedAt?: string + createdAt?: string +} + +export interface IntentTransaction { + intentId: string + chainId: number + metaTxnId: string + txnHash?: string + toAddress: string + tokenAddress: string + tokenAmount: bigint + txnStatus: TransactionStatus +} + +export interface IntentProviderQuote { + quoteProvider: string + quoteProviderRequestId: string + quoteProviderFeeUsd: number + fromAmount: bigint + fromAmountMin: bigint + toAmount: bigint + toAmountMin: bigint + maxSlippage: number + priceImpact: number + priceImpactUsd: number +} + +export interface IntentFees { + originGas: IntentTransactionGasFee + destinationGas: IntentTransactionGasFee + provider: IntentProviderFees + feeTokenAddress: string + feeTokenTotal: bigint + totalFeeAmount: bigint + totalFeeUsd: number +} + +export interface IntentProviderFees { + quoteProvider: string + quoteProviderFee: bigint + quoteProviderFeeUsd: number + trailsFee: bigint + trailsFeeUsd: number + totalFeeAmount: bigint + totalFeeUsd: number +} + +export interface IntentTransactionGasFee { + chainId: number + totalGasLimit: bigint + gasPrice: bigint + nativeTokenSymbol: string + nativeTokenPriceUsd?: number + chainGasUsageStatus: ChainGasUsageStatus + totalFeeAmount: bigint + totalFeeUsd: string + metaTxnFeeDetails: Array + metaTxnGasQuote: string +} + +export interface MetaTxnFeeDetail { + metaTxnId: string + estimatedGasLimit: bigint + feeNative: bigint +} + +export interface TokenPrice { + token: Token + price?: Price + price24hChange?: Price + price24hVol?: Price + floorPrice: Price + buyPrice: Price + sellPrice: Price + updatedAt: string +} + +export interface Token { + chainId: number + tokenAddress: string + tokenId?: string +} + +export interface Price { + value: number + currency: string +} + +export interface CCTPTransfer { + id: string + sourceTxHash: string + sourceChainId: number + destinationChainId: number + message: string + attestation: string + status: string + createdAt: string + updatedAt: string +} + +export interface GetIntentEntrypointDepositParams { + userAddress: string + tokenAddress: string + amount: bigint + intentAddress: string + chainID: number + deadline: number + intentSignature: string + usePermit?: boolean + permitAmount?: bigint + permitDeadline?: number + permitSignature?: string + feeAmount?: bigint +} + +export interface GetIntentEntrypointDepositResult { + depositWalletAddress: string + metaTxn: MetaTxn + feeQuote: string + entrypointAddress: string +} + +export interface GetIntentEntrypointDepositFeeOptionsParams { + userAddress: string + tokenAddress: string + intentAddress: string + amount: bigint + chainID: number +} + +export interface GetIntentEntrypointDepositFeeOptionsResult { + gasEstimate: GasEstimate + feeOptions: Array + expiresAt: number + feeCollector: string +} + +export interface GasEstimate { + totalGas: number + gasPrice: string + nativeCost: string + nativeCostUsd: number +} + +export interface FeeOption { + tokenAddress: string + tokenSymbol: string + tokenDecimals: number + amount: string + amountUsd: number + feeCollector: string +} + +export interface TrailsAddressOverrides { + sequenceWalletFactoryAddress?: string + sequenceWalletMainModuleAddress?: string + sequenceWalletMainModuleUpgradableAddress?: string + sequenceWalletGuestModuleAddress?: string + sequenceWalletUtilsAddress?: string +} + +export enum SortOrder { + DESC = 'DESC', + ASC = 'ASC' +} + +export interface Version { + webrpcVersion: string + schemaVersion: string + schemaHash: string + appVersion: string +} + +export interface RuntimeStatus { + healthOK: boolean + startTime: string + uptime: number + ver: string + branch: string + commitHash: string + runnables: any +} + +export interface QuoteIntentResponse { + intent: Intent +} + +export interface CommitIntentRequest { + intent: Intent +} + +export interface CommitIntentResponse { + intentId: string +} + +export interface ExecuteIntentRequest { + intentId: string + transferTransactionHash: string +} + +export interface ExecuteIntentResponse { + intentId: string + intentStatus: IntentStatus +} + +export interface GetIntentReceiptRequest { + intentId: string +} + +export interface GetIntentReceiptResponse { + intentReceipt: IntentReceipt +} + +export interface WaitIntentReceiptRequest { + intentId: string +} + +export interface WaitIntentReceiptResponse { + intentReceipt: IntentReceipt + done: boolean +} + +export interface GetIntentRequest { + intentId: string +} + +export interface GetIntentResponse { + intent: Intent +} + +export interface SearchIntentsRequest { + byOriginIntentAddress?: string + byTransferTransactionHash?: string + byOwnerAddress?: string +} + +export interface SearchIntentsResponse { + intents: Array +} + +export interface GetIntentTransactionHistoryRequest { + page?: Page +} + +export interface GetIntentTransactionHistoryResponse { + transactions: Array + nextPage?: Page +} + +export interface GetIntentGasFeeOptionsRequest { + params: GetIntentEntrypointDepositFeeOptionsParams +} + +export interface GetIntentGasFeeOptionsResponse { + result: GetIntentEntrypointDepositFeeOptionsResult +} + +export interface GetIntentEntryTransactionRequest { + params: GetIntentEntrypointDepositParams +} + +export interface GetIntentEntryTransactionResponse { + result: GetIntentEntrypointDepositResult +} + +export interface Page { + column?: string + before?: any + after?: any + sort?: Array + pageSize?: number + more?: boolean +} + +export interface SortBy { + column: string + order: SortOrder +} + +export interface PingRequest {} + +export interface PingResponse { + status: boolean +} + +export interface VersionRequest {} + +export interface VersionResponse { + version: Version +} + +export interface RuntimeStatusRequest {} + +export interface RuntimeStatusResponse { + status: RuntimeStatus +} + +export interface ClockRequest {} + +export interface ClockResponse { + serverTime: string +} + +// +// Client +// + +export class Trails implements TrailsClient { + protected hostname: string + protected fetch: Fetch + protected path = '/rpc/Trails/' + + constructor(hostname: string, fetch: Fetch) { + this.hostname = hostname.replace(/\/*$/, '') + this.fetch = (input: RequestInfo, init?: RequestInit) => fetch(input, init) + } + + private url(name: string): string { + return this.hostname + this.path + name + } + + queryKey = { + ping: () => ['Trails', 'ping'] as const, + version: () => ['Trails', 'version'] as const, + runtimeStatus: () => ['Trails', 'runtimeStatus'] as const, + clock: () => ['Trails', 'clock'] as const, + quoteIntent: (req: QuoteIntentRequest) => ['Trails', 'quoteIntent', req] as const, + commitIntent: (req: CommitIntentRequest) => ['Trails', 'commitIntent', req] as const, + executeIntent: (req: ExecuteIntentRequest) => ['Trails', 'executeIntent', req] as const, + waitIntentReceipt: (req: WaitIntentReceiptRequest) => ['Trails', 'waitIntentReceipt', req] as const, + getIntentReceipt: (req: GetIntentReceiptRequest) => ['Trails', 'getIntentReceipt', req] as const, + getIntent: (req: GetIntentRequest) => ['Trails', 'getIntent', req] as const, + searchIntents: (req: SearchIntentsRequest) => ['Trails', 'searchIntents', req] as const, + getIntentTransactionHistory: (req: GetIntentTransactionHistoryRequest) => + ['Trails', 'getIntentTransactionHistory', req] as const, + getIntentGasFeeOptions: (req: GetIntentGasFeeOptionsRequest) => ['Trails', 'getIntentGasFeeOptions', req] as const, + getIntentEntryTransaction: (req: GetIntentEntryTransactionRequest) => ['Trails', 'getIntentEntryTransaction', req] as const + } + + ping = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Ping'), createHttpRequest('{}', headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'PingResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + version = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Version'), createHttpRequest('{}', headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'VersionResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + runtimeStatus = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('RuntimeStatus'), createHttpRequest('{}', headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'RuntimeStatusResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + clock = (headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('Clock'), createHttpRequest('{}', headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'ClockResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + quoteIntent = (req: QuoteIntentRequest, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('QuoteIntent'), createHttpRequest(JsonEncode(req, 'QuoteIntentRequest'), headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'QuoteIntentResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + commitIntent = (req: CommitIntentRequest, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('CommitIntent'), createHttpRequest(JsonEncode(req, 'CommitIntentRequest'), headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'CommitIntentResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + executeIntent = (req: ExecuteIntentRequest, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch( + this.url('ExecuteIntent'), + createHttpRequest(JsonEncode(req, 'ExecuteIntentRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'ExecuteIntentResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + waitIntentReceipt = ( + req: WaitIntentReceiptRequest, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch( + this.url('WaitIntentReceipt'), + createHttpRequest(JsonEncode(req, 'WaitIntentReceiptRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'WaitIntentReceiptResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + getIntentReceipt = ( + req: GetIntentReceiptRequest, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch( + this.url('GetIntentReceipt'), + createHttpRequest(JsonEncode(req, 'GetIntentReceiptRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'GetIntentReceiptResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + getIntent = (req: GetIntentRequest, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch(this.url('GetIntent'), createHttpRequest(JsonEncode(req, 'GetIntentRequest'), headers, signal)).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'GetIntentResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + searchIntents = (req: SearchIntentsRequest, headers?: object, signal?: AbortSignal): Promise => { + return this.fetch( + this.url('SearchIntents'), + createHttpRequest(JsonEncode(req, 'SearchIntentsRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'SearchIntentsResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + getIntentTransactionHistory = ( + req: GetIntentTransactionHistoryRequest, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch( + this.url('GetIntentTransactionHistory'), + createHttpRequest(JsonEncode(req, 'GetIntentTransactionHistoryRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'GetIntentTransactionHistoryResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + getIntentGasFeeOptions = ( + req: GetIntentGasFeeOptionsRequest, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch( + this.url('GetIntentGasFeeOptions'), + createHttpRequest(JsonEncode(req, 'GetIntentGasFeeOptionsRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'GetIntentGasFeeOptionsResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } + + getIntentEntryTransaction = ( + req: GetIntentEntryTransactionRequest, + headers?: object, + signal?: AbortSignal + ): Promise => { + return this.fetch( + this.url('GetIntentEntryTransaction'), + createHttpRequest(JsonEncode(req, 'GetIntentEntryTransactionRequest'), headers, signal) + ).then( + res => { + return buildResponse(res).then(_data => { + return JsonDecode(_data, 'GetIntentEntryTransactionResponse') + }) + }, + error => { + throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error instanceof Error ? error.message : String(error)}` }) + } + ) + } +} + +const createHttpRequest = (body: string = '{}', headers: object = {}, signal: AbortSignal | null = null): object => { + const reqHeaders: { [key: string]: string } = { ...headers, 'Content-Type': 'application/json' } + return { method: 'POST', headers: reqHeaders, body, signal } +} + +const buildResponse = (res: Response): Promise => { + return res.text().then(text => { + let data + try { + data = JSON.parse(text) + } catch (error) { + throw WebrpcBadResponseError.new({ + status: res.status, + cause: `JSON.parse(): ${error instanceof Error ? error.message : String(error)}: response text: ${text}` + }) + } + if (!res.ok) { + const code: number = typeof data.code === 'number' ? data.code : 0 + throw (webrpcErrorByCode[code] || WebrpcError).new(data) + } + return data + }) +} + +export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise + +// +// BigInt helpers +// + +const BIG_INT_FIELDS: { [typ: string]: (string | [string, string])[] } = { + CommitIntentRequest: [['intent', 'Intent']], + GetIntentEntryTransactionRequest: [['params', 'GetIntentEntrypointDepositParams']], + GetIntentEntrypointDepositFeeOptionsParams: ['amount'], + GetIntentEntrypointDepositParams: ['amount', 'permitAmount', 'feeAmount'], + GetIntentGasFeeOptionsRequest: [['params', 'GetIntentEntrypointDepositFeeOptionsParams']], + GetIntentReceiptResponse: [['intentReceipt', 'IntentReceipt']], + GetIntentResponse: [['intent', 'Intent']], + GetIntentTransactionHistoryResponse: [['transactions', 'IntentTransaction[]']], + Intent: [ + ['quoteRequest', 'QuoteIntentRequest'], + 'destinationSalt', + ['calls', 'IntentCalls[]'], + ['quote', 'IntentProviderQuote'], + ['fees', 'IntentFees'] + ], + IntentCalls: ['space', 'nonce', ['calls', 'TransactionCall[]']], + IntentFees: [ + ['originGas', 'IntentTransactionGasFee'], + ['destinationGas', 'IntentTransactionGasFee'], + ['provider', 'IntentProviderFees'], + 'feeTokenTotal', + 'totalFeeAmount' + ], + IntentProviderFees: ['quoteProviderFee', 'trailsFee', 'totalFeeAmount'], + IntentProviderQuote: ['fromAmount', 'fromAmountMin', 'toAmount', 'toAmountMin'], + IntentReceipt: [ + ['originTransaction', 'IntentTransaction'], + ['destinationTransaction', 'IntentTransaction'] + ], + IntentTransaction: ['tokenAmount'], + IntentTransactionGasFee: ['totalGasLimit', 'gasPrice', 'totalFeeAmount', ['metaTxnFeeDetails', 'MetaTxnFeeDetail[]']], + MetaTxnFeeDetail: ['estimatedGasLimit', 'feeNative'], + QuoteIntentRequest: ['originTokenAmount', 'destinationTokenAmount'], + QuoteIntentResponse: [['intent', 'Intent']], + SearchIntentsResponse: [['intents', 'Intent[]']], + TransactionCall: ['value', 'gasLimit'], + WaitIntentReceiptResponse: [['intentReceipt', 'IntentReceipt']] +} + +// Encode in-place: mutate provided object graph to serialize bigints to strings. +function encodeType(typ: string, obj: any): any { + if (obj == null || typeof obj !== 'object') return obj + const descs = BIG_INT_FIELDS[typ] || [] + if (!descs.length) return obj + for (const d of descs) { + if (Array.isArray(d)) { + const [fieldName, nestedType] = d + if (fieldName.endsWith('[]')) { + const base = fieldName.slice(0, -2) + const arr = obj[base] + if (Array.isArray(arr)) { + for (let i = 0; i < arr.length; i++) arr[i] = encodeType(nestedType, arr[i]) + } + } else if (obj[fieldName]) { + obj[fieldName] = encodeType(nestedType, obj[fieldName]) + } + continue + } + if (d.endsWith('[]')) { + const base = d.slice(0, -2) + const arr = obj[base] + if (Array.isArray(arr)) { + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] === 'bigint') arr[i] = arr[i].toString() + } + } + continue + } + if (typeof obj[d] === 'bigint') obj[d] = obj[d].toString() + } + return obj +} + +// Decode in-place: mutate object graph; throw if expected numeric string is invalid. +function decodeType(typ: string, obj: any): any { + if (obj == null || typeof obj !== 'object') return obj + const descs = BIG_INT_FIELDS[typ] || [] + if (!descs.length) return obj + for (const d of descs) { + if (Array.isArray(d)) { + const [fieldName, nestedType] = d + if (fieldName.endsWith('[]')) { + const base = fieldName.slice(0, -2) + const arr = obj[base] + if (Array.isArray(arr)) { + for (let i = 0; i < arr.length; i++) arr[i] = decodeType(nestedType, arr[i]) + } + } else if (obj[fieldName]) { + obj[fieldName] = decodeType(nestedType, obj[fieldName]) + } + continue + } + if (d.endsWith('[]')) { + const base = d.slice(0, -2) + const arr = obj[base] + if (Array.isArray(arr)) { + for (let i = 0; i < arr.length; i++) { + const v = arr[i] + if (typeof v === 'string') { + try { + arr[i] = BigInt(v) + } catch (e) { + throw WebrpcBadResponseError.new({ cause: `Invalid bigint value for ${base}[${i}]: ${v}` }) + } + } + } + } + continue + } + const v = obj[d] + if (typeof v === 'string') { + try { + obj[d] = BigInt(v) + } catch (e) { + throw WebrpcBadResponseError.new({ cause: `Invalid bigint value for ${d}: ${v}` }) + } + } + } + return obj +} + +// Encode object of given root type to JSON with BigInts converted to decimal strings. +export const JsonEncode = (obj: T, typ: string = ''): string => { + return JSON.stringify(encodeType(typ, obj)) +} + +// Decode data (JSON string or already-parsed object) and convert declared BigInt string fields back to BigInt. +export const JsonDecode = (data: string | any, typ: string = ''): T => { + let parsed: any = data + if (typeof data === 'string') { + try { + parsed = JSON.parse(data) + } catch (err) { + throw WebrpcBadResponseError.new({ cause: `JsonDecode: JSON.parse failed: ${(err as Error).message}` }) + } + } + return decodeType(typ, parsed) as T +} + +// +// Errors +// + +type WebrpcErrorParams = { name?: string; code?: number; message?: string; status?: number; cause?: string } + +export class WebrpcError extends Error { + code: number + status: number + + constructor(error: WebrpcErrorParams = {}) { + super(error.message) + this.name = error.name || 'WebrpcEndpointError' + this.code = typeof error.code === 'number' ? error.code : 0 + this.message = error.message || `endpoint error` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcError.prototype) + } + + static new(payload: any): WebrpcError { + return new this({ message: payload.message, code: payload.code, status: payload.status, cause: payload.cause }) + } +} + +export class WebrpcEndpointError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcEndpoint' + this.code = typeof error.code === 'number' ? error.code : 0 + this.message = error.message || `endpoint error` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcEndpointError.prototype) + } +} + +export class WebrpcRequestFailedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcRequestFailed' + this.code = typeof error.code === 'number' ? error.code : -1 + this.message = error.message || `request failed` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype) + } +} + +export class WebrpcBadRouteError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcBadRoute' + this.code = typeof error.code === 'number' ? error.code : -2 + this.message = error.message || `bad route` + this.status = typeof error.status === 'number' ? error.status : 404 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcBadRouteError.prototype) + } +} + +export class WebrpcBadMethodError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcBadMethod' + this.code = typeof error.code === 'number' ? error.code : -3 + this.message = error.message || `bad method` + this.status = typeof error.status === 'number' ? error.status : 405 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcBadMethodError.prototype) + } +} + +export class WebrpcBadRequestError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcBadRequest' + this.code = typeof error.code === 'number' ? error.code : -4 + this.message = error.message || `bad request` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcBadRequestError.prototype) + } +} + +export class WebrpcBadResponseError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcBadResponse' + this.code = typeof error.code === 'number' ? error.code : -5 + this.message = error.message || `bad response` + this.status = typeof error.status === 'number' ? error.status : 500 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcBadResponseError.prototype) + } +} + +export class WebrpcServerPanicError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcServerPanic' + this.code = typeof error.code === 'number' ? error.code : -6 + this.message = error.message || `server panic` + this.status = typeof error.status === 'number' ? error.status : 500 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcServerPanicError.prototype) + } +} + +export class WebrpcInternalErrorError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcInternalError' + this.code = typeof error.code === 'number' ? error.code : -7 + this.message = error.message || `internal error` + this.status = typeof error.status === 'number' ? error.status : 500 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype) + } +} + +export class WebrpcClientAbortedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcClientAborted' + this.code = typeof error.code === 'number' ? error.code : -8 + this.message = error.message || `request aborted by client` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcClientAbortedError.prototype) + } +} + +export class WebrpcStreamLostError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcStreamLost' + this.code = typeof error.code === 'number' ? error.code : -9 + this.message = error.message || `stream lost` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcStreamLostError.prototype) + } +} + +export class WebrpcStreamFinishedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'WebrpcStreamFinished' + this.code = typeof error.code === 'number' ? error.code : -10 + this.message = error.message || `stream finished` + this.status = typeof error.status === 'number' ? error.status : 200 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype) + } +} + +// +// Schema errors +// + +export class UnauthorizedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Unauthorized' + this.code = typeof error.code === 'number' ? error.code : 1000 + this.message = error.message || `Unauthorized access` + this.status = typeof error.status === 'number' ? error.status : 401 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, UnauthorizedError.prototype) + } +} + +export class PermissionDeniedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'PermissionDenied' + this.code = typeof error.code === 'number' ? error.code : 1001 + this.message = error.message || `Permission denied` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, PermissionDeniedError.prototype) + } +} + +export class SessionExpiredError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'SessionExpired' + this.code = typeof error.code === 'number' ? error.code : 1002 + this.message = error.message || `Session expired` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, SessionExpiredError.prototype) + } +} + +export class MethodNotFoundError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'MethodNotFound' + this.code = typeof error.code === 'number' ? error.code : 1003 + this.message = error.message || `Method not found` + this.status = typeof error.status === 'number' ? error.status : 404 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, MethodNotFoundError.prototype) + } +} + +export class RequestConflictError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'RequestConflict' + this.code = typeof error.code === 'number' ? error.code : 1004 + this.message = error.message || `Conflict with target resource` + this.status = typeof error.status === 'number' ? error.status : 409 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, RequestConflictError.prototype) + } +} + +export class AbortedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Aborted' + this.code = typeof error.code === 'number' ? error.code : 1005 + this.message = error.message || `Request aborted` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, AbortedError.prototype) + } +} + +export class GeoblockedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Geoblocked' + this.code = typeof error.code === 'number' ? error.code : 1006 + this.message = error.message || `Geoblocked region` + this.status = typeof error.status === 'number' ? error.status : 451 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, GeoblockedError.prototype) + } +} + +export class RateLimitedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'RateLimited' + this.code = typeof error.code === 'number' ? error.code : 1007 + this.message = error.message || `Rate-limited. Please slow down.` + this.status = typeof error.status === 'number' ? error.status : 429 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, RateLimitedError.prototype) + } +} + +export class ProjectNotFoundError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'ProjectNotFound' + this.code = typeof error.code === 'number' ? error.code : 1008 + this.message = error.message || `Project not found` + this.status = typeof error.status === 'number' ? error.status : 401 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, ProjectNotFoundError.prototype) + } +} + +export class AccessKeyNotFoundError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'AccessKeyNotFound' + this.code = typeof error.code === 'number' ? error.code : 1101 + this.message = error.message || `Access key not found` + this.status = typeof error.status === 'number' ? error.status : 401 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, AccessKeyNotFoundError.prototype) + } +} + +export class AccessKeyMismatchError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'AccessKeyMismatch' + this.code = typeof error.code === 'number' ? error.code : 1102 + this.message = error.message || `Access key mismatch` + this.status = typeof error.status === 'number' ? error.status : 409 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, AccessKeyMismatchError.prototype) + } +} + +export class InvalidOriginError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'InvalidOrigin' + this.code = typeof error.code === 'number' ? error.code : 1103 + this.message = error.message || `Invalid origin for Access Key` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, InvalidOriginError.prototype) + } +} + +export class InvalidServiceError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'InvalidService' + this.code = typeof error.code === 'number' ? error.code : 1104 + this.message = error.message || `Service not enabled for Access key` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, InvalidServiceError.prototype) + } +} + +export class UnauthorizedUserError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'UnauthorizedUser' + this.code = typeof error.code === 'number' ? error.code : 1105 + this.message = error.message || `Unauthorized user` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, UnauthorizedUserError.prototype) + } +} + +export class QuotaExceededError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'QuotaExceeded' + this.code = typeof error.code === 'number' ? error.code : 1200 + this.message = error.message || `Quota request exceeded` + this.status = typeof error.status === 'number' ? error.status : 429 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, QuotaExceededError.prototype) + } +} + +export class QuotaRateLimitError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'QuotaRateLimit' + this.code = typeof error.code === 'number' ? error.code : 1201 + this.message = error.message || `Quota rate limit exceeded` + this.status = typeof error.status === 'number' ? error.status : 429 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, QuotaRateLimitError.prototype) + } +} + +export class NoDefaultKeyError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'NoDefaultKey' + this.code = typeof error.code === 'number' ? error.code : 1300 + this.message = error.message || `No default access key found` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, NoDefaultKeyError.prototype) + } +} + +export class MaxAccessKeysError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'MaxAccessKeys' + this.code = typeof error.code === 'number' ? error.code : 1301 + this.message = error.message || `Access keys limit reached` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, MaxAccessKeysError.prototype) + } +} + +export class AtLeastOneKeyError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'AtLeastOneKey' + this.code = typeof error.code === 'number' ? error.code : 1302 + this.message = error.message || `You need at least one Access Key` + this.status = typeof error.status === 'number' ? error.status : 403 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, AtLeastOneKeyError.prototype) + } +} + +export class TimeoutError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Timeout' + this.code = typeof error.code === 'number' ? error.code : 1900 + this.message = error.message || `Request timed out` + this.status = typeof error.status === 'number' ? error.status : 408 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, TimeoutError.prototype) + } +} + +export class InvalidArgumentError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'InvalidArgument' + this.code = typeof error.code === 'number' ? error.code : 2000 + this.message = error.message || `Invalid argument` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, InvalidArgumentError.prototype) + } +} + +export class UnexpectedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Unexpected' + this.code = typeof error.code === 'number' ? error.code : 2001 + this.message = error.message || `Unexpected server error` + this.status = typeof error.status === 'number' ? error.status : 500 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, UnexpectedError.prototype) + } +} + +export class UnavailableError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'Unavailable' + this.code = typeof error.code === 'number' ? error.code : 2002 + this.message = error.message || `Unavailable resource` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, UnavailableError.prototype) + } +} + +export class QueryFailedError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'QueryFailed' + this.code = typeof error.code === 'number' ? error.code : 2003 + this.message = error.message || `Query failed` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, QueryFailedError.prototype) + } +} + +export class IntentStatusError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'IntentStatus' + this.code = typeof error.code === 'number' ? error.code : 2004 + this.message = error.message || `Invalid intent status` + this.status = typeof error.status === 'number' ? error.status : 422 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, IntentStatusError.prototype) + } +} + +export class NotFoundError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'NotFound' + this.code = typeof error.code === 'number' ? error.code : 8000 + this.message = error.message || `Resource not found` + this.status = typeof error.status === 'number' ? error.status : 400 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, NotFoundError.prototype) + } +} + +export class UnsupportedNetworkError extends WebrpcError { + constructor(error: WebrpcErrorParams = {}) { + super(error) + this.name = error.name || 'UnsupportedNetwork' + this.code = typeof error.code === 'number' ? error.code : 8008 + this.message = error.message || `Unsupported network` + this.status = typeof error.status === 'number' ? error.status : 422 + if (error.cause !== undefined) this.cause = error.cause + Object.setPrototypeOf(this, UnsupportedNetworkError.prototype) + } +} + +export enum errors { + WebrpcEndpoint = 'WebrpcEndpoint', + WebrpcRequestFailed = 'WebrpcRequestFailed', + WebrpcBadRoute = 'WebrpcBadRoute', + WebrpcBadMethod = 'WebrpcBadMethod', + WebrpcBadRequest = 'WebrpcBadRequest', + WebrpcBadResponse = 'WebrpcBadResponse', + WebrpcServerPanic = 'WebrpcServerPanic', + WebrpcInternalError = 'WebrpcInternalError', + WebrpcClientAborted = 'WebrpcClientAborted', + WebrpcStreamLost = 'WebrpcStreamLost', + WebrpcStreamFinished = 'WebrpcStreamFinished', + Unauthorized = 'Unauthorized', + PermissionDenied = 'PermissionDenied', + SessionExpired = 'SessionExpired', + MethodNotFound = 'MethodNotFound', + RequestConflict = 'RequestConflict', + Aborted = 'Aborted', + Geoblocked = 'Geoblocked', + RateLimited = 'RateLimited', + ProjectNotFound = 'ProjectNotFound', + AccessKeyNotFound = 'AccessKeyNotFound', + AccessKeyMismatch = 'AccessKeyMismatch', + InvalidOrigin = 'InvalidOrigin', + InvalidService = 'InvalidService', + UnauthorizedUser = 'UnauthorizedUser', + QuotaExceeded = 'QuotaExceeded', + QuotaRateLimit = 'QuotaRateLimit', + NoDefaultKey = 'NoDefaultKey', + MaxAccessKeys = 'MaxAccessKeys', + AtLeastOneKey = 'AtLeastOneKey', + Timeout = 'Timeout', + InvalidArgument = 'InvalidArgument', + Unexpected = 'Unexpected', + Unavailable = 'Unavailable', + QueryFailed = 'QueryFailed', + IntentStatus = 'IntentStatus', + NotFound = 'NotFound', + UnsupportedNetwork = 'UnsupportedNetwork' +} + +export enum WebrpcErrorCodes { + WebrpcEndpoint = 0, + WebrpcRequestFailed = -1, + WebrpcBadRoute = -2, + WebrpcBadMethod = -3, + WebrpcBadRequest = -4, + WebrpcBadResponse = -5, + WebrpcServerPanic = -6, + WebrpcInternalError = -7, + WebrpcClientAborted = -8, + WebrpcStreamLost = -9, + WebrpcStreamFinished = -10, + Unauthorized = 1000, + PermissionDenied = 1001, + SessionExpired = 1002, + MethodNotFound = 1003, + RequestConflict = 1004, + Aborted = 1005, + Geoblocked = 1006, + RateLimited = 1007, + ProjectNotFound = 1008, + AccessKeyNotFound = 1101, + AccessKeyMismatch = 1102, + InvalidOrigin = 1103, + InvalidService = 1104, + UnauthorizedUser = 1105, + QuotaExceeded = 1200, + QuotaRateLimit = 1201, + NoDefaultKey = 1300, + MaxAccessKeys = 1301, + AtLeastOneKey = 1302, + Timeout = 1900, + InvalidArgument = 2000, + Unexpected = 2001, + Unavailable = 2002, + QueryFailed = 2003, + IntentStatus = 2004, + NotFound = 8000, + UnsupportedNetwork = 8008 +} + +export const webrpcErrorByCode: { [code: number]: any } = { + [0]: WebrpcEndpointError, + [-1]: WebrpcRequestFailedError, + [-2]: WebrpcBadRouteError, + [-3]: WebrpcBadMethodError, + [-4]: WebrpcBadRequestError, + [-5]: WebrpcBadResponseError, + [-6]: WebrpcServerPanicError, + [-7]: WebrpcInternalErrorError, + [-8]: WebrpcClientAbortedError, + [-9]: WebrpcStreamLostError, + [-10]: WebrpcStreamFinishedError, + [1000]: UnauthorizedError, + [1001]: PermissionDeniedError, + [1002]: SessionExpiredError, + [1003]: MethodNotFoundError, + [1004]: RequestConflictError, + [1005]: AbortedError, + [1006]: GeoblockedError, + [1007]: RateLimitedError, + [1008]: ProjectNotFoundError, + [1101]: AccessKeyNotFoundError, + [1102]: AccessKeyMismatchError, + [1103]: InvalidOriginError, + [1104]: InvalidServiceError, + [1105]: UnauthorizedUserError, + [1200]: QuotaExceededError, + [1201]: QuotaRateLimitError, + [1300]: NoDefaultKeyError, + [1301]: MaxAccessKeysError, + [1302]: AtLeastOneKeyError, + [1900]: TimeoutError, + [2000]: InvalidArgumentError, + [2001]: UnexpectedError, + [2002]: UnavailableError, + [2003]: QueryFailedError, + [2004]: IntentStatusError, + [8000]: NotFoundError, + [8008]: UnsupportedNetworkError +} + +// +// Webrpc +// + +export const WebrpcHeader = 'Webrpc' + +export const WebrpcHeaderValue = 'webrpc@v0.30.2;gen-typescript@v0.22.2;trails-api@v1.0.0' + +type WebrpcGenVersions = { + WebrpcGenVersion: string + codeGenName: string + codeGenVersion: string + schemaName: string + schemaVersion: string +} + +export function VersionFromHeader(headers: Headers): WebrpcGenVersions { + const headerValue = headers.get(WebrpcHeader) + if (!headerValue) { + return { + WebrpcGenVersion: '', + codeGenName: '', + codeGenVersion: '', + schemaName: '', + schemaVersion: '' + } + } + + return parseWebrpcGenVersions(headerValue) +} + +function parseWebrpcGenVersions(header: string): WebrpcGenVersions { + const versions = header.split(';') + if (versions.length < 3) { + return { + WebrpcGenVersion: '', + codeGenName: '', + codeGenVersion: '', + schemaName: '', + schemaVersion: '' + } + } + + const [_, WebrpcGenVersion] = versions[0]!.split('@') + const [codeGenName, codeGenVersion] = versions[1]!.split('@') + const [schemaName, schemaVersion] = versions[2]!.split('@') + + return { + WebrpcGenVersion: WebrpcGenVersion ?? '', + codeGenName: codeGenName ?? '', + codeGenVersion: codeGenVersion ?? '', + schemaName: schemaName ?? '', + schemaVersion: schemaVersion ?? '' + } +}