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

MIN-1549 MIN-1548: LBE V2 and Expired Order Monitor #44

Merged
merged 43 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
415d914
config+schema+constant
m1n999999 Oct 24, 2024
2640f6c
wip
m1n999999 Oct 24, 2024
a3b6c16
use custom schema
m1n999999 Oct 25, 2024
e045810
type
m1n999999 Oct 25, 2024
4c4b468
wip
m1n999999 Oct 28, 2024
ab163cb
finish deposit/withdraw order
m1n999999 Oct 29, 2024
f4d48fe
close event and add more seller
m1n999999 Oct 29, 2024
3260eb8
add type ProjectDetails+remove Dummy type
m1n999999 Oct 29, 2024
7cfdd85
create event example
m1n999999 Oct 30, 2024
8236737
lbe v2 adapter
m1n999999 Oct 30, 2024
59bca26
close lbe v2
m1n999999 Oct 30, 2024
e16020b
fix deposit
m1n999999 Oct 30, 2024
60bdb2c
example for deposit and withdraw
m1n999999 Oct 31, 2024
2ac00de
example for cancel event
m1n999999 Oct 31, 2024
ac55fec
example+wip of counting seller
m1n999999 Oct 31, 2024
bd4da1b
wip
m1n999999 Nov 5, 2024
375f37e
update pnpm-lock
m1n999999 Nov 5, 2024
abc0e5c
counting seller
m1n999999 Nov 6, 2024
6d42c86
collect manager
m1n999999 Nov 6, 2024
6df1fbe
Merge branch 'main' into lbe-v2
m1n999999 Nov 6, 2024
eef0461
collect order
m1n999999 Nov 6, 2024
8dc57b1
redeem orders
m1n999999 Nov 7, 2024
923feae
refund
m1n999999 Nov 7, 2024
460fa4e
validate create amm
m1n999999 Nov 7, 2024
8183a3d
create amm pool
m1n999999 Nov 8, 2024
924c4f6
wip
m1n999999 Nov 11, 2024
9ce8417
wip 1
m1n999999 Nov 12, 2024
2b01f37
finish implement worker
m1n999999 Nov 12, 2024
d67ba87
fix build
m1n999999 Nov 12, 2024
030673d
fix worker
m1n999999 Nov 13, 2024
a39b946
seperate lbe-v2 file
m1n999999 Nov 19, 2024
c211411
fix worker
m1n999999 Nov 21, 2024
b22a0c8
udpate order and example for it
m1n999999 Nov 22, 2024
501287b
remove redudant
m1n999999 Nov 22, 2024
4259587
update mainnet config
m1n999999 Nov 22, 2024
6f6a941
new version of lucid
m1n999999 Nov 25, 2024
322945b
lbe v2 specification
m1n999999 Nov 26, 2024
a4861df
move compareUTxO to calculate file
m1n999999 Nov 26, 2024
76664d4
update docs
m1n999999 Nov 26, 2024
b8b730b
remove personal info
m1n999999 Nov 26, 2024
cab955b
add check field length action in fromPlutusData+add dcos
m1n999999 Nov 26, 2024
829b973
docs
m1n999999 Nov 26, 2024
6898dfd
MIN-1548: Auto cancel expired orders worker (#47)
m1n999999 Nov 27, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run_install: false
- uses: actions/setup-node@v4
with:
node-version: 20.x
node-version: 20.17
cache: "pnpm"
- name: Install dependencies
run: pnpm install --frozen-lockfile
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20
v20.17
320 changes: 311 additions & 9 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import {
NetworkId,
OrderV2,
PoolV1,
PoolV2,
StableOrder,
StableswapCalculation,
StableswapConstant,
} from "../src";
import { LbeV2 } from "../src/lbe-v2/lbe-v2";
import { Stableswap } from "../src/stableswap";
import { LbeV2Types } from "../src/types/lbe-v2";
import { Slippage } from "../src/utils/slippage.internal";

const MIN: Asset = {
Expand All @@ -42,11 +45,11 @@ const MIN: Asset = {

async function main(): Promise<void> {
const network: Network = "Preprod";
const blockfrostProjectId = "<YOUR_BLOCKFROST_API_KEY>";
const blockfrostProjectId = "hihi";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const address =
"addr_test1vrd9v47japxwp8540vsrh4grz4u9urfpfawwy7sf6r0vxqgm7wdxh";
"addr_test1qqf2dhk96l2kq4xh2fkhwksv0h49vy9exw383eshppn863jereuqgh2zwxsedytve5gp9any9jwc5hz98sd47rwfv40stc26fr";
const lucid = await getBackendLucidInstance(
network,
blockfrostProjectId,
Expand All @@ -62,17 +65,15 @@ async function main(): Promise<void> {
})
);

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

const txComplete = await _stopV2TxExample(
const txComplete = await _lbeV2AddMoreSellersExample(
lucid,
blockfrostAdapter,
address,
utxos
blockfrostAdapter
);
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();
const signedTx = await txComplete.signWithPrivateKey("hihi").complete();

const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
}
Expand Down Expand Up @@ -102,6 +103,7 @@ async function getPoolById(
};
}

// MARK: DEX V1
async function _depositTxExample(
network: Network,
lucid: Lucid,
Expand Down Expand Up @@ -362,6 +364,7 @@ async function _cancelTxExample(
});
}

// MARK: DEX V2
async function _createPoolV2(
lucid: Lucid,
blockFrostAdapter: BlockfrostAdapter
Expand Down Expand Up @@ -848,6 +851,7 @@ async function _cancelV2TxExample(
});
}

// MARK: STABLESWAP
async function _swapStableExample(
lucid: Lucid,
blockfrostAdapter: BlockfrostAdapter,
Expand Down Expand Up @@ -1140,6 +1144,304 @@ async function _cancelStableExample(lucid: Lucid): Promise<TxComplete> {
});
}

// MARK: LBE V2
const ONE_MINUTE_IN_MS = 1000 * 60;
m1n999999 marked this conversation as resolved.
Show resolved Hide resolved
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
const _ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;

// Example Tx: e1f42baa7b685acf083d5a3ffe4eefd1f53f4682226f0f39de56310de108239b
async function _createLbeV2EventExample(
lucid: Lucid,
address: Address,
blockfrostAdapter: BlockfrostAdapter
): Promise<TxComplete> {
const baseAsset = Asset.fromString(
"d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0bfdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc"
);
const curSlot = lucid.currentSlot();
const curDate = lucid.utils.slotToUnixTime(curSlot);
const lbeV2Parameters: LbeV2Types.LbeV2Parameters = {
baseAsset: baseAsset,
reserveBase: 100_000n,
raiseAsset: ADA,
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(
lbeV2Parameters.baseAsset,
lbeV2Parameters.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();
return new LbeV2(lucid).createEvent({
factoryUtxo: factoryUtxos[0],
lbeV2Parameters: lbeV2Parameters,
currentSlot: currentSlot,
sellerOwner: address,
sellerCount: 10,
projectDetails: projectDetails,
});
}

// 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,
address: Address,
blockfrostAdapter: BlockfrostAdapter
): Promise<TxComplete> {
const baseAsset = Asset.fromString(
"e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d865190718981e4e7fab3eb80963f14148714d7a7847652d4017d0fb744db075027"
);
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");

return new LbeV2(lucid).cancelEvent({
treasuryUtxo: treasuryUtxos[0],
cancelData: { reason: LbeV2Types.CancelReason.BY_OWNER, owner: address },
currentSlot: await blockfrostAdapter.currentSlot(),
});
}

// Example Tx: 7af5ea80b6a4a587e2c6cfce383367829f0cb68c90b65656c8198a72afc3f419
async function _lbeV2DepositOrderExample(
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 seller = await blockfrostAdapter.getLbeV2SellerByLbeId(lbeId);
invariant(seller !== null, `Can not find seller by lbeId ${lbeId}`);
const sellerUtxos = await lucid.utxosByOutRef([
{ txHash: seller.txIn.txHash, outputIndex: seller.txIn.index },
]);
invariant(sellerUtxos.length === 1, "Can not find seller Utxo");

const orders = await blockfrostAdapter.getLbeV2OrdersByLbeIdAndOwner(
lbeId,
address
);
const orderUtxos =
orders.length > 0
? await lucid.utxosByOutRef(
orders.map((o) => ({
txHash: o.txIn.txHash,
outputIndex: o.txIn.index,
}))
)
: [];

invariant(
orderUtxos.length === orders.length,
"Can not find enough order Utxos"
);

const currentSlot = await blockfrostAdapter.currentSlot();
return new LbeV2(lucid).depositOrWithdrawOrder({
currentSlot: currentSlot,
existingOrderUtxos: orderUtxos,
treasuryUtxo: treasuryUtxos[0],
sellerUtxo: sellerUtxos[0],
owner: address,
action: { type: "deposit", additionalAmount: 1_000_000n },
});
}

// Example Tx: 3388b9ce7f2175576b12ac48eacfb78da24b2319ab0595b5cc6bf9531e781eef
async function _lbeV2WithdrawOrderExample(
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 seller = await blockfrostAdapter.getLbeV2SellerByLbeId(lbeId);
invariant(seller !== null, `Can not find seller by lbeId ${lbeId}`);
const sellerUtxos = await lucid.utxosByOutRef([
{ txHash: seller.txIn.txHash, outputIndex: seller.txIn.index },
]);
invariant(sellerUtxos.length === 1, "Can not find seller Utxo");

const orders = await blockfrostAdapter.getLbeV2OrdersByLbeIdAndOwner(
lbeId,
address
);
const orderUtxos =
orders.length > 0
? await lucid.utxosByOutRef(
orders.map((o) => ({
txHash: o.txIn.txHash,
outputIndex: o.txIn.index,
}))
)
: [];

invariant(
orderUtxos.length === orders.length,
"Can not find enough order Utxos"
);

const currentSlot = await blockfrostAdapter.currentSlot();
return new LbeV2(lucid).depositOrWithdrawOrder({
currentSlot: currentSlot,
existingOrderUtxos: orderUtxos,
treasuryUtxo: treasuryUtxos[0],
sellerUtxo: sellerUtxos[0],
owner: address,
action: { type: "withdraw", withdrawalAmount: 1_000_000n },
});
}

// FAIL ???
async function _lbeV2CloseEventExample(
lucid: Lucid,
address: Address,
blockfrostAdapter: BlockfrostAdapter
): Promise<TxComplete> {
const baseAsset = Asset.fromString(
"d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0bfdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc"
);
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 headAndTailFactory =
await blockfrostAdapter.getLbeV2HeadAndTailFactory(lbeId);
invariant(
headAndTailFactory,
`Can not find head and tail factory by lbeId ${lbeId}`
);
const { head: headFactory, tail: tailFactory } = headAndTailFactory;

const treasuryUtxos = await lucid.utxosByOutRef([
{ txHash: treasury.txIn.txHash, outputIndex: treasury.txIn.index },
]);
invariant(treasuryUtxos.length !== 0, "Can not find treasury Utxo");

const headFactoryUtxos = await lucid.utxosByOutRef([
{ txHash: headFactory.txIn.txHash, outputIndex: headFactory.txIn.index },
]);

invariant(headFactoryUtxos.length !== 0, "Can not find head factory Utxo");

const tailFactoryUtxos = await lucid.utxosByOutRef([
{ txHash: tailFactory.txIn.txHash, outputIndex: tailFactory.txIn.index },
]);
invariant(tailFactoryUtxos.length !== 0, "Can not find tail factory Utxo");
const currentSlot = await blockfrostAdapter.currentSlot();

return new LbeV2(lucid).closeEventTx({
treasuryUtxo: treasuryUtxos[0],
headFactoryUtxo: headFactoryUtxos[0],
tailFactoryUtxo: tailFactoryUtxos[0],
currentSlot: currentSlot,
owner: address,
});
}

/**
* Initialize Lucid Instance for Browser Environment
* @param network Network you're working on
Expand Down
Loading