Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): auto fee rate #67

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/selfish-eggs-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@ckb-ccc/core": patch
"@ckb-ccc/ccc": patch
"ckb-ccc": patch
"@ckb-ccc/connector": patch
"@ckb-ccc/connector-react": patch
---

feat(core): auto fee rate
10 changes: 6 additions & 4 deletions packages/core/src/ckb/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1824,9 +1824,11 @@ export class Transaction {
async completeFee(
from: Signer,
change: (tx: Transaction, capacity: Num) => Promise<NumLike> | NumLike,
feeRate: NumLike,
expectedFeeRate?: NumLike,
filter?: ClientCollectableSearchKeyFilterLike,
): Promise<[number, boolean]> {
const feeRate = expectedFeeRate ?? (await from.client.getFeeRate());

// Complete all inputs extra infos for cache
await this.getInputsCapacity(from.client);

Expand Down Expand Up @@ -1902,7 +1904,7 @@ export class Transaction {
completeFeeChangeToLock(
from: Signer,
change: ScriptLike,
feeRate: NumLike,
feeRate?: NumLike,
filter?: ClientCollectableSearchKeyFilterLike,
): Promise<[number, boolean]> {
const script = Script.from(change);
Expand All @@ -1926,7 +1928,7 @@ export class Transaction {

async completeFeeBy(
from: Signer,
feeRate: NumLike,
feeRate?: NumLike,
filter?: ClientCollectableSearchKeyFilterLike,
): Promise<[number, boolean]> {
const { script } = await from.getRecommendedAddressObj();
Expand All @@ -1937,7 +1939,7 @@ export class Transaction {
completeFeeChangeToOutput(
from: Signer,
index: NumLike,
feeRate: NumLike,
feeRate?: NumLike,
filter?: ClientCollectableSearchKeyFilterLike,
): Promise<[number, boolean]> {
const change = Number(numFrom(index));
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "../ckb/index.js";
import { Zero } from "../fixedPoint/index.js";
import { Hex, HexLike, hexFrom } from "../hex/index.js";
import { Num, NumLike, numFrom } from "../num/index.js";
import { Num, NumLike, numFrom, numMax } from "../num/index.js";
import { apply, reduceAsync } from "../utils/index.js";
import { ClientCache } from "./cache/index.js";
import { ClientCacheMemory } from "./cache/memory.js";
Expand Down Expand Up @@ -101,6 +101,13 @@ export abstract class Client {
Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfo[] }
>;

abstract getFeeRateStatistics(
blockRange?: NumLike,
): Promise<{ mean: Num; median: Num }>;
async getFeeRate(blockRange?: NumLike): Promise<Num> {
return numMax((await this.getFeeRateStatistics(blockRange)).median, 1000);
}

abstract getTip(): Promise<Num>;
abstract getTipHeader(verbosity?: number | null): Promise<ClientBlockHeader>;
abstract getBlockByNumber(
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/client/jsonRpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ export abstract class ClientJsonRpc extends Client {
return this.url_;
}

/**
* Get fee rate statistics
*
* @returns Fee rate statistics
*/

getFeeRateStatistics = this.buildSender(
"get_fee_rate_statistics",
[(n) => apply(numFrom, n)],
({ mean, median }) => ({ mean: numFrom(mean), median: numFrom(median) }),
) as Client["getFeeRateStatistics"];

/**
* Get tip block number
*
Expand Down
6 changes: 3 additions & 3 deletions packages/demo/src/app/connected/(tools)/IssueXUdtSus/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function IssueXUdtSul() {
],
});
await susTx.completeInputsByCapacity(signer);
await susTx.completeFeeBy(signer, 1000);
await susTx.completeFeeBy(signer);
const susTxHash = await signer.sendTransaction(susTx);
log("Transaction sent:", explorerTransaction(susTxHash));
await signer.client.cache.markUnusable({
Expand All @@ -106,7 +106,7 @@ export default function IssueXUdtSul() {
],
});
await lockTx.completeInputsByCapacity(signer);
await lockTx.completeFeeBy(signer, 1000);
await lockTx.completeFeeBy(signer);
const lockTxHash = await signer.sendTransaction(lockTx);
log("Transaction sent:", explorerTransaction(lockTxHash));

Expand Down Expand Up @@ -166,7 +166,7 @@ export default function IssueXUdtSul() {
mintTx.outputs[1].type!.args = ccc.hexFrom(
ccc.bytesFrom(ccc.hashTypeId(mintTx.inputs[0], 1)).slice(0, 20),
);
await mintTx.completeFeeBy(signer, 1000);
await mintTx.completeFeeBy(signer);
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(mintTx)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function IssueXUdtTypeId() {
typeIdTx.inputs[0],
0,
);
await typeIdTx.completeFeeBy(signer, 1000);
await typeIdTx.completeFeeBy(signer);
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(typeIdTx)),
Expand All @@ -130,7 +130,7 @@ export default function IssueXUdtTypeId() {
],
});
await lockTx.completeInputsByCapacity(signer);
await lockTx.completeFeeBy(signer, 1000);
await lockTx.completeFeeBy(signer);
const lockTxHash = await signer.sendTransaction(lockTx);
log("Transaction sent:", explorerTransaction(lockTxHash));

Expand Down Expand Up @@ -195,7 +195,7 @@ export default function IssueXUdtTypeId() {
mintTx.outputs[2].type!.args = ccc.hexFrom(
ccc.bytesFrom(ccc.hashTypeId(mintTx.inputs[0], 2)).slice(0, 20),
);
await mintTx.completeFeeBy(signer, 1000);
await mintTx.completeFeeBy(signer);
log(
"Transaction sent:",
explorerTransaction(await signer.sendTransaction(mintTx)),
Expand Down
11 changes: 7 additions & 4 deletions packages/demo/src/app/connected/(tools)/NervosDao/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function DaoButton({ dao }: { dao: ccc.Cell }) {
);

await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
await tx.completeFeeBy(signer);
} else {
if (!infos[3]) {
error("Unexpected no found deposit info");
Expand Down Expand Up @@ -225,7 +225,7 @@ function DaoButton({ dao }: { dao: ccc.Cell }) {
);

await tx.completeInputsByCapacity(signer);
await tx.completeFeeChangeToOutput(signer, 0, 1000);
await tx.completeFeeChangeToOutput(signer, 0);
tx.outputs[0].capacity += profit;
}

Expand Down Expand Up @@ -280,6 +280,7 @@ export default function Transfer() {
const { explorerTransaction } = useGetExplorerLink();

const [amount, setAmount] = useState<string>("");
const [feeRate, setFeeRate] = useState<undefined | ccc.Num>();
const [daos, setDaos] = useState<ccc.Cell[]>([]);

useEffect(() => {
Expand Down Expand Up @@ -347,7 +348,9 @@ export default function Transfer() {
);

await tx.completeInputsAll(signer);
await tx.completeFeeChangeToOutput(signer, 0, 1000);
const feeRate = await signer.client.getFeeRate();
setFeeRate(feeRate);
await tx.completeFeeChangeToOutput(signer, 0, feeRate);

const amount = ccc.fixedPointToString(tx.outputs[0].capacity);
log("You can deposit at most", amount, "CKB");
Expand Down Expand Up @@ -394,7 +397,7 @@ export default function Transfer() {
tx.outputs[0].capacity = ccc.fixedPointFrom(amount);

await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
await tx.completeFeeBy(signer, feeRate);

// Sign and send the transaction
log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function ClaimButton({ cell, lock }: { cell: ccc.Cell; lock: ccc.Script }) {
);

await tx.completeInputsByCapacity(signer);
await tx.completeFeeChangeToOutput(signer, 0, 1000);
await tx.completeFeeChangeToOutput(signer, 0);

log(
"Transaction sent:",
Expand Down Expand Up @@ -147,7 +147,7 @@ export default function TimeLockedTransfer() {

// Complete missing parts for transaction
await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
await tx.completeFeeBy(signer);

log(
"Transaction sent:",
Expand Down
7 changes: 5 additions & 2 deletions packages/demo/src/app/connected/(tools)/Transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function Transfer() {
const { explorerTransaction } = useGetExplorerLink();

const [transferTo, setTransferTo] = useState<string>("");
const [feeRate, setFeeRate] = useState<undefined | ccc.Num>();
const [amount, setAmount] = useState<string>("");
const [data, setData] = useState<string>("");

Expand Down Expand Up @@ -62,8 +63,10 @@ export default function Transfer() {

// Complete missing parts for transaction
await tx.completeInputsAll(signer);
const feeRate = await signer.client.getFeeRate();
setFeeRate(feeRate);
// Change all balance to the first output
await tx.completeFeeChangeToOutput(signer, 0, 1000);
await tx.completeFeeChangeToOutput(signer, 0, feeRate);
const amount = ccc.fixedPointToString(tx.outputs[0].capacity);
log("You can transfer at most", amount, "CKB");
setAmount(amount);
Expand Down Expand Up @@ -100,7 +103,7 @@ export default function Transfer() {

// Complete missing parts for transaction
await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
await tx.completeFeeBy(signer, feeRate);

// Sign and send the transaction
log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function TransferXUdt() {
ccc.KnownScript.XUdt,
);
await tx.completeInputsByCapacity(signer);
await tx.completeFeeBy(signer, 1000);
await tx.completeFeeBy(signer);

// Sign and send the transaction
log(
Expand Down