Skip to content

Commit

Permalink
Lucid Evolution integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Aug 31, 2024
1 parent 6212d28 commit b039e4b
Show file tree
Hide file tree
Showing 16 changed files with 2,186 additions and 1,729 deletions.
3,770 changes: 2,135 additions & 1,635 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"prepublishOnly": "npm run test"
},
"dependencies": {
"@lucid-evolution/lucid": "^0.3.19",
"@types/crypto-js": "^4.1.1",
"axios": "^0.26.1",
"axios-retry": "^3.5.1",
"bottleneck": "^2.19.5",
"crypto-js": "^4.1.1",
"lodash": "^4.17.21",
"lucid-cardano": "^0.10.7"
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/core": "^7.21.4",
Expand All @@ -45,3 +45,4 @@
"typescript": "^5.0.3"
}
}

2 changes: 1 addition & 1 deletion src/definition-builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DatumParameters, DefinitionConstr, DefinitionField } from './types';
import { DatumParameterKey } from './constants';
import _ from 'lodash';
import { datumJsonToCbor } from '@app/utils';
import { datumJsonToCbor } from '@lucid-evolution/lucid';

export class DefinitionBuilder {
private _definition: DefinitionConstr;
Expand Down
9 changes: 5 additions & 4 deletions src/dex/minswap-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
UTxO
} from '@app/types';
import { DefinitionBuilder } from '@app/definition-builder';
import { correspondingReserves, lucidUtils } from '@app/utils';
import { correspondingReserves } from '@app/utils';
import { AddressType, DatumParameterKey } from '@app/constants';
import order from '@dex/definitions/minswap-v2/order';
import { BaseApi } from '@dex/api/base-api';
import pool from '@dex/definitions/minswap-v2/pool';
import { AddressDetails, Script } from 'lucid-cardano';
import { AddressDetails, credentialToAddress, getAddressDetails, Script } from '@lucid-evolution/lucid';

export class MinswapV2 extends BaseDex {

Expand Down Expand Up @@ -211,7 +211,8 @@ export class MinswapV2 extends BaseDex {
this.buildSwapOrderPayment(
swapParameters,
{
address: lucidUtils.credentialToAddress(
address: credentialToAddress(
'Mainnet',
{
type: 'Script',
hash: this.orderScriptHash,
Expand All @@ -238,7 +239,7 @@ export class MinswapV2 extends BaseDex {

public async buildCancelSwapOrder(txOutputs: UTxO[], returnAddress: string): Promise<PayToAddress[]> {
const relevantUtxo: UTxO | undefined = txOutputs.find((utxo: UTxO) => {
const addressDetails: AddressDetails | undefined = lucidUtils.getAddressDetails(utxo.address);
const addressDetails: AddressDetails | undefined = getAddressDetails(utxo.address);

return (addressDetails.paymentCredential?.hash ?? '') === this.orderScriptHash;
});
Expand Down
2 changes: 1 addition & 1 deletion src/dex/minswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import order from '@dex/definitions/minswap/order';
import { BaseApi } from '@dex/api/base-api';
import { MinswapApi } from '@dex/api/minswap-api';
import pool from '@dex/definitions/minswap/pool';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

export class Minswap extends BaseDex {

Expand Down
2 changes: 1 addition & 1 deletion src/dex/muesliswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import pool from '@dex/definitions/muesliswap/pool';
import order from '@dex/definitions/muesliswap/order';
import { BaseApi } from '@dex/api/base-api';
import { MuesliSwapApi } from '@dex/api/muesliswap-api';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

export class MuesliSwap extends BaseDex {

Expand Down
2 changes: 1 addition & 1 deletion src/dex/spectrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import pool from './definitions/spectrum/pool';
import order from './definitions/spectrum/order';
import { correspondingReserves, tokensMatch } from '@app/utils';
import { SpectrumApi } from '@dex/api/spectrum-api';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

const MAX_INT: bigint = 9_223_372_036_854_775_807n;

Expand Down
2 changes: 1 addition & 1 deletion src/dex/sundaeswap-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import pool from '@dex/definitions/sundaeswap-v1/pool';
import order from '@dex/definitions/sundaeswap-v1/order';
import { BaseApi } from '@dex/api/base-api';
import { SundaeSwapV1Api } from '@dex/api/sundaeswap-v1-api';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

export class SundaeSwapV1 extends BaseDex {

Expand Down
9 changes: 5 additions & 4 deletions src/dex/sundaeswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Asset, Token } from './models/asset';
import { BaseDex } from './base-dex';
import { AssetBalance, DatumParameters, DefinitionConstr, DefinitionField, PayToAddress, RequestConfig, SpendUTxO, SwapFee, UTxO } from '@app/types';
import { DefinitionBuilder } from '@app/definition-builder';
import { correspondingReserves, lucidUtils, tokensMatch } from '@app/utils';
import { correspondingReserves, tokensMatch } from '@app/utils';
import { AddressType, DatumParameterKey } from '@app/constants';
import pool from '@dex/definitions/sundaeswap-v3/pool';
import order from '@dex/definitions/sundaeswap-v3/order';
import { BaseApi } from '@dex/api/base-api';
import { AddressDetails, Lucid, Script, Utils } from 'lucid-cardano';
import { AddressDetails, credentialToAddress, getAddressDetails, Script } from '@lucid-evolution/lucid';
import { SundaeSwapV3Api } from '@dex/api/sundaeswap-v3-api';
import { BaseWalletProvider } from '@providers/wallet/base-wallet-provider';

Expand Down Expand Up @@ -165,7 +165,8 @@ export class SundaeSwapV3 extends BaseDex {

return [
this.buildSwapOrderPayment(swapParameters, {
address: lucidUtils.credentialToAddress(
address: credentialToAddress(
'Mainnet',
{
type: 'Script',
hash: this.orderScriptHash,
Expand Down Expand Up @@ -195,7 +196,7 @@ export class SundaeSwapV3 extends BaseDex {
}

const relevantUtxo: UTxO | undefined = txOutputs.find((utxo: UTxO) => {
const addressDetails: AddressDetails | undefined = lucidUtils.getAddressDetails(utxo.address);
const addressDetails: AddressDetails | undefined = getAddressDetails(utxo.address);

return (addressDetails.paymentCredential?.hash ?? '') === this.orderScriptHash;
});
Expand Down
2 changes: 1 addition & 1 deletion src/dex/teddyswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import pool from './definitions/teddyswap/pool';
import order from './definitions/teddyswap/order';
import { correspondingReserves, tokensMatch } from '@app/utils';
import { TeddyswapApi } from '@dex/api/teddyswap-api';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

const MAX_INT: bigint = 9_223_372_036_854_775_807n;

Expand Down
2 changes: 1 addition & 1 deletion src/dex/vyfinance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AddressType, DatumParameterKey } from '@app/constants';
import order from '@dex/definitions/vyfinance/order';
import { BaseApi } from '@dex/api/base-api';
import { VyfinanceApi } from '@dex/api/vyfinance-api';
import { Data, Script } from 'lucid-cardano';
import { Data, Script } from '@lucid-evolution/lucid';

/**
* VyFinance constants.
Expand Down
2 changes: 1 addition & 1 deletion src/dex/wingriders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import order from '@dex/definitions/wingriders/order';
import { BaseApi } from '@dex/api/base-api';
import { WingRidersApi } from '@dex/api/wingriders-api';
import pool from "@dex/definitions/wingriders/pool";
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

/**
* WingRiders constants.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/data/kupo-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
UTxO
} from '@app/types';
import axios, { AxiosInstance } from 'axios';
import { Data } from 'lucid-cardano';
import { Data } from '@lucid-evolution/lucid';
import { appendSlash } from '@app/utils';

export class KupoProvider extends BaseDataProvider {
Expand Down
59 changes: 28 additions & 31 deletions src/providers/wallet/lucid-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ import {
AddressDetails,
Assets,
Blockfrost,
Datum, Kupmios,
CBORHex,
getAddressDetails,
Kupmios,
Lucid,
TxComplete,
LucidEvolution,
TxHash,
TxSignBuilder,
TxSigned,
Unit
} from 'lucid-cardano';
} from '@lucid-evolution/lucid';
import { AddressType } from '@app/constants';

export class LucidProvider extends BaseWalletProvider {

public isWalletLoaded: boolean = false;

private _api: Lucid;
private _api: LucidEvolution;
private _usableAddress: string;
private _paymentCredential: string;
private _stakingCredential: string | undefined;
Expand All @@ -46,31 +49,20 @@ export class LucidProvider extends BaseWalletProvider {

public loadWallet(walletApi: Cip30Api, config: BlockfrostConfig | KupmiosConfig): Promise<BaseWalletProvider> {
return this.loadLucid(config)
.then((lucid: Lucid) => {
.then((lucid: LucidEvolution) => {
this._api = lucid;

this._api.selectWallet(walletApi);
this._api.selectWallet.fromAPI(walletApi);

return this.loadWalletInformation();
});
}

public loadWalletFromSeedPhrase(seed: string[], options: WalletOptions = {}, config: BlockfrostConfig | KupmiosConfig): Promise<BaseWalletProvider> {
return this.loadLucid(config)
.then((lucid: Lucid) => {
.then((lucid: LucidEvolution) => {
this._api = lucid;

const addressType: 'Base' | 'Enterprise' = options.addressType === AddressType.Enterprise
? 'Enterprise'
: 'Base';

this._api.selectWalletFromSeed(
seed.join(' '),
{
addressType: addressType,
accountIndex: options.accountIndex ?? 0,
},
);
this._api.selectWallet.fromSeed(seed.join(' '));

return this.loadWalletInformation();
});
Expand Down Expand Up @@ -112,7 +104,7 @@ export class LucidProvider extends BaseWalletProvider {
], spendUtxo.redeemer);

if (spendUtxo.validator) {
transaction.providerData.tx.attachSpendingValidator(spendUtxo.validator);
transaction.providerData.tx.attach.SpendingValidator(spendUtxo.validator);
}

if (spendUtxo.signer) {
Expand All @@ -123,19 +115,23 @@ export class LucidProvider extends BaseWalletProvider {

switch (payToAddress.addressType) {
case AddressType.Contract:
transaction.providerData.tx.payToContract(
transaction.providerData.tx.pay.ToContract(
payToAddress.address,
payToAddress.isInlineDatum
? {
inline: payToAddress.datum as Datum,
kind: 'inline',
value: payToAddress.datum as CBORHex,
}
: payToAddress.datum as Datum,
: {
kind: 'asHash',
value: payToAddress.datum as CBORHex
},
payment,
);
break;
case AddressType.Base:
case AddressType.Enterprise:
transaction.providerData.tx.payToAddress(
transaction.providerData.tx.pay.ToAddress(
payToAddress.address,
payment,
);
Expand All @@ -146,7 +142,7 @@ export class LucidProvider extends BaseWalletProvider {
});

return transaction.providerData.tx.complete()
.then((tx: TxComplete) => {
.then((tx: TxSignBuilder) => {
transaction.providerData.tx = tx;

return transaction;
Expand All @@ -158,7 +154,7 @@ export class LucidProvider extends BaseWalletProvider {
throw new Error('Must load wallet before signing transaction.');
}

return transaction.providerData.tx.sign().complete()
return transaction.providerData.tx.sign.withWallet().complete()
.then((signedTx: TxSigned) => {
transaction.providerData.tx = signedTx;

Expand All @@ -183,9 +179,9 @@ export class LucidProvider extends BaseWalletProvider {
}

private loadWalletInformation(): Promise<BaseWalletProvider> {
return this._api.wallet.address()
return this._api.wallet().address()
.then((address: Address) => {
const details: AddressDetails = this._api.utils.getAddressDetails(
const details: AddressDetails = getAddressDetails(
address,
);

Expand All @@ -199,8 +195,8 @@ export class LucidProvider extends BaseWalletProvider {
});
}

private loadLucid(config: BlockfrostConfig | KupmiosConfig): Promise<Lucid> {
return Lucid.new(
private loadLucid(config: BlockfrostConfig | KupmiosConfig): Promise<LucidEvolution> {
return Lucid(
'kupoUrl' in config
? new Kupmios(
config.kupoUrl,
Expand All @@ -209,7 +205,8 @@ export class LucidProvider extends BaseWalletProvider {
: new Blockfrost(
config.url,
config.projectId
)
),
'Mainnet',
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AddressType, DatumParameterKey, TransactionStatus } from './constants';
import { Token } from '@dex/models/asset';
import { BaseDex } from '@dex/base-dex';
import { LiquidityPool } from '@dex/models/liquidity-pool';
import { Script } from 'lucid-cardano';
import { Script } from '@lucid-evolution/lucid';

export interface DexterConfig {
shouldFetchMetadata?: boolean;
Expand Down
43 changes: 0 additions & 43 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Token } from '@dex/models/asset';
import { LiquidityPool } from '@dex/models/liquidity-pool';
import { C, Datum, fromHex, Lucid, toHex, Utils } from 'lucid-cardano';
import { DatumJson } from '@app/types';

export const lucidUtils: Utils = new Utils(new Lucid());

export function tokensMatch(tokenA: Token, tokenB: Token): boolean {
const tokenAId: string = tokenA === 'lovelace' ? 'lovelace' : tokenA.identifier();
Expand All @@ -23,43 +19,4 @@ export function appendSlash(value?: string) {
if (value.endsWith('/')) return;

return `${value}/`;
}

/**
* Modified version from lucid
*/
export function datumJsonToCbor(json: DatumJson): Datum {
const convert = (json: DatumJson): C.PlutusData => {
if (!isNaN(json.int!)) {
return C.PlutusData.new_integer(C.BigInt.from_str(json.int!.toString()));
} else if (json.bytes || !isNaN(Number(json.bytes))) {
return C.PlutusData.new_bytes(fromHex(json.bytes!));
} else if (json.map) {
const l = C.PlutusList.new();
(json as any).forEach((v: DatumJson) => {
l.add(convert(v));
});
return C.PlutusData.new_list(l);
} else if (json.list) {
const l = C.PlutusList.new();
json.list.forEach((v: DatumJson) => {
l.add(convert(v));
});
return C.PlutusData.new_list(l);
} else if (!isNaN(json.constructor! as unknown as number)) {
const l = C.PlutusList.new();
json.fields!.forEach((v: DatumJson) => {
l.add(convert(v));
});
return C.PlutusData.new_constr_plutus_data(
C.ConstrPlutusData.new(
C.BigNum.from_str(json.constructor!.toString()),
l,
),
);
}
throw new Error("Unsupported type");
};

return toHex(convert(json).to_bytes());
}

0 comments on commit b039e4b

Please sign in to comment.