Skip to content

Commit

Permalink
collect order
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Nov 6, 2024
1 parent 6df1fbe commit eef0461
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 15 deletions.
245 changes: 230 additions & 15 deletions src/lbe-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import { LbeV2Types } from "./types/lbe-v2";
import { NetworkEnvironment, NetworkId } from "./types/network";
import { lucidToNetworkEnv } from "./utils/network.internal";

function compareUtxo(s1: UTxO, s2: UTxO): number {
if (s1.txHash === s2.txHash) {
return s1.outputIndex - s2.outputIndex;
}

if (s1.txHash < s2.txHash) {
return -1;
}
if (s1.txHash === s2.txHash) {
return 0;
}
return 1;
}

export type LbeV2SocialLinks = {
twitter?: string;
telegram?: string;
Expand Down Expand Up @@ -100,6 +114,12 @@ export type CollectManagerOptions = {
currentSlot: number;
};

export type CollectOrdersOptions = {
treasuryUtxo: UTxO;
orderUtxos: UTxO[];
currentSlot: number;
};

const THREE_HOUR_IN_MS = 3 * 60 * 60 * 1000;

export class LbeV2 {
Expand Down Expand Up @@ -1336,19 +1356,7 @@ export class LbeV2 {
const managerDatum = LbeV2Types.ManagerDatum.fromPlutusData(
Data.from(rawManagerDatum)
);
const sortedSellerUtxos = [...sellerUtxos].sort((s1, s2) => {
if (s1.txHash === s2.txHash) {
return s1.outputIndex - s2.outputIndex;
}

if (s1.txHash < s2.txHash) {
return -1;
}
if (s1.txHash === s2.txHash) {
return 0;
}
return 1;
});
const sortedSellerUtxos = [...sellerUtxos].sort(compareUtxo);

const sellerDatums = sortedSellerUtxos.map((utxo) => {
const rawSellerDatum = utxo.datum;
Expand Down Expand Up @@ -1526,6 +1534,13 @@ export class LbeV2 {
Data.from(rawManagerDatum)
);

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

const lucidTx = this.lucid.newTx();

// READ FROM
Expand Down Expand Up @@ -1585,14 +1600,214 @@ export class LbeV2 {
);

// PAY TO
// TODO
lucidTx.payToContract(
treasuryUtxo.address,
{
inline: Data.to(
LbeV2Types.TreasuryDatum.toPlutusData({
...treasuryDatum,
isManagerCollected: true,
reserveRaise: managerDatum.reserveRaise,
totalPenalty: managerDatum.totalPenalty,
})
),
},
treasuryUtxo.assets
);

// VALID TIME RANGE
lucidTx.validFrom(currentTime).validTo(currentTime + THREE_HOUR_IN_MS);

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

return lucidTx.complete();
}

// MARK: COLLECT ORDERS
validateCollectOrders(options: CollectOrdersOptions): void {
const { treasuryUtxo, orderUtxos } = options;
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"
);
let collectAmount = 0n;
for (const orderUtxo of orderUtxos) {
const rawOrderDatum = orderUtxo.datum;
invariant(rawOrderDatum, "Order utxo must have inline datum");
const orderDatum = LbeV2Types.OrderDatum.fromPlutusData(
Data.from(rawOrderDatum),
this.networkId
);
invariant(
config.orderAsset in orderUtxo.assets,
"Order utxo assets must have order asset"
);
invariant(
PoolV2.computeLPAssetName(
treasuryDatum.baseAsset,
treasuryDatum.raiseAsset
) ===
PoolV2.computeLPAssetName(
orderDatum.baseAsset,
orderDatum.raiseAsset
),
"treasury, order must share the same lbe id"
);
collectAmount += orderDatum.amount + orderDatum.penaltyAmount;
}

const remainAmount =
treasuryDatum.reserveRaise +
treasuryDatum.totalPenalty -
treasuryDatum.collectedFund;
invariant(
treasuryDatum.isManagerCollected === true,
"LBE didn't collect manager"
);
invariant(
orderUtxos.length >= LbeV2Constant.MINIMUM_ORDER_COLLECTED ||
collectAmount === remainAmount,
`validateCollectOrders: not collect enough orders LBE having base asset ${treasuryDatum.baseAsset.toString()} and raise asset ${treasuryDatum.raiseAsset.toString()}`
);
}

async collectOrders(options: CollectOrdersOptions): Promise<TxComplete> {
this.validateCollectOrders(options);
const { treasuryUtxo, orderUtxos, 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 sortedOrderUtxos = [...orderUtxos].sort(compareUtxo);
const orderDatums = sortedOrderUtxos.map((utxo) => {
const rawOrderDatum = utxo.datum;
invariant(rawOrderDatum, "Order utxo must have inline datum");
return LbeV2Types.OrderDatum.fromPlutusData(
Data.from(rawOrderDatum),
this.networkId
);
});

let deltaCollectedFund = 0n;
for (const orderDatum of orderDatums) {
deltaCollectedFund += orderDatum.amount + orderDatum.penaltyAmount;
}

const lucidTx = this.lucid.newTx();

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

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

// COLLECT FROM
lucidTx.collectFrom(
orderUtxos,
Data.to(
LbeV2Types.OrderRedeemer.toPlutusData(
LbeV2Types.OrderRedeemer.COLLECT_ORDER
)
)
);
lucidTx.collectFrom(
[treasuryUtxo],
Data.to(
LbeV2Types.TreasuryRedeemer.toPlutusData({
type: LbeV2Types.TreasuryRedeemerType.COLLECT_ORDERS,
})
)
);

// PAY TO
const newTreasuryAssets: Assets = { ...treasuryUtxo.assets };
const raiseAssetUnit = Asset.toString(treasuryDatum.raiseAsset);
if (raiseAssetUnit in newTreasuryAssets) {
newTreasuryAssets[raiseAssetUnit] =
newTreasuryAssets[raiseAssetUnit] + deltaCollectedFund;
} else {
newTreasuryAssets[raiseAssetUnit] = deltaCollectedFund;
}
lucidTx.payToContract(
config.treasuryAddress,
{
inline: Data.to(
LbeV2Types.TreasuryDatum.toPlutusData({
...treasuryDatum,
collectedFund: treasuryDatum.collectedFund + deltaCollectedFund,
})
),
},
newTreasuryAssets
);
for (let i = 0; i < orderDatums.length; ++i) {
const orderDatum = orderDatums[i];
const orderUtxo = sortedOrderUtxos[i];
lucidTx.payToContract(
orderUtxo.address,
{
inline: Data.to(
LbeV2Types.OrderDatum.toPlutusData({
...orderDatum,
isCollected: true,
})
),
},
{
[config.orderAsset]: 1n,
lovelace:
LbeV2Constant.ORDER_MIN_ADA + LbeV2Constant.ORDER_COMMISSION,
}
);
}

// WITHDRAW
lucidTx.withdraw(
config.factoryRewardAddress,
0n,
Data.to(
LbeV2Types.FactoryRedeemer.toPlutusData({
type: LbeV2Types.FactoryRedeemerType.MANAGE_ORDER,
})
)
);

// VALID TIME RANGE
lucidTx.validFrom(currentTime).validTo(currentTime + THREE_HOUR_IN_MS);

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

return lucidTx.complete();
Expand Down
5 changes: 5 additions & 0 deletions src/types/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ export namespace LbeV2Constant {
factoryHash: string;
factoryHashBech32: string;
factoryAddress: string;
factoryRewardAddress: string;

treasuryAsset: string;
treasuryHash: string;
Expand Down Expand Up @@ -630,6 +631,8 @@ export namespace LbeV2Constant {
"script10uhhdx5jvr4knq3jqg40q0a6zths520eflyncn7jvfyhyqahrl3",
factoryAddress:
"addr_test1wplj7a56jfswk6vzxgpz4uplhgfw7z3fl98uj0z06f3yjusz7ufvk",
factoryRewardAddress:
"stake_test17plj7a56jfswk6vzxgpz4uplhgfw7z3fl98uj0z06f3yjuszkz3mu",

treasuryAsset: TESTNET_FACTORY_HASH + TREASURY_AUTH_AN,
treasuryHash: "f0dbf7cdc1042f403cad57cff6f602b2e657f8f557b8cf8c23482954",
Expand Down Expand Up @@ -664,6 +667,7 @@ export namespace LbeV2Constant {
factoryHash: MAINNET_FACTORY_HASH,
factoryHashBech32: "TODO",
factoryAddress: "TODO",
factoryRewardAddress: "TODO",

treasuryAsset: MAINNET_FACTORY_HASH + TREASURY_AUTH_AN,
treasuryHash: "1ce6abbd967cab867ad73855f8b154fcc57e41b15605b91590451650",
Expand Down Expand Up @@ -771,6 +775,7 @@ export enum MetadataMessage {
LBE_V2_ADD_SELLERS = "SDK Minswap: Lbe V2 add more sellers",
LBE_V2_COUNTING_SELLERS = "SDK Minswap: Lbe V2 counting sellers",
LBE_V2_COLLECT_MANAGER = "SDK Minswap: Lbe V2 collect manager",
LBE_V2_COLLECT_ORDER = "SDK Minswap: Lbe V2 collect order",
}

export const FIXED_DEPOSIT_ADA = 2_000_000n;
Expand Down

0 comments on commit eef0461

Please sign in to comment.