Skip to content

Commit

Permalink
Replace Subscan API (#201)
Browse files Browse the repository at this point in the history
* WIP: replace subscan API

* adjustments

* last fixes

* fix lint errors

* remove console.log

* fix build error

* rename subscan api consts
  • Loading branch information
TopETH authored Jul 31, 2024
1 parent ebd3490 commit 115b53b
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 244 deletions.
10 changes: 6 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
WS_ROCOCO_CORETIME_CHAIN="WSS endpoint of the coretime chain on Rococo"
WS_KUSAMA_CORETIME_CHAIN="WSS endpoint of the coretime chain on Kusama"
WS_WESTEND_CORETIME_CHAIN="WSS endpiontof the coretime chain on Westend"
WS_WESTEND_CORETIME_CHAIN="WSS endpiont of the coretime chain on Westend"

WS_ROCOCO_RELAY_CHAIN="WSS endpoint of the coretime relay chain - Rococo"
WS_KUSAMA_RELAY_CHAIN="WSS endpoint of the coretime relay chain - Kusama"
WS_WESTEND_RELAY_CHAIN="WSS endpoint of the coretime relay chain - Westend"

WS_REGIONX_COCOS_CHAIN="WSS endpoint of the regionx chain"

SUBSCAN_CORETIME_ROCOCO_API="API endpoint for Rococo Coretime"
SUBSCAN_CORETIME_KUSAMA_API="API endpoint for Kusama Coretime"
SUBSCAN_CORETIME_WESTEND_API="API endpoint for Westend Coretime"
ROCOCO_CORETIME_INDEXER="Subquery indexer for Rococo Coretime"
KUSAMA_CORETIME_INDEXER="Subquery indexer for Kusama Coretime"

ROCOCO_CORETIME_DICT="Subquery dictionary for Rococo Coretime"
KUSAMA_CORETIME_DICT="Subquery dictionary for Kusama Coretime"

EXPERIMENTAL=false
13 changes: 9 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ const nextConfig = {
WS_KUSAMA_CORETIME_CHAIN: process.env.WS_KUSAMA_CORETIME_CHAIN || '',
WS_WESTEND_CORETIME_CHAIN: process.env.WS_WESTEND_CORETIME_CHAIN || '',

SUBSCAN_CORETIME_ROCOCO_API: process.env.SUBSCAN_CORETIME_ROCOCO_API || '',
SUBSCAN_CORETIME_KUSAMA_API: process.env.SUBSCAN_CORETIME_KUSAMA_API || '',
SUBSCAN_CORETIME_WESTEND_API:
process.env.SUBSCAN_CORETIME_WESTEND_API || '',
ROCOCO_CORETIME_INDEXER: process.env.ROCOCO_CORETIME_INDEXER || '',
KUSAMA_CORETIME_INDEXER: process.env.KUSAMA_CORETIME_INDEXER || '',
SUBSCAN_CORETIME_WESTEND_INDEXER:
process.env.SUBSCAN_CORETIME_WESTEND_INDEXER || '',

ROCOCO_CORETIME_DICT: process.env.ROCOCO_CORETIME_DICT || '',
KUSAMA_CORETIME_DICT: process.env.KUSAMA_CORETIME_DICT || '',
SUBSCAN_CORETIME_WESTEND_DICT:
process.env.SUBSCAN_CORETIME_WESTEND_DICT || '',

WS_REGIONX_COCOS_CHAIN: process.env.WS_REGIONX_COCOS_CHAIN || '',

Expand Down
91 changes: 55 additions & 36 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
import { SUBSCAN_CORETIME_API } from '@/consts';
import { Address, NetworkType } from '@/models';
import { API_CORETIME_DICT, API_CORETIME_INDEXER } from '@/consts';
import { Address, ApiResponse, NetworkType } from '@/models';

import { fetchGraphql } from '../utils/fetchGraphql';

export const fetchPurchaseHistoryData = async (
network: NetworkType,
regionBegin: number,
page: number,
row: number
) => {
const res = await fetch(
`${SUBSCAN_CORETIME_API[network]}/api/scan/broker/purchased`,
{
method: 'POST',
body: JSON.stringify({
region_begin: regionBegin,
row,
page,
}),
}
);
return res;
after: string | null,
orderBy = 'HEIGHT_DESC'
): Promise<ApiResponse> => {
const query = `{
purchases(
after: ${after}
filter: {begin: {equalTo: ${regionBegin}}}
orderBy: ${orderBy}
) {
nodes {
account
core
extrinsicId
height
price
purchaseType
timestamp
}
pageInfo {
hasNextPage
endCursor
}
totalCount
}
}`;
return fetchGraphql(API_CORETIME_INDEXER[network], query);
};

export const fetchAccountExtrinsics = async (
network: NetworkType,
address: Address,
page: number,
row: number
) => {
const res = await fetch(
`${SUBSCAN_CORETIME_API[network]}/api/v2/scan/extrinsics`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
address,
row,
page,
}),
}
);
return res;
after: string | null,
orderBy = 'BLOCK_HEIGHT_DESC'
): Promise<ApiResponse> => {
const query = `{
extrinsics(
after: ${after}
filter: {signer: {equalTo: "${address}"}}
orderBy: ${orderBy}
) {
nodes {
id
module
call
blockHeight
success
timestamp
}
pageInfo {
hasNextPage
endCursor
}
totalCount
}
}`;
return fetchGraphql(API_CORETIME_DICT[network], query);
};
11 changes: 5 additions & 6 deletions src/components/Elements/Selectors/NetworkSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useRouter } from 'next/router';
import {
Kusama as KusamaIcon,
Rococo as RococoIcon,
Westend as WestendIcon,
} from '@/assets/networks/relay';
import { useNetwork } from '@/contexts/network';
import { NetworkType } from '@/models';
Expand Down Expand Up @@ -36,11 +35,11 @@ const RelaySelect = () => {
label: 'Kusama',
icon: KusamaIcon,
},
{
value: NetworkType.WESTEND,
label: 'Westend',
icon: WestendIcon,
},
// {
// value: NetworkType.WESTEND,
// label: 'Westend',
// icon: WestendIcon,
// },
];

return (
Expand Down
7 changes: 1 addition & 6 deletions src/components/Modals/Regions/PurchaseHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ export const PurchaseHistoryModal = ({
const {
saleInfo: { regionBegin },
} = useSaleInfo();
const { loading, data, isError } = usePurchaseHistory(
network,
regionBegin,
0,
1000
);
const { loading, data, isError } = usePurchaseHistory(network, regionBegin);

return (
<Dialog
Expand Down
7 changes: 5 additions & 2 deletions src/components/Tables/PurchaseHistoryTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const PurchaseHistoryTable = ({ data }: PurchaseHistoryTableProps) => {
: data
).map(
(
{ address, core, extrinsic_index, timestamp, price, type },
{ address, core, extrinsicId: extrinsic_index, timestamp, price, type },
index
) => (
<StyledTableRow key={index}>
Expand Down Expand Up @@ -107,7 +107,10 @@ export const PurchaseHistoryTable = ({ data }: PurchaseHistoryTableProps) => {
</StyledTableCell>
<StyledTableCell align='center'>{type}</StyledTableCell>
<StyledTableCell align='center'>
{timeAgo.format(timestamp * 1000, 'round-minute')}
{timeAgo.format(
new Date(timestamp).getTime(),
'round-minute'
)}
</StyledTableCell>
</StyledTableRow>
)
Expand Down
5 changes: 4 additions & 1 deletion src/components/Tables/TxHistoryTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export const TxHistoryTable = ({ data }: TxHistoryTableProps) => {
)}
</StyledTableCell>
<StyledTableCell align='center'>
{timeAgo.format(timestamp * 1000, 'round-minute')}
{timeAgo.format(
new Date(timestamp).getTime(),
'round-minute'
)}
</StyledTableCell>
</StyledTableRow>
)
Expand Down
15 changes: 11 additions & 4 deletions src/consts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { NetworkType } from '@/models';

export const APP_NAME = 'coretime-hub';

export const SUBSCAN_CORETIME_API = {
[NetworkType.ROCOCO]: process.env.SUBSCAN_CORETIME_ROCOCO_API ?? '',
[NetworkType.KUSAMA]: process.env.SUBSCAN_CORETIME_KUSAMA_API ?? '',
[NetworkType.WESTEND]: process.env.SUBSCAN_CORETIME_WESTEND_API ?? '',
export const API_CORETIME_INDEXER = {
[NetworkType.ROCOCO]: process.env.ROCOCO_CORETIME_INDEXER ?? '',
[NetworkType.KUSAMA]: process.env.KUSAMA_CORETIME_INDEXER ?? '',
[NetworkType.WESTEND]: process.env.SUBSCAN_CORETIME_WESTEND_INDEXER ?? '',
[NetworkType.NONE]: '',
};

export const API_CORETIME_DICT = {
[NetworkType.ROCOCO]: process.env.ROCOCO_CORETIME_DICT ?? '',
[NetworkType.KUSAMA]: process.env.KUSAMA_CORETIME_DICT ?? '',
[NetworkType.WESTEND]: process.env.SUBSCAN_CORETIME_WESTEND_DICT ?? '',
[NetworkType.NONE]: '',
};

Expand Down
4 changes: 2 additions & 2 deletions src/contexts/balance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const BalanceProvider = ({ children }: Props) => {
if (!regionxApi || regionxApiState !== ApiState.READY) return;
unsubscribeRegionx = await regionxApi.queryMulti(
[
[regionxApi.query.system.account, address],
[regionxApi.query.tokens.accounts, [address, 1]], // RELAY_ASSET_ID
[regionxApi.query.system?.account, address],
[regionxApi.query.tokens?.accounts, [address, 1]], // RELAY_ASSET_ID
],
([
{
Expand Down
64 changes: 33 additions & 31 deletions src/hooks/accountExtrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fetchAccountExtrinsics } from '@/apis';
import {
AccountTxHistoryItem,
Address,
ExtrinsicsResponse,
ApiResponse,
NetworkType,
} from '@/models';

Expand All @@ -25,40 +25,42 @@ export const useAccountExtrinsics = (
try {
setLoading(true);

let page = 0;
const txHistory: AccountTxHistoryItem[] = [];

for (;;) {
const res = await fetchAccountExtrinsics(network, account, page, 100);
if (res.status !== 200) {
setError(true);
break;
} else {
const { message, data } = await res.json();
if (message !== 'Success') {
setError(true);
} else {
const { count, extrinsics } = data as ExtrinsicsResponse;
let finished = false;
let after: string | null = null;

if (extrinsics !== null) {
txHistory.push(
...extrinsics.map(
(item) =>
({
extrinsicId: item.extrinsic_index,
module: item.call_module,
call: item.call_module_function,
timestamp: item.block_timestamp,
success: item.success,
}) as AccountTxHistoryItem
)
);
}
const result = [];
while (!finished) {
const res: ApiResponse = await fetchAccountExtrinsics(
network,
account,
after
);
const { status, data } = res;
if (status !== 200) break;

++page;
if (txHistory.length === count) break;
}
}
if (data.extrinsics.nodes !== null)
result.push(...data.extrinsics.nodes);

finished = !data.extrinsics.pageInfo.hasNextPage;
after = data.extrinsics.pageInfo.endCursor;
}
if (!finished) {
setError(true);
} else {
txHistory.push(
...result.map(
(item) =>
({
extrinsicId: item.id,
module: item.module,
call: item.call,
timestamp: item.timestamp,
success: item.success,
} as AccountTxHistoryItem)
)
);
}

setData(txHistory);
Expand Down
Loading

0 comments on commit 115b53b

Please sign in to comment.