Skip to content

Commit

Permalink
chore: bump deps and simplify address generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Jul 1, 2024
1 parent e475c4d commit 0eb3bb2
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 151 deletions.
18 changes: 9 additions & 9 deletions docs/typescript/core/classes/Lucid.TxBuilderLucidV3.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ TxBuilder.cancel

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:765](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L765)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:674](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L674)

___

Expand Down Expand Up @@ -126,7 +126,7 @@ TxBuilder.deposit

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:933](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L933)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:842](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L842)

___

Expand All @@ -151,7 +151,7 @@ The generated Bech32 address.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1164](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1164)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1073](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1073)

___

Expand Down Expand Up @@ -273,7 +273,7 @@ Throws an error if the retrieval of UTXOs fails or if no UTXOs are available.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1207](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1207)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1116](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1116)

___

Expand Down Expand Up @@ -384,7 +384,7 @@ TxBuilder.orderRouteSwap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:624](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L624)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:533](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L533)

___

Expand Down Expand Up @@ -414,7 +414,7 @@ TxBuilder.swap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:552](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L552)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:461](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L461)

___

Expand Down Expand Up @@ -446,7 +446,7 @@ TxBuilder.update

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:847](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L847)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:756](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L756)

___

Expand Down Expand Up @@ -476,7 +476,7 @@ TxBuilder.withdraw

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:981](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L981)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:890](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L890)

___

Expand Down Expand Up @@ -506,4 +506,4 @@ TxBuilder.zap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1026](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1026)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:935](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L935)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"devDependencies": {
"@semantic-release/git": "^10.0.1",
"@sundaeswap/asset": "^0.7.1",
"@sundaeswap/asset": "^1.0.3",
"@sundaeswap/docgen": "^0.1.2",
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.11",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
"docs:ci": "yarn docs --unsafe"
},
"devDependencies": {
"@sundaeswap/bigint-math": "^0.5.9",
"@sundaeswap/cpp": "^0.6.7",
"@sundaeswap/fraction": "^0.5.9",
"@sundaeswap/bigint-math": "^0.6.3",
"@sundaeswap/cpp": "^1.0.3",
"@sundaeswap/fraction": "^1.0.3",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/user-event": "^14.3.0",
"@types/jest": "^28.1.3",
Expand All @@ -94,10 +94,10 @@
"typescript": "^4.6.4"
},
"peerDependencies": {
"@sundaeswap/asset": "^0.7.1",
"@sundaeswap/bigint-math": "^0.5.9",
"@sundaeswap/cpp": "^0.6.9",
"@sundaeswap/fraction": "^0.5.9",
"@sundaeswap/asset": "^1.0.3",
"@sundaeswap/bigint-math": "^0.6.3",
"@sundaeswap/cpp": "^1.0.3",
"@sundaeswap/fraction": "^1.0.3",
"lucid-cardano": "^0.10.7"
},
"gitHead": "ddc937102065a5a581fe7affe02474c0e68ad9a0"
Expand Down
62 changes: 61 additions & 1 deletion packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AssetAmount, IAssetAmountMetadata } from "@sundaeswap/asset";
import { sqrt } from "@sundaeswap/bigint-math";
import { C, Constr, Data, UTxO } from "lucid-cardano";
import { C, Constr, Credential, Data, Lucid, UTxO } from "lucid-cardano";

import {
EDatumType,
Expand Down Expand Up @@ -606,4 +606,64 @@ export class DatumBuilderLucidV3 implements DatumBuilder {

return undefined;
}

static addressSchemaToBech32(
datum: V3Types.TAddressSchema,
lucid: Lucid
): string {
let paymentKeyHash: string;
let paymentAddressType: "Key" | "Script";
if ((datum.paymentCredential as V3Types.TVKeyCredential)?.VKeyCredential) {
paymentAddressType = "Key";
paymentKeyHash = (datum.paymentCredential as V3Types.TVKeyCredential)
.VKeyCredential.bytes;
} else if ((datum.paymentCredential as V3Types.TSCredential)?.SCredential) {
paymentAddressType = "Script";
paymentKeyHash = (datum.paymentCredential as V3Types.TSCredential)
.SCredential.bytes;
} else {
throw new Error(
"Could not determine the address type from supplied payment credential."
);
}

const result: Record<string, Credential> = {
paymentCredential: {
hash: paymentKeyHash,
type: paymentAddressType,
},
};

if (datum.stakeCredential?.keyHash) {
let stakingKeyHash: string | undefined;
let stakingAddressType: "Key" | "Script" | undefined;
if (
(datum.stakeCredential.keyHash as V3Types.TVKeyCredential)
?.VKeyCredential
) {
stakingAddressType = "Key";
stakingKeyHash = (
datum.stakeCredential.keyHash as V3Types.TVKeyCredential
).VKeyCredential.bytes;
} else if (
(datum.stakeCredential.keyHash as V3Types.TSCredential)?.SCredential
) {
stakingAddressType = "Script";
stakingKeyHash = (datum.stakeCredential.keyHash as V3Types.TSCredential)
.SCredential.bytes;
}

if (stakingKeyHash && stakingAddressType) {
result.stakeCredential = {
hash: stakingKeyHash,
type: stakingAddressType,
};
}
}

return lucid.utils.credentialToAddress(
result.paymentCredential,
result.stakeCredential
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import { jest } from "@jest/globals";
import { AssetAmount } from "@sundaeswap/asset";

import { Lucid } from "lucid-cardano";
import { EDatumType } from "../../@types/index.js";
import { LucidHelper } from "../../exports/lucid.js";
import { PREVIEW_DATA } from "../../exports/testing.js";
import { ADA_METADATA } from "../../exports/utilities.js";
import { DatumBuilderLucidV3 } from "../DatumBuilder.Lucid.V3.class.js";
import { TSignatureSchema } from "../contracts/contracts.v3.js";
import { V3Types } from "../contracts/index.js";

let builderInstance: DatumBuilderLucidV3;

Expand Down Expand Up @@ -271,3 +273,86 @@ describe("static getDestinationAddressesFromDatum()", () => {
).toMatchObject(expectedAddresses);
});
});

describe("static addressSchemaToCredential", () => {
it("should correctly convert address schemas to their Credentials", async () => {
const lucidInstance = await Lucid.new(undefined, "Preview");
const paymentAddress: V3Types.TAddressSchema = {
paymentCredential: {
VKeyCredential: {
bytes: "c279a3fb3b4e62bbc78e288783b58045d4ae82a18867d8352d02775a",
},
},
stakeCredential: null,
};

expect(
DatumBuilderLucidV3.addressSchemaToBech32(paymentAddress, lucidInstance)
).toEqual(
"addr_test1vrp8nglm8d8x9w783c5g0qa4spzaft5z5xyx0kp495p8wkswmc787"
);

const paymentAddressWithStakingKey: V3Types.TAddressSchema = {
paymentCredential: {
VKeyCredential: {
bytes: "c279a3fb3b4e62bbc78e288783b58045d4ae82a18867d8352d02775a",
},
},
stakeCredential: {
keyHash: {
VKeyCredential: {
bytes: "121fd22e0b57ac206fefc763f8bfa0771919f5218b40691eea4514d0",
},
},
},
};

expect(
DatumBuilderLucidV3.addressSchemaToBech32(
paymentAddressWithStakingKey,
lucidInstance
)
).toEqual(
"addr_test1qrp8nglm8d8x9w783c5g0qa4spzaft5z5xyx0kp495p8wksjrlfzuz6h4ssxlm78v0utlgrhryvl2gvtgp53a6j9zngqtjfk6s"
);

const scriptAddress: V3Types.TAddressSchema = {
paymentCredential: {
SCredential: {
bytes: "cfad1914b599d18bffd14d2bbd696019c2899cbdd6a03325cdf680bc",
},
},
stakeCredential: null,
};

expect(
DatumBuilderLucidV3.addressSchemaToBech32(scriptAddress, lucidInstance)
).toEqual(
"addr_test1wr866xg5kkvarzll69xjh0tfvqvu9zvuhht2qve9ehmgp0qfgf3wc"
);

const scriptAddressWithStakingKey: V3Types.TAddressSchema = {
paymentCredential: {
SCredential: {
bytes: "cfad1914b599d18bffd14d2bbd696019c2899cbdd6a03325cdf680bc",
},
},
stakeCredential: {
keyHash: {
VKeyCredential: {
bytes: "121fd22e0b57ac206fefc763f8bfa0771919f5218b40691eea4514d0",
},
},
},
};

expect(
DatumBuilderLucidV3.addressSchemaToBech32(
scriptAddressWithStakingKey,
lucidInstance
)
).toEqual(
"addr_test1zr866xg5kkvarzll69xjh0tfvqvu9zvuhht2qve9ehmgp0qjrlfzuz6h4ssxlm78v0utlgrhryvl2gvtgp53a6j9zngq4qar8t"
);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/DatumBuilders/contracts/contracts.v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export const AddressSchema = Data.Object({
})
),
});
export type TAddressSchema = Data.Static<typeof AddressSchema>;
export const Address = AddressSchema as unknown as TAddressSchema;

export const DestinationSchema = Data.Object({
address: AddressSchema,
Expand Down
Loading

0 comments on commit 0eb3bb2

Please sign in to comment.