Skip to content

Commit 8248ee0

Browse files
authored
feat: optionally use allbridge header if provided (#790)
### Description Add support for our Allbridge header to prevent rate-limiting; Users without this header / api-key should not be impacted.
1 parent 6f983ec commit 8248ee0

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"start": "ts-node ./scripts/start.ts",
3434
"deploy:staging": "gcloud beta functions deploy --gen2 --concurrency=80 --cpu 1 --memory=256MB --project=celo-mobile-alfajores --region=us-central1 --runtime=nodejs20 --env-vars-file=src/api/staging.yaml --update-labels \"valora-repo=hooks,commit-sha=$(git rev-parse HEAD)\"",
3535
"deploy:staging:http": "yarn deploy:staging --trigger-http --allow-unauthenticated",
36-
"deploy:production": "gcloud beta functions deploy --gen2 --concurrency=80 --cpu 1 --memory=512MB --project=celo-mobile-mainnet --region=us-central1 --runtime=nodejs20 --env-vars-file=src/api/production.yaml --update-labels \"valora-repo=hooks,commit-sha=$(git rev-parse HEAD)\" --set-secrets=NETWORK_ID_TO_RPC_URL=hooks-rpc-urls:latest",
36+
"deploy:production": "gcloud beta functions deploy --gen2 --concurrency=80 --cpu 1 --memory=512MB --project=celo-mobile-mainnet --region=us-central1 --runtime=nodejs20 --env-vars-file=src/api/production.yaml --update-labels \"valora-repo=hooks,commit-sha=$(git rev-parse HEAD)\" --set-secrets=NETWORK_ID_TO_RPC_URL=hooks-rpc-urls:latest,ALLBRIDGE_API_KEY=allbridge-api-key:latest",
3737
"deploy:production:http": "yarn deploy:production --trigger-http --allow-unauthenticated"
3838
},
3939
"dependencies": {

src/apps/allbridge/api.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import got from '../../utils/got'
22
import { NetworkId } from '../../types/networkId'
33
import { Address } from 'viem'
44
import { LRUCache } from 'lru-cache'
5+
import { getConfig } from '../../config'
56

67
type SupportedAllbridgeChainSymbols =
78
| 'ETH'
@@ -97,12 +98,17 @@ export async function getAllbridgeTokenInfo({
9798
TOKEN_INFO_RESPONSE_KEY,
9899
) as AllbridgeApiResponse
99100
if (!allbridgeTokensInfoResponse) {
101+
const headers: Record<string, string> = {
102+
'x-Sdk-Agent': 'AllbridgeCoreSDK/3.21.0', // as in https://github.com/allbridge-io/allbridge-core-js-sdk/blob/5deb0623d6fffcbc7a85dba22b98942c47d4af6c/src/client/core-api/api-client.ts#L48
103+
}
104+
105+
const allBridgeApiKey = getConfig().ALLBRIDGE_API_KEY
106+
if (allBridgeApiKey) {
107+
headers['valora-allbridge-core'] = allBridgeApiKey
108+
}
109+
100110
allbridgeTokensInfoResponse = await got
101-
.get('https://core.api.allbridgecoreapi.net/token-info', {
102-
headers: {
103-
'x-Sdk-Agent': 'AllbridgeCoreSDK/3.21.0', // as in https://github.com/allbridge-io/allbridge-core-js-sdk/blob/5deb0623d6fffcbc7a85dba22b98942c47d4af6c/src/client/core-api/api-client.ts#L48
104-
},
105-
})
111+
.get('https://core.api.allbridgecoreapi.net/token-info', { headers })
106112
.json()
107113
cache.set(TOKEN_INFO_RESPONSE_KEY, allbridgeTokensInfoResponse)
108114
}

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Config {
1111
EARN_SUPPORTED_NETWORK_IDS: NetworkId[]
1212
SIMULATE_TRANSACTIONS_URL?: string
1313
GET_SWAP_QUOTE_URL: string
14+
ALLBRIDGE_API_KEY?: string
1415
}
1516

1617
export function networkIdToRpcUrlTransform(val: string | undefined) {
@@ -53,6 +54,7 @@ export function getConfig(): Config {
5354
EARN_SUPPORTED_NETWORK_IDS: z
5455
.string()
5556
.transform((val) => val.split(',') as NetworkId[]),
57+
ALLBRIDGE_API_KEY: z.string().optional(),
5658
})
5759

5860
// Provide defaults in development

0 commit comments

Comments
 (0)