Skip to content

Commit

Permalink
fix: add signer keys from datum in v1
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Oct 3, 2024
1 parent a37497f commit 32ee2f3
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 63 deletions.
28 changes: 14 additions & 14 deletions docs/typescript/core/Blaze/classes/TxBuilderBlazeV1.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The network id to use when building the transaction.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:101](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L101)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:110](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L110)

## Properties

Expand All @@ -53,7 +53,7 @@ A configured Blaze instance to use.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:102](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L102)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:111](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L111)

## Methods

Expand All @@ -79,7 +79,7 @@ The parameter you want to retrieve.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:243](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L243)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:252](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L252)

***

Expand Down Expand Up @@ -119,7 +119,7 @@ const txHash = await sdk.builder().cancel({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:576](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L576)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:582](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L582)

***

Expand All @@ -140,7 +140,7 @@ will re-populate with real data.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:188](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L188)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:197](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L197)

***

Expand All @@ -164,7 +164,7 @@ The name of the validator script to retrieve.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:207](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L207)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:216](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L216)

***

Expand Down Expand Up @@ -233,7 +233,7 @@ const migrationResult = await sdk.builder().migrateLiquidityToV3([

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:1106](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L1106)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:1136](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L1136)

***

Expand All @@ -257,7 +257,7 @@ Returns a new Tx instance from Blaze. Throws an error if not ready.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:253](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L253)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:262](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L262)

***

Expand Down Expand Up @@ -285,7 +285,7 @@ The result of the transaction.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:415](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L415)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:421](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L421)

***

Expand Down Expand Up @@ -325,7 +325,7 @@ const txHash = await sdk.builder().swap({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:329](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L329)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:335](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L335)

***

Expand Down Expand Up @@ -376,7 +376,7 @@ const txHash = await sdk.builder().update({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:682](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L682)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:712](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L712)

***

Expand Down Expand Up @@ -416,7 +416,7 @@ const txHash = await sdk.builder().withdraw({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:847](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L847)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:877](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L877)

***

Expand Down Expand Up @@ -456,7 +456,7 @@ const txHash = await sdk.builder().zap({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:915](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L915)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:945](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L945)

***

Expand Down Expand Up @@ -486,4 +486,4 @@ The protocol network.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:230](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L230)
[packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts:239](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts#L239)
8 changes: 4 additions & 4 deletions docs/typescript/core/Lucid/classes/TxBuilderLucidV1.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ const migrationResult = await sdk.builder().migrateLiquidityToV3([

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:913](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L913)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:919](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L919)

***

Expand Down Expand Up @@ -376,7 +376,7 @@ const txHash = await sdk.builder().update({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:550](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L550)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:556](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L556)

***

Expand Down Expand Up @@ -416,7 +416,7 @@ const txHash = await sdk.builder().withdraw({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:689](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L689)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:695](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L695)

***

Expand Down Expand Up @@ -456,7 +456,7 @@ const txHash = await sdk.builder().zap({

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:745](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L745)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:751](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L751)

***

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/@types/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface IDepositConfigArgs extends IOrderConfigArgs {
* The arguments configuration for building a valid cancellation transaction.
*/
export interface ICancelConfigArgs extends IBaseConfig {
ownerAddress: string;
ownerAddress?: string;
utxo: TUTXO;
}

Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/Configs/CancelConfig.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CancelConfig extends OrderConfig<ICancelConfigArgs> {

setFromObject({ utxo, ownerAddress, referralFee }: ICancelConfigArgs): void {
this.setUTXO(utxo);
this.setOwnerAddress(ownerAddress);
ownerAddress && this.setOwnerAddress(ownerAddress);
referralFee && this.setReferralFee(referralFee);
}

Expand All @@ -45,11 +45,5 @@ export class CancelConfig extends OrderConfig<ICancelConfigArgs> {
"You did not add the order's UTXO for this cancellation. Set a valid UTXO with .setUTXO()",
);
}

if (!this.ownerAddress) {
throw new Error(
"An owner address is required for validation purposes. Set the owner address of the order with .setOwnerAddress()",
);
}
}
}
26 changes: 17 additions & 9 deletions packages/core/src/DatumBuilders/ContractTypes/Contract.Blaze.v1.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { Data, Static } from "@blaze-cardano/sdk";

export const PaymentStakingHashSchema = Data.Enum([
Data.Object({
KeyHash: Data.Object({
value: Data.Bytes(),
}),
export const KeyHashSchema = Data.Object({
KeyHash: Data.Object({
value: Data.Bytes(),
}),
Data.Object({
ScriptHash: Data.Object({
value: Data.Bytes(),
}),
});
export type TKeyHashSchema = Static<typeof KeyHashSchema>;
export const KeyHash = KeyHashSchema as unknown as TKeyHashSchema;

export const ScriptHashSchema = Data.Object({
ScriptHash: Data.Object({
value: Data.Bytes(),
}),
});
export type TScriptHashSchema = Static<typeof ScriptHashSchema>;
export const ScriptHash = ScriptHashSchema as unknown as TScriptHashSchema;

export const PaymentStakingHashSchema = Data.Enum([
KeyHashSchema,
ScriptHashSchema,
]);
export type TPaymentStakingHash = Static<typeof PaymentStakingHashSchema>;
export const PaymentStakingHash =
Expand Down
56 changes: 43 additions & 13 deletions packages/core/src/TxBuilders/TxBuilder.Blaze.V1.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ import { DepositConfig } from "../Configs/DepositConfig.class.js";
import { SwapConfig } from "../Configs/SwapConfig.class.js";
import { WithdrawConfig } from "../Configs/WithdrawConfig.class.js";
import { ZapConfig } from "../Configs/ZapConfig.class.js";
import {
DepositOrder,
SwapOrder,
TDepositOrder,
TKeyHashSchema,
TSwapOrder,
TWithdrawOrder,
WithdrawOrder,
} from "../DatumBuilders/ContractTypes/Contract.Blaze.v1.js";
import { OrderDatum as V3OrderDatum } from "../DatumBuilders/ContractTypes/Contract.Blaze.v3.js";
import { DatumBuilderBlazeV1 } from "../DatumBuilders/DatumBuilder.Blaze.V1.class.js";
import { DatumBuilderBlazeV3 } from "../DatumBuilders/DatumBuilder.Blaze.V3.class.js";
Expand Down Expand Up @@ -284,9 +293,6 @@ export class TxBuilderBlazeV1 extends TxBuilderV1 {
}

if (fee?.feeLabel) {
/**
* @todo Ensure metadata is correctly attached.
*/
const data = new Core.AuxiliaryData();
const map = new Map<bigint, Core.Metadatum>();
map.set(
Expand Down Expand Up @@ -576,9 +582,7 @@ export class TxBuilderBlazeV1 extends TxBuilderV1 {
async cancel(
cancelArgs: ICancelConfigArgs,
): Promise<IComposedTx<BlazeTx, Core.Transaction>> {
const { utxo, referralFee, ownerAddress } = new CancelConfig(
cancelArgs,
).buildArgs();
const { utxo, referralFee } = new CancelConfig(cancelArgs).buildArgs();

const tx = this.newTxInstance(referralFee);
const [utxoToSpend] = await this.blaze.provider.resolveUnspentOutputs([
Expand Down Expand Up @@ -632,16 +636,42 @@ export class TxBuilderBlazeV1 extends TxBuilderV1 {
);

tx.provideScript(scriptValidator);
const details = Core.Address.fromBech32(ownerAddress);
const paymentCred = details.asBase()?.getPaymentCredential().hash;
const stakingCred = details.asBase()?.getStakeCredential().hash;

if (paymentCred && spendingDatum.toCbor().includes(paymentCred)) {
tx.addRequiredSigner(Core.Ed25519KeyHashHex(paymentCred));
// Must try deserializing the datum with each order type.
let data: TSwapOrder | TWithdrawOrder | TDepositOrder | undefined;
[SwapOrder, WithdrawOrder, DepositOrder].forEach((type) => {
if (data) {
return;
}

try {
data = Data.from(spendingDatum, type);
} catch (e) {}
});

if (!data) {
throw new Error(
"Could not determine order type based on the order's datum.",
);
}

if (stakingCred && spendingDatum.toCbor().includes(stakingCred)) {
tx.addRequiredSigner(Core.Ed25519KeyHashHex(stakingCred));
tx.addRequiredSigner(
Core.Ed25519KeyHashHex(
(
data.orderAddresses.destination.credentials
.paymentKey as TKeyHashSchema
).KeyHash.value,
),
);

const stakingKeyCred =
data.orderAddresses.destination.credentials.stakingKey?.value;
if (stakingKeyCred) {
tx.addRequiredSigner(
Core.Ed25519KeyHashHex(
(stakingKeyCred as TKeyHashSchema).KeyHash.value,
),
);
}

tx.setMinimumFee(310_000n);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ export class TxBuilderLucidV1 extends TxBuilderV1 {
cancelArgs,
).buildArgs();

if (!ownerAddress) {
throw new Error(
"An owner address is required to be explicitly supplied when using the Lucid builder.",
);
}

const tx = this.newTxInstance(referralFee);
const utxosToSpend = await this.lucid.provider.getUtxosByOutRef([
{ outputIndex: utxo.index, txHash: utxo.hash },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,14 @@ describe("TxBuilderBlazeV1", () => {
const txWithReferralAndLabel = builder.newTxInstance({
destination: TEST_REFERRAL_DEST,
payment: new AssetAmount(1_500_000n, ADA_METADATA),
// feeLabel: "Test Label",
feeLabel: "Test Label",
});

const txComplete = await txWithReferralAndLabel.complete();
// const metadata = txComplete.auxiliaryData()?.metadata();
const metadata = txComplete.auxiliaryData()?.metadata()?.metadata();

/**
* @TODO Restore this once Blaze fixes metadata bug.
*/
// expect(metadata).not.toBeUndefined();
// expect(metadata?.metadata()?.get(674n)?.asText()).toEqual(
// "Test Label: 1.5 ADA"
// );
expect(metadata).not.toBeUndefined();
expect(metadata?.get(674n)?.asText()).toEqual("Test Label: 1.5 ADA");

let referralAddressOutput: Core.TransactionOutput | undefined;
[...Array(txComplete.body().outputs().length).keys()].forEach((index) => {
Expand Down Expand Up @@ -1100,7 +1095,6 @@ describe("TxBuilderBlazeV1", () => {
);

const { build, datum, fees } = await builder.cancel({
ownerAddress: PREVIEW_DATA.addresses.current,
utxo: {
hash: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV1.txHash,
index: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV1.outputIndex,
Expand All @@ -1119,7 +1113,7 @@ describe("TxBuilderBlazeV1", () => {
}),
} as ITxBuilderFees);

const { builtTx } = await build();
const { builtTx, cbor } = await build();

expect(
builtTx
Expand Down Expand Up @@ -1176,7 +1170,6 @@ describe("TxBuilderBlazeV1", () => {
// Don't care to mock v3 so it will throw.
try {
await builder.cancel({
ownerAddress: PREVIEW_DATA.addresses.current,
utxo: {
hash: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV3.txHash,
index: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV3.outputIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ describe("TxBuilderBlazeV3", () => {
);

const { build, fees } = await builder.cancel({
ownerAddress: PREVIEW_DATA.addresses.current,
utxo: {
hash: "b18feb718648b33ef4900519b76f72f46723577ebad46191e2f8e1076c2b632c",
index: 0,
Expand Down Expand Up @@ -283,7 +282,6 @@ describe("TxBuilderBlazeV3", () => {
// Don't care to mock v3 so it will throw.
try {
await builder.cancel({
ownerAddress: PREVIEW_DATA.addresses.current,
utxo: {
hash: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV1.txHash,
index: PREVIEW_DATA.wallet.submittedOrderUtxos.swapV1.outputIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ describe("YieldFarmingBlaze", () => {
),
);

console.log(tx.builtTx.body().toCbor());
const outputWithAssets = tx.builtTx
.body()
.outputs()
Expand Down

0 comments on commit 32ee2f3

Please sign in to comment.