Skip to content

Commit fd7aa5d

Browse files
committed
replace subgraph
1 parent 6f82216 commit fd7aa5d

File tree

8 files changed

+509
-1647
lines changed

8 files changed

+509
-1647
lines changed

components/APISourceToggle.tsx

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
1-
// Borrowed from: https://dev.to/murtuzaalisurti/dark-mode-toggle-animation-using-css-27il
21
import React from "react";
2+
import Toggle from 'react-toggle'
3+
import "react-toggle/style.css"
34

4-
const DarkModeToggle: React.FC<{ isDarkModeOn: boolean }> = ({
5-
isDarkModeOn,
6-
}) => {
7-
// React.useEffect(() => {
8-
// if (isDarkModeOn) {
9-
// document.querySelector(".sun-logo").classList.remove("animate-sun");
10-
// document.querySelector(".moon-logo").classList.remove("animate-moon");
11-
// } else {
12-
// document.querySelector(".sun-logo").classList.add("animate-sun");
13-
// document.querySelector(".moon-logo").classList.add("animate-moon");
14-
// }
15-
// }, [isDarkModeOn]);
5+
const APISourceToggle: React.FC = () => {
6+
const [apiUsingLoopring, setApiUsingLoopring] = React.useState(true)
7+
React.useEffect(() => {
8+
setApiUsingLoopring(window.localStorage.getItem('apiUsingLoopring') === 'false' ? false : true)
9+
}, []);
1610
return (
17-
<div className="container p-1 rounded flex items-center justify-center">
18-
<div className="sun sun-logo flex items-center justify-center">
19-
<svg viewBox="0 0 512 512" width="16">
20-
<path
21-
d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"
22-
fill="#FFF"
23-
/>
24-
</svg>
25-
</div>
26-
<div className="moon moon-logo flex items-center justify-center">
27-
<svg viewBox="0 0 512 512" width="16" className="z-1">
28-
<path
29-
d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 0 0 283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"
30-
stroke="#000"
31-
fill="#000"
32-
/>
33-
</svg>
34-
</div>
35-
</div>
11+
<label className="border-b border-t p-2 lg:border-none lg:p-0" style={{display: 'flex'}}>
12+
<Toggle
13+
checked={apiUsingLoopring}
14+
onChange={() => {
15+
window.localStorage.setItem('apiUsingLoopring', !apiUsingLoopring ? 'true' : 'false')
16+
setApiUsingLoopring(!apiUsingLoopring)
17+
}}
18+
/>
19+
<span style={{
20+
marginLeft: '4px',
21+
fontSize: '12px',
22+
}}> API Source: {apiUsingLoopring ? 'Loopring' : 'Subgraph'}</span>
23+
</label>
3624
);
3725
};
3826

39-
export default DarkModeToggle;
27+
export default APISourceToggle;

hooks/useTransaction.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11

22

33
import React from "react";
4-
import { useTransactionQuery } from "../generated/loopringExplorer";
5-
import { convertTransactionData, getBlock, getTransactionData } from "../utils/transaction";
6-
import { dataByBlockIdAndIndex } from 'loopring36-block-parser';
4+
import { getTransactionData } from "../utils/transaction";
5+
76
export const useTransaction = ({ txId }: { txId: string }) => {
87
const [blockId, index] = txId.split('-');
98
const [data, setData] = React.useState(undefined as any | undefined)
109
const [loading, setLoading] = React.useState(false)
10+
const [failed, setFailed] = React.useState(false)
1111
React.useEffect(() => {
1212
setLoading(true)
1313
getTransactionData(Number(blockId), Number(index))
1414
.then(setData)
15+
.catch(() => {
16+
setFailed(true)
17+
})
1518
}, [])
1619

1720
return {
1821
loading,
19-
data
22+
data,
23+
failed
2024
}
2125
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
"graphql": "^16.4.0",
2222
"graphql-request": "^3.4.0",
2323
"js-cookie": "^3.0.1",
24-
"loopring36-block-parser": "/Users/lijingguo/Codes/loopring36_block_parser",
24+
"loopring36-block-parser2": "^0.0.1",
2525
"next": "^12.1.5",
2626
"numeral": "^2.0.6",
2727
"postcss-import": "^13.0.0",
2828
"postcss-preset-env": "7.4.4",
2929
"react": "^18.0.0",
3030
"react-dom": "^18.0.0",
3131
"react-icons": "^4.3.1",
32+
"react-toggle": "^4.1.3",
3233
"recharts": "2.1.9",
3334
"sass": "^1.32.8",
3435
"swr": "^0.5.6"

pages/_app.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '../styles/globals.scss';
1111
import DarkModeToggle from '../components/DarkModeToggle';
1212
import ConsentContextProvider from '../components/ConsentContextProvider';
1313
import apolloClient from '../graphql';
14+
import APISourceToggle from '../components/APISourceToggle';
1415

1516
const MyApp: React.FC<AppProps> = ({ Component, pageProps }) => {
1617
const router = useRouter();
@@ -61,7 +62,7 @@ const MyApp: React.FC<AppProps> = ({ Component, pageProps }) => {
6162
<header className="bg-white w-screen px-4 py-2 dark:bg-loopring-dark-background">
6263
<div className="container h-full w-full lg:w-11/12 m-auto flex md:items-center justify-between">
6364
<Link href="/">
64-
<a className="h-full flex items-center w-4/6 cursor-pointer">
65+
<a className="h-full flex items-center w-3/6 cursor-pointer">
6566
<Image
6667
src={darkMode ? '/logo-white.svg' : '/logo-blue.svg'}
6768
width="100"
@@ -119,9 +120,7 @@ const MyApp: React.FC<AppProps> = ({ Component, pageProps }) => {
119120
Smart Wallet
120121
</a>
121122
</Link>
122-
<button onClick={toggleDarkMode} className="self-start p-2 lg:p-0">
123-
<DarkModeToggle isDarkModeOn={darkMode} />
124-
</button>
123+
<APISourceToggle />
125124
<button onClick={toggleDarkMode} className="self-start p-2 lg:p-0">
126125
<DarkModeToggle isDarkModeOn={darkMode} />
127126
</button>

pages/index.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@ import Pairs from '../components/Pairs';
1010
import Transactions from '../components/Transactions';
1111
import NetworkStats from '../components/NetworkStats';
1212
import { useNetworkStatsQuery } from '../generated/loopringExplorer';
13-
import { dataByBlockIdAndIndex } from 'loopring36-block-parser';
1413

15-
// dataByBlockIdAndIndex('mainnet')(11431, 2)
16-
// .then(x => {
17-
// console.log('sdjksdjsk', JSON.stringify(x))
18-
// // debugger
19-
// })
20-
// .catch(x => {
21-
// debugger
22-
// })
23-
// console.log('aaaa', JSON.stringify(a))
2414
export const getServerSideProps: GetServerSideProps = async () => {
2515
try {
2616
const res = await client.query({ query: FETCH_NETWORK_STATS });

pages/tx/[id].tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import NoTransactionFound from '../../components/transactionDetail/NoTransaction
2323
import { useTransactionQuery } from '../../generated/loopringExplorer';
2424
import { useTransaction } from '../../hooks/useTransaction';
2525

26-
export const Transaction: React.FC<{ txId: string }> = ({ txId }) => {
27-
const {data, loading} = useTransaction({txId})
26+
27+
28+
29+
const TransactionRaw: React.FC<{ txId: string, data, loading }> = ({ txId, data, loading }) => {
30+
// const {data, loading} = useTransaction({txId})
2831

2932
// const { data, loading } = useTransactionQuery({
3033
// variables: {
@@ -102,6 +105,23 @@ export const Transaction: React.FC<{ txId: string }> = ({ txId }) => {
102105
);
103106
};
104107

108+
const TransactionBySubgraph: React.FC<{ txId: string }> = ({txId}) => {
109+
const { data, loading } = useTransactionQuery({
110+
variables: {
111+
id: txId,
112+
},
113+
});
114+
return <TransactionRaw data={data} loading={loading} txId={txId}/>
115+
}
116+
export const Transaction: React.FC<{ txId: string }> = ({ txId }) => {
117+
const {data, loading, failed} = useTransaction({txId})
118+
const useLegacy = failed || localStorage.getItem('apiUsingLoopring') === 'false'
119+
console.log('useLegacy', useLegacy)
120+
return useLegacy
121+
? <TransactionBySubgraph txId={txId}/>
122+
: <TransactionRaw data={data} loading={loading} txId={txId}/>
123+
}
124+
105125
const TransactionPage: React.FC<{}> = () => {
106126
const router = useRouter();
107127
const txId = router.query.id as string;

utils/transaction.ts

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { BigNumber } from "ethers"
2-
import { formatUnits } from "ethers/lib/utils";
32
import { dataByBlockIdAndIndex } from 'loopring36-block-parser';
43

54
export const getBlock = (blockId: number) => fetch(`https://api3.loopring.io/api/v3/block/getBlock?id=${blockId}`)
@@ -11,6 +10,9 @@ export const getAccount = (accountId: number) => fetch(`https://api3.loopring.io
1110
export const getTokens = () => fetch(`https://api3.loopring.io/api/v3/exchange/tokens`)
1211
.then(x => x.json())
1312

13+
export const getPools = () => fetch(`https://api3.loopring.io/api/v3/amm/pools`)
14+
.then(x => x.json())
15+
1416
const convertTransactionData_Transfer = async (origin: any) => {
1517
const fromAddress = (await getAccount(origin.accountFromID)).owner
1618
const toAddress = (await getAccount(origin.accountToID)).owner
@@ -67,30 +69,19 @@ const convertTransactionData_Withdraw = async (origin: any) => {
6769
}
6870
}
6971
}
70-
// todo
72+
7173
const convertTransactionData_Swap = async (origin: any) => {
7274
const tokens = await getTokens()
73-
// const tokenInfo = tokens.find(x => x.tokenId === origin.tokenID)
74-
const tokenAInfo = tokens.find(x => x.tokenId === origin.tokenAB)
75-
const tokenBInfo = tokens.find(x => x.tokenId === origin.tokenBB)
76-
75+
const { pools } = await getPools()
76+
const tokenAInfo = tokens.find(x => x.tokenId === origin.tokenAS)
77+
const tokenBInfo = tokens.find(x => x.tokenId === origin.tokenBS)
78+
const found = pools.find(x => {
79+
const [t1Id, t2Id] = x.tokens.pooled
80+
return (origin.tokenAB === t1Id && origin.tokenBB === t2Id)
81+
|| (origin.tokenAB === t2Id && origin.tokenBB === t1Id)
82+
})
83+
// debugger
7784
const accountAddress = (await getAccount(origin.accountIdA)).owner
78-
// const {
79-
// block,
80-
// account,
81-
// tokenA,
82-
// tokenB,
83-
// data,
84-
// fillSA,
85-
// fillSB,
86-
// tokenAPrice,
87-
// tokenBPrice,
88-
// pair,
89-
// feeA,
90-
// feeB,
91-
// pool,
92-
// __typename,
93-
// }
9485
return {
9586
transaction: {
9687
account: {
@@ -107,14 +98,19 @@ const convertTransactionData_Swap = async (origin: any) => {
10798
},
10899
fillSA: origin.fillSA,
109100
fillSB: origin.fillSB,
110-
111-
tokenAPrice: formatUnits(origin.fillSA.toString(), tokenAInfo.decimals), //todo
112-
tokenBPrice: formatUnits(origin.fillSB.toString(), tokenBInfo.decimals), //todo
113-
pair: '0x0000000000000000000000000000000000000000', //todo
114-
feeA: origin.feeA,
101+
tokenBPrice: BigNumber.from(origin.fillSA.toString())
102+
.mul('1' + '0'.repeat(tokenBInfo.decimals))
103+
.div(origin.fillSB.toString()),
104+
tokenAPrice: BigNumber.from(origin.fillSB.toString())
105+
.mul('1' + '0'.repeat(tokenAInfo.decimals))
106+
.div(origin.fillSA.toString()),
107+
pair: {
108+
id: found.tokens.pooled.join('-')
109+
},
110+
feeA: origin.feeA,
115111
feeB: origin.feeB,
116112
pool: {
117-
address: '0x0000000000000000000000000000000000000000', //todo
113+
address: found.address,
118114
},
119115
__typename: "Swap",
120116
data: origin.txData,
@@ -123,27 +119,12 @@ const convertTransactionData_Swap = async (origin: any) => {
123119
}
124120
const convertTransactionData_Trade = async (origin: any) => {
125121
const tokens = await getTokens()
126-
// const tokenInfo = tokens.find(x => x.tokenId === origin.tokenID)
127122
const tokenAInfo = tokens.find(x => x.tokenId === origin.tokenAS)
128123
const tokenBInfo = tokens.find(x => x.tokenId === origin.tokenBS)
129124

130125
const accountAddressA = (await getAccount(origin.accountIdA)).owner
131126
const accountAddressB = (await getAccount(origin.accountIdB)).owner
132127

133-
// const {
134-
// block,
135-
// accountA,
136-
// accountB,
137-
// tokenA,
138-
// tokenB,
139-
// data,
140-
// fillSA,
141-
// fillSB,
142-
// feeA,
143-
// feeB,
144-
// tokenAPrice,
145-
// tokenBPrice,
146-
// } = transaction;
147128
return {
148129
transaction: {
149130
accountA: {
@@ -164,28 +145,20 @@ const convertTransactionData_Trade = async (origin: any) => {
164145
},
165146
fillSA: origin.fillSA,
166147
fillSB: origin.fillSB,
167-
// BN
168148
tokenBPrice: BigNumber.from(origin.fillSA.toString())
169149
.mul('1' + '0'.repeat(tokenBInfo.decimals))
170-
.div(origin.fillSB.toString()), //todo
150+
.div(origin.fillSB.toString()),
171151
tokenAPrice: BigNumber.from(origin.fillSB.toString())
172152
.mul('1' + '0'.repeat(tokenAInfo.decimals))
173-
.div(origin.fillSA.toString()), //todo
174-
// tokenBPrice: origin.fillSB.mul('1' + '0'.repeat(tokenAInfo.decimals)).div(origin.fillSA), //todo
175-
// tokenAPrice: formatUnits(origin.fillSA.toString(), tokenAInfo.decimals), //todo
176-
// tokenBPrice: formatUnits(origin.fillSB.toString(), tokenBInfo.decimals), //todo
177-
// pair: '0x0000000000000000000000000000000000000000', //todo
178-
feeA: origin.feeA,
179-
feeB: origin.feeB,
180-
// pool: {
181-
// address: '0x0000000000000000000000000000000000000000', //todo
182-
// },
153+
.div(origin.fillSA.toString()),
154+
feeA: origin.feeA,
155+
feeB: origin.feeB,
183156
__typename: "OrderbookTrade",
184157
data: origin.txData,
185158
}
186159
}
187160
}
188-
// 040002a6ea000e62d800002cff000267e00006000019aee56156c61900000000000000000000000000000000000000000000000000000000000000000000000000000000
161+
189162
const convertTransactionData_Deposit = async (origin: any) => {
190163
const tokens = await getTokens()
191164
const tokenInfo = tokens.find(x => x.tokenId === origin.tokenID)
@@ -305,7 +278,6 @@ const convertTransactionData_Pre = (origin: any) => {
305278
}
306279
}
307280
export const convertTransactionData = async (origin: any) => {
308-
// debugger
309281
const nextOrigin = convertTransactionData_Pre(origin)
310282
if (nextOrigin.type === 'TRANSFER') {
311283
return convertTransactionData_Transfer(nextOrigin)
@@ -332,7 +304,7 @@ export const convertTransactionData = async (origin: any) => {
332304
export const getTransactionData = (blockId: number, index: number) => {
333305
return Promise.all([
334306
getBlock(blockId),
335-
dataByBlockIdAndIndex('mainnet')(blockId, Number(index))
307+
dataByBlockIdAndIndex('mainnet', 'https://mainnet.infura.io/v3/a06ed9c6b5424b61beafff27ecc3abf3')(blockId, Number(index))
336308
.then(convertTransactionData)
337309
]).then(([blockRaw, data]) => {
338310
const block = {

0 commit comments

Comments
 (0)