Skip to content

Commit 209c2a9

Browse files
committed
chore: make changes for PR reviews
1 parent b61235a commit 209c2a9

6 files changed

Lines changed: 166 additions & 170 deletions

File tree

packages/core/src/ckb/transaction.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ describe("Transaction", () => {
12341234
],
12351235
});
12361236

1237-
await tx.completeByFeePayer(client, mockFeePayer1, mockFeePayer2);
1237+
await tx.completeByFeePayer(mockFeePayer1, mockFeePayer2);
12381238

12391239
expect(mockFeePayer1.prepareTransaction).toHaveBeenCalledWith(tx);
12401240
expect(mockFeePayer2.prepareTransaction).toHaveBeenCalledWith(tx);
@@ -1252,7 +1252,7 @@ describe("Transaction", () => {
12521252
],
12531253
});
12541254

1255-
await tx.completeByFeePayer(client, mockFeePayer1, mockFeePayer2);
1255+
await tx.completeByFeePayer(mockFeePayer1, mockFeePayer2);
12561256

12571257
// Verify both methods were called
12581258
expect(mockFeePayer1.prepareTransaction).toHaveBeenCalled();
@@ -1293,7 +1293,7 @@ describe("Transaction", () => {
12931293
],
12941294
});
12951295

1296-
await tx.completeByFeePayer(client, mockFeePayer1);
1296+
await tx.completeByFeePayer(mockFeePayer1);
12971297

12981298
expect(mockFeePayer1.prepareTransaction).toHaveBeenCalledTimes(1);
12991299
expect(mockFeePayer1.completeTxFee).toHaveBeenCalledTimes(1);
@@ -1312,7 +1312,7 @@ describe("Transaction", () => {
13121312
});
13131313

13141314
// Should not throw with empty fee payer list
1315-
await expect(tx.completeByFeePayer(client)).resolves.not.toThrow();
1315+
await expect(tx.completeByFeePayer()).resolves.not.toThrow();
13161316
});
13171317

13181318
it("should handle multiple fee payers in sequence", async () => {
@@ -1349,7 +1349,7 @@ describe("Transaction", () => {
13491349
callOrder.push("complete2");
13501350
});
13511351

1352-
await tx.completeByFeePayer(client, mockFeePayer1, mockFeePayer2);
1352+
await tx.completeByFeePayer(mockFeePayer1, mockFeePayer2);
13531353

13541354
// Verify order: all prepareTransaction calls first, then all completeTxFee calls
13551355
expect(callOrder).toEqual([
@@ -1375,9 +1375,9 @@ describe("Transaction", () => {
13751375
mockFeePayer1.prepareTransaction as ReturnType<typeof vi.fn>
13761376
).mockRejectedValue(error);
13771377

1378-
await expect(
1379-
tx.completeByFeePayer(client, mockFeePayer1),
1380-
).rejects.toThrow("Prepare transaction failed");
1378+
await expect(tx.completeByFeePayer(mockFeePayer1)).rejects.toThrow(
1379+
"Prepare transaction failed",
1380+
);
13811381
});
13821382

13831383
it("should propagate errors from completeTxFee", async () => {
@@ -1395,9 +1395,9 @@ describe("Transaction", () => {
13951395
mockFeePayer1.completeTxFee as ReturnType<typeof vi.fn>
13961396
).mockRejectedValue(error);
13971397

1398-
await expect(
1399-
tx.completeByFeePayer(client, mockFeePayer1),
1400-
).rejects.toThrow("Complete fee failed");
1398+
await expect(tx.completeByFeePayer(mockFeePayer1)).rejects.toThrow(
1399+
"Complete fee failed",
1400+
);
14011401
});
14021402

14031403
it("should handle fee payer that modifies transaction in prepareTransaction", async () => {
@@ -1427,7 +1427,7 @@ describe("Transaction", () => {
14271427
mockFeePayer1.prepareTransaction as ReturnType<typeof vi.fn>
14281428
).mockResolvedValue(modifiedTx);
14291429

1430-
await tx.completeByFeePayer(client, mockFeePayer1);
1430+
await tx.completeByFeePayer(mockFeePayer1);
14311431

14321432
// prepareTransaction is called with a clone of the original transaction
14331433
expect(mockFeePayer1.prepareTransaction).toHaveBeenCalled();

packages/core/src/ckb/transaction.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,6 @@ export class Transaction extends mol.Entity.Base<
19571957
}> {
19581958
const { addedCount, accumulated } = await from.completeInputs(
19591959
this,
1960-
from.client,
19611960
filter,
19621961
accumulator,
19631962
init,
@@ -1972,7 +1971,6 @@ export class Transaction extends mol.Entity.Base<
19721971
): Promise<number> {
19731972
const addedCount = await from.completeInputsByCapacity(
19741973
this,
1975-
from.client,
19761974
capacityTweak,
19771975
{
19781976
filter,
@@ -2165,7 +2163,7 @@ export class Transaction extends mol.Entity.Base<
21652163
shouldAddInputs?: boolean;
21662164
},
21672165
): Promise<[number, boolean]> {
2168-
const result = await from.completeFee(this, from.client, {
2166+
const result = await from.completeFee(this, {
21692167
changeFn: change,
21702168
feeRate: expectedFeeRate,
21712169
filter,
@@ -2279,17 +2277,14 @@ export class Transaction extends mol.Entity.Base<
22792277
return this.completeFeeChangeToLock(from, script, feeRate, filter, options);
22802278
}
22812279

2282-
async completeByFeePayer(
2283-
client: Client,
2284-
...feePayers: FeePayer[]
2285-
): Promise<void> {
2280+
async completeByFeePayer(...feePayers: FeePayer[]): Promise<void> {
22862281
let tx = this.clone();
22872282
for (const feePayer of feePayers) {
22882283
tx = await feePayer.prepareTransaction(tx);
22892284
}
22902285

22912286
for (const feePayer of feePayers) {
2292-
await feePayer.completeTxFee(tx, client);
2287+
await feePayer.completeTxFee(tx);
22932288
}
22942289

22952290
this.copy(tx);

packages/core/src/signer/feePayer/feePayer.ts

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Address } from "../../address/index.js";
21
import { Cell, Transaction, TransactionLike } from "../../ckb/index.js";
32
import { Client } from "../../client/client.js";
43
import { ClientCollectableSearchKeyFilterLike } from "../../client/clientTypes.advanced.js";
@@ -13,46 +12,19 @@ export interface FeeRateOptionsLike {
1312
}
1413

1514
export abstract class FeePayer {
16-
constructor() {}
15+
constructor(protected client_: Client) {}
1716

18-
abstract completeTxFee(
19-
tx: Transaction,
20-
client: Client,
21-
options?: FeeRateOptionsLike,
22-
): Promise<void>;
23-
24-
/**
25-
* Gets an array of Address objects associated with the signer.
26-
*
27-
* @returns A promise that resolves to an array of Address objects.
28-
*/
29-
async getAddressObjs(): Promise<Address[]> {
30-
throw new Error("FeePayer.getAddressObjs not implemented");
31-
}
32-
33-
/**
34-
* Gets the recommended Address object for the signer.
35-
*
36-
* @param _preference - Optional preference parameter.
37-
* @returns A promise that resolves to the recommended Address object.
38-
*/
39-
async getRecommendedAddressObj(_preference?: unknown): Promise<Address> {
40-
return (await this.getAddressObjs())[0];
17+
get client(): Client {
18+
return this.client_;
4119
}
4220

43-
/**
44-
* Gets the recommended address for the signer as a string.
45-
*
46-
* @param preference - Optional preference parameter.
47-
* @returns A promise that resolves to the recommended address as a string.
48-
*/
49-
async getRecommendedAddress(preference?: unknown): Promise<string> {
50-
return (await this.getRecommendedAddressObj(preference)).toString();
51-
}
21+
abstract completeTxFee(
22+
txLike: TransactionLike,
23+
options?: FeeRateOptionsLike,
24+
): Promise<Transaction>;
5225

53-
async completeInputs<T>(
26+
abstract completeInputs<T>(
5427
tx: Transaction,
55-
client: Client,
5628
filter: ClientCollectableSearchKeyFilterLike,
5729
accumulator: (
5830
acc: T,
@@ -64,53 +36,7 @@ export abstract class FeePayer {
6436
): Promise<{
6537
addedCount: number;
6638
accumulated?: T;
67-
}> {
68-
const collectedCells = [];
69-
70-
let acc: T = init;
71-
let fulfilled = false;
72-
for (const address of await this.getAddressObjs()) {
73-
for await (const cell of client.findCells({
74-
script: address.script,
75-
scriptType: "lock",
76-
filter,
77-
scriptSearchMode: "exact",
78-
withData: true,
79-
})) {
80-
if (
81-
tx.inputs.some(({ previousOutput }) =>
82-
previousOutput.eq(cell.outPoint),
83-
)
84-
) {
85-
continue;
86-
}
87-
const i = collectedCells.push(cell);
88-
const next = await Promise.resolve(
89-
accumulator(acc, cell, i - 1, collectedCells),
90-
);
91-
if (next === undefined) {
92-
fulfilled = true;
93-
break;
94-
}
95-
acc = next;
96-
}
97-
if (fulfilled) {
98-
break;
99-
}
100-
}
101-
102-
collectedCells.forEach((cell) => tx.addInput(cell));
103-
if (fulfilled) {
104-
return {
105-
addedCount: collectedCells.length,
106-
};
107-
}
108-
109-
return {
110-
addedCount: collectedCells.length,
111-
accumulated: acc,
112-
};
113-
}
39+
}>;
11440

11541
/**
11642
* Prepares a transaction before signing.

0 commit comments

Comments
 (0)