From b324282eecce0fbe8f6162a4c6f63546d40b4893 Mon Sep 17 00:00:00 2001 From: Sirapop Na Ranong Date: Wed, 30 Oct 2024 21:21:27 +0700 Subject: [PATCH 1/2] feat: use GET request with queryString for most of requests --- package-lock.json | 4 ++-- package.json | 2 +- src/lib/api/axelarscan.js | 6 ++++-- src/lib/api/gmp.js | 6 ++++-- src/lib/api/token-transfer.js | 8 +++++--- src/lib/api/validator.js | 18 ++++++++++-------- src/lib/parser.js | 6 ++++++ 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1263c894..d5a3515c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "axelarscan-ui", - "version": "0.1.13", + "version": "0.1.14", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "axelarscan-ui", - "version": "0.1.13", + "version": "0.1.14", "license": "MIT", "dependencies": { "@axelar-network/axelarjs-sdk": "0.16.3-alpha.1", diff --git a/package.json b/package.json index 3f1db15e..92415064 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axelarscan-ui", - "version": "0.1.13", + "version": "0.1.14", "description": "Axelarscan UI", "scripts": { "dev": "next dev", diff --git a/src/lib/api/axelarscan.js b/src/lib/api/axelarscan.js index 61da1635..054f93d3 100644 --- a/src/lib/api/axelarscan.js +++ b/src/lib/api/axelarscan.js @@ -1,5 +1,7 @@ -const request = async (method, params) => { - const response = await fetch(`${process.env.NEXT_PUBLIC_AXELARSCAN_API_URL}/${method}`, { method: 'POST', body: JSON.stringify(params) }).catch(error => { return null }) +import { objToQS } from '@/lib/parser' + +const request = async (method, params, requestMethod = 'GET') => { + const response = await fetch(`${process.env.NEXT_PUBLIC_AXELARSCAN_API_URL}/${method}${requestMethod === 'GET' ? objToQS(params) : ''}`, { method: requestMethod, body: requestMethod === 'GET' ? undefined : JSON.stringify(params) }).catch(error => { return null }) return response && await response.json() } diff --git a/src/lib/api/gmp.js b/src/lib/api/gmp.js index f504a067..52eecc81 100644 --- a/src/lib/api/gmp.js +++ b/src/lib/api/gmp.js @@ -1,5 +1,7 @@ -const request = async (method, params) => { - const response = await fetch(`${process.env.NEXT_PUBLIC_GMP_API_URL}/${method}`, { method: 'POST', body: JSON.stringify(params) }).catch(error => { return null }) +import { objToQS } from '@/lib/parser' + +const request = async (method, params, requestMethod = 'GET') => { + const response = await fetch(`${process.env.NEXT_PUBLIC_GMP_API_URL}/${method}${requestMethod === 'GET' ? objToQS(params) : ''}`, { method: requestMethod, body: requestMethod === 'GET' ? undefined : JSON.stringify(params) }).catch(error => { return null }) return response && await response.json() } diff --git a/src/lib/api/token-transfer.js b/src/lib/api/token-transfer.js index 4bbc1216..b0117403 100644 --- a/src/lib/api/token-transfer.js +++ b/src/lib/api/token-transfer.js @@ -1,5 +1,7 @@ -const request = async (method, params) => { - const response = await fetch(`${process.env.NEXT_PUBLIC_TOKEN_TRANSFER_API_URL}/${method}`, { method: 'POST', body: JSON.stringify(params) }).catch(error => { return null }) +import { objToQS } from '@/lib/parser' + +const request = async (method, params, requestMethod = 'GET') => { + const response = await fetch(`${process.env.NEXT_PUBLIC_TOKEN_TRANSFER_API_URL}/${method}${requestMethod === 'GET' ? objToQS(params) : ''}`, { method: requestMethod, body: requestMethod === 'GET' ? undefined : JSON.stringify(params) }).catch(error => { return null }) return response && await response.json() } @@ -12,4 +14,4 @@ export const transfersTotalActiveUsers = async params => await request('transfer export const transfersTopUsers = async params => await request('transfersTopUsers', params) export const searchDepositAddresses = async params => await request('searchDepositAddresses', params) export const searchBatches = async params => await request('searchBatches', params) -export const getBatch = async (chain, batchId) => await request('lcd', { path: `/axelar/evm/v1beta1/batched_commands/${chain}/${batchId}` }) +export const getBatch = async (chain, batchId) => await request('lcd', { path: `/axelar/evm/v1beta1/batched_commands/${chain}/${batchId}` }, 'POST') diff --git a/src/lib/api/validator.js b/src/lib/api/validator.js index f70025ff..0a129afd 100644 --- a/src/lib/api/validator.js +++ b/src/lib/api/validator.js @@ -1,14 +1,16 @@ -const request = async (method, params) => { - const response = await fetch(`${process.env.NEXT_PUBLIC_VALIDATOR_API_URL}/${method}`, { method: 'POST', body: JSON.stringify(params) }).catch(error => { return null }) +import { objToQS } from '@/lib/parser' + +const request = async (method, params, requestMethod = 'GET') => { + const response = await fetch(`${process.env.NEXT_PUBLIC_VALIDATOR_API_URL}/${method}${requestMethod === 'GET' ? objToQS(params) : ''}`, { method: requestMethod, body: requestMethod === 'GET' ? undefined : JSON.stringify(params) }).catch(error => { return null }) return response && await response.json() } -export const rpc = async params => await request('rpc', params) -export const getRPCStatus = async params => await request('rpc', { ...params, path: '/status' }) -export const lcd = async params => await request('lcd', params) -export const getBlock = async height => await request('lcd', { path: `/cosmos/base/tendermint/v1beta1/blocks/${height}` }) -export const getValidatorSets = async (height = 'latest') => await request('lcd', { path: `/validatorsets/${height}` }) -export const getTransaction = async txhash => await request('lcd', { path: `/cosmos/tx/v1beta1/txs/${txhash}` }) +export const rpc = async params => await request('rpc', params, 'POST') +export const getRPCStatus = async params => await request('rpc', { ...params, path: '/status' }, 'POST') +export const lcd = async params => await request('lcd', params, 'POST') +export const getBlock = async height => await request('lcd', { path: `/cosmos/base/tendermint/v1beta1/blocks/${height}` }, 'POST') +export const getValidatorSets = async (height = 'latest') => await request('lcd', { path: `/validatorsets/${height}` }, 'POST') +export const getTransaction = async txhash => await request('lcd', { path: `/cosmos/tx/v1beta1/txs/${txhash}` }, 'POST') export const searchBlocks = async params => await request('searchBlocks', params) export const searchTransactions = async params => await request('searchTransactions', params) export const getTransactions = async params => await request('getTransactions', params) diff --git a/src/lib/parser.js b/src/lib/parser.js index 71a36083..16f2eb6c 100644 --- a/src/lib/parser.js +++ b/src/lib/parser.js @@ -3,6 +3,12 @@ const { base64, getAddress, hexlify, toUtf8String } = { ...utils } const decodeBase64 = base64.decode import _ from 'lodash' +export const objToQS = obj => { + const qs = Object.entries({ ...obj }).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&') + if (!qs) return '' + return `?${qs}` +} + export const getIcapAddress = string => { try { return string?.startsWith('0x') ? getAddress(string) : string From 626080d49043c2fd65bf2d90f5b99667641eabd8 Mon Sep 17 00:00:00 2001 From: Sirapop Date: Wed, 30 Oct 2024 22:12:27 +0700 Subject: [PATCH 2/2] chore: remove unused break --- src/components/Account.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/Account.jsx b/src/components/Account.jsx index f8b6f1f5..b3bfaa1b 100644 --- a/src/components/Account.jsx +++ b/src/components/Account.jsx @@ -375,7 +375,6 @@ function Delegations({ data }) { return toArray(delegations?.data).length > 0 case 'redelegations': return toArray(redelegations?.data).length > 0 - break case 'unstakings': return toArray(unbondings?.data).length > 0 default: