Skip to content

Commit

Permalink
add collateral to tx
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalakhov committed Sep 8, 2023
1 parent af8111a commit 34be72a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
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.195",
"version": "0.1.196",
"description": "ErgoDEX SDK for Cardano",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/amm/interpreters/ammActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DefaultAmmActions implements AmmActions {
return {
inputs: ctx.inputs,
outputs: outputs,
collateral: ctx.collateralInputs,
changeAddr: ctx.changeAddr,
mintingScripts: mintAssets,
ttl: ctx.ttl
Expand Down
4 changes: 3 additions & 1 deletion src/amm/interpreters/ammTxBuilder/ammTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {PoolCreationParams, PoolCreationTxBuilder, PoolCreationTxInfo} from "./p
import {RedeemAmmTxBuilder, RedeemParams, RedeemTxInfo} from "./redeemAmmTxBuilder"
import {SwapAmmTxBuilder, SwapParams, SwapTxInfo} from "./swapAmmTxBuilder"
import {FullTxIn} from "../../../cardano/entities/txIn"
import {CollateralSelector} from "../../../cardano/wallet/collateralSelector"

export interface AmmTxBuilder {
swap(params: SwapParams): Promise<[Transaction | null, TxCandidate, SwapTxInfo, Error | null]>;
Expand Down Expand Up @@ -39,13 +40,14 @@ export class DefaultAmmTxCandidateBuilder implements AmmTxBuilder {
ammActions: AmmActions,
inputSelector: InputSelector,
private inputCollector: InputCollector,
collateralSelector: CollateralSelector,
R: CardanoWasm,
private txAsm: TxAsm
) {
this.swapAmmTxBuilder = new SwapAmmTxBuilder(txMath, ammOuptuts, ammActions, inputSelector, R)
this.redeemAmmTxBuilder = new RedeemAmmTxBuilder(txMath, ammOuptuts, ammActions, inputSelector, R)
this.depositAmmTxBuilder = new DepositAmmTxBuilder(txMath, ammOuptuts, ammActions, inputSelector, R)
this.poolTxBuilder = new PoolCreationTxBuilder(txMath, ammOuptuts, ammActions, inputSelector)
this.poolTxBuilder = new PoolCreationTxBuilder(txMath, ammOuptuts, ammActions, inputSelector, collateralSelector)
}

async swap(
Expand Down
9 changes: 8 additions & 1 deletion src/amm/interpreters/ammTxBuilder/poolCreationTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {OrderKind} from "../../models/opRequests"
import {AmmActions} from "../ammActions"
import {AmmOutputs} from "../ammOutputs"
import {selectInputs} from "./selectInputs"
import {CollateralSelector} from "../../../cardano/wallet/collateralSelector"

export interface PoolCreationParams {
readonly x: AssetAmount
Expand All @@ -27,6 +28,7 @@ export interface PoolCreationParams {
readonly txFees: AmmTxFeeMapping
readonly changeAddress: Addr
readonly pk: PubKeyHash
readonly collateralAmount: bigint;
}

export interface PoolCreationTxInfo {
Expand All @@ -43,6 +45,7 @@ export class PoolCreationTxBuilder {
private ammOutputs: AmmOutputs,
private ammActions: AmmActions,
private inputSelector: InputSelector,
private collateralSelector: CollateralSelector
) {
}

Expand All @@ -51,6 +54,7 @@ export class PoolCreationTxBuilder {
const totalOrderBudget = add(orderBudget, AdaEntry(userTxFee || params.txFees.poolCreation))


const collateralOrError = await this.collateralSelector.getCollateral(params.collateralAmount);
const inputsOrError = await selectInputs(totalOrderBudget, params.changeAddress, this.inputSelector, allInputs, this.txMath)
const inputForMinting = await this
.inputSelector
Expand All @@ -59,6 +63,9 @@ export class PoolCreationTxBuilder {
if (inputForMinting instanceof Error) {
throw inputForMinting
}
if (collateralOrError instanceof Error) {
throw collateralOrError;
}

const inputs: FullTxIn[] = inputsOrError instanceof Error ? [] : inputsOrError

Expand Down Expand Up @@ -101,7 +108,7 @@ export class PoolCreationTxBuilder {
},
{
changeAddr: params.changeAddress,
collateralInputs: [],
collateralInputs: collateralOrError,
inputs: Object.values(inputsDictionary)
}
),
Expand Down
3 changes: 1 addition & 2 deletions src/cardano/wallet/entities/txContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {Addr} from "../../entities/address"
import {FullTxIn} from "../../entities/txIn"
import {TxOut} from "../../entities/txOut"

export type TxContext = {
inputs: FullTxIn[]
collateralInputs: TxOut[]
collateralInputs: FullTxIn[]
changeAddr: Addr
ttl?: number
}

0 comments on commit 34be72a

Please sign in to comment.