From 5aba7ef886eea93c947dbd62be23412e0fe4dcf1 Mon Sep 17 00:00:00 2001 From: Asia Date: Tue, 21 May 2024 18:50:57 +0200 Subject: [PATCH] feat: fetch wrapper for interactions and contract definition loaders --- .../WarpGatewayContractDefinitionLoader.ts | 7 +++++- .../impl/WarpGatewayInteractionsLoader.ts | 5 +++- tools/fetch-options-plugin.ts | 23 +++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts b/src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts index 62c1b4af..8516af4a 100644 --- a/src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts +++ b/src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts @@ -12,6 +12,7 @@ import { Warp, WarpEnvironment } from '../../Warp'; import { TagsParser } from './TagsParser'; import { Transaction } from '../../../utils/types/arweave-types'; import { getJsonResponse, stripTrailingSlash } from '../../../utils/utils'; +import { WarpFetchWrapper } from '../../../core/WarpFetchWrapper'; /** * Makes use of Warp Gateway ({@link https://github.com/redstone-finance/redstone-sw-gateway}) @@ -26,6 +27,7 @@ export class WarpGatewayContractDefinitionLoader implements DefinitionLoader { private arweaveWrapper: ArweaveWrapper; private readonly tagsParser: TagsParser; private _warp: Warp; + private _warpFetchWrapper: WarpFetchWrapper; constructor(arweave: Arweave, env: WarpEnvironment) { this.contractDefinitionLoader = new ArweaveContractDefinitionLoader(arweave, env); @@ -36,7 +38,9 @@ export class WarpGatewayContractDefinitionLoader implements DefinitionLoader { try { const baseUrl = stripTrailingSlash(this._warp.gwUrl()); const result: ContractDefinition = await getJsonResponse( - fetch(`${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId ? `&srcTxId=${evolvedSrcTxId}` : ''}`) + this._warpFetchWrapper.fetch( + `${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId ? `&srcTxId=${evolvedSrcTxId}` : ''}` + ) ); if (result.srcBinary != null && !(result.srcBinary instanceof Buffer)) { @@ -75,5 +79,6 @@ export class WarpGatewayContractDefinitionLoader implements DefinitionLoader { this._warp = warp; this.arweaveWrapper = new ArweaveWrapper(warp); this.contractDefinitionLoader.warp = warp; + this._warpFetchWrapper = new WarpFetchWrapper(warp); } } diff --git a/src/core/modules/impl/WarpGatewayInteractionsLoader.ts b/src/core/modules/impl/WarpGatewayInteractionsLoader.ts index 2b3b4cfe..49c8d217 100644 --- a/src/core/modules/impl/WarpGatewayInteractionsLoader.ts +++ b/src/core/modules/impl/WarpGatewayInteractionsLoader.ts @@ -7,6 +7,7 @@ import { GW_TYPE, InteractionsLoader } from '../InteractionsLoader'; import { EvaluationOptions } from '../StateEvaluator'; import { Warp } from '../../Warp'; import { AbortError } from './HandlerExecutorFactory'; +import { WarpFetchWrapper } from '../../../core/WarpFetchWrapper'; export type ConfirmationStatus = | { @@ -51,6 +52,7 @@ type InteractionsResult = { */ export class WarpGatewayInteractionsLoader implements InteractionsLoader { private _warp: Warp; + private _warpFetchWrapper: WarpFetchWrapper; constructor( private readonly confirmationStatus: ConfirmationStatus = null, @@ -93,7 +95,7 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader { page++; const response = await getJsonResponse( - fetch( + this._warpFetchWrapper.fetch( `${url}?${new URLSearchParams({ contractId: contractId, ...(this._warp.whoAmI ? { client: this._warp.whoAmI } : ''), @@ -140,5 +142,6 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader { set warp(warp: Warp) { this._warp = warp; + this._warpFetchWrapper = new WarpFetchWrapper(warp); } } diff --git a/tools/fetch-options-plugin.ts b/tools/fetch-options-plugin.ts index 69630e86..a78c329f 100644 --- a/tools/fetch-options-plugin.ts +++ b/tools/fetch-options-plugin.ts @@ -5,6 +5,8 @@ import fs from 'fs'; import path from 'path'; import { LoggerFactory } from '../src/logging/LoggerFactory'; import { defaultCacheOptions, WarpFactory } from '../src/core/WarpFactory'; +import { DeployPlugin } from 'warp-contracts-plugin-deploy'; +import { ArweaveSigner } from 'warp-arbundles'; class FetchOptionsPlugin implements WarpPlugin { process(request: FetchRequest): Partial { @@ -18,6 +20,17 @@ class FetchOptionsPlugin implements WarpPlugin { }; } + if ( + url == + 'https://gw.warp.cc/gateway/v3/interactions-sort-key?contractId=nf5TUVkzyZBGtl0NmVXZvheC3EN5d4XA-5ewpGgaYRo&fromSdk=true&confirmationStatus=not_corrupted' + ) { + fetchOptions = { + headers: { + 'x-api-key': 'test' + } + }; + } + return fetchOptions; } @@ -32,18 +45,20 @@ async function main() { const logger = LoggerFactory.INST.create('FetchOptionsPlugin'); try { - const warp = WarpFactory.forMainnet({ ...defaultCacheOptions, inMemory: true }).use(new FetchOptionsPlugin()); + const warp = WarpFactory.forMainnet({ ...defaultCacheOptions, inMemory: true }) + .use(new DeployPlugin()) + .use(new FetchOptionsPlugin()); const jsContractSrc = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.js'), 'utf8'); const initialState = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.json'), 'utf8'); - const { contractTxId } = await warp.createContract.deploy({ - wallet, + const { contractTxId } = await warp.deploy({ + wallet: new ArweaveSigner(wallet), initState: initialState, src: jsContractSrc }); - const contract = warp.contract(contractTxId).connect(wallet); + const contract = warp.contract(contractTxId).connect(new ArweaveSigner(wallet)); await contract.writeInteraction({ function: 'transfer',