Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projected NFT funnel #259

Merged
merged 25 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ bin

# logs
*.log

.idea/
67 changes: 67 additions & 0 deletions packages/engine/paima-funnel/src/cde/cardanoProjectedNFT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type {
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';

export default async function getCdeProjectedNFTData(
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 },
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this handle pagination properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, as long as the range is not too big it will work fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the request is not split to pages

DEFAULT_FUNNEL_TIMEOUT
);

return events
.map(e => eventToCdeDatum(e, extension, getBlockNumber(e.actionSlot)))
.filter(e => e != null)
.map(e => e!);
ecioppettini marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

actionTxId: event.actionTxId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that games only care about NFTs getting locked and queued to unlock. We don't care if the user ever actually ends up withdrawing their NFT from the lock later.

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,
};
}
4 changes: 4 additions & 0 deletions packages/engine/paima-funnel/src/cde/reading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ async function getSpecificCdeData(
// this is used by the block funnel, which can't get information for this
// extension
return [];
case ChainDataExtensionType.CardanoProjectedNFT:
// this is used by the block funnel, which can't get information for this
// extension
return [];
default:
assertNever(extension);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/paima-funnel/src/funnels/BaseFunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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';
import { FUNNEL_PRESYNC_FINISHED } from '@paima/utils';
import type { FUNNEL_PRESYNC_FINISHED } from '@paima/utils';

export type FunnelSharedData = {
readonly web3: Web3;
Expand Down
69 changes: 50 additions & 19 deletions packages/engine/paima-funnel/src/funnels/carp/funnel.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import {
ChainDataExtensionType,
DEFAULT_FUNNEL_TIMEOUT,
ENV,
Network,
delay,
doLog,
ENV,
logError,
Network,
timeout,
} from '@paima/utils';
import type { ChainDataExtensionDatum } from '@paima/sm';
import type {
ChainDataExtensionCardanoProjectedNFT} from '@paima/sm';
import {
type ChainData,
type ChainDataExtension,
type ChainDataExtensionCardanoDelegation,
type ChainDataExtensionDatum,
type PresyncChainData,
} from '@paima/sm';
import { composeChainData, groupCdeData } from '../../utils.js';
import { BaseFunnel } from '../BaseFunnel.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';
Expand Down Expand Up @@ -140,27 +143,45 @@ export class CarpFunnel extends BaseFunnel implements ChainFunnel {
let basePromise = this.baseFunnel.readPresyncData(args);

if (arg && arg.from >= 0 && arg.from < this.cache.getState().startingSlot) {
const [poolEvents, data] = await Promise.all([
const [carpEvents, data] = await Promise.all([
Promise.all(
this.sharedData.extensions
.filter(extension => extension.cdeType === ChainDataExtensionType.CardanoPool)
.filter(
extension =>
extension.cdeType === ChainDataExtensionType.CardanoPool ||
extension.cdeType === ChainDataExtensionType.CardanoProjectedNFT
)
.map(extension => {
const data = getCdePoolData(
this.carpUrl,
extension as ChainDataExtensionCardanoDelegation,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
);
return data;
if (extension.cdeType === ChainDataExtensionType.CardanoPool) {
const data = getCdePoolData(
this.carpUrl,
extension ,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
);
return data;
} else {
// ProjectedNFT
const data = getCdeProjectedNFTData(
this.carpUrl,
extension as ChainDataExtensionCardanoProjectedNFT,
arg.from,
Math.min(arg.to, this.cache.getState().startingSlot - 1),
slot => {
return slot;
}
);
return data;
}
})
),
basePromise,
]);

let grouped = groupCdeData(Network.CARDANO, arg.from, arg.to, poolEvents);
let grouped = groupCdeData(Network.CARDANO, arg.from, arg.to, carpEvents);

if (grouped.length > 0) {
data[Network.CARDANO] = grouped;
Expand Down Expand Up @@ -283,15 +304,25 @@ async function readDataInternal(

switch (extension.cdeType) {
case ChainDataExtensionType.CardanoPool:
const data = getCdePoolData(
const poolData = getCdePoolData(
carpUrl,
extension,
min,
Math.min(max, extension.stopSlot || max),
mapSlotToBlockNumber
);

return poolData;
case ChainDataExtensionType.CardanoProjectedNFT:
const projectedNFTData = getCdeProjectedNFTData(
carpUrl,
extension,
min,
Math.min(max, extension.stopSlot || max),
mapSlotToBlockNumber
);

return data;
return projectedNFTData;
default:
return Promise.resolve([]);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/engine/paima-runtime/src/cde-config/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
CdeBaseConfig,
CdeEntryTypeName,
ChainDataExtensionCardanoDelegationConfig,
ChainDataExtensionCardanoProjectedNFTConfig,
ChainDataExtensionErc20Config,
ChainDataExtensionErc20DepositConfig,
ChainDataExtensionErc6551RegistryConfig,
Expand Down Expand Up @@ -89,6 +90,8 @@ export function parseCdeConfigFile(configFileData: string): Static<typeof CdeCon
return checkOrError(entry.name, ChainDataExtensionErc6551RegistryConfig, entry);
case CdeEntryTypeName.CardanoDelegation:
return checkOrError(entry.name, ChainDataExtensionCardanoDelegationConfig, entry);
case CdeEntryTypeName.CardanoProjectedNFT:
return checkOrError(entry.name, ChainDataExtensionCardanoProjectedNFTConfig, entry);
default:
assertNever(entry.type);
}
Expand Down Expand Up @@ -203,6 +206,13 @@ async function instantiateExtension(
hash: hashConfig(config),
cdeType: ChainDataExtensionType.CardanoPool,
};
case CdeEntryTypeName.CardanoProjectedNFT:
return {
...config,
cdeId: index,
hash: hashConfig(config),
cdeType: ChainDataExtensionType.CardanoProjectedNFT,
};
default:
assertNever(config);
}
Expand Down
10 changes: 8 additions & 2 deletions packages/engine/paima-runtime/src/cde-config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type { ChainDataExtension } from '@paima/sm';

export function getEarliestStartBlockheight(config: ChainDataExtension[]): number {
const minStartBlockheight = config.reduce((min, cde) => {
if (cde.cdeType !== ChainDataExtensionType.CardanoPool) {
if (
cde.cdeType !== ChainDataExtensionType.CardanoPool &&
cde.cdeType !== ChainDataExtensionType.CardanoProjectedNFT
) {
return Math.min(min, cde.startBlockHeight);
}
return min;
Expand All @@ -16,7 +19,10 @@ export function getEarliestStartBlockheight(config: ChainDataExtension[]): numbe

export function getEarliestStartSlot(config: ChainDataExtension[]): number {
const minStartSlot = config.reduce((min, cde) => {
if (cde.cdeType === ChainDataExtensionType.CardanoPool) {
if (
cde.cdeType === ChainDataExtensionType.CardanoPool ||
cde.cdeType === ChainDataExtensionType.CardanoProjectedNFT
) {
return Math.min(min, cde.startSlot);
}
return min;
Expand Down
69 changes: 69 additions & 0 deletions packages/engine/paima-sm/src/cde-cardano-projected-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ENV } from '@paima/utils';
import type { CdeCardanoProjectedNFTDatum } from './types.js';
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;

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;
}
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;
}
3 changes: 3 additions & 0 deletions packages/engine/paima-sm/src/cde-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import processErc20DepositDatum from './cde-erc20-deposit.js';
import processErc6551RegistryDatum from './cde-erc6551-registry.js';
import processGenericDatum from './cde-generic.js';
import processCardanoDelegationDatum from './cde-cardano-pool.js';
import processCardanoProjectedNFT from './cde-cardano-projected-nft.js';
import assertNever from 'assert-never';
import type { SQLUpdate } from '@paima/db';

Expand All @@ -32,6 +33,8 @@ export async function cdeTransitionFunction(
return await processErc6551RegistryDatum(cdeDatum);
case ChainDataExtensionDatumType.CardanoPool:
return await processCardanoDelegationDatum(cdeDatum);
case ChainDataExtensionDatumType.CardanoProjectedNFT:
return await processCardanoProjectedNFT(cdeDatum);
default:
assertNever(cdeDatum);
}
Expand Down
Loading