Skip to content

Commit

Permalink
Lint & minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gostkin committed Dec 8, 2023
1 parent 4db7a54 commit 8cd3b3a
Show file tree
Hide file tree
Showing 10 changed files with 826 additions and 526 deletions.
1,042 changes: 658 additions & 384 deletions package-lock.json

Large diffs are not rendered by default.

103 changes: 57 additions & 46 deletions packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
import type {
CdeCardanoProjectedNFTDatum,
ChainDataExtensionCardanoProjectedNFT,
ChainDataExtensionDatum,
CdeCardanoProjectedNFTDatum,
ChainDataExtensionCardanoProjectedNFT,
ChainDataExtensionDatum,
} from '@paima/sm';
import { ChainDataExtensionDatumType, DEFAULT_FUNNEL_TIMEOUT, timeout } from '@paima/utils';
import { Routes, query } from '@dcspark/carp-client/client/src';
import type { ProjectedNftRangeResponse } from "@dcspark/carp-client/shared/models/ProjectedNftRange";
import type { ProjectedNftRangeResponse } from '@dcspark/carp-client/shared/models/ProjectedNftRange';

export default async function getCdeProjectedNFTData(
url: string,
extension: ChainDataExtensionCardanoProjectedNFT,
fromAbsoluteSlot: number,
toAbsoluteSlot: number,
getBlockNumber: (slot: number) => number
url: string,
extension: ChainDataExtensionCardanoProjectedNFT,
fromAbsoluteSlot: number,
toAbsoluteSlot: number,
getBlockNumber: (slot: number) => number
): Promise<ChainDataExtensionDatum[]> {
const events = await timeout(
query(url, Routes.projectedNftEventsRange, {
range: { minSlot: fromAbsoluteSlot, maxSlot: toAbsoluteSlot },
}),
DEFAULT_FUNNEL_TIMEOUT
);

return events.map(e => eventToCdeDatum(e, extension, getBlockNumber(e.actionSlot))).filter(e => e != null).map(e => e!!);
const events = await timeout(
query(url, Routes.projectedNftEventsRange, {
range: { minSlot: fromAbsoluteSlot, maxSlot: toAbsoluteSlot },
}),
DEFAULT_FUNNEL_TIMEOUT
);

return events
.map(e => eventToCdeDatum(e, extension, getBlockNumber(e.actionSlot)))
.filter(e => e != null)
.map(e => e!!);
}

function eventToCdeDatum(
event: ProjectedNftRangeResponse[0],
extension: ChainDataExtensionCardanoProjectedNFT,
blockNumber: number
event: ProjectedNftRangeResponse[0],
extension: ChainDataExtensionCardanoProjectedNFT,
blockNumber: number
): CdeCardanoProjectedNFTDatum | null {
if (event.ownerAddress == null || event.actionTxId == null || event.status == null) {
return null;
}

return {
cdeId: extension.cdeId,
cdeDatumType: ChainDataExtensionDatumType.CardanoProjectedNFT,
blockNumber,
payload: {
ownerAddress: event.ownerAddress!!,

actionTxId: event.actionTxId,
actionOutputIndex: event.actionOutputIndex || undefined,

previousTxHash: event.previousTxHash || undefined,
previousTxOutputIndex: event.previousTxOutputIndex || undefined,

asset: event.asset,
amount: event.amount,
status: event.status,
plutusDatum: event.plutusDatum || "",

forHowLong: event.forHowLong || undefined,
},
scheduledPrefix: extension.scheduledPrefix,
};
if (
event.ownerAddress === null ||
event.ownerAddress == '' ||
event.actionTxId === null ||
event.actionTxId == '' ||
event.status === null ||
event.actionTxId == ''
) {
return null;
}

return {
cdeId: extension.cdeId,
cdeDatumType: ChainDataExtensionDatumType.CardanoProjectedNFT,
blockNumber,
payload: {
ownerAddress: event.ownerAddress != null ? event.ownerAddress : '',

actionTxId: event.actionTxId,
actionOutputIndex: event.actionOutputIndex != null ? event.actionOutputIndex : undefined,

previousTxHash: event.previousTxHash != null ? event.previousTxHash : undefined,
previousTxOutputIndex:
event.previousTxOutputIndex != null ? event.previousTxOutputIndex : undefined,

asset: event.asset,
amount: event.amount,
status: event.status,
plutusDatum: event.plutusDatum != null ? event.plutusDatum : '',

forHowLong: event.forHowLong != null ? event.forHowLong : undefined,
},
scheduledPrefix: extension.scheduledPrefix,
};
}
2 changes: 1 addition & 1 deletion packages/engine/paima-funnel/src/funnels/BaseFunnel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainFunnel, ReadPresyncDataFrom } from '@paima/runtime';
import type {ChainData, ChainDataExtension, PresyncChainData } from '@paima/sm';
import type { ChainData, ChainDataExtension, PresyncChainData } from '@paima/sm';
import type { PaimaL2Contract, Web3 } from '@paima/utils';
import type { FunnelCacheManager } from './FunnelCache.js';
import type { PoolClient } from 'pg';
Expand Down
61 changes: 33 additions & 28 deletions packages/engine/paima-funnel/src/funnels/carp/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import {
import {
type ChainData,
type ChainDataExtension,
type ChainDataExtensionCardanoDelegation, ChainDataExtensionCardanoProjectedNFT,
type ChainDataExtensionCardanoDelegation,
ChainDataExtensionCardanoProjectedNFT,
type ChainDataExtensionDatum,
type PresyncChainData,
} from '@paima/sm';
import {composeChainData, groupCdeData} from '../../utils.js';
import type {FunnelSharedData} from '../BaseFunnel.js';
import {BaseFunnel} from '../BaseFunnel.js';
import type {PoolClient} from 'pg';
import type {ChainFunnel, ReadPresyncDataFrom} from '@paima/runtime';
import { composeChainData, groupCdeData } from '../../utils.js';
import type { FunnelSharedData } from '../BaseFunnel.js';
import { BaseFunnel } from '../BaseFunnel.js';
import type { PoolClient } from 'pg';
import type { ChainFunnel, ReadPresyncDataFrom } from '@paima/runtime';
import getCdePoolData from '../../cde/cardanoPool.js';
import getCdeProjectedNFTData from '../../cde/cardanoProjectedNFT.js';
import {query} from '@dcspark/carp-client/client/src/index';
import {Routes} from '@dcspark/carp-client/shared/routes';
import {FUNNEL_PRESYNC_FINISHED} from '@paima/utils/src/constants';
import {CarpFunnelCacheEntry} from '../FunnelCache.js';
import { query } from '@dcspark/carp-client/client/src/index';
import { Routes } from '@dcspark/carp-client/shared/routes';
import { FUNNEL_PRESYNC_FINISHED } from '@paima/utils/src/constants';
import { CarpFunnelCacheEntry } from '../FunnelCache.js';

const confirmationDepth = 10;
const delayForWaitingForFinalityLoop = 1000;
Expand Down Expand Up @@ -124,28 +125,33 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
const [carpEvents, data] = await Promise.all([
Promise.all(
this.sharedData.extensions
.filter(extension => extension.cdeType === ChainDataExtensionType.CardanoPool || extension.cdeType === ChainDataExtensionType.CardanoProjectedNFT)
.filter(
extension =>
extension.cdeType === ChainDataExtensionType.CardanoPool ||
extension.cdeType === ChainDataExtensionType.CardanoProjectedNFT
)
.map(extension => {
if (extension.cdeType === ChainDataExtensionType.CardanoPool) {
const data = getCdePoolData(
this.carpUrl,
extension as ChainDataExtensionCardanoDelegation,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
this.carpUrl,
extension as ChainDataExtensionCardanoDelegation,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
);
return data;
} else { // ProjectedNFT
} else {
// ProjectedNFT
const data = getCdeProjectedNFTData(
this.carpUrl,
extension as ChainDataExtensionCardanoProjectedNFT,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
this.carpUrl,
extension as ChainDataExtensionCardanoProjectedNFT,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
);
return data;
}
Expand Down Expand Up @@ -291,8 +297,7 @@ async function readDataInternal(
Network.EVM,
data[0].blockNumber,
data[data.length - 1].blockNumber,
poolEvents
.filter(data => data.length > 0)
poolEvents.filter(data => data.length > 0)
);

return grouped;
Expand Down
14 changes: 10 additions & 4 deletions packages/engine/paima-runtime/src/cde-config/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import * as fs from 'fs/promises';

import {ChainDataExtensionType, doLog} from '@paima/utils';
import { ChainDataExtensionType, doLog } from '@paima/utils';

import type {ChainDataExtension} from '@paima/sm';
import type { ChainDataExtension } from '@paima/sm';

export function getEarliestStartBlockheight(config: ChainDataExtension[]): number {
const startBlockheights = config
.map(cde => {
if (cde.cdeType != ChainDataExtensionType.CardanoPool && cde.cdeType != ChainDataExtensionType.CardanoProjectedNFT) {
if (
cde.cdeType != ChainDataExtensionType.CardanoPool &&
cde.cdeType != ChainDataExtensionType.CardanoProjectedNFT
) {
return cde.startBlockHeight;
} else {
return null;
Expand All @@ -21,7 +24,10 @@ export function getEarliestStartBlockheight(config: ChainDataExtension[]): numbe
export function getEarliestStartSlot(config: ChainDataExtension[]): number {
const startSlots = config
.map(cde => {
if (cde.cdeType == ChainDataExtensionType.CardanoPool || cde.cdeType == ChainDataExtensionType.CardanoProjectedNFT) {
if (
cde.cdeType == ChainDataExtensionType.CardanoPool ||
cde.cdeType == ChainDataExtensionType.CardanoProjectedNFT
) {
return cde.startSlot;
} else {
return null;
Expand Down
6 changes: 2 additions & 4 deletions packages/engine/paima-runtime/src/runtime-loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import process from 'process';
import { doLog, logError, delay, Network, ENV } from '@paima/utils';
import { tx, DataMigrations } from '@paima/db';
import { getEarliestStartBlockheight, getEarliestStartSlot } from './cde-config/utils.js';
import type { ChainFunnel,
IFunnelFactory,
ReadPresyncDataFrom
} from './types.js';import type { ChainData, ChainDataExtension, GameStateMachine } from '@paima/sm';
import type { ChainFunnel, IFunnelFactory, ReadPresyncDataFrom } from './types.js';
import type { ChainData, ChainDataExtension, GameStateMachine } from '@paima/sm';
import { run } from './run-flag.js';
import { snapshotIfTime } from './snapshots.js';
import {
Expand Down
113 changes: 59 additions & 54 deletions packages/engine/paima-sm/src/cde-cardano-projected-nft.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,69 @@
import { ENV } from '@paima/utils';
import type { CdeCardanoProjectedNFTDatum } from './types';
import {createScheduledData, cdeCardanoProjectedNftInsertData, cdeCardanoProjectedNftUpdateData} from '@paima/db';
import {
createScheduledData,
cdeCardanoProjectedNftInsertData,
cdeCardanoProjectedNftUpdateData,
} from '@paima/db';
import type { SQLUpdate } from '@paima/db';

export default async function processDatum(cdeDatum: CdeCardanoProjectedNFTDatum): Promise<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
const ownerAddress = cdeDatum.payload.ownerAddress;
const previousTxHash = cdeDatum.payload.previousTxHash;
const previousOutputIndex = cdeDatum.payload.previousTxOutputIndex;
const currentTxHash = cdeDatum.payload.actionTxId;
const currentOutputIndex = cdeDatum.payload.actionOutputIndex;
const amount = cdeDatum.payload.amount;
const asset = cdeDatum.payload.asset;
const status = cdeDatum.payload.status;
const datum = cdeDatum.payload.plutusDatum;
const forHowLong = cdeDatum.payload.forHowLong;
export default async function processDatum(
cdeDatum: CdeCardanoProjectedNFTDatum
): Promise<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
const ownerAddress = cdeDatum.payload.ownerAddress;
const previousTxHash = cdeDatum.payload.previousTxHash;
const previousOutputIndex = cdeDatum.payload.previousTxOutputIndex;
const currentTxHash = cdeDatum.payload.actionTxId;
const currentOutputIndex = cdeDatum.payload.actionOutputIndex;
const amount = cdeDatum.payload.amount;
const asset = cdeDatum.payload.asset;
const status = cdeDatum.payload.status;
const datum = cdeDatum.payload.plutusDatum;
const forHowLong = cdeDatum.payload.forHowLong;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${status}`;
const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${asset}|${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,
asset: asset,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong
},
],
];
return updateList;
}
if (previousTxHash === undefined || previousOutputIndex === undefined) {
const updateList: SQLUpdate[] = [
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
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,
asset: asset,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong
},
],
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
cdeCardanoProjectedNftInsertData,
{
cde_id: cdeId,
owner_address: ownerAddress,
current_tx_hash: currentTxHash,
current_tx_output_index: currentOutputIndex,
asset: asset,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong,
},
],
];
return updateList;

}
const updateList: SQLUpdate[] = [
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
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,
asset: asset,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong,
},
],
];
return updateList;
}
6 changes: 3 additions & 3 deletions packages/engine/paima-sm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ export const ChainDataExtensionCardanoProjectedNFTConfig = Type.Object({
});

export type ChainDataExtensionCardanoProjectedNFT = ChainDataExtensionBase &
Static<typeof ChainDataExtensionCardanoProjectedNFTConfig> & {
cdeType: ChainDataExtensionType.CardanoProjectedNFT;
};
Static<typeof ChainDataExtensionCardanoProjectedNFTConfig> & {
cdeType: ChainDataExtensionType.CardanoProjectedNFT;
};

export const CdeConfig = Type.Object({
extensions: Type.Array(
Expand Down
Loading

0 comments on commit 8cd3b3a

Please sign in to comment.