Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@
"@snapshot-labs/snapshot.js": "^0.14.21",
"@unstoppabledomains/resolution": "^9.2.2",
"@webinterop/dns-connect": "^0.3.1",
"axios": "^0.25.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.1",
"jsdom": "^19.0.0",
"node-fetch": "v2.7.0",
"nodemon": "^2.0.7",
"redis": "^4.6.10",
"sharp": "^0.34.5",
Expand Down
17 changes: 12 additions & 5 deletions src/addressResolvers/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import axios from 'axios';
import { FetchError, isSilencedError, isStarknetAddress, withoutEmptyValues } from './utils';
import { Address, Handle } from '../utils';

Expand All @@ -18,9 +17,17 @@ async function apiCall(
resolve_type: RESOLVE_TYPE,
needles: string[]
): Promise<Record<string, string>> {
const requests = needles.map(needle =>
axios.get(buildApiUrl(resolve_type, needle), { timeout: 5e3 })
);
const requests = needles.map(async needle => {
const response = await fetch(buildApiUrl(resolve_type, needle), {
signal: AbortSignal.timeout(5e3)
});

if (!response.ok) {
throw new Error(`Starknet API request failed with status ${response.status}`);
}

return response.json() as Promise<Record<string, string>>;
Comment on lines +25 to +35
});
const responses = await Promise.allSettled(requests);

return withoutEmptyValues(
Expand All @@ -30,7 +37,7 @@ async function apiCall(
let value: string | undefined;

if (response.status === 'fulfilled') {
value = response.value.data[resolve_type === 'addr_to_domain' ? 'domain' : 'addr'];
value = response.value[resolve_type === 'addr_to_domain' ? 'domain' : 'addr'];
}

return [needle, value];
Expand Down
1 change: 0 additions & 1 deletion src/lookupDomains/shibarium.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import fetch from 'node-fetch';
import constants from '../constants.json';
import { Address, Handle } from '../utils';

Expand Down
1 change: 0 additions & 1 deletion src/resolvers/farcaster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getAddress } from '@ethersproject/address';
import fetch from 'node-fetch';
import { max } from '../constants.json';
import { Address, resize } from '../utils';
import { fetchHttpImage } from './utils';
Expand Down
13 changes: 4 additions & 9 deletions src/resolvers/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import axios from 'axios';
import { provider as getProvider } from '../addressResolvers/utils';
import { max } from '../constants.json';
import { getUrl, resize } from '../utils';
import { axiosDefaultParams, fetchHttpImage } from './utils';
import { DEFAULT_TIMEOUT, fetchHttpImage } from './utils';

const DEFAULT_IMG_URL = 'https://starknet.id/api/identicons/0';
const provider = getProvider('0x534e5f4d41494e');
Expand Down Expand Up @@ -34,13 +33,9 @@ async function getImage(domainOrAddress: string): Promise<string | null> {
}

async function fetchImageOrMetadata(url: string): Promise<Buffer | { image?: string }> {
const response = await axios({
url,
responseType: 'arraybuffer',
...axiosDefaultParams
});
const contentType: string = response.headers['content-type'] || '';
const data = Buffer.from(response.data);
const response = await fetch(url, { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
const contentType: string = response.headers.get('content-type') || '';
const data = Buffer.from(await response.arrayBuffer());
Comment on lines 35 to +43
if (contentType.includes('application/json')) {
return JSON.parse(data.toString('utf-8'));
}
Expand Down
21 changes: 3 additions & 18 deletions src/resolvers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import http from 'http';
import https from 'https';
import axios from 'axios';

export const axiosDefaultParams = {
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
timeout: 5e3
};
export const DEFAULT_TIMEOUT = 5e3;

export async function fetchHttpImage(url: string): Promise<Buffer> {
return (
await axios({
url,
...{
responseType: 'arraybuffer',
...axiosDefaultParams
}
})
).data;
const response = await fetch(url, { signal: AbortSignal.timeout(DEFAULT_TIMEOUT) });
return Buffer.from(await response.arrayBuffer());
}
Comment on lines 5 to 13
32 changes: 24 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createHash } from 'crypto';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import snapshot from '@snapshot-labs/snapshot.js';
import axios from 'axios';
import { Response } from 'express';
import sharp from 'sharp';
import chains from './chains.json';
Expand Down Expand Up @@ -183,31 +182,48 @@ export const getBaseAssetIconUrl = (chainId: string) => {
return 'https://static.cdnlogo.com/logos/e/81/ethereum-eth.svg';
};

export function graphQlCall(
export async function graphQlCall(
url: string,
query: string,
variables?: Record<string, any>,
options: any = {
headers: {}
}
) {
const data: { query: string; variables?: Record<string, any> } = { query };
const body: { query: string; variables?: Record<string, any> } = { query };
if (variables) {
data.variables = variables;
body.variables = variables;
}

return axios({
url: url,
const response = await fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json',
...Object.fromEntries(
Object.entries(options.headers).filter(([, value]) => value !== undefined && value !== null)
)
},
timeout: 5e3,
data
signal: AbortSignal.timeout(5e3),
body: JSON.stringify(body)
});

if (!response.ok) {
throw new FetchError(`GraphQL request failed with status ${response.status}`, response.status);
}

// Preserve the previous axios response shape (`{ data: <body> }`) so callers
// that destructure `{ data: { data } }` keep working.
return { data: await response.json() };
}

class FetchError extends Error {
response: { status: number };

constructor(message: string, status: number) {
super(message);
this.name = 'FetchError';
this.response = { status };
}
}
Comment on lines +212 to 238

/**
Expand Down
14 changes: 1 addition & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3566,13 +3566,6 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.25.0:
version "0.25.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.25.0.tgz#349cfbb31331a9b4453190791760a8d35b093e0a"
integrity sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==
dependencies:
follow-redirects "^1.14.7"

babel-jest@^28.1.3:
version "28.1.3"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-28.1.3.tgz#c1187258197c099072156a0a121c11ee1e3917d5"
Expand Down Expand Up @@ -4894,11 +4887,6 @@ flatted@^3.2.9:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.4.2.tgz#f5c23c107f0f37de8dbdf24f13722b3b98d52726"
integrity sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==

follow-redirects@^1.14.7:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
Expand Down Expand Up @@ -6318,7 +6306,7 @@ negotiator@0.6.3:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-fetch@^2.6.1, node-fetch@^2.6.12, node-fetch@^2.7.0, node-fetch@v2.7.0:
node-fetch@^2.6.1, node-fetch@^2.6.12, node-fetch@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down
Loading