Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions examples/dao-tx-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getBackendBlockfrostLucidInstance, NetworkId } from "../src";
import { Dao } from "../src/dao";

async function main() {
const networkId: NetworkId = NetworkId.TESTNET;
const blockfrostProjectId = "<YOUR_BLOCKFROST_API_KEY>";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const walletAddr = "<YOUR_WALLET_ADDRESS>";

const lucid = await getBackendBlockfrostLucidInstance(
networkId,
blockfrostProjectId,
blockfrostUrl,
walletAddr
);
lucid.selectWalletFromSeed("<YOUR_WALLET_SEED_PHRASE>");

const daoTx = new Dao(lucid);

const tx = await daoTx.updatePoolFeeTx({
managerAddress: walletAddr,
poolLPAsset: {
policyId: "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b",
tokenName:
"de0c83dde8b048b4883695cc3977619598e65a2d2c21ac2b57a5fb2494f19056",
},
newFeeA: 0.9,
newFeeB: 0.3,
});

const signedTx = await tx.sign().commit();
const txHash = await signedTx.submit();
console.log("Transaction submitted successfully: ", txHash);
}

main();
89 changes: 89 additions & 0 deletions src/dao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import invariant from "@minswap/tiny-invariant";
import { Addresses, Lucid, TxComplete } from "@spacebudz/lucid";
import JSONBig from "json-bigint";

import { Asset } from "./types/asset";
import { DexV2Constant, MetadataMessage } from "./types/constants";
import { NetworkId } from "./types/network";

/**
* Request to update the trading fees for a liquidity pool.
* @property managerAddress - The address of the pool manager authorized to update fees
* @property poolLPAsset - The LP token asset identifying the pool
* @property newFeeA - The new fee for trading direction A as a percentage (0.05% - 20%)
* @property newFeeB - The new fee for trading direction B as a percentage (0.05% - 20%)
* @property version - Protocol version for the fee request format
*/
export type PoolFeeRequest = {
managerAddress: string;
poolLPAsset: Asset;
newFeeA: number;
newFeeB: number;
version: "1";
};

export type RequestPoolFeeOptions = {
request: Omit<PoolFeeRequest, "version">;
};

export class Dao {
private readonly lucid: Lucid;
private readonly networkId: NetworkId;

constructor(lucid: Lucid) {
this.lucid = lucid;
this.networkId =
lucid.network === "Mainnet" ? NetworkId.MAINNET : NetworkId.TESTNET;
}

/**
* Creates a transaction to update the trading fees for a liquidity pool.
* This method builds a transaction with metadata that requests fee changes for a pool.
* The transaction must be signed by the pool manager address.
*/
async updatePoolFeeTx(
options: Omit<PoolFeeRequest, "version">
): Promise<TxComplete> {
const { managerAddress, poolLPAsset, newFeeA, newFeeB } = options;
const newFeeABps = BigInt(Math.floor(newFeeA * 100));
const newFeeBBps = BigInt(Math.floor(newFeeB * 100));

invariant(
newFeeABps >= DexV2Constant.MIN_TRADING_FEE &&
newFeeABps <= DexV2Constant.MAX_TRADING_FEE,
`Liquidity Pool Fee A must be in 0.05% - 20%, actual: ${newFeeA}%`
);
invariant(
newFeeBBps >= DexV2Constant.MIN_TRADING_FEE &&
newFeeBBps <= DexV2Constant.MAX_TRADING_FEE,
`Liquidity Pool Fee B must be in 0.05% - 20%, actual: ${newFeeB}%`
);
const v2Configs = DexV2Constant.CONFIG[this.networkId];
invariant(
poolLPAsset.policyId === v2Configs.lpPolicyId,
`invalid Pool LP Token ${poolLPAsset}`
);

const feeRequestJSON = JSONBig.stringify({
managerAddress: managerAddress,
poolLPAsset: Asset.toDottedString(poolLPAsset),
newFeeA: newFeeABps.toString(),
newFeeB: newFeeBBps.toString(),
version: "1",
}).match(/.{1,64}/g);
const paymentCred = Addresses.addressToCredential(managerAddress);
invariant(
paymentCred.type === "Key",
"Manager address must be a key address"
);

return this.lucid
.newTx()
.addSigner(paymentCred.hash)
.attachMetadata(674, {
msg: [MetadataMessage.DAO_POOL_FEE_UPDATE],
extraData: feeRequestJSON,
})
.commit();
}
}
15 changes: 13 additions & 2 deletions src/types/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export namespace Asset {
return policyId + tokenName
}

export function toDottedString(asset: Asset): string {
const { policyId, tokenName } = asset
if (policyId === "" && tokenName === "") {
return "lovelace"
}
if (asset.tokenName === "") {
return asset.policyId;
}
return `${asset.policyId}.${asset.tokenName}`;
}

export function toPlutusData(asset: Asset): Constr<DataType> {
const { policyId, tokenName } = asset
return new Constr(0, [
Expand All @@ -51,8 +62,8 @@ export namespace Asset {
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}`
data.fields.length === 2,
`Asset fields length must be 2, actual: ${data.fields.length}`
);
return {
policyId: data.fields[0] as string,
Expand Down
Loading
Loading