Skip to content

Commit

Permalink
feat: fetch wrapper for interactions and contract definition loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed May 21, 2024
1 parent 5fc79b1 commit 512d7f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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);
Expand All @@ -36,7 +38,7 @@ export class WarpGatewayContractDefinitionLoader implements DefinitionLoader {
try {
const baseUrl = stripTrailingSlash(this._warp.gwUrl());
const result: ContractDefinition<State> = await getJsonResponse(
fetch(`${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId ? `&srcTxId=${evolvedSrcTxId}` : ''}`)
this._warpFetchWrapper.fetch(`${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId ? `&srcTxId=${evolvedSrcTxId}` : ''}`)

Check failure on line 41 in src/core/modules/impl/WarpGatewayContractDefinitionLoader.ts

View workflow job for this annotation

GitHub Actions / build

Replace ``${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId·?·`&srcTxId=${evolvedSrcTxId}`·:·''}`` with `⏎··········`${baseUrl}/gateway/contract?txId=${contractTxId}${evolvedSrcTxId·?·`&srcTxId=${evolvedSrcTxId}`·:·''}`⏎········`
);

if (result.srcBinary != null && !(result.srcBinary instanceof Buffer)) {
Expand Down Expand Up @@ -75,5 +77,6 @@ export class WarpGatewayContractDefinitionLoader implements DefinitionLoader {
this._warp = warp;
this.arweaveWrapper = new ArweaveWrapper(warp);
this.contractDefinitionLoader.warp = warp;
this._warpFetchWrapper = new WarpFetchWrapper(warp);
}
}
5 changes: 4 additions & 1 deletion src/core/modules/impl/WarpGatewayInteractionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| {
Expand Down Expand Up @@ -51,6 +52,7 @@ type InteractionsResult = {
*/
export class WarpGatewayInteractionsLoader implements InteractionsLoader {
private _warp: Warp;
private _warpFetchWrapper: WarpFetchWrapper;

constructor(
private readonly confirmationStatus: ConfirmationStatus = null,
Expand Down Expand Up @@ -93,7 +95,7 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {

page++;
const response = await getJsonResponse<InteractionsResult>(
fetch(
this._warpFetchWrapper.fetch(
`${url}?${new URLSearchParams({
contractId: contractId,
...(this._warp.whoAmI ? { client: this._warp.whoAmI } : ''),
Expand Down Expand Up @@ -140,5 +142,6 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {

set warp(warp: Warp) {
this._warp = warp;
this._warpFetchWrapper = new WarpFetchWrapper(warp);
}
}
19 changes: 15 additions & 4 deletions tools/fetch-options-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ 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<FetchRequest, RequestInit> {
process(request: FetchRequest): Partial<RequestInit> {
const url = request.input;
console.log(url);

let fetchOptions: Partial<RequestInit> = {};

Expand All @@ -18,6 +21,14 @@ class FetchOptionsPlugin implements WarpPlugin<FetchRequest, RequestInit> {
};
}

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

Expand All @@ -32,18 +43,18 @@ 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',
Expand Down

0 comments on commit 512d7f3

Please sign in to comment.