Skip to content

Commit

Permalink
Queries & rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
gostkin committed Dec 4, 2023
1 parent 86fb02e commit 0d8d5ff
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 73 deletions.
24 changes: 6 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ChainDataExtensionDatumType, DEFAULT_FUNNEL_TIMEOUT, timeout } from '@paima/utils';
import type {
CdeCardanoProjectedNFTDatum,
ChainDataExtensionCardanoProjectedNFT,
ChainDataExtensionDatum,
} from '@paima/runtime';
} from '@paima/sm';
import { ChainDataExtensionDatumType, DEFAULT_FUNNEL_TIMEOUT, timeout } from '@paima/utils';
import { Routes, query } from '@dcspark/carp-client/client/src';
import {ProjectedNftRangeResponse} from "@dcspark/carp-client/shared/models/ProjectedNftRange";
import type { ProjectedNftRangeResponse } from "@dcspark/carp-client/shared/models/ProjectedNftRange";

export default async function getCdeData(
url: string,
Expand All @@ -21,24 +21,37 @@ 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.actionSlot))).filter(e => e != null).map(e => e!!);
}

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

return {
cdeId: extension.cdeId,
cdeDatumType: ChainDataExtensionDatumType.CardanoProjectedNFT,
blockNumber,
payload: {
address: event.address,
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,
plutusDatum: event.plutusDatum || "",

forHowLong: event.forHowLong || undefined,
},
scheduledPrefix: extension.scheduledPrefix,
};
Expand Down
51 changes: 41 additions & 10 deletions packages/engine/paima-sm/src/cde-cardano-projected-nft.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
import { ENV } from '@paima/utils';
import type { CdeCardanoProjectedNFTDatum } from '@paima/runtime';
import { createScheduledData, cdeCardanoProjectedNftInsertData } from '@paima/db';
import type { CdeCardanoProjectedNFTDatum } from './types';
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 address = cdeDatum.payload.address;
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}|${address}|${asset}|${amount}|${status}|${datum}`;
const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${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),
[
cdeCardanoProjectedNftInsertData,
cdeCardanoProjectedNftUpdateData,
{
cde_id: cdeId,
address: cdeDatum.payload.address,
asset: cdeDatum.payload.asset,
amount: cdeDatum.payload.amount,
status: cdeDatum.payload.status,
datum: cdeDatum.payload.plutusDatum
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;

}
49 changes: 46 additions & 3 deletions packages/engine/paima-sm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ interface CdeDatumCardanoPoolPayload {
pool: string | undefined;
}

interface CdeDatumCardanoProjectedNFTPayload {
ownerAddress: string,

actionTxId: string,
actionOutputIndex: number | undefined,

previousTxHash: string | undefined,
previousTxOutputIndex: number | undefined,

asset: string,
amount: number,
status: string,
plutusDatum: string,

forHowLong: number | undefined,
}

type ChainDataExtensionPayload =
| CdeDatumErc20TransferPayload
| CdeDatumErc721MintPayload
Expand All @@ -80,7 +97,8 @@ type ChainDataExtensionPayload =
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
| CdeDatumGenericPayload
| CdeDatumErc6551RegistryPayload
| CdeDatumCardanoPoolPayload;
| CdeDatumCardanoPoolPayload
| CdeDatumCardanoProjectedNFTPayload;

interface CdeDatumBase {
cdeId: number;
Expand Down Expand Up @@ -129,14 +147,21 @@ export interface CdeCardanoPoolDatum extends CdeDatumBase {
scheduledPrefix: string;
}

export interface CdeCardanoProjectedNFTDatum extends CdeDatumBase {
cdeDatumType: ChainDataExtensionDatumType.CardanoProjectedNFT;
payload: CdeDatumCardanoProjectedNFTPayload;
scheduledPrefix: string;
}

export type ChainDataExtensionDatum =
| CdeErc20TransferDatum
| CdeErc721MintDatum
| CdeErc721TransferDatum
| CdeErc20DepositDatum
| CdeGenericDatum
| CdeErc6551RegistryDatum
| CdeCardanoPoolDatum;
| CdeCardanoPoolDatum
| CdeCardanoProjectedNFTDatum;

export enum CdeEntryTypeName {
Generic = 'generic',
Expand All @@ -145,6 +170,7 @@ export enum CdeEntryTypeName {
ERC721 = 'erc721',
ERC6551Registry = 'erc6551-registry',
CardanoDelegation = 'cardano-stake-delegation',
CardanoProjectedNFT = 'cardano-projected-nft',
}

const EvmAddress = Type.Transform(Type.RegExp('0x[0-9a-fA-F]{40}'))
Expand Down Expand Up @@ -262,6 +288,21 @@ export type ChainDataExtensionCardanoDelegation = ChainDataExtensionBase &
cdeType: ChainDataExtensionType.CardanoPool;
};

export const ChainDataExtensionCardanoProjectedNFTConfig = Type.Intersect([
Type.Object({
type: Type.Literal(CdeEntryTypeName.CardanoProjectedNFT),
contract: Type.String(),
scheduledPrefix: Type.String(),
startSlot: Type.Number(),
stopSlot: Type.Optional(Type.Number()),
name: Type.String(),
}),
]);
export type ChainDataExtensionCardanoProjectedNFT = ChainDataExtensionBase &
Static<typeof ChainDataExtensionCardanoProjectedNFTConfig> & {
cdeType: ChainDataExtensionType.CardanoProjectedNFT;
};

export const CdeConfig = Type.Object({
extensions: Type.Array(
Type.Union([
Expand All @@ -271,6 +312,7 @@ export const CdeConfig = Type.Object({
ChainDataExtensionGenericConfig,
ChainDataExtensionErc6551RegistryConfig,
ChainDataExtensionCardanoDelegationConfig,
ChainDataExtensionCardanoProjectedNFTConfig,
])
),
});
Expand All @@ -295,7 +337,8 @@ export type ChainDataExtension =
| ChainDataExtensionErc20Deposit
| ChainDataExtensionGeneric
| ChainDataExtensionErc6551Registry
| ChainDataExtensionCardanoDelegation;
| ChainDataExtensionCardanoDelegation
| ChainDataExtensionCardanoProjectedNFT;

export type GameStateTransitionFunctionRouter = (
blockHeight: number
Expand Down
11 changes: 8 additions & 3 deletions packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ CREATE TABLE cde_cardano_pool_delegation (
CREATE TABLE cde_cardano_projected_nft (
cde_id INTEGER NOT NULL,
id SERIAL,
address TEXT NOT NULL,
owner_address TEXT NOT NULL,
previous_tx_hash TEXT,
previous_tx_output_index INTEGER,
current_tx_hash TEXT NOT NULL,
current_tx_output_index INTEGER,
asset TEXT NOT NULL,
amount TEXT NOT NULL,
amount BIGINT NOT NULL,
status TEXT NOT NULL,
datum TEXT NOT NULL,
plutus_datum TEXT NOT NULL,
for_how_long BIGINT,
PRIMARY KEY (cde_id, id)
);
2 changes: 2 additions & 0 deletions packages/node-sdk/paima-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type * from './sql/emulated.queries.js';
export type * from './types.js';
export * from './sql/cde-cardano-pool-delegation.queries.js';
export type * from './sql/cde-cardano-pool-delegation.queries.js';
export * from './sql/cde-cardano-projected-nft.queries.js';
export type * from './sql/cde-cardano-projected-nft.queries.js';
export * from './sql/cde-tracking-cardano.queries.js';
export type * from './sql/cde-tracking-cardano.queries.js';

Expand Down
Loading

0 comments on commit 0d8d5ff

Please sign in to comment.