diff --git a/packages/engine/paima-funnel/src/cde/cardanoPool.ts b/packages/engine/paima-funnel/src/cde/cardanoPool.ts index 0197dffa4..9860c0228 100644 --- a/packages/engine/paima-funnel/src/cde/cardanoPool.ts +++ b/packages/engine/paima-funnel/src/cde/cardanoPool.ts @@ -13,7 +13,8 @@ export default async function getCdeData( fromAbsoluteSlot: number, toAbsoluteSlot: number, getBlockNumber: (slot: number) => number, - absoluteSlotToEpoch: (slot: number) => number + absoluteSlotToEpoch: (slot: number) => number, + network: string ): Promise { const events = await timeout( query(url, Routes.delegationForPool, { @@ -24,7 +25,7 @@ export default async function getCdeData( ); return events.map(e => - eventToCdeDatum(e, extension, getBlockNumber(e.slot), absoluteSlotToEpoch(e.slot)) + eventToCdeDatum(e, extension, getBlockNumber(e.slot), absoluteSlotToEpoch(e.slot), network) ); } @@ -32,7 +33,8 @@ function eventToCdeDatum( event: DelegationForPoolResponse[0], extension: ChainDataExtensionCardanoDelegation, blockNumber: number, - epoch: number + epoch: number, + network: string ): CdeCardanoPoolDatum { return { cdeId: extension.cdeId, @@ -44,5 +46,6 @@ function eventToCdeDatum( epoch, }, scheduledPrefix: extension.scheduledPrefix, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts b/packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts index 678be70c8..668e8bc25 100644 --- a/packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts +++ b/packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts @@ -13,7 +13,8 @@ export default async function getCdeProjectedNFTData( extension: ChainDataExtensionCardanoProjectedNFT, fromAbsoluteSlot: number, toAbsoluteSlot: number, - getBlockNumber: (slot: number) => number + getBlockNumber: (slot: number) => number, + network: string ): Promise { const events = await timeout( query(url, Routes.projectedNftEventsRange, { @@ -24,7 +25,7 @@ export default async function getCdeProjectedNFTData( ); return events - .map(e => eventToCdeDatum(e, extension, getBlockNumber(e.actionSlot))) + .map(e => eventToCdeDatum(e, extension, getBlockNumber(e.actionSlot), network)) .filter(e => e != null) .map(e => e!); } @@ -32,7 +33,8 @@ export default async function getCdeProjectedNFTData( function eventToCdeDatum( event: ProjectedNftRangeResponse[0], extension: ChainDataExtensionCardanoProjectedNFT, - blockNumber: number + blockNumber: number, + network: string ): CdeCardanoProjectedNFTDatum | null { if ( event.actionTxId === null || @@ -65,5 +67,6 @@ function eventToCdeDatum( forHowLong: event.forHowLong != null ? event.forHowLong : undefined, }, scheduledPrefix: extension.scheduledPrefix, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/delayedAsset.ts b/packages/engine/paima-funnel/src/cde/delayedAsset.ts index b451c82b2..391c6e815 100644 --- a/packages/engine/paima-funnel/src/cde/delayedAsset.ts +++ b/packages/engine/paima-funnel/src/cde/delayedAsset.ts @@ -12,7 +12,8 @@ export default async function getCdeData( extension: ChainDataExtensionCardanoDelayedAsset, fromAbsoluteSlot: number, toAbsoluteSlot: number, - getBlockNumber: (slot: number) => number + getBlockNumber: (slot: number) => number, + network: string ): Promise { const events = await timeout( query(url, Routes.assetUtxos, { @@ -23,13 +24,14 @@ export default async function getCdeData( DEFAULT_FUNNEL_TIMEOUT ); - return events.map(e => eventToCdeDatum(e, extension, getBlockNumber(e.slot))); + return events.map(e => eventToCdeDatum(e, extension, getBlockNumber(e.slot), network)); } function eventToCdeDatum( event: AssetUtxosResponse[0], extension: ChainDataExtensionCardanoDelayedAsset, - blockNumber: number + blockNumber: number, + network: string ): CdeCardanoAssetUtxoDatum { return { cdeId: extension.cdeId, @@ -44,5 +46,6 @@ function eventToCdeDatum( policyId: event.policyId, assetName: event.assetName, }, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/erc20.ts b/packages/engine/paima-funnel/src/cde/erc20.ts index 4eed0e448..c2eea78f8 100644 --- a/packages/engine/paima-funnel/src/cde/erc20.ts +++ b/packages/engine/paima-funnel/src/cde/erc20.ts @@ -10,7 +10,8 @@ import { DEFAULT_FUNNEL_TIMEOUT } from '@paima/utils'; export default async function getCdeData( extension: ChainDataExtensionErc20, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { // TOOD: typechain is missing the proper type generation for getPastEvents // https://github.com/dethcrypto/TypeChain/issues/767 @@ -21,10 +22,14 @@ export default async function getCdeData( }), DEFAULT_FUNNEL_TIMEOUT )) as unknown as ERC20Transfer[]; - return events.map((e: ERC20Transfer) => transferToCdeDatum(e, extension.cdeId)); + return events.map((e: ERC20Transfer) => transferToCdeDatum(e, extension.cdeId, network)); } -function transferToCdeDatum(event: ERC20Transfer, cdeId: number): CdeErc20TransferDatum { +function transferToCdeDatum( + event: ERC20Transfer, + cdeId: number, + network: string +): CdeErc20TransferDatum { return { cdeId: cdeId, cdeDatumType: ChainDataExtensionDatumType.ERC20Transfer, @@ -34,5 +39,6 @@ function transferToCdeDatum(event: ERC20Transfer, cdeId: number): CdeErc20Transf to: event.returnValues.to.toLowerCase(), value: event.returnValues.value, }, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/erc20Deposit.ts b/packages/engine/paima-funnel/src/cde/erc20Deposit.ts index d7cbbbffd..501c666e9 100644 --- a/packages/engine/paima-funnel/src/cde/erc20Deposit.ts +++ b/packages/engine/paima-funnel/src/cde/erc20Deposit.ts @@ -6,7 +6,8 @@ import { DEFAULT_FUNNEL_TIMEOUT } from '@paima/utils'; export default async function getCdeData( extension: ChainDataExtensionErc20Deposit, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { // TOOD: typechain is missing the proper type generation for getPastEvents // https://github.com/dethcrypto/TypeChain/issues/767 @@ -18,12 +19,13 @@ export default async function getCdeData( }), DEFAULT_FUNNEL_TIMEOUT )) as unknown as Transfer[]; - return events.map((e: Transfer) => transferToCdeDatum(e, extension)).flat(); + return events.map((e: Transfer) => transferToCdeDatum(e, extension, network)).flat(); } function transferToCdeDatum( event: Transfer, - extension: ChainDataExtensionErc20Deposit + extension: ChainDataExtensionErc20Deposit, + network: string ): ChainDataExtensionDatum[] { if (event.returnValues.to.toLowerCase() !== extension.depositAddress) { return []; @@ -38,6 +40,7 @@ function transferToCdeDatum( value: event.returnValues.value, }, scheduledPrefix: extension.scheduledPrefix, + network, }, ]; } diff --git a/packages/engine/paima-funnel/src/cde/erc6551Registry.ts b/packages/engine/paima-funnel/src/cde/erc6551Registry.ts index 6d1721bb7..0180068d2 100644 --- a/packages/engine/paima-funnel/src/cde/erc6551Registry.ts +++ b/packages/engine/paima-funnel/src/cde/erc6551Registry.ts @@ -14,7 +14,8 @@ import type { AccountCreated } from '@paima/utils'; export default async function getCdeData( extension: ChainDataExtensionErc6551Registry, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { const { implementation, tokenContract, tokenId } = extension; @@ -61,13 +62,14 @@ export default async function getCdeData( : withFilter.filter(e => e.returnValues.tokenId === extension.tokenId); return withFilter; })(); - const result = filteredEvents.map((e: AccountCreated) => toDatum(e, extension)).flat(); + const result = filteredEvents.map((e: AccountCreated) => toDatum(e, extension, network)).flat(); return result; } function toDatum( event: AccountCreated, - extension: ChainDataExtensionErc6551Registry + extension: ChainDataExtensionErc6551Registry, + network: string ): CdeErc6551RegistryDatum { return { cdeId: extension.cdeId, @@ -81,5 +83,6 @@ function toDatum( tokenId: event.returnValues.tokenId, salt: event.returnValues.salt, }, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/erc721.ts b/packages/engine/paima-funnel/src/cde/erc721.ts index 5163cb2c9..a239a7fb7 100644 --- a/packages/engine/paima-funnel/src/cde/erc721.ts +++ b/packages/engine/paima-funnel/src/cde/erc721.ts @@ -10,7 +10,8 @@ import type { ERC721Transfer as Transfer } from '@paima/utils'; export default async function getCdeData( extension: ChainDataExtensionErc721, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { // TOOD: typechain is missing the proper type generation for getPastEvents // https://github.com/dethcrypto/TypeChain/issues/767 @@ -21,12 +22,13 @@ export default async function getCdeData( }), DEFAULT_FUNNEL_TIMEOUT )) as unknown as Transfer[]; - return events.map((e: Transfer) => transferToCdeData(e, extension)).flat(); + return events.map((e: Transfer) => transferToCdeData(e, extension, network)).flat(); } function transferToTransferDatum( event: Transfer, - extension: ChainDataExtensionErc721 + extension: ChainDataExtensionErc721, + network: string ): CdeErc721TransferDatum { return { cdeId: extension.cdeId, @@ -37,12 +39,14 @@ function transferToTransferDatum( to: event.returnValues.to.toLowerCase(), tokenId: event.returnValues.tokenId, }, + network, }; } function transferToMintDatum( event: Transfer, - extension: ChainDataExtensionErc721 + extension: ChainDataExtensionErc721, + network: string ): CdeErc721MintDatum { return { cdeId: extension.cdeId, @@ -54,17 +58,19 @@ function transferToMintDatum( }, contractAddress: extension.contractAddress, scheduledPrefix: extension.scheduledPrefix, + network, }; } function transferToCdeData( event: Transfer, - extension: ChainDataExtensionErc721 + extension: ChainDataExtensionErc721, + network: string ): ChainDataExtensionDatum[] { - const transferDatum = transferToTransferDatum(event, extension); + const transferDatum = transferToTransferDatum(event, extension, network); const fromAddr = event.returnValues.from; if (fromAddr.match(/^0x0+$/g)) { - const mintDatum = transferToMintDatum(event, extension); + const mintDatum = transferToMintDatum(event, extension, network); return [mintDatum, transferDatum]; } else { return [transferDatum]; diff --git a/packages/engine/paima-funnel/src/cde/generic.ts b/packages/engine/paima-funnel/src/cde/generic.ts index 0bf051c83..1c2f33597 100644 --- a/packages/engine/paima-funnel/src/cde/generic.ts +++ b/packages/engine/paima-funnel/src/cde/generic.ts @@ -9,7 +9,8 @@ import type { export default async function getCdeData( extension: ChainDataExtensionGeneric, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { // TOOD: typechain is missing the proper type generation for getPastEvents // https://github.com/dethcrypto/TypeChain/issues/767 @@ -23,15 +24,20 @@ export default async function getCdeData( }), DEFAULT_FUNNEL_TIMEOUT ); - return events.map(e => eventToCdeDatum(e, extension)); + return events.map(e => eventToCdeDatum(e, extension, network)); } -function eventToCdeDatum(event: EventData, extension: ChainDataExtensionGeneric): CdeGenericDatum { +function eventToCdeDatum( + event: EventData, + extension: ChainDataExtensionGeneric, + network: string +): CdeGenericDatum { return { cdeId: extension.cdeId, cdeDatumType: ChainDataExtensionDatumType.Generic, blockNumber: event.blockNumber, scheduledPrefix: extension.scheduledPrefix, payload: event.returnValues, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/paimaErc721.ts b/packages/engine/paima-funnel/src/cde/paimaErc721.ts index db528c096..4c5aae22b 100644 --- a/packages/engine/paima-funnel/src/cde/paimaErc721.ts +++ b/packages/engine/paima-funnel/src/cde/paimaErc721.ts @@ -15,7 +15,8 @@ import type { PaimaMinted as Minted, PaimaERC721Transfer as Transfer } from '@pa export default async function getCdeData( extension: ChainDataExtensionPaimaErc721, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { // TOOD: typechain is missing the proper type generation for getPastEvents // https://github.com/dethcrypto/TypeChain/issues/767 @@ -23,8 +24,10 @@ export default async function getCdeData( fetchTransferEvents(extension, fromBlock, toBlock), fetchMintedEvents(extension, fromBlock, toBlock), ]); - const transferData = transferEvents.map((e: Transfer) => transferToTransferDatum(e, extension)); - const mintData = mintedEvents.map((e: Minted) => mintedToMintDatum(e, extension)); + const transferData = transferEvents.map((e: Transfer) => + transferToTransferDatum(e, extension, network) + ); + const mintData = mintedEvents.map((e: Minted) => mintedToMintDatum(e, extension, network)); return mergeSortedArrays( mintData, transferData, @@ -62,7 +65,8 @@ async function fetchMintedEvents( function transferToTransferDatum( event: Transfer, - extension: ChainDataExtensionPaimaErc721 + extension: ChainDataExtensionPaimaErc721, + network: string ): CdeErc721TransferDatum { return { cdeId: extension.cdeId, @@ -73,12 +77,14 @@ function transferToTransferDatum( to: event.returnValues.to.toLowerCase(), tokenId: event.returnValues.tokenId, }, + network, }; } function mintedToMintDatum( event: Minted, - extension: ChainDataExtensionPaimaErc721 + extension: ChainDataExtensionPaimaErc721, + network: string ): CdeErc721MintDatum { return { cdeId: extension.cdeId, @@ -90,5 +96,6 @@ function mintedToMintDatum( }, contractAddress: extension.contractAddress, scheduledPrefix: extension.scheduledPrefix, + network, }; } diff --git a/packages/engine/paima-funnel/src/cde/reading.ts b/packages/engine/paima-funnel/src/cde/reading.ts index 8a07e78f2..1e9eb3bbc 100644 --- a/packages/engine/paima-funnel/src/cde/reading.ts +++ b/packages/engine/paima-funnel/src/cde/reading.ts @@ -10,19 +10,23 @@ import getCdeErc20DepositData from './erc20Deposit.js'; import getCdeGenericData from './generic.js'; import getCdeErc6551RegistryData from './erc6551Registry.js'; import assertNever from 'assert-never'; +import { networkInterfaces } from 'os'; export async function getUngroupedCdeData( web3: Web3, extensions: ChainDataExtension[], fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { if (fromBlock > toBlock) { return extensions.map(_ => []); } const allData = await Promise.all( extensions.map(extension => - 'startBlockHeight' in extension ? getSpecificCdeData(extension, fromBlock, toBlock) : [] + 'startBlockHeight' in extension + ? getSpecificCdeData(extension, fromBlock, toBlock, network) + : [] ) ); return allData; @@ -31,7 +35,8 @@ export async function getUngroupedCdeData( async function getSpecificCdeData( extension: ChainDataExtension & { startBlockHeight: number }, fromBlock: number, - toBlock: number + toBlock: number, + network: string ): Promise { if (fromBlock > toBlock || toBlock < extension.startBlockHeight) { return []; @@ -40,17 +45,17 @@ async function getSpecificCdeData( } switch (extension.cdeType) { case ChainDataExtensionType.ERC20: - return await getCdeErc20Data(extension, fromBlock, toBlock); + return await getCdeErc20Data(extension, fromBlock, toBlock, network); case ChainDataExtensionType.ERC721: - return await getCdeErc721Data(extension, fromBlock, toBlock); + return await getCdeErc721Data(extension, fromBlock, toBlock, network); case ChainDataExtensionType.PaimaERC721: - return await getCdePaimaErc721Data(extension, fromBlock, toBlock); + return await getCdePaimaErc721Data(extension, fromBlock, toBlock, network); case ChainDataExtensionType.ERC20Deposit: - return await getCdeErc20DepositData(extension, fromBlock, toBlock); + return await getCdeErc20DepositData(extension, fromBlock, toBlock, network); case ChainDataExtensionType.Generic: - return await getCdeGenericData(extension, fromBlock, toBlock); + return await getCdeGenericData(extension, fromBlock, toBlock, network); case ChainDataExtensionType.ERC6551Registry: - return await getCdeErc6551RegistryData(extension, fromBlock, toBlock); + return await getCdeErc6551RegistryData(extension, fromBlock, toBlock, network); case ChainDataExtensionType.CardanoPool: // this is used by the block funnel, which can't get information for this // extension diff --git a/packages/engine/paima-funnel/src/funnels/block/funnel.ts b/packages/engine/paima-funnel/src/funnels/block/funnel.ts index a30d48dc3..ca8576d8f 100644 --- a/packages/engine/paima-funnel/src/funnels/block/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/block/funnel.ts @@ -97,7 +97,8 @@ export class BlockFunnel extends BaseFunnel implements ChainFunnel { this.sharedData.web3, this.sharedData.extensions.filter(extension => extension.network === this.chainName), blockNumber, - blockNumber + blockNumber, + this.chainName ), ]); @@ -134,7 +135,8 @@ export class BlockFunnel extends BaseFunnel implements ChainFunnel { this.sharedData.web3, this.sharedData.extensions.filter(extension => extension.network === this.chainName), fromBlock, - toBlock + toBlock, + this.chainName ), ]); const cdeData = groupCdeData( @@ -178,7 +180,8 @@ export class BlockFunnel extends BaseFunnel implements ChainFunnel { this.sharedData.web3, this.sharedData.extensions.filter(extension => extension.network === this.chainName), fromBlock, - toBlock + toBlock, + this.chainName ); return { [this.chainName]: groupCdeData( diff --git a/packages/engine/paima-funnel/src/funnels/carp/funnel.ts b/packages/engine/paima-funnel/src/funnels/carp/funnel.ts index 715cec954..245af14e8 100644 --- a/packages/engine/paima-funnel/src/funnels/carp/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/carp/funnel.ts @@ -219,7 +219,8 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel { slot => { return slot; }, - slot => absoluteSlotToEpoch(this.era, slot) + slot => absoluteSlotToEpoch(this.era, slot), + this.chainName ); promises.push(data); @@ -233,7 +234,8 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel { Math.min(arg.to, this.cache.getState().startingSlot - 1), slot => { return slot; - } + }, + this.chainName ); promises.push(data); @@ -247,7 +249,8 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel { Math.min(arg.to, this.cache.getState().startingSlot - 1), slot => { return slot; - } + }, + this.chainName ); promises.push(data); @@ -407,7 +410,8 @@ async function readDataInternal( min, Math.min(max, extension.stopSlot || max), mapSlotToBlockNumber, - slot => absoluteSlotToEpoch(era, slot) + slot => absoluteSlotToEpoch(era, slot), + chainName ); return poolData; case ChainDataExtensionType.CardanoProjectedNFT: @@ -416,7 +420,8 @@ async function readDataInternal( extension, min, Math.min(max, extension.stopSlot || max), - mapSlotToBlockNumber + mapSlotToBlockNumber, + chainName ); return projectedNFTData; case ChainDataExtensionType.CardanoAssetUtxo: @@ -425,7 +430,8 @@ async function readDataInternal( extension, min, Math.min(max, extension.stopSlot || max), - mapSlotToBlockNumber + mapSlotToBlockNumber, + chainName ); return delayedAssetData; default: diff --git a/packages/engine/paima-funnel/src/funnels/emulated/funnel.ts b/packages/engine/paima-funnel/src/funnels/emulated/funnel.ts index 61b3a5260..f36a3e931 100644 --- a/packages/engine/paima-funnel/src/funnels/emulated/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/emulated/funnel.ts @@ -377,6 +377,7 @@ export class EmulatedBlocksFunnel extends BaseFunnel { mergedBlocks.map(block => block.extensionDatums ?? []), nextBlockBlockNumber ); + const nextBlockInternalEvents = mergedBlocks.map(block => block.internalEvents || []).flat(); // if this emulated block contains multiple blocks in the underlying chain if (mergedBlocks.length > 0) { @@ -395,7 +396,7 @@ export class EmulatedBlocksFunnel extends BaseFunnel { timestamp: nextBlockEndTimestamp, submittedData: nextBlockSubmittedData, extensionDatums: nextBlockExtensionDatums, - network: (await GlobalConfig.mainEvmConfig())[0], + internalEvents: nextBlockInternalEvents, }; }; diff --git a/packages/engine/paima-funnel/src/funnels/parallelEvm/funnel.ts b/packages/engine/paima-funnel/src/funnels/parallelEvm/funnel.ts index 8dd18e45a..a6b12bb8b 100644 --- a/packages/engine/paima-funnel/src/funnels/parallelEvm/funnel.ts +++ b/packages/engine/paima-funnel/src/funnels/parallelEvm/funnel.ts @@ -318,7 +318,8 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel { extension => !extension.network || extension.network === this.chainName ), blockNumber, - blockNumber + blockNumber, + this.chainName ); for (const extensionData of cdeData) { @@ -356,7 +357,8 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel { this.web3, this.sharedData.extensions.filter(extension => extension.network === this.chainName), fromBlock, - toBlock + toBlock, + this.chainName ); let mappedFrom: number | undefined; @@ -432,7 +434,8 @@ export class ParallelEvmFunnel extends BaseFunnel implements ChainFunnel { this.web3, this.sharedData.extensions.filter(extension => extension.network === this.chainName), fromBlock, - toBlock + toBlock, + this.chainName ); return { diff --git a/packages/engine/paima-funnel/src/reading.ts b/packages/engine/paima-funnel/src/reading.ts index b6b33d4d1..640782d92 100644 --- a/packages/engine/paima-funnel/src/reading.ts +++ b/packages/engine/paima-funnel/src/reading.ts @@ -69,7 +69,6 @@ function blockDataToChainData(block: BlockTransactionString, network: string): C blockHash: block.hash, blockNumber: block.number, submittedData: [], // this will be merged in after - network, }; } diff --git a/packages/engine/paima-sm/src/cde-cardano-pool.ts b/packages/engine/paima-sm/src/cde-cardano-pool.ts index f3d66774c..4595d8aad 100644 --- a/packages/engine/paima-sm/src/cde-cardano-pool.ts +++ b/packages/engine/paima-sm/src/cde-cardano-pool.ts @@ -3,7 +3,10 @@ import { createScheduledData, cdeCardanoPoolInsertData, removeOldEntries } from import type { SQLUpdate } from '@paima/db'; import type { CdeCardanoPoolDatum } from './types.js'; -export default async function processDatum(cdeDatum: CdeCardanoPoolDatum): Promise { +export default async function processDatum( + cdeDatum: CdeCardanoPoolDatum, + inPresync: boolean +): Promise { const cdeId = cdeDatum.cdeId; const prefix = cdeDatum.scheduledPrefix; const address = cdeDatum.payload.address; @@ -12,8 +15,11 @@ export default async function processDatum(cdeDatum: CdeCardanoPoolDatum): Promi const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); const scheduledInputData = `${prefix}|${address}|${pool}`; - const updateList: SQLUpdate[] = [ - createScheduledData(scheduledInputData, scheduledBlockHeight), + const updateList: SQLUpdate[] = inPresync + ? [] + : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; + + updateList.push( [ cdeCardanoPoolInsertData, { @@ -28,7 +34,7 @@ export default async function processDatum(cdeDatum: CdeCardanoPoolDatum): Promi { address: cdeDatum.payload.address, }, - ], - ]; + ] + ); return updateList; } diff --git a/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts b/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts index 76165da75..629a8a0ff 100644 --- a/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts +++ b/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts @@ -8,7 +8,8 @@ import { import type { SQLUpdate } from '@paima/db'; export default async function processDatum( - cdeDatum: CdeCardanoProjectedNFTDatum + cdeDatum: CdeCardanoProjectedNFTDatum, + inPresync: boolean ): Promise { const cdeId = cdeDatum.cdeId; const prefix = cdeDatum.scheduledPrefix; @@ -28,37 +29,16 @@ export default async function processDatum( const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${policyId}|${assetName}|${status}`; if (previousTxHash === undefined || previousOutputIndex === undefined) { - const updateList: SQLUpdate[] = [ - createScheduledData(scheduledInputData, scheduledBlockHeight), - [ - cdeCardanoProjectedNftInsertData, - { - cde_id: cdeId, - owner_address: ownerAddress, - current_tx_hash: currentTxHash, - current_tx_output_index: currentOutputIndex, - policy_id: policyId, - asset_name: assetName, - amount: amount, - status: status, - plutus_datum: datum, - for_how_long: forHowLong, - }, - ], - ]; - return updateList; - } - const updateList: SQLUpdate[] = [ - createScheduledData(scheduledInputData, scheduledBlockHeight), - [ - cdeCardanoProjectedNftUpdateData, + const updateList: SQLUpdate[] = inPresync + ? [] + : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; + updateList.push([ + cdeCardanoProjectedNftInsertData, { cde_id: cdeId, owner_address: ownerAddress, - new_tx_hash: currentTxHash, - new_tx_output_index: currentOutputIndex, - previous_tx_hash: previousTxHash, - previous_tx_output_index: previousOutputIndex, + current_tx_hash: currentTxHash, + current_tx_output_index: currentOutputIndex, policy_id: policyId, asset_name: assetName, amount: amount, @@ -66,7 +46,29 @@ export default async function processDatum( plutus_datum: datum, for_how_long: forHowLong, }, - ], - ]; + ]); + return updateList; + } + const updateList: SQLUpdate[] = inPresync + ? [] + : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; + + updateList.push([ + cdeCardanoProjectedNftUpdateData, + { + cde_id: cdeId, + owner_address: ownerAddress, + new_tx_hash: currentTxHash, + new_tx_output_index: currentOutputIndex, + previous_tx_hash: previousTxHash, + previous_tx_output_index: previousOutputIndex, + policy_id: policyId, + asset_name: assetName, + amount: amount, + status: status, + plutus_datum: datum, + for_how_long: forHowLong, + }, + ]); return updateList; } diff --git a/packages/engine/paima-sm/src/cde-erc20-deposit.ts b/packages/engine/paima-sm/src/cde-erc20-deposit.ts index 18b32546c..234eb339e 100644 --- a/packages/engine/paima-sm/src/cde-erc20-deposit.ts +++ b/packages/engine/paima-sm/src/cde-erc20-deposit.ts @@ -12,7 +12,8 @@ import { export default async function processErc20Datum( readonlyDBConn: PoolClient, - cdeDatum: CdeErc20DepositDatum + cdeDatum: CdeErc20DepositDatum, + inPresync: boolean ): Promise { const cdeId = cdeDatum.cdeId; const { from, value } = cdeDatum.payload; @@ -29,9 +30,11 @@ export default async function processErc20Datum( const updateList: SQLUpdate[] = []; try { - const scheduledInputData = `${prefix}|${fromAddr}|${value}`; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); - updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); + if (!inPresync) { + const scheduledInputData = `${prefix}|${fromAddr}|${value}`; + const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); + } if (fromRow.length > 0) { const oldTotal = BigInt(fromRow[0].total_deposited); diff --git a/packages/engine/paima-sm/src/cde-erc721-mint.ts b/packages/engine/paima-sm/src/cde-erc721-mint.ts index d54cbbb5d..84acf818d 100644 --- a/packages/engine/paima-sm/src/cde-erc721-mint.ts +++ b/packages/engine/paima-sm/src/cde-erc721-mint.ts @@ -4,10 +4,11 @@ import { createScheduledData } from '@paima/db'; import type { SQLUpdate } from '@paima/db'; export default async function processErc721Datum( - cdeDatum: CdeErc721MintDatum + cdeDatum: CdeErc721MintDatum, + inPresync: boolean ): Promise { const [address, prefix] = [cdeDatum.contractAddress, cdeDatum.scheduledPrefix]; - if (!prefix) { + if (!prefix && inPresync) { return []; } const { tokenId, mintData } = cdeDatum.payload; diff --git a/packages/engine/paima-sm/src/cde-generic.ts b/packages/engine/paima-sm/src/cde-generic.ts index 09ae9b78c..5694874c4 100644 --- a/packages/engine/paima-sm/src/cde-generic.ts +++ b/packages/engine/paima-sm/src/cde-generic.ts @@ -3,7 +3,10 @@ import type { CdeGenericDatum } from './types.js'; import { createScheduledData, cdeGenericInsertData } from '@paima/db'; import type { SQLUpdate } from '@paima/db'; -export default async function processDatum(cdeDatum: CdeGenericDatum): Promise { +export default async function processDatum( + cdeDatum: CdeGenericDatum, + inPresync: boolean +): Promise { const cdeId = cdeDatum.cdeId; const blockHeight = cdeDatum.blockNumber; const payload = cdeDatum.payload; @@ -13,9 +16,14 @@ export default async function processDatum(cdeDatum: CdeGenericDatum): Promise { switch (cdeDatum.cdeDatumType) { case ChainDataExtensionDatumType.ERC20Transfer: @@ -25,17 +26,17 @@ export async function cdeTransitionFunction( case ChainDataExtensionDatumType.ERC721Transfer: return await processErc721TransferDatum(readonlyDBConn, cdeDatum); case ChainDataExtensionDatumType.ERC721Mint: - return await processErc721MintDatum(cdeDatum); + return await processErc721MintDatum(cdeDatum, inPresync); case ChainDataExtensionDatumType.ERC20Deposit: - return await processErc20DepositDatum(readonlyDBConn, cdeDatum); + return await processErc20DepositDatum(readonlyDBConn, cdeDatum, inPresync); case ChainDataExtensionDatumType.Generic: - return await processGenericDatum(cdeDatum); + return await processGenericDatum(cdeDatum, inPresync); case ChainDataExtensionDatumType.ERC6551Registry: return await processErc6551RegistryDatum(cdeDatum); case ChainDataExtensionDatumType.CardanoPool: - return await processCardanoDelegationDatum(cdeDatum); + return await processCardanoDelegationDatum(cdeDatum, inPresync); case ChainDataExtensionDatumType.CardanoProjectedNFT: - return await processCardanoProjectedNFT(cdeDatum); + return await processCardanoProjectedNFT(cdeDatum, inPresync); case ChainDataExtensionDatumType.CardanoAssetUtxo: return await processCardanoAssetUtxoDatum(cdeDatum); default: diff --git a/packages/engine/paima-sm/src/index.ts b/packages/engine/paima-sm/src/index.ts index 7c89d7073..04bb325d3 100644 --- a/packages/engine/paima-sm/src/index.ts +++ b/packages/engine/paima-sm/src/index.ts @@ -116,7 +116,8 @@ const SM: GameStateMachineInitializer = { latestCdeData.blockNumber, latestCdeData.network, latestCdeData.extensionDatums, - dbTx + dbTx, + true ); if (cdeDataLength > 0) { doLog( @@ -127,7 +128,8 @@ const SM: GameStateMachineInitializer = { const cdeDataLength = await processCardanoCdeData( latestCdeData.blockNumber, latestCdeData.extensionDatums, - dbTx + dbTx, + true ); if (cdeDataLength > 0) { doLog( @@ -194,12 +196,27 @@ const SM: GameStateMachineInitializer = { // Generate Prando object const randomnessGenerator = new Prando(seed); - const cdeDataLength = await processCdeData( - latestChainData.blockNumber, - latestChainData.network, - latestChainData.extensionDatums, - dbTx - ); + const extensionsPerNetwork: { [network: string]: ChainDataExtensionDatum[] } = {}; + + for (const extensionData of latestChainData.extensionDatums || []) { + if (!extensionsPerNetwork[extensionData.network]) { + extensionsPerNetwork[extensionData.network] = []; + } + + extensionsPerNetwork[extensionData.network].push(extensionData); + } + + let cdeDataLength = 0; + + for (const network of Object.keys(extensionsPerNetwork)) { + cdeDataLength += await processCdeData( + latestChainData.blockNumber, + network, + extensionsPerNetwork[network], + dbTx, + false + ); + } await processInternalEvents(latestChainData.internalEvents, dbTx); @@ -244,6 +261,7 @@ const SM: GameStateMachineInitializer = { async function processCdeDataBase( cdeData: ChainDataExtensionDatum[] | undefined, dbTx: PoolClient, + inPresync: boolean, markProcessed: () => Promise ): Promise { if (!cdeData) { @@ -251,7 +269,7 @@ async function processCdeDataBase( } for (const datum of cdeData) { - const sqlQueries = await cdeTransitionFunction(dbTx, datum); + const sqlQueries = await cdeTransitionFunction(dbTx, datum, inPresync); try { for (const [query, params] of sqlQueries) { await query.run(params, dbTx); @@ -275,9 +293,10 @@ async function processCdeData( blockHeight: number, network: string, cdeData: ChainDataExtensionDatum[] | undefined, - dbTx: PoolClient + dbTx: PoolClient, + inPresync: boolean ): Promise { - return await processCdeDataBase(cdeData, dbTx, async () => { + return await processCdeDataBase(cdeData, dbTx, inPresync, async () => { await markCdeBlockheightProcessed.run({ block_height: blockHeight, network }, dbTx); return; }); @@ -286,9 +305,10 @@ async function processCdeData( async function processCardanoCdeData( slot: number, cdeData: ChainDataExtensionDatum[] | undefined, - dbTx: PoolClient + dbTx: PoolClient, + inPresync: boolean ): Promise { - return await processCdeDataBase(cdeData, dbTx, async () => { + return await processCdeDataBase(cdeData, dbTx, inPresync, async () => { await markCardanoCdeSlotProcessed.run({ slot: slot }, dbTx); return; }); diff --git a/packages/engine/paima-sm/src/types.ts b/packages/engine/paima-sm/src/types.ts index 217572ec3..9c46e3ca5 100644 --- a/packages/engine/paima-sm/src/types.ts +++ b/packages/engine/paima-sm/src/types.ts @@ -24,7 +24,6 @@ export interface ChainData { timestamp: number; blockHash: string; blockNumber: number; - network: string; submittedData: SubmittedData[]; extensionDatums?: ChainDataExtensionDatum[]; /** @@ -133,6 +132,7 @@ interface CdeDatumBase { cdeDatumType: ChainDataExtensionDatumType; blockNumber: number; payload: ChainDataExtensionPayload; + network: string; } export interface CdeErc20TransferDatum extends CdeDatumBase {