Skip to content

Commit

Permalink
Merge pull request #36 from SundaeSwap-finance/pi/time-handling
Browse files Browse the repository at this point in the history
Handle time slightly differently
  • Loading branch information
Quantumplation authored Mar 25, 2024
2 parents a2f3ab0 + bc9febf commit 4bfe56c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/taste-test/src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export type TScriptType =
* Common arguments for the deposit and update methods of the TasteTest class instance.
*/
export interface IBaseArgs {
time?: number;
deadline?: number;
referralFee?: ITxBuilderReferralFee;
validatorAddress: string;
scripts: {
Expand Down Expand Up @@ -108,8 +110,8 @@ export interface IUpdateArgs extends IDepositArgs {
* Arguments for the deposit withdraw of the TasteTest class instance.
*/
export interface IWithdrawArgs extends IBaseArgs {
deadline: number;
penaltyAddress: string;
deadline: number;
}

/**
Expand Down
39 changes: 27 additions & 12 deletions packages/taste-test/src/lib/classes/TasteTest.Lucid.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export class TasteTestLucid implements AbstractTasteTest {
const correctAmount =
BigInt(args.assetAmount.amount) + TT_UTXO_ADDITIONAL_ADA;

const [lowerBound, upperBound] = this._getTxBounds(args.time, args.deadline);

const tx = this.lucid
.newTx()
.collectFrom([coveringNode], redeemerNodeValidator)
Expand All @@ -225,8 +227,8 @@ export class TasteTestLucid implements AbstractTasteTest {
)
.addSignerKey(userKey)
.mintAssets(assets, redeemerNodePolicy)
.validFrom(Date.now() - VALID_FROM_TOLERANCE_MS)
.validTo(Date.now() + VALID_TO_TOLERANCE_MS);
.validFrom(lowerBound)
.validTo(upperBound);

await Promise.all([
this._attachScriptsOrReferenceInputs(tx, args.scripts.policy),
Expand Down Expand Up @@ -297,6 +299,8 @@ export class TasteTestLucid implements AbstractTasteTest {
lovelace: ownNode.assets.lovelace + args.assetAmount.amount,
};

const [lowerBound, upperBound] = this._getTxBounds(args.time, args.deadline);

const tx = this.lucid
.newTx()
.collectFrom([ownNode], redeemerNodeValidator)
Expand All @@ -305,8 +309,8 @@ export class TasteTestLucid implements AbstractTasteTest {
{ inline: ownNode.datum },
newNodeAssets
)
.validFrom(Date.now() - VALID_FROM_TOLERANCE_MS)
.validTo(Date.now() + VALID_TO_TOLERANCE_MS);
.validFrom(lowerBound)
.validTo(upperBound);

await this._attachScriptsOrReferenceInputs(tx, args.scripts.validator);

Expand Down Expand Up @@ -399,9 +403,10 @@ export class TasteTestLucid implements AbstractTasteTest {

const redeemerNodeValidator = Data.to("LinkedListAct", NodeValidatorAction);

const beforeDeadline = Date.now() < args.deadline;
const [lowerBound, upperBound] = this._getTxBounds(args.time, args.deadline);
const beforeDeadline = upperBound < args.deadline;
const beforeTwentyFourHours =
Date.now() < args.deadline - TWENTY_FOUR_HOURS_MS;
upperBound < args.deadline - TWENTY_FOUR_HOURS_MS;

if (beforeDeadline && !beforeTwentyFourHours) {
const penaltyAmount = divCeil(
Expand All @@ -422,8 +427,8 @@ export class TasteTestLucid implements AbstractTasteTest {
})
.addSignerKey(userKey)
.mintAssets(assetsToBurn, redeemerNodePolicy)
.validFrom(Date.now() - VALID_FROM_TOLERANCE_MS)
.validTo(Date.now() + VALID_TO_TOLERANCE_MS);
.validFrom(lowerBound)
.validTo(upperBound);

await Promise.all([
this._attachScriptsOrReferenceInputs(tx, args.scripts.policy),
Expand Down Expand Up @@ -452,8 +457,8 @@ export class TasteTestLucid implements AbstractTasteTest {
)
.addSignerKey(userKey)
.mintAssets(assetsToBurn, redeemerNodePolicy)
.validFrom(Date.now() - VALID_FROM_TOLERANCE_MS)
.validTo(Date.now() + VALID_TO_TOLERANCE_MS);
.validFrom(lowerBound)
.validTo(upperBound);

await Promise.all([
this._attachScriptsOrReferenceInputs(tx, args.scripts.policy),
Expand Down Expand Up @@ -534,13 +539,15 @@ export class TasteTestLucid implements AbstractTasteTest {
throw new Error("Could not derive a PolicyID for burning the node NFT!");
}

const [lowerBound, upperBound] = this._getTxBounds(args.time);

const tx = this.lucid
.newTx()
.collectFrom([ownNode], redeemerNodeValidator)
.readFrom([rewardFoldUtxo])
.addSignerKey(userKey)
.validFrom(Date.now() - VALID_FROM_TOLERANCE_MS)
.validTo(Date.now() + VALID_TO_TOLERANCE_MS);
.validFrom(lowerBound)
.validTo(upperBound);

if (args?.burnFoldToken) {
tx.mintAssets(
Expand Down Expand Up @@ -712,4 +719,12 @@ export class TasteTestLucid implements AbstractTasteTest {
tx.readFrom(await this.lucid.utxosByOutRef([script.value.outRef]));
}
}

private _getTxBounds(time?: number, deadline?: number): [number, number] {
const lowerBound = (time ?? Date.now()) - VALID_FROM_TOLERANCE_MS;
const naturalUpperBound = (time ?? Date.now()) + VALID_TO_TOLERANCE_MS
const upperBound = !!deadline ? Math.min(deadline - 1, naturalUpperBound) : naturalUpperBound;

return [lowerBound, upperBound];
}
}
5 changes: 3 additions & 2 deletions packages/taste-test/src/lib/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const FOLDING_FEE_ADA = 1_000_000n;
export const MIN_COMMITMENT_ADA = 1_000_000n;
export const TT_UTXO_ADDITIONAL_ADA = NODE_DEPOSIT_ADA + FOLDING_FEE_ADA * 2n;

export const VALID_FROM_TOLERANCE_MS = 100_000;
export const VALID_TO_TOLERANCE_MS = 1000_000;
export const VALID_FROM_TOLERANCE_MS = 20_000;
// TODO: return this from the API
export const VALID_TO_TOLERANCE_MS = 600_000;

0 comments on commit 4bfe56c

Please sign in to comment.