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

ci: test HTTP gateways in E2E integration test #1869

Merged
merged 2 commits into from
Aug 30, 2023
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
6 changes: 6 additions & 0 deletions .changeset/purple-toys-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@api3/airnode-examples': patch
'@api3/airnode-node': patch
---

Test HTTP gateways in E2E integration test
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@
"enabled": false
},
"httpGateway": {
"enabled": false
"enabled": true,
"maxConcurrency": 20,
"corsOrigins": []
},
"httpSignedDataGateway": {
"enabled": false
"enabled": true,
"maxConcurrency": 20,
"corsOrigins": []
},
"oevGateway": {
"enabled": false
Expand All @@ -89,8 +93,20 @@
"cacheResponses": false
}
],
"http": [],
"httpSignedData": []
"http": [
{
"endpointId": "0xfb87102cdabadf905321521ba0b3cbf74ad09c5d400ac2eccdbef8d6143e78c4",
"oisTitle": "CoinGecko basic request",
"endpointName": "coinMarketData"
}
],
"httpSignedData": [
{
"endpointId": "0xfb87102cdabadf905321521ba0b3cbf74ad09c5d400ac2eccdbef8d6143e78c4",
"oisTitle": "CoinGecko basic request",
"endpointName": "coinMarketData"
}
]
},
"templates": [],
"ois": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ const createConfig = async (generateExampleFile: boolean): Promise<Config> => ({
enabled: false,
},
httpGateway: {
enabled: false,
enabled: true,
maxConcurrency: 20,
corsOrigins: [],
},
httpSignedDataGateway: {
enabled: false,
enabled: true,
maxConcurrency: 20,
corsOrigins: [],
},
oevGateway: {
enabled: false,
Expand All @@ -97,8 +101,20 @@ const createConfig = async (generateExampleFile: boolean): Promise<Config> => ({
cacheResponses: false,
},
],
http: [],
httpSignedData: [],
http: [
{
endpointId: '0xfb87102cdabadf905321521ba0b3cbf74ad09c5d400ac2eccdbef8d6143e78c4',
oisTitle: 'CoinGecko basic request',
endpointName: 'coinMarketData',
},
],
httpSignedData: [
{
endpointId: '0xfb87102cdabadf905321521ba0b3cbf74ad09c5d400ac2eccdbef8d6143e78c4',
oisTitle: 'CoinGecko basic request',
endpointName: 'coinMarketData',
},
],
},
templates: [],
ois: [
Expand Down
40 changes: 33 additions & 7 deletions packages/airnode-examples/test/e2e/coingecko-local.feature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { Config } from '@api3/airnode-node';
import { ethers } from 'ethers';
import { Config, DEFAULT_PATH_KEY } from '@api3/airnode-node';
import { logger } from '@api3/airnode-utilities';
import { runCommand, runCommandInBackground } from '../utils';

Expand Down Expand Up @@ -31,6 +32,7 @@ const removeFulfillmentGasLimit = () => {
const config: Config = JSON.parse(readFileSync(configPath, 'utf8'));
delete config.chains[0].options.fulfillmentGasLimit;
writeFileSync(configPath, JSON.stringify(config, null, 2));
return config;
};

describe('Coingecko integration with containerized Airnode and hardhat', () => {
Expand All @@ -41,7 +43,7 @@ describe('Coingecko integration with containerized Airnode and hardhat', () => {
runCommand('yarn deploy-rrp-dry-run');
runCommand('yarn create-airnode-config');
runCommand('yarn create-airnode-secrets');
removeFulfillmentGasLimit();
const config = removeFulfillmentGasLimit();
runCommand(`yarn ts-node integrations/${integration}/deploy-authorizers-and-update-config`);
runCommandInBackground('yarn run-airnode-locally');

Expand All @@ -55,15 +57,39 @@ describe('Coingecko integration with containerized Airnode and hardhat', () => {

const pathOfResponseText = 'Ethereum price is';
expect(response).toContain(pathOfResponseText);

const priceText = response.split(pathOfResponseText)[1];
expect(priceText).toContain('USD');
const priceStr = priceText.split('USD')[0].trim();
const price = Number(priceStr);
logger.log(`Blockchain request: The Ethereum price is ${price} USD.`);

const endpointId = config.triggers.rrp[0].endpointId;
const timesReservedParameter = config.ois[0].endpoints[0].reservedParameters.find((rp) => rp.name === '_times');
const timesValue = Number(timesReservedParameter!.fixed);

const httpResponse = runCommand(
`curl --silent --show-error -X POST -H 'Content-Type: application/json' -d '{"parameters": {"coinId": "ethereum"}}' 'http://localhost:3000/http-data/${DEFAULT_PATH_KEY}/${endpointId}'`
);
const httpGatewayPrice = Number(JSON.parse(httpResponse).values[0]) / timesValue;
expect(httpGatewayPrice).toEqual(expect.any(Number));
logger.log(`HTTP Gateway request: The Ethereum price is ${price} USD.`);

const signedHttpResponse = runCommand(
`curl --silent --show-error -X POST -H 'Content-Type: application/json' -d '{"encodedParameters": "0x3173000000000000000000000000000000000000000000000000000000000000636f696e49640000000000000000000000000000000000000000000000000000657468657265756d000000000000000000000000000000000000000000000000"}' 'http://localhost:3000/http-signed-data/${DEFAULT_PATH_KEY}/${endpointId}'`
);
const encodedValue = JSON.parse(signedHttpResponse).encodedValue;
const decodedBigNumber: ethers.BigNumber = ethers.utils.defaultAbiCoder.decode(['int256'], encodedValue)[0];
const signedGatewayPrice = decodedBigNumber.toNumber() / timesValue;
expect(signedGatewayPrice).toEqual(expect.any(Number));
logger.log(`Signed HTTP Gateway request: The Ethereum price is ${price} USD.`);

const price = priceText.split('USD')[0].trim();
expect(Number(price)).toEqual(expect.any(Number));
expect(Number(price).toString()).toBe(price);
// Values returned by all three requests should be similar
const allowedDifference = 50; // very conservative
expect(Math.abs(httpGatewayPrice - signedGatewayPrice)).toBeLessThan(allowedDifference);
expect(Math.abs(httpGatewayPrice - price)).toBeLessThan(allowedDifference);
expect(Math.abs(signedGatewayPrice - price)).toBeLessThan(allowedDifference);

logger.log(`The Ethereum price is ${price} USD.`);
logger.log('All three requests returned similar Ethereum price values.');
} finally {
runCommand('yarn stop-local-airnode');
}
Expand Down
1 change: 1 addition & 0 deletions packages/airnode-node/src/workers/local-gateways/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './validation';
export { DEFAULT_PATH_KEY } from './server';
2 changes: 1 addition & 1 deletion packages/airnode-node/src/workers/local-gateways/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const httpSignedDataBodySchema = z.object({
encodedParameters: z.string(),
});
const DEFAULT_PORT = 3000;
const DEFAULT_PATH_KEY = '01234567-abcd-abcd-abcd-012345678abc';
export const DEFAULT_PATH_KEY = '01234567-abcd-abcd-abcd-012345678abc';

export function getGatewaysUrl(port: number = DEFAULT_PORT, path?: string) {
const base = `http://localhost:${port || DEFAULT_PORT}`;
Expand Down
Loading