Skip to content

Commit

Permalink
add check field length action in fromPlutusData+add dcos
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Nov 26, 2024
1 parent b8b730b commit cab955b
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 6 deletions.
174 changes: 171 additions & 3 deletions docs/lbe-v2/lbe-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This documentation provides guidelines for communicating with and interacting wi

[LBE Specification](./LBE%20Specification.pdf)

Worker is a class that executes off-chain actions when an event ends.

### Transaction Builder Function

- **LBEV2 Class**: Located in `src/lbe-v2/lbe-v2.ts`.
Expand All @@ -17,7 +19,103 @@ This documentation provides guidelines for communicating with and interacting wi

## Example Usage

### **Create Order**
### Create Event

```ts
const network: Network = "Preprod";
const blockfrostProjectId = "<YOUR_BLOCKFROST_API_KEY>";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const address =
"addr_test1qqf2dhk96l2kq4xh2fkhwksv0h49vy9exw383eshppn863jereuqgh2zwxsedytve5gp9any9jwc5hz98sd47rwfv40stc26fr";
const lucid = await getBackendLucidInstance(
network,
blockfrostProjectId,
blockfrostUrl,
address
);

const blockfrostAdapter = new BlockfrostAdapter(
NetworkId.TESTNET,
new BlockFrostAPI({
projectId: blockfrostProjectId,
network: "preprod",
})
);

const baseAsset: Asset = {
policyId: "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b",
tokenName: "fdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc",
};
const raiseAsset: Asset = {
policyId: "",
tokenName: "",
};
const curSlot = lucid.currentSlot();
const curDate = lucid.utils.slotToUnixTime(curSlot);
const lbeV2Parameters: LbeV2Types.LbeV2Parameters = {
baseAsset: baseAsset,
reserveBase: 100_000n,
raiseAsset: raiseAsset,
startTime: BigInt(curDate + ONE_HOUR_IN_MS),
endTime: BigInt(curDate + 2 * ONE_HOUR_IN_MS),
owner: address,
receiver: address,
poolAllocation: 100n,
minimumOrderRaise: undefined,
minimumRaise: 10_000_000n,
maximumRaise: 100_000_000n,
penaltyConfig: {
penaltyStartTime: BigInt(curDate + ONE_HOUR_IN_MS + 20 * ONE_MINUTE_IN_MS),
percent: 20n,
},
revocable: true,
poolBaseFee: 30n,
};
const factory = await blockfrostAdapter.getLbeV2Factory(baseAsset, raiseAsset);
invariant(factory !== null, "Can not find factory");
const factoryUtxos = await lucid.utxosByOutRef([
{ outputIndex: factory.txIn.index, txHash: factory.txIn.txHash },
]);
invariant(factoryUtxos.length !== 0, "Can not find factory utxo");
const projectDetails = {
eventName: "TEST SDK",
description: "test lbe v2 in public sdk",
socialLinks: {
twitter: "https://x.com/MinswapDEX",
telegram: "https://t.me/MinswapMafia",
discord: "https://discord.gg/minswap",
website: "https://minswap.org/",
},
tokenomics: [
{
tag: "admin",
percentage: "70",
},
{
tag: "LBE",
percentage: "30",
},
],
};
const currentSlot = await blockfrostAdapter.currentSlot();
const txComplete = await new LbeV2(lucid).createEvent({
factoryUtxo: factoryUtxos[0],
lbeV2Parameters: lbeV2Parameters,
currentSlot: currentSlot,
sellerOwner: address,
sellerCount: 10,
projectDetails: projectDetails,
});
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();

const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
```

### Create Order

```ts
const network: Network = "Preprod";
Expand All @@ -41,7 +139,15 @@ const blockfrostAdapter = new BlockfrostAdapter(
})
);

const lbeId = "<LBE_ID>";
const baseAsset: Asset = {
policyId: "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b",
tokenName: "fdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc",
};
const raiseAsset: Asset = {
policyId: "",
tokenName: "",
};
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([
Expand Down Expand Up @@ -93,7 +199,69 @@ const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
```

### **Run Worker**
### Add seller(Increase Concurrency Performance)

```ts
const network: Network = "Preprod";
const blockfrostProjectId = "<YOUR_BLOCKFROST_API_KEY>";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const address =
"addr_test1qqf2dhk96l2kq4xh2fkhwksv0h49vy9exw383eshppn863jereuqgh2zwxsedytve5gp9any9jwc5hz98sd47rwfv40stc26fr";
const lucid = await getBackendLucidInstance(
network,
blockfrostProjectId,
blockfrostUrl,
address
);

const blockfrostAdapter = new BlockfrostAdapter(
NetworkId.TESTNET,
new BlockFrostAPI({
projectId: blockfrostProjectId,
network: "preprod",
})
);

const baseAsset: Asset = {
policyId: "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b",
tokenName: "fdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc",
};
const raiseAsset: Asset = {
policyId: "",
tokenName: "",
};
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");

const txComplete = await new LbeV2(lucid).addSellers({
treasuryUtxo: treasuryUtxos[0],
managerUtxo: managerUtxos[0],
addSellerCount: 2,
sellerOwner: address,
currentSlot: await blockfrostAdapter.currentSlot(),
});
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();

const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
```

### Run Worker

```ts
const network: Network = "Preprod";
Expand Down
5 changes: 5 additions & 0 deletions src/types/asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Constr, Data } from "@minswap/lucid-cardano";
import invariant from "@minswap/tiny-invariant";

import { StringUtils } from "./string";

Expand Down Expand Up @@ -48,6 +49,10 @@ export namespace Asset {
if (data.index !== 0) {
throw new Error(`Index of Asset must be 0, actual: ${data.index}`)
}
invariant(
data.fields.length === 2,
`Asset fields length must be 2, actual: ${data.fields.length}`
);
return {
policyId: data.fields[0] as string,
tokenName: data.fields[1] as string
Expand Down
44 changes: 41 additions & 3 deletions src/types/lbe-v2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Address, Constr, Data } from "@minswap/lucid-cardano";
import invariant from "@minswap/tiny-invariant";

import { Asset, LbeV2Constant, NetworkId, PoolV2 } from "..";
import { AddressPlutusData } from "./address.internal";
import { Bool, Options } from "./common";
import { TxIn, Value } from "./tx.internal";

export namespace LbeV2Types {
export enum ReceiverDatumType {
NO_DATUM = 0,
Expand Down Expand Up @@ -37,16 +39,28 @@ export namespace LbeV2Types {

export function fromPlutusData(data: Constr<Data>): ReceiverDatum {
switch (data.index) {
case 0: {
case ReceiverDatumType.NO_DATUM: {
invariant(
data.fields.length === 0,
`NO_DATUM Receiver Datum fields length must be 0, actual: ${data.fields.length}`
);
return { type: ReceiverDatumType.NO_DATUM };
}
case 1: {
case ReceiverDatumType.DATUM_HASH: {
invariant(
data.fields.length === 1,
`DATUM_HASH Receiver Datum fields length must be 1, actual: ${data.fields.length}`
);
return {
type: ReceiverDatumType.DATUM_HASH,
hash: data.fields[0] as string,
};
}
case 2: {
case ReceiverDatumType.INLINE_DATUM: {
invariant(
data.fields.length === 1,
`INLINE_DATUM Receiver Datum fields length must be 1, actual: ${data.fields.length}`
);
return {
type: ReceiverDatumType.INLINE_DATUM,
hash: data.fields[0] as string,
Expand Down Expand Up @@ -74,6 +88,10 @@ export namespace LbeV2Types {
export function fromPlutusData(data: Constr<Data>): PenaltyConfig {
switch (data.index) {
case 0: {
invariant(
data.fields.length === 2,
`Penalty Config fields length must be 2, actual: ${data.fields.length}`
);
return {
penaltyStartTime: data.fields[0] as bigint,
percent: data.fields[1] as bigint,
Expand Down Expand Up @@ -196,6 +214,10 @@ export namespace LbeV2Types {
);
}

invariant(
data.fields.length === 25,
`Treasury Datum fields length must be 25, actual: ${data.fields.length}`
);
const fields = data.fields;
return {
factoryPolicyId: fields[0] as string,
Expand Down Expand Up @@ -411,6 +433,10 @@ export namespace LbeV2Types {
export function fromPlutusData(data: Constr<Data>): FactoryDatum {
switch (data.index) {
case 0: {
invariant(
data.fields.length === 2,
`Factory Datum fields length must be 2, actual: ${data.fields.length}`
);
return {
head: data.fields[0] as string,
tail: data.fields[1] as string,
Expand Down Expand Up @@ -541,6 +567,10 @@ export namespace LbeV2Types {
switch (data.index) {
case 0: {
const fields = data.fields;
invariant(
fields.length === 6,
`Manager Datum fields length must be 6, actual: ${fields.length}`
);
return {
factoryPolicyId: fields[0] as string,
baseAsset: Asset.fromPlutusData(fields[1] as Constr<Data>),
Expand Down Expand Up @@ -634,6 +664,10 @@ export namespace LbeV2Types {
switch (data.index) {
case 0: {
const fields = data.fields;
invariant(
fields.length === 6,
`Seller Datum fields length must be 6, actual: ${fields.length}`
);
return {
factoryPolicyId: fields[0] as string,
owner: AddressPlutusData.fromPlutusData(
Expand Down Expand Up @@ -732,6 +766,10 @@ export namespace LbeV2Types {
switch (data.index) {
case 0: {
const fields = data.fields;
invariant(
fields.length === 7,
`Order Datum fields length must be 7, actual: ${fields.length}`
);
return {
factoryPolicyId: fields[0] as string,
baseAsset: Asset.fromPlutusData(fields[1] as Constr<Data>),
Expand Down

0 comments on commit cab955b

Please sign in to comment.