Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch wrapper for interactions and contract definition loaders #524

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"gen-esm-wrapper": "^1.1.3",
"jest": "^28.1.3",
"jest": "^29.7.0",
"json-schema-to-typescript": "^11.0.1",
"node-stdlib-browser": "^1.2.0",
"prettier": "^2.3.2",
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/unit/gateway-interactions.loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with correct params', async () => {
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`, undefined);
});
it('should be called accordingly to the amount of pages', async () => {
const fetchMock = jest.spyOn(global, 'fetch').mockImplementation(
Expand All @@ -110,7 +110,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
);
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`, undefined);
/*expect(fetchMock).toBeCalledWith(`${baseUrl}&page=2&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=3&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=4&fromSdk=true`);
Expand All @@ -120,12 +120,12 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with confirmationStatus set to "confirmed"', async () => {
const loader = getLoader({ confirmed: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=confirmed`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=confirmed`, undefined);
});
it('should be called with confirmationStatus set to "not_corrupted"', async () => {
const loader = getLoader({ notCorrupted: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=not_corrupted`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=not_corrupted`, undefined);
});
it('should throw an error in case of timeout', async () => {
jest.spyOn(global, 'fetch').mockImplementation(() => Promise.reject({ status: 504, ok: false }));
Expand Down
7 changes: 6 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,9 @@ 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}` : ''}`
)
);

if (result.srcBinary != null && !(result.srcBinary instanceof Buffer)) {
Expand Down Expand Up @@ -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);
}
}
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);
}
}
23 changes: 19 additions & 4 deletions tools/fetch-options-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchRequest, RequestInit> {
process(request: FetchRequest): Partial<RequestInit> {
Expand All @@ -18,6 +20,17 @@ 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 +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',
Expand Down
Loading
Loading