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

Clean unlimited balances simulation #165

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
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
29 changes: 21 additions & 8 deletions packages/simulation-sdk/src/SimulationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
} from "./errors.js";
import {
type MaybeDraft,
type SimulationResult,
produceImmutable,
simulateOperation,
simulateOperations,
Expand Down Expand Up @@ -659,20 +660,20 @@ export class SimulationState implements InputSimulationState {
operations: Operation[],
holders: Address[],
) {
const virtualBundlerData = produceImmutable(this, (draft) => {
const virtualHoldersData = produceImmutable(this, (draft) => {
holders
.flatMap((holder) => Object.values(draft.holdings[holder] ?? {}))
.forEach((bundlerTokenData) => {
.forEach((holderTokenData) => {
// Virtual balance to calculate the amount required.
bundlerTokenData.balance += MathLib.MAX_UINT_160;
holderTokenData.balance += MathLib.MAX_UINT_160;
});
});

const steps = simulateOperations(operations, virtualBundlerData);
const steps = simulateOperations(operations, virtualHoldersData);

const bundlerTokenDiffs = holders
const holdersTokenDiffs = holders
.flatMap((holder) =>
keys(virtualBundlerData.holdings[holder]).map(
keys(virtualHoldersData.holdings[holder]).map(
(token) => [holder, token] as const,
),
)
Expand Down Expand Up @@ -700,10 +701,22 @@ export class SimulationState implements InputSimulationState {
}));

return {
requiredTokenAmounts: bundlerTokenDiffs.filter(
requiredTokenAmounts: holdersTokenDiffs.filter(
({ required }) => required > 0n,
),
steps,
steps: steps.map((step) =>
produceImmutable(step, (draft) =>
holders
.flatMap((holder) => Object.values(draft.holdings[holder] ?? {}))
.forEach((holderTokenData) => {
// Change virtual balance back to real value
holderTokenData.balance = MathLib.max(
0n,
holderTokenData.balance - MathLib.MAX_UINT_160,
);
}),
),
) as SimulationResult,
};
}
}