Skip to content

Commit

Permalink
Initial setups
Browse files Browse the repository at this point in the history
  • Loading branch information
gostkin committed Nov 27, 2023
1 parent 94ddeaf commit d28860b
Show file tree
Hide file tree
Showing 19 changed files with 996 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ bin

# logs
*.log

.idea/
800 changes: 766 additions & 34 deletions package-lock.json

Large diffs are not rendered by default.

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

export default async function getCdeData(
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.slot)));
}

function eventToCdeDatum(
event: ProjectedNftRangeResponse[0],
extension: ChainDataExtensionCardanoProjectedNFT,
blockNumber: number
): CdeCardanoProjectedNFTDatum {
return {
cdeId: extension.cdeId,
cdeDatumType: ChainDataExtensionDatumType.CardanoProjectedNFT,
blockNumber,
payload: {
address: event.address,
asset: event.asset,
amount: event.amount,
status: event.status,
plutusDatum: event.plutusDatum,
},
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
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 @@ -29,6 +29,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
8 changes: 4 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,13 @@
import * as fs from 'fs/promises';

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

import type { ChainDataExtension } from '../types.js';
import type {ChainDataExtension} from '../types.js';

export function getEarliestStartBlockheight(config: ChainDataExtension[]): number {
const startBlockheights = config
.map(cde => {
if (cde.cdeType != ChainDataExtensionType.CardanoPool) {
if (cde.cdeType != ChainDataExtensionType.CardanoPool && cde.cdeType != ChainDataExtensionType.CardanoProjectedNFT) {
return cde.startBlockHeight;
} else {
return null;
Expand All @@ -22,7 +22,7 @@ export function getEarliestStartBlockheight(config: ChainDataExtension[]): numbe
export function getEarliestStartSlot(config: ChainDataExtension[]): number {
const startSlots = config
.map(cde => {
if (cde.cdeType == ChainDataExtensionType.CardanoPool) {
if (cde.cdeType == ChainDataExtensionType.CardanoPool || cde.cdeType == ChainDataExtensionType.CardanoProjectedNFT) {
return cde.startSlot;
} else {
return null;
Expand Down
41 changes: 38 additions & 3 deletions packages/engine/paima-runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ interface CdeDatumCardanoPoolPayload {
pool: string | undefined;
}

interface CdeDatumCardanoProjectedNFTPayload {
address: string,
asset: string,
amount: string,
status: string,
plutusDatum: string,
}

type ChainDataExtensionPayload =
| CdeDatumErc20TransferPayload
| CdeDatumErc721MintPayload
Expand All @@ -84,7 +92,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 @@ -133,14 +142,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 @@ -149,6 +165,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 @@ -265,11 +282,27 @@ export const ChainDataExtensionCardanoDelegationConfig = Type.Intersect([
}),
]);

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 ChainDataExtensionCardanoDelegation = ChainDataExtensionBase &
Static<typeof ChainDataExtensionCardanoDelegationConfig> & {
cdeType: ChainDataExtensionType.CardanoPool;
};

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

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

export type ReadPresyncDataFrom = {
network: Network;
Expand Down
33 changes: 33 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,33 @@
import { ENV } from '@paima/utils';
import type { CdeCardanoProjectedNFTDatum } from '@paima/runtime';
import { createScheduledData, cdeCardanoProjectedNftInsertData } 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 amount = cdeDatum.payload.amount;
const asset = cdeDatum.payload.asset;
const status = cdeDatum.payload.status;
const datum = cdeDatum.payload.plutusDatum;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledInputData = `${prefix}|${address}|${asset}|${amount}|${status}|${datum}`;

const updateList: SQLUpdate[] = [
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
cdeCardanoProjectedNftInsertData,
{
cde_id: cdeId,
address: cdeDatum.payload.address,
asset: cdeDatum.payload.asset,
amount: cdeDatum.payload.amount,
status: cdeDatum.payload.status,
datum: cdeDatum.payload.plutusDatum
},
],
];
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
1 change: 1 addition & 0 deletions packages/node-sdk/paima-db/migrations/down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ DROP TABLE nonces;
DROP TABLE scheduled_data;
DROP TABLE block_heights;
DROP TABLE cde_cardano_pool_delegation;
DROP TABLE cde_cardano_projected_nft;
11 changes: 11 additions & 0 deletions packages/node-sdk/paima-db/migrations/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@ CREATE TABLE cde_cardano_pool_delegation (
pool TEXT,
PRIMARY KEY (cde_id, address)
);

CREATE TABLE cde_cardano_projected_nft (
cde_id INTEGER NOT NULL,
id SERIAL,
address TEXT NOT NULL,
asset TEXT NOT NULL,
amount TEXT NOT NULL,
status TEXT NOT NULL,
datum TEXT NOT NULL,
PRIMARY KEY (cde_id, id)
);
28 changes: 28 additions & 0 deletions packages/node-sdk/paima-db/src/sql/cde-cardano-projected-nft.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @name cdeCardanoGetProjectedNft */
SELECT * FROM cde_cardano_projected_nft
WHERE address = :address!;

/* @name cdeCardanoProjectedNftInsertData */
INSERT INTO cde_cardano_pool_delegation(
cde_id,
address,
asset,
amount,
status,
datum,
) VALUES (
:cde_id!,
:address!,
:asset!
:amount!
:status!
:datum!
) ON CONFLICT (cde_id, address) DO
UPDATE SET pool = :pool!;

cde_id: cdeId,
address: cdeDatum.payload.address,
asset: cdeDatum.payload.asset,
amount: cdeDatum.payload.amount,
status: cdeDatum.payload.status,
datum: cdeDatum.payload.plutusDatum
13 changes: 13 additions & 0 deletions packages/node-sdk/paima-utils-backend/src/cde-access-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
cdeErc6551GetOwnedAccounts,
cdeErc6551GetOwner,
cdeCardanoPoolGetAddressDelegation,
cdeCardanoGetProjectedNft,
} from '@paima/db';
import type { OwnedNftsResponse, GenericCdeDataUnit, TokenIdPair } from './types.js';

Expand Down Expand Up @@ -190,3 +191,15 @@ export async function internalGetCardanoAddressDelegation(

return results[0].pool;
}

export async function internalGetCardanoProjectedNft(
readonlyDBConn: Pool,
address: string
): Promise<string | null> {
const results = await cdeCardanoGetProjectedNft.run({ address }, readonlyDBConn);
if (results.length === 0) {
return null;
}

return results[0].pool;
}
11 changes: 11 additions & 0 deletions packages/node-sdk/paima-utils-backend/src/cde-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
internalGetErc6551AccountOwner,
internalGetAllOwnedErc6551Accounts,
internalGetCardanoAddressDelegation,
internalGetCardanoProjectedNft,
} from './cde-access-internals.js';
import type { OwnedNftsResponse, GenericCdeDataUnit, TokenIdPair } from './types.js';

Expand Down Expand Up @@ -167,3 +168,13 @@ export async function getCardanoAddressDelegation(
): Promise<string | null> {
return await internalGetCardanoAddressDelegation(readonlyDBConn, address);
}

/**
* Fetch the pool this address is delegating to, if any.
*/
export async function getCardanoAddressProjectedNft(
readonlyDBConn: Pool,
address: string
): Promise<string | null> {
return await internalGetCardanoProjectedNft(readonlyDBConn, address);
}
2 changes: 2 additions & 0 deletions packages/paima-sdk/paima-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const enum ChainDataExtensionType {
Generic = 5,
ERC6551Registry = 6,
CardanoPool = 7,
CardanoProjectedNFT = 8,
}

export const enum ChainDataExtensionDatumType {
Expand All @@ -29,6 +30,7 @@ export const enum ChainDataExtensionDatumType {
Generic,
ERC6551Registry,
CardanoPool,
CardanoProjectedNFT,
}

export const enum Network {
Expand Down
Loading

0 comments on commit d28860b

Please sign in to comment.