{"version":3,"sources":["../src/EngineBaseClient.ts"],"sourcesContent":["import {\n  getSignedTransactionRequest,\n  SignableRequestType,\n  SignableRequestTypeToParams,\n  WalletClientWithAccount,\n} from '@vertex-protocol/contracts';\nimport { WalletNotProvidedError } from '@vertex-protocol/utils';\nimport axios, { AxiosInstance, AxiosResponse } from 'axios';\nimport {\n  EngineServerExecuteRequestByType,\n  EngineServerExecuteRequestType,\n  EngineServerExecuteResult,\n  EngineServerExecuteSuccessResult,\n  EngineServerQueryRequest,\n  EngineServerQueryRequestByType,\n  EngineServerQueryRequestType,\n  EngineServerQueryResponse,\n  EngineServerQueryResponseByType,\n  EngineServerQuerySuccessResponse,\n  GetEngineNoncesParams,\n  GetEngineNoncesResponse,\n} from './types';\nimport { EngineServerFailureError } from './types/EngineServerFailureError';\n\nexport interface EngineClientOpts {\n  // Server URL\n  url: string;\n  // Wallet client for EIP712 signing\n  walletClient?: WalletClientWithAccount;\n  // Linked signer registered through the engine, if provided, execute requests will use this signer\n  linkedSignerWalletClient?: WalletClientWithAccount;\n}\n\n// Only 1 key can be defined per execute request\ntype EngineExecuteRequestBody = Partial<EngineServerExecuteRequestByType>;\n\ntype EngineQueryRequestResponse<\n  T extends EngineServerQueryRequestType = EngineServerQueryRequestType,\n> = EngineServerQueryResponse<T>;\n\n/**\n * Base client for all engine requests\n */\nexport class EngineBaseClient {\n  readonly opts: EngineClientOpts;\n  readonly axiosInstance: AxiosInstance;\n\n  constructor(opts: EngineClientOpts) {\n    this.opts = opts;\n    this.axiosInstance = axios.create({\n      withCredentials: true,\n    });\n  }\n\n  /**\n   * Sets the linked signer for execute requests\n   *\n   * @param linkedSignerWalletClient The linkedSigner to use for all signatures. Set to null to revert to the chain signer\n   */\n  public setLinkedSigner(\n    linkedSignerWalletClient: WalletClientWithAccount | null,\n  ) {\n    this.opts.linkedSignerWalletClient = linkedSignerWalletClient ?? undefined;\n  }\n\n  public async getTxNonce(address?: string): Promise<string> {\n    const addr = address ?? this.opts.walletClient?.account.address;\n\n    if (!addr) {\n      throw new WalletNotProvidedError();\n    }\n\n    return (\n      await this.getNonces({\n        address: addr,\n      })\n    ).txNonce;\n  }\n\n  public async getNonces(\n    params: GetEngineNoncesParams,\n  ): Promise<GetEngineNoncesResponse> {\n    const baseResp = await this.query('nonces', params);\n\n    return {\n      orderNonce: baseResp.order_nonce,\n      txNonce: baseResp.tx_nonce,\n    };\n  }\n\n  /**\n   * Queries the engine, all query params are stringified into the query string\n   *\n   * @param requestType\n   * @param params\n   * @public\n   */\n  public async query<TRequestType extends EngineServerQueryRequestType>(\n    requestType: TRequestType,\n    params: EngineServerQueryRequestByType[TRequestType],\n  ): Promise<EngineServerQueryResponseByType[TRequestType]> {\n    const request = this.getQueryRequest(requestType, params);\n    const response = await this.axiosInstance.post<EngineQueryRequestResponse>(\n      `${this.opts.url}/query`,\n      request,\n    );\n\n    this.checkResponseStatus(response);\n    this.checkServerStatus(response);\n\n    // checkServerStatus throws on failure responses so the cast to the success response is acceptable here\n    const successResponse = response as AxiosResponse<\n      EngineServerQuerySuccessResponse<TRequestType>\n    >;\n\n    return successResponse.data.data;\n  }\n\n  /**\n   * A simple, typechecked fn for constructing a query request in the format expected by the server.\n   *\n   * @param requestType\n   * @param params\n   */\n  public getQueryRequest<TRequestType extends EngineServerQueryRequestType>(\n    requestType: TRequestType,\n    params: EngineServerQueryRequestByType[TRequestType],\n  ): EngineServerQueryRequest<TRequestType> {\n    return {\n      type: requestType,\n      ...params,\n    };\n  }\n\n  /**\n   * POSTs an execute message to the engine and returns the successful response. Throws the failure response wrapped\n   * in an EngineServerFailureError on failure.\n   *\n   * @param requestType\n   * @param params\n   * @public\n   */\n  public async execute<TRequestType extends EngineServerExecuteRequestType>(\n    requestType: TRequestType,\n    params: EngineServerExecuteRequestByType[TRequestType],\n  ): Promise<EngineServerExecuteSuccessResult<TRequestType>> {\n    const reqBody = this.getExecuteRequest(requestType, params);\n    const response = await this.axiosInstance.post<\n      EngineServerExecuteResult<TRequestType>\n    >(`${this.opts.url}/execute`, reqBody);\n\n    this.checkResponseStatus(response);\n    this.checkServerStatus(response);\n\n    // checkServerStatus catches the failure result and throws the error, so the cast to the success response is acceptable here\n    return response.data as EngineServerExecuteSuccessResult<TRequestType>;\n  }\n\n  /**\n   * A simple, typechecked fn for constructing an execute request in the format expected by the server.\n   *\n   * @param requestType\n   * @param params\n   */\n  public getExecuteRequest<TRequestType extends EngineServerExecuteRequestType>(\n    requestType: TRequestType,\n    params: EngineServerExecuteRequestByType[TRequestType],\n  ): EngineExecuteRequestBody {\n    return {\n      [requestType]: params,\n    };\n  }\n\n  /**\n   * Signs a given request with the signer provided to the engine\n   *\n   * @param requestType\n   * @param verifyingContract\n   * @param chainId\n   * @param params\n   * @public\n   */\n  public async sign<T extends SignableRequestType>(\n    requestType: T,\n    verifyingContract: string,\n    chainId: number,\n    params: SignableRequestTypeToParams[T],\n  ) {\n    // Use the linked signer if provided, otherwise use the default signer provided to the engine\n    const walletClient =\n      this.opts.linkedSignerWalletClient ?? this.opts.walletClient;\n\n    if (!walletClient) {\n      throw new WalletNotProvidedError();\n    }\n\n    return getSignedTransactionRequest({\n      chainId,\n      requestParams: params,\n      requestType,\n      walletClient,\n      verifyingContract,\n    });\n  }\n\n  private checkResponseStatus(response: AxiosResponse) {\n    if (response.status !== 200 || !response.data) {\n      throw Error(\n        `Unexpected response from server: ${response.status} ${response.statusText}`,\n      );\n    }\n  }\n\n  private checkServerStatus(\n    response: AxiosResponse<\n      EngineServerExecuteResult | EngineQueryRequestResponse\n    >,\n  ) {\n    if (response.data.status !== 'success') {\n      throw new EngineServerFailureError(response.data);\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAKO;AACP,mBAAuC;AACvC,mBAAoD;AAepD,sCAAyC;AAqBlC,IAAM,mBAAN,MAAuB;AAAA,EAI5B,YAAY,MAAwB;AAClC,SAAK,OAAO;AACZ,SAAK,gB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