Skip to content

Commit

Permalink
Merge pull request #22 from spectrum-finance/dev-33_create-pool-test
Browse files Browse the repository at this point in the history
Dev 33. Create pool api
  • Loading branch information
Ridel1e authored Aug 30, 2023
2 parents 662059e + 5f025bb commit 144d88a
Show file tree
Hide file tree
Showing 23 changed files with 482 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spectrumlabs/cardano-dex-sdk",
"version": "0.1.170",
"version": "0.1.180",
"description": "ErgoDEX SDK for Cardano",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
13 changes: 12 additions & 1 deletion src/amm/contractData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {AssetClass} from "../cardano/entities/assetClass"
import {Datum, HexString} from "../cardano/types"
import {decodeHex, encodeHex} from "../utils/hex"
import {CardanoWasm} from "../utils/rustLoader"
import {DepositRequest, RedeemRequest, SwapRequest} from "./models/opRequests"
import {DepositRequest, PoolCreationRequest, RedeemRequest, SwapRequest} from "./models/opRequests"
import {OrderAction} from "./models/orderAction"
import {DepositConfig, RedeemConfig, SwapConfig} from "./models/orderConfig"
import {PoolConfig} from "./models/poolConfig"
Expand Down Expand Up @@ -95,6 +95,17 @@ export function mkSwapDatum(conf: SwapRequest, R: CardanoWasm): PlutusData {
)
}

export function mkPoolDatum(conf: PoolCreationRequest, R: CardanoWasm): PlutusData {
const x = mkAssetClass(conf.x.asset, R)
const y = mkAssetClass(conf.y.asset, R)
const lq = mkAssetClass(conf.lq.asset, R)
const nft = mkAssetClass(conf.nft.asset, R)
const feeNum: PlutusData = R.PlutusData.new_integer(R.BigInt.from_str(conf.feeNum.toString()))
const adminPolicy: PlutusData = R.PlutusData.new_list(R.PlutusList.new());
const lqBound = R.PlutusData.new_integer(R.BigInt.from_str('0'))
return mkProductN([nft, x, y, lq, feeNum, adminPolicy, lqBound], R)
}

export function mkDepositDatum(conf: DepositRequest, R: CardanoWasm): PlutusData {
const poolNft = mkAssetClass(conf.poolId, R)
const x = mkAssetClass(conf.x.asset, R)
Expand Down
9 changes: 9 additions & 0 deletions src/amm/domain/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import {HexString} from "../../cardano/types";
import {AssetAmount} from "../../domain/assetAmount";
import {decimalToFractional} from "../../utils/math"
import {ExUnitsDescriptor} from "../interpreters/refundTxBuilder/refundTxBuilder"

export type MintingAsset = {
amount: AssetAmount,
script: HexString,
exUnits: ExUnitsDescriptor
}

export type FeePerToken = {
numerator: bigint
Expand Down
50 changes: 42 additions & 8 deletions src/amm/interpreters/ammActions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Addr} from "../../cardano/entities/address"
import {TxCandidate} from "../../cardano/entities/tx"
import {emptyValue, Value} from "../../cardano/entities/value"
import {Value} from "../../cardano/entities/value"
import {TxContext} from "../../cardano/wallet/entities/txContext"
import {MintingAsset} from "../domain/models"
import {OrderKind, OrderRequest} from "../models/opRequests"
import {AmmOutputs} from "./ammOutputs"
import {DEFAULT_EX_UNITS_MEM, DEFAULT_EX_UNITS_STEPS} from "./refundTxBuilder/refundTxBuilder"

export interface AmmActions {
createOrder(req: OrderRequest, ctx: TxContext): TxCandidate
Expand All @@ -14,7 +16,8 @@ export function mkAmmActions(outputs: AmmOutputs, uiRewardAddr: Addr): AmmAction
}

class DefaultAmmActions implements AmmActions {
constructor(public readonly outputs: AmmOutputs, public readonly uiRewardAddr: Addr) {}
constructor(public readonly outputs: AmmOutputs, public readonly uiRewardAddr: Addr) {
}

createOrder(req: OrderRequest, ctx: TxContext): TxCandidate {
const orderCandidate = () => {
Expand All @@ -25,18 +28,49 @@ class DefaultAmmActions implements AmmActions {
return this.outputs.redeem(req)
case OrderKind.Swap:
return this.outputs.swap(req)
case OrderKind.PoolCreation:
return this.outputs.poolCreation(req)
}
}

const outputs = [orderCandidate()]
const outputs = orderCandidate()
if (req.uiFee > 0n) outputs.push({value: Value(req.uiFee), addr: this.uiRewardAddr})

const mintAssets = this.getMintingAssets(req)

return {
inputs: ctx.inputs,
outputs: outputs,
valueMint: emptyValue,
changeAddr: ctx.changeAddr,
ttl: ctx.ttl
inputs: ctx.inputs,
outputs: outputs,
changeAddr: ctx.changeAddr,
mintingScripts: mintAssets,
ttl: ctx.ttl
}
}

private getMintingAssets(req: OrderRequest): MintingAsset[] {
if (req.kind === OrderKind.PoolCreation) {

const nftMintingData: MintingAsset = {
amount: req.nft,
script: req.nftMintingScript,
exUnits: {
mem: DEFAULT_EX_UNITS_MEM,
steps: DEFAULT_EX_UNITS_STEPS
}
}

const lqMintingData: MintingAsset = {
amount: req.lq,
script: req.lqMintingScript,
exUnits: {
mem: '23000',
steps: '8300000'
}
}

return [nftMintingData, lqMintingData]
} else {
return []
}
}
}
49 changes: 35 additions & 14 deletions src/amm/interpreters/ammOutputs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {TxOutCandidate} from "../../cardano/entities/txOut"
import {remove, Value} from "../../cardano/entities/value";
import {TxMath} from "../../cardano/wallet/txMath"
import {encodeHex} from "../../utils/hex"
import {CardanoWasm} from "../../utils/rustLoader"
import {mkDepositDatum, mkRedeemDatum, mkSwapDatum} from "../contractData"
import {DepositRequest, RedeemRequest, SwapRequest} from "../models/opRequests"
import {mkDepositDatum, mkPoolDatum, mkRedeemDatum, mkSwapDatum} from "../contractData"
import {calculateInitUserRewardLq} from "../math/pool";
import {DepositRequest, PoolCreationRequest, RedeemRequest, SwapRequest} from "../models/opRequests"
import {OrderAddrs} from "../scripts"

export interface AmmOutputs {
deposit(req: DepositRequest): TxOutCandidate
deposit(req: DepositRequest): TxOutCandidate[]

redeem(req: RedeemRequest): TxOutCandidate
redeem(req: RedeemRequest): TxOutCandidate[]

swap(req: SwapRequest): TxOutCandidate
swap(req: SwapRequest): TxOutCandidate[]

poolCreation(req: PoolCreationRequest): TxOutCandidate[]
}

export function mkAmmOutputs(addrs: OrderAddrs, txMath: TxMath, R: CardanoWasm): AmmOutputsImpl {
Expand All @@ -23,32 +27,49 @@ class AmmOutputsImpl implements AmmOutputs {
public readonly addrs: OrderAddrs,
public readonly txMath: TxMath,
public readonly R: CardanoWasm
) {}
) {
}

deposit(req: DepositRequest): TxOutCandidate {
deposit(req: DepositRequest): TxOutCandidate[] {
const data = encodeHex(mkDepositDatum(req, this.R).to_bytes())
return {
return [{
value: req.orderValue,
addr: this.addrs.ammDeposit,
data
}
}]
}

redeem(req: RedeemRequest): TxOutCandidate {
redeem(req: RedeemRequest): TxOutCandidate[] {
const data = encodeHex(mkRedeemDatum(req, this.R).to_bytes())
return {
return [{
value: req.orderValue,
addr: this.addrs.ammRedeem,
data
}
}]
}

swap(req: SwapRequest): TxOutCandidate {
swap(req: SwapRequest): TxOutCandidate[] {
const data = encodeHex(mkSwapDatum(req, this.R).to_bytes())
return {
return [{
value: req.orderValue,
addr: this.addrs.ammSwap,
data
}]
}

poolCreation(req: PoolCreationRequest): TxOutCandidate[] {
const data = encodeHex(mkPoolDatum(req, this.R).to_bytes())
const userOutputValue = Value(req.minAdaForUserOutput, calculateInitUserRewardLq(req.x, req.y, req.lq.asset))
const poolOutput = {
value: remove(req.poolValue, userOutputValue),
addr: this.addrs.ammPool,
data
}
const userLqOutput = {
value: userOutputValue,
addr: req.userAddress,
data
}
return [poolOutput, userLqOutput]
}
}
Loading

0 comments on commit 144d88a

Please sign in to comment.