Skip to content

Commit db3d33e

Browse files
Merge pull request #59 from SundaeSwap-finance/pi/SSW-307-lp-minting-optimization
Resolve SSW-307
2 parents 1fe274c + b6bc2b8 commit db3d33e

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

validators/pool.ak

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,29 @@ validator(settings_policy_id: PolicyId) {
459459
pool_output_datum_correct,
460460
}
461461
}
462-
// when minting an LP token, we just need to make sure the pool NFT is present in one of the inputs,
463-
// meaning the pool script will enforce the correct name and quantity.
464-
//
465-
// Of particular note, you might expect this to fail when minting the initial LP tokens
466-
// but the minting policy only runs once, so it would be running with a different redeemer in that case
467-
// And it's not possible to include a *separate* minting redeemer to run the script twice; (TODO: check this)
468-
// even if it were, the pool token wouldn't be on the **inuputs** when we're minting the pool.
462+
// When minting an LP token, we just need to make sure the pool script is being spent, as it will enforce the correct
463+
// name and quantity of the LP tokens.
469464
//
470-
// It's also important that the pool script and the minting script checks that *no other* tokens of this policy are minted,
471-
// for example for a different pool. It should be ok if a token from a *different* policy is minted, though.
465+
// To do that, we could check for the pool NFT on the inputs, but this is expensive, especially if the pool input ends up being one of the last.
466+
// So instead we check that the pool NFT is in the first output (this is safe to assume because it's unique, and if it's in any other output it will fail)
467+
// and that we're not minting the pool token (i.e. someone could "pretend" to mint LP tokens, but also mint the pool token to make it look like a scoop)
468+
//
469+
// So, lets enumerate the possible cases:
470+
// - We use the CreatePool redeemer; this checks that *only* the correct pool token and correct number of LP tokens are minted
471+
// - We use the MintLP redeemer; this checks that the pool token (which is unique and locked in the pool script) is in the outputs, and not minted
472+
// - the pool script checks that only the correct number of LP tokens, and nothing else under this policy ID, are minted
473+
// And the impossible cases:
474+
// - During CreatePool, it would be impossible to mint multiple of the same pool tokens; a different pool token; a different number of LP tokens; or a different pool's LP tokens
475+
// - During MintLP, it would be impossible to mint the relevant pool token; thus, the pool script must run, and thus it will be impossible to mint another pool token, a different pool
476+
// ident pool token, a different quantity of LP tokens, or a different pools LP tokens
472477
MintLP(pool_ident) -> {
473478
expect Mint(own_policy_id) = ctx.purpose
474479
let pool_nft_name = shared.pool_nft_name(pool_ident)
475-
let allows_to_spend =
476-
fn(v) {
477-
value.quantity_of(v, own_policy_id, pool_nft_name) == 1
478-
}
479-
list.any(
480-
ctx.transaction.inputs,
481-
fn(input) { allows_to_spend(input.output.value) },
482-
)
480+
expect Some(pool_output) = list.head(ctx.transaction.outputs)
481+
and {
482+
(pool_output.value |> value.quantity_of(own_policy_id, pool_nft_name)) == 1,
483+
(ctx.transaction.mint |> value.from_minted_value |> value.quantity_of(own_policy_id, pool_nft_name)) == 0,
484+
}
483485
}
484486
}
485487
}

0 commit comments

Comments
 (0)