Skip to content

Commit

Permalink
schedule presync stf events to the first sync block
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Apr 5, 2024
1 parent dbac191 commit 15f41a9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 53 deletions.
5 changes: 3 additions & 2 deletions packages/engine/paima-sm/src/cde-cardano-mint-burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { SQLUpdate } from '@paima/db';
import type { CdeCardanoMintBurnDatum } from './types.js';

export default async function processDatum(
cdeDatum: CdeCardanoMintBurnDatum
cdeDatum: CdeCardanoMintBurnDatum,
inPresync: boolean
): Promise<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
Expand All @@ -14,7 +15,7 @@ export default async function processDatum(
const inputAddresses = cdeDatum.payload.inputAddresses;
const outputAddresses = cdeDatum.payload.outputAddresses;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const scheduledInputData = `${prefix}|${txId}|${metadata}|${assets}|${JSON.stringify(inputAddresses)}|${JSON.stringify(outputAddresses)}`;

const updateList: SQLUpdate[] = [
Expand Down
14 changes: 6 additions & 8 deletions packages/engine/paima-sm/src/cde-cardano-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ export default async function processDatum(
const address = cdeDatum.payload.address;
const pool = cdeDatum.payload.pool;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const scheduledInputData = `${prefix}|${address}|${pool}`;

const updateList: SQLUpdate[] = inPresync
? []
: [createScheduledData(scheduledInputData, scheduledBlockHeight)];

updateList.push(
const updateList: SQLUpdate[] = [
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
cdeCardanoPoolInsertData,
{
Expand All @@ -34,7 +31,8 @@ export default async function processDatum(
{
address: cdeDatum.payload.address,
},
]
);
],
];

return updateList;
}
62 changes: 31 additions & 31 deletions packages/engine/paima-sm/src/cde-cardano-projected-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,50 @@ export default async function processDatum(
const datum = cdeDatum.payload.plutusDatum;
const forHowLong = cdeDatum.payload.forHowLong;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${policyId}|${assetName}|${status}`;

if (previousTxHash === undefined || previousOutputIndex === undefined) {
const updateList: SQLUpdate[] = inPresync
? []
: [createScheduledData(scheduledInputData, scheduledBlockHeight)];
updateList.push([
cdeCardanoProjectedNftInsertData,
const updateList: SQLUpdate[] = [
createScheduledData(scheduledInputData, scheduledBlockHeight),
[
cdeCardanoProjectedNftInsertData,
{
cde_id: cdeId,
owner_address: ownerAddress,
current_tx_hash: currentTxHash,
current_tx_output_index: currentOutputIndex,
policy_id: policyId,
asset_name: assetName,
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,
current_tx_hash: currentTxHash,
current_tx_output_index: currentOutputIndex,
new_tx_hash: currentTxHash,
new_tx_output_index: currentOutputIndex,
previous_tx_hash: previousTxHash,
previous_tx_output_index: previousOutputIndex,
policy_id: policyId,
asset_name: assetName,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong,
},
]);
return updateList;
}
const updateList: SQLUpdate[] = inPresync
? []
: [createScheduledData(scheduledInputData, scheduledBlockHeight)];
],
];

updateList.push([
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,
policy_id: policyId,
asset_name: assetName,
amount: amount,
status: status,
plutus_datum: datum,
for_how_long: forHowLong,
},
]);
return updateList;
}
5 changes: 3 additions & 2 deletions packages/engine/paima-sm/src/cde-cardano-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { SQLUpdate } from '@paima/db';
import type { CdeCardanoTransferDatum } from './types.js';

export default async function processDatum(
cdeDatum: CdeCardanoTransferDatum
cdeDatum: CdeCardanoTransferDatum,
inPresync: boolean
): Promise<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
Expand All @@ -14,7 +15,7 @@ export default async function processDatum(
const outputs = JSON.stringify(cdeDatum.payload.outputs);
const metadata = cdeDatum.payload.metadata || undefined;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const scheduledInputData = `${prefix}|${txId}|${metadata}|${inputCredentials}|${outputs}`;

const updateList: SQLUpdate[] = [
Expand Down
8 changes: 3 additions & 5 deletions packages/engine/paima-sm/src/cde-erc20-deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ export default async function processErc20Datum(

const updateList: SQLUpdate[] = [];
try {
if (!inPresync) {
const scheduledInputData = `${prefix}|${fromAddr}|${value}`;
const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight));
}
const scheduledInputData = `${prefix}|${fromAddr}|${value}`;
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight));

if (fromRow.length > 0) {
const oldTotal = BigInt(fromRow[0].total_deposited);
Expand Down
4 changes: 2 additions & 2 deletions packages/engine/paima-sm/src/cde-erc721-mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default async function processErc721Datum(
inPresync: boolean
): Promise<SQLUpdate[]> {
const [address, prefix] = [cdeDatum.contractAddress, cdeDatum.scheduledPrefix];
if (!prefix || inPresync) {
if (!prefix) {
return [];
}
const { tokenId, mintData } = cdeDatum.payload;
const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const scheduledInputData = `${prefix}|${address}|${tokenId}|${mintData}`;
return [createScheduledData(scheduledInputData, scheduledBlockHeight)];
}
2 changes: 1 addition & 1 deletion packages/engine/paima-sm/src/cde-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function processDatum(
const payload = cdeDatum.payload;
const prefix = cdeDatum.scheduledPrefix;

const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1);
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber;
const stringifiedPayload = JSON.stringify(payload);
const scheduledInputData = `${prefix}|${stringifiedPayload}`;

Expand Down
4 changes: 2 additions & 2 deletions packages/engine/paima-sm/src/cde-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export async function cdeTransitionFunction(
case ChainDataExtensionDatumType.CardanoAssetUtxo:
return await processCardanoAssetUtxoDatum(cdeDatum);
case ChainDataExtensionDatumType.CardanoTransfer:
return await processCardanoTransferDatum(cdeDatum);
return await processCardanoTransferDatum(cdeDatum, inPresync);
case ChainDataExtensionDatumType.CardanoMintBurn:
return await processCardanoMintBurnDatum(cdeDatum);
return await processCardanoMintBurnDatum(cdeDatum, inPresync);
default:
assertNever(cdeDatum);
}
Expand Down

0 comments on commit 15f41a9

Please sign in to comment.