Skip to content

Commit

Permalink
example+wip of counting seller
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Oct 31, 2024
1 parent 2ac00de commit ac55fec
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 2 deletions.
37 changes: 36 additions & 1 deletion examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function main(): Promise<void> {

// const utxos = await lucid.utxosAt(address);

const txComplete = await _cancelLbeV2EventByOwnerExample(
const txComplete = await _lbeV2AddMoreSellersExample(
lucid,
address,
blockfrostAdapter
Expand Down Expand Up @@ -1221,6 +1221,41 @@ async function _createLbeV2EventExample(
});
}

// Example Tx: b3c7049ff4402bdb2f3fe6522c720fad499d5f3dae512299dfb3a5e011a66496
async function _lbeV2AddMoreSellersExample(
lucid: Lucid,
address: Address,
blockfrostAdapter: BlockfrostAdapter
): Promise<TxComplete> {
const baseAsset = Asset.fromString(
"e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed7243414b45"
);
const raiseAsset = Asset.fromString("lovelace");

const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);
const treasury = await blockfrostAdapter.getLbeV2TreasuryByLbeId(lbeId);
invariant(treasury !== null, `Can not find treasury by lbeId ${lbeId}`);
const treasuryUtxos = await lucid.utxosByOutRef([
{ txHash: treasury.txIn.txHash, outputIndex: treasury.txIn.index },
]);
invariant(treasuryUtxos.length === 1, "Can not find treasury Utxo");

const manager = await blockfrostAdapter.getLbeV2ManagerByLbeId(lbeId);
invariant(manager !== null, `Can not find manager by lbeId ${lbeId}`);
const managerUtxos = await lucid.utxosByOutRef([
{ txHash: manager.txIn.txHash, outputIndex: manager.txIn.index },
]);
invariant(managerUtxos.length === 1, "Can not find manager Utxo");

return new LbeV2(lucid).addSellers({
treasuryUtxo: treasuryUtxos[0],
managerUtxo: managerUtxos[0],
addSellerCount: 2,
sellerOwner: address,
currentSlot: await blockfrostAdapter.currentSlot(),
});
}

// Example Tx: b1819fbee0bb1eace80f97a75089a8b87047ea2f18959092949306e5301b048d
async function _cancelLbeV2EventByOwnerExample(
lucid: Lucid,
Expand Down
174 changes: 173 additions & 1 deletion src/lbe-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export type AddSellersOptions = {
currentSlot: number;
};

export type CountingSellersOptions = {
treasuryUtxo: UTxO;
managerUtxo: UTxO;
sellerUtxos: UTxO[];
currentSlot: number;
};

const THREE_HOUR_IN_MS = 3 * 60 * 60 * 1000;

export class LbeV2 {
Expand All @@ -102,7 +109,7 @@ export class LbeV2 {
}

// MARK: CREATE EVENT
private validateCreateEvent(options: LbeV2CreateEventOptions): void {
validateCreateEvent(options: LbeV2CreateEventOptions): void {
const { lbeV2Parameters, currentSlot, factoryUtxo, projectDetails } =
options;
const currentTime = this.lucid.utils.slotToUnixTime(currentSlot);
Expand Down Expand Up @@ -1218,4 +1225,169 @@ export class LbeV2 {

return lucidTx.complete();
}

// MARK: COUNTING SELLER
validateCountingSeller(options: CountingSellersOptions): void {
const { treasuryUtxo, managerUtxo, sellerUtxos, currentSlot } = options;
const currentTime = this.lucid.utils.slotToUnixTime(currentSlot);
const config = LbeV2Constant.CONFIG[this.networkId];

const rawTreasuryDatum = treasuryUtxo.datum;
invariant(rawTreasuryDatum, "Treasury utxo must have inline datum");
const treasuryDatum = LbeV2Types.TreasuryDatum.fromPlutusData(
this.networkId,
Data.from(rawTreasuryDatum)
);
invariant(
config.treasuryAsset in treasuryUtxo.assets,
"Treasury utxo assets must have treasury asset"
);

const rawManagerDatum = managerUtxo.datum;
invariant(rawManagerDatum, "Treasury utxo must have inline datum");
const managerDatum = LbeV2Types.ManagerDatum.fromPlutusData(
Data.from(rawManagerDatum)
);
invariant(
config.managerAsset in managerUtxo.assets,
"Manager utxo assets must have manager asset"
);

invariant(addSellerCount > 0, "Must add at least one seller");
invariant(
PoolV2.computeLPAssetName(
treasuryDatum.baseAsset,
treasuryDatum.raiseAsset
) ===
PoolV2.computeLPAssetName(
managerDatum.baseAsset,
managerDatum.raiseAsset
),
"treasury, manager must have same Lbe ID"
);
invariant(
currentTime > treasuryDatum.endTime,
"Must counting seller in encounter phase"
);
}

async countingSellers(options: CountingSellersOptions): Promise<TxComplete> {
this.validateAddSeller(options);
const {
treasuryUtxo,
managerUtxo,
addSellerCount,
sellerOwner,
currentSlot,
} = options;
const currentTime = this.lucid.utils.slotToUnixTime(currentSlot);
const config = LbeV2Constant.CONFIG[this.networkId];

const rawTreasuryDatum = treasuryUtxo.datum;
invariant(rawTreasuryDatum, "Treasury utxo must have inline datum");
const treasuryDatum = LbeV2Types.TreasuryDatum.fromPlutusData(
this.networkId,
Data.from(rawTreasuryDatum)
);

const rawManagerDatum = managerUtxo.datum;
invariant(rawManagerDatum, "Treasury utxo must have inline datum");
const managerDatum = LbeV2Types.ManagerDatum.fromPlutusData(
Data.from(rawManagerDatum)
);

const lucidTx = this.lucid.newTx();

// READ FROM
const factoryRefs = await this.lucid.utxosByOutRef([
LbeV2Constant.DEPLOYED_SCRIPTS[this.networkId].factory,
]);
invariant(
factoryRefs.length === 1,
"cannot find deployed script for LbeV2 Factory"
);
lucidTx.readFrom(factoryRefs);

const managerRefs = await this.lucid.utxosByOutRef([
LbeV2Constant.DEPLOYED_SCRIPTS[this.networkId].manager,
]);
invariant(
managerRefs.length === 1,
"cannot find deployed script for LbeV2 Manager"
);
lucidTx.readFrom(managerRefs);

lucidTx.readFrom([treasuryUtxo]);

// COLLECT FROM
lucidTx.collectFrom(
[managerUtxo],
Data.to(
LbeV2Types.ManagerRedeemer.toPlutusData(
LbeV2Types.ManagerRedeemer.ADD_SELLERS
)
)
);

// MINT
lucidTx.mintAssets(
{ [config.sellerAsset]: BigInt(addSellerCount) },
Data.to(
LbeV2Types.FactoryRedeemer.toPlutusData({
type: LbeV2Types.FactoryRedeemerType.MINT_SELLER,
})
)
);

// PAY TO
const newManagerDatum: LbeV2Types.ManagerDatum = {
...managerDatum,
sellerCount: managerDatum.sellerCount + BigInt(addSellerCount),
};
lucidTx.payToContract(
config.managerAddress,
{
inline: Data.to(LbeV2Types.ManagerDatum.toPlutusData(newManagerDatum)),
},
{ ...managerUtxo.assets }
);
for (let i = 0; i < addSellerCount; ++i) {
lucidTx.payToContract(
config.sellerAddress,
{
inline: Data.to(
LbeV2Types.SellerDatum.toPlutusData({
factoryPolicyId: config.factoryHash,
owner: sellerOwner,
baseAsset: treasuryDatum.baseAsset,
raiseAsset: treasuryDatum.raiseAsset,
amount: 0n,
penaltyAmount: 0n,
})
),
},
{
[config.sellerAsset]: 1n,
lovelace: LbeV2Constant.SELLER_MIN_ADA,
}
);
}

// VALID TIME RANGE
lucidTx
.validFrom(currentTime)
.validTo(
Math.min(
currentTime + THREE_HOUR_IN_MS,
Number(treasuryDatum.endTime) - 1000
)
);

// METADATA
lucidTx.attachMetadata(674, {
msg: [MetadataMessage.ADD_SELLERS],
});

return lucidTx.complete();
}
}

0 comments on commit ac55fec

Please sign in to comment.