diff --git a/package-lock.json b/package-lock.json index a81a003f4..370af78d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -118,7 +118,7 @@ "path-browserify": "^1.0.1", "postcss": "^8.4.6", "postcss-cli": "^8.3.1", - "prettier": "^2.3.1", + "prettier": "^2.8.8", "process": "^0.11.10", "redux-devtools-extension": "^2.13.9", "shelljs": "^0.8.5", @@ -22266,8 +22266,7 @@ "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "extraneous": true, "os": [ - "darwin", - "linux" + "darwin" ], "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" diff --git a/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.integration.test.ts b/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.integration.test.ts index cffd9501d..c3858275e 100644 --- a/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.integration.test.ts +++ b/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.integration.test.ts @@ -12,14 +12,18 @@ const mockUseProviderContext = useProviderContext as jest.Mock; describe("Fetch Escrow Transfers", () => { jest.setTimeout(15000); - beforeAll(() => { mockUseProviderContext.mockReturnValue({ provider: amoyProvider, providerOrSigner: amoyProvider }); }); describe("fetch from title escrow transfers", () => { it("should return parsed transfer logs in valid format", async () => { - const escrowTransfers = await fetchEscrowTransfers(amoyProvider, "0x1F6D8888Fc6B75E10b1840620E8229C3C487a925"); + const blockNo = 6000000; + const escrowTransfers = await fetchEscrowTransfers( + amoyProvider, + "0x1F6D8888Fc6B75E10b1840620E8229C3C487a925", + blockNo + ); expect(escrowTransfers).toEqual([ { type: "TRANSFER_HOLDER", @@ -211,7 +215,8 @@ describe("Fetch Escrow Transfers", () => { describe("fetch from title escrow owner transfers", () => { it("should return parsed transfer logs for beneficiary in valid format", async () => { const titleEscrow = TitleEscrow__factory.connect("0x1F6D8888Fc6B75E10b1840620E8229C3C487a925", amoyProvider); - const ownerTransfers = await fetchOwnerTransfers(titleEscrow, amoyProvider); + const blockNo = 6000000; + const ownerTransfers = await fetchOwnerTransfers(titleEscrow, amoyProvider, blockNo); expect(ownerTransfers).toEqual([ { type: "TRANSFER_BENEFICIARY", @@ -248,7 +253,8 @@ describe("Fetch Escrow Transfers", () => { describe("fetch from title escrow holder transfers", () => { it("should return parsed transfer logs for holder in valid format", async () => { const titleEscrow = TitleEscrow__factory.connect("0x1F6D8888Fc6B75E10b1840620E8229C3C487a925", amoyProvider); - const holderTransfers = await fetchHolderTransfers(titleEscrow, amoyProvider); + const blockNo = 6000000; + const holderTransfers = await fetchHolderTransfers(titleEscrow, amoyProvider, blockNo); expect(holderTransfers).toEqual([ { type: "TRANSFER_HOLDER", diff --git a/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.ts b/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.ts index 6a5fd3003..3b21908ea 100644 --- a/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.ts +++ b/src/common/hooks/useEndorsementChain/fetchEscrowTransfer.ts @@ -5,14 +5,15 @@ import { EventFragment, Result } from "ethers/lib/utils"; export const fetchEscrowTransfers = async ( provider: providers.Provider, - address: string + address: string, + fromBlockNumber: number ): Promise => { const titleEscrowContract = TitleEscrow__factory.connect(address, provider); const isTitleEscrow = await titleEscrowContract.supportsInterface("0x079dff60"); if (!isTitleEscrow) throw new Error(`Contract ${address} is not a title escrow contract`); // https://ethereum.stackexchange.com/questions/109326/combine-multiple-event-filters-in-ethersjs - const holderChangeLogsDeferred = await fetchHolderTransfers(titleEscrowContract, provider); - const ownerChangeLogsDeferred = await fetchOwnerTransfers(titleEscrowContract, provider); + const holderChangeLogsDeferred = await fetchHolderTransfers(titleEscrowContract, provider, fromBlockNumber); + const ownerChangeLogsDeferred = await fetchOwnerTransfers(titleEscrowContract, provider, fromBlockNumber); const [holderChangeLogs, ownerChangeLogs] = await Promise.all([holderChangeLogsDeferred, ownerChangeLogsDeferred]); return [...holderChangeLogs, ...ownerChangeLogs]; }; @@ -22,11 +23,11 @@ export const fetchEscrowTransfers = async ( */ export const fetchOwnerTransfers = async ( titleEscrowContract: TitleEscrow, - provider: providers.Provider + provider: providers.Provider, + fromBlockNumber: number ): Promise => { const ownerChangeFilter = titleEscrowContract.filters.BeneficiaryTransfer(null, null); - const ownerChangeLogs = await provider.getLogs({ ...ownerChangeFilter, fromBlock: 0 }); - + const ownerChangeLogs = await provider.getLogs({ ...ownerChangeFilter, fromBlock: fromBlockNumber }); const ownerChangeLogsParsed = getParsedLogs(ownerChangeLogs, titleEscrowContract); return ownerChangeLogsParsed.map((event) => ({ type: "TRANSFER_BENEFICIARY", @@ -68,10 +69,11 @@ export const getParsedLogs = (logs: providers.Log[], titleEscrow: TitleEscrow): */ export const fetchHolderTransfers = async ( titleEscrowContract: TitleEscrow, - provider: providers.Provider + provider: providers.Provider, + fromBlockNumber: number ): Promise => { const holderChangeFilter = titleEscrowContract.filters.HolderTransfer(null, null); - const holderChangeLogs = await provider.getLogs({ ...holderChangeFilter, fromBlock: 0 }); + const holderChangeLogs = await provider.getLogs({ ...holderChangeFilter, fromBlock: fromBlockNumber }); const holderChangeLogsParsed = getParsedLogs(holderChangeLogs, titleEscrowContract); return holderChangeLogsParsed.map((event) => ({ type: "TRANSFER_HOLDER", diff --git a/src/common/hooks/useEndorsementChain/fetchTokenTransfer.integration.test.ts b/src/common/hooks/useEndorsementChain/fetchTokenTransfer.integration.test.ts index 99e74e771..ddc3d1017 100644 --- a/src/common/hooks/useEndorsementChain/fetchTokenTransfer.integration.test.ts +++ b/src/common/hooks/useEndorsementChain/fetchTokenTransfer.integration.test.ts @@ -22,10 +22,13 @@ describe("Fetch Token Transfers", () => { "0x71D28767662cB233F887aD2Bb65d048d760bA694", amoyProvider ); + const blockNo = 6000000; if (!tokenRegistry) throw new Error("Unable to connect to token registry: Test Failed"); const tokenTransfers = await fetchTokenTransfers( + amoyProvider, tokenRegistry, - "0xe8b9fb84485cbf9dd59eabe1dcddf44d9e3ff820aedb7d02a138ef4a116f0ec9" + "0xe8b9fb84485cbf9dd59eabe1dcddf44d9e3ff820aedb7d02a138ef4a116f0ec9", + blockNo ); expect(tokenTransfers).toEqual([ { diff --git a/src/common/hooks/useEndorsementChain/fetchTokenTransfer.ts b/src/common/hooks/useEndorsementChain/fetchTokenTransfer.ts index 20ea15f36..7d0533d54 100644 --- a/src/common/hooks/useEndorsementChain/fetchTokenTransfer.ts +++ b/src/common/hooks/useEndorsementChain/fetchTokenTransfer.ts @@ -4,16 +4,19 @@ import { LogDescription } from "ethers/lib/utils"; import { BurnAddress, InitialAddress } from "../../../constants/chain-info"; import { TokenTransferEvent, TokenTransferEventType } from "../../../types"; import { sortLogChain } from "./helpers"; +import { providers } from "ethers"; export const fetchTokenTransfers = async ( + provider: providers.Provider, tokenRegistry: TradeTrustToken, - tokenId: string + tokenId: string, + fromBlockNumber: number ): Promise => { // Fetch transfer logs from token registry const tokenRegistryAddress = tokenRegistry.address; const identifyTokenTransferEvents = identifyTokenTransferEventsFunction(tokenRegistryAddress); const transferLogFilter = tokenRegistry.filters.Transfer(null, null, tokenId); - const logs = await tokenRegistry.queryFilter(transferLogFilter, 0); + const logs = await tokenRegistry.queryFilter(transferLogFilter, fromBlockNumber); if (logs.length === 0) { throw new Error("Unminted Title Escrow"); } diff --git a/src/common/hooks/useEndorsementChain/useEndorsementChain.ts b/src/common/hooks/useEndorsementChain/useEndorsementChain.ts index c7f051eb3..11d57ceda 100644 --- a/src/common/hooks/useEndorsementChain/useEndorsementChain.ts +++ b/src/common/hooks/useEndorsementChain/useEndorsementChain.ts @@ -8,7 +8,7 @@ import { fetchTokenTransfers } from "./fetchTokenTransfer"; import { getEndorsementChain } from "./retrieveEndorsementChain"; import { retrieveTitleEscrowAddressOnFactory } from "../useTitleEscrowContract"; import { getErrorMessage } from "../../utils/errorHandling"; - +import { ChainId, ChainInfo } from "../../../constants/chain-info"; export const useEndorsementChain = ( tokenRegistryAddress: string, tokenId: string @@ -31,9 +31,11 @@ export const useEndorsementChain = ( setEndorsementChain(undefined); setPending(true); try { - const tokenLogs = await fetchTokenTransfers(tokenRegistry, tokenId); + const chainId: ChainId = (await provider.getNetwork()).chainId; + const fromBlockNumber = ChainInfo[chainId]?.blockNumber ?? 0; + const tokenLogs = await fetchTokenTransfers(provider, tokenRegistry, tokenId, fromBlockNumber); const escrowAddress = await retrieveTitleEscrowAddressOnFactory(tokenRegistry, tokenId, providerOrSigner); - const titleEscrowLogs = await fetchEscrowTransfers(provider, escrowAddress); + const titleEscrowLogs = await fetchEscrowTransfers(provider, escrowAddress, fromBlockNumber); const transferEvents = mergeTransfers([...titleEscrowLogs, ...tokenLogs]); const retrievedEndorsementChain = await getEndorsementChain(provider, transferEvents); setEndorsementChain(retrievedEndorsementChain); diff --git a/src/constants/chain-info.ts b/src/constants/chain-info.ts index e9b1774c8..d3b060d90 100644 --- a/src/constants/chain-info.ts +++ b/src/constants/chain-info.ts @@ -6,6 +6,7 @@ export interface ChainInfoObject { chainId: ChainId; networkName: string; // network name that aligns with existing NETWORK_NAME networkLabel: string; + blockNumber?: number; explorerUrl: string; rpcUrl?: string; nativeCurrency?: { @@ -113,6 +114,7 @@ export const ChainInfo: ChainInfo = { iconImage: "/static/images/networks/xdc.png", networkName: "xdc", networkLabel: "XDC Network", + blockNumber: 60000000, explorerUrl: "https://xdcscan.io", rpcUrl: "https://rpc.ankr.com/xdc", nativeCurrency: { @@ -127,6 +129,7 @@ export const ChainInfo: ChainInfo = { iconImage: "/static/images/networks/xdc.png", networkName: "xdcapothem", networkLabel: "XDC Testnet Apothem", + blockNumber: 50000000, explorerUrl: "https://apothem.xdcscan.io", rpcUrl: "https://rpc.ankr.com/xdc_testnet", nativeCurrency: {