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 20 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ bin

# logs
*.log

.idea/

packages/engine/paima-rest/src/tsoa/routes.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't add this to gitingore I think because it will break some other systems that require parsing this file. The real solution would be trying to find out why this file sometimes gets formatted by a tool (possibly prettier) despite being specified in the .prettierignore. Running prettier on the git repo as expected ignores this file, but it's possible nx or some other tool is not respecting this somewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but how git relates to these systems?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's just a file that is not tracked by the repo if it is ignored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why would it break anything? otherwise one should always specify all subfolders or do git rm on this file which is not very convenient. if we don't need a file in a repo it's better to ignore it imo if it doesn't break things

Copy link
Contributor

@SebastienGllmt SebastienGllmt Dec 21, 2023

Choose a reason for hiding this comment

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

Many systems and tools look at git for their behavior. Please let's not waste time arguing about this and just do it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as discussed on the call i added back this file since it was not tracked by git and removed it from gitignore

2,587 changes: 1,472 additions & 1,115 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/batcher/address-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"pg": "^8.7.3",
"web3": "1.10.0",
"assert-never": "^1.2.1",
"@dcspark/carp-client": "^2.3.0"
"@dcspark/carp-client": "^2.3.1"
},
"devDependencies": {
"@types/pg": "^8.6.5"
Expand Down
Empty file modified packages/build-utils/paima-build-utils/scripts/change-db.js
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion packages/engine/paima-funnel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
},
"dependencies": {
"assert-never": "^1.2.1",
"@dcspark/carp-client": "^2.3.0"
"@dcspark/carp-client": "^2.3.1"
}
}
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
68 changes: 49 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,28 @@
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 +142,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 +303,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