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(helpers): refresh txSkeleton cell deps #687

Merged
merged 3 commits into from
May 15, 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
5 changes: 5 additions & 0 deletions .changeset/modern-panthers-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/helpers": minor
---

feat: refreshTypeIdCellDeps to refresh typeid cell deps
28 changes: 23 additions & 5 deletions packages/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export function minimalScriptCapacity(
return BigInt(result.toString());
}

const ONE_CKB = 100_000_000;

export function minimalScriptCapacityCompatible(
script: Script,
{ validate = true }: { validate?: boolean } = {}
Expand All @@ -54,7 +56,7 @@ export function minimalScriptCapacityCompatible(
// hash_type field
bytes += 1;

return BI.from(bytes).mul(100000000);
return BI.from(bytes).mul(ONE_CKB);
}

export function minimalCellCapacity(
Expand Down Expand Up @@ -86,7 +88,7 @@ export function minimalCellCapacityCompatible(
if (fullCell.data) {
bytes += bytify(fullCell.data).length;
}
return BI.from(bytes).mul(100000000);
return BI.from(bytes).mul(ONE_CKB);
}

export function locateCellDep(
Expand All @@ -113,6 +115,16 @@ export function locateCellDep(

let HAS_WARNED_FOR_DEPRECATED_ADDRESS = false;

// |format type|description |
// |-----------|----------------------------------------------------------|
// |0x00 |full version identifies the hash_type |
// |0x01 |short version for locks with popular code_hash, deprecated|
// |0x02 |full version with hash_type = "Data", deprecated |
// |0x04 |full version with hash_type = "Type", deprecated |

const CKB2019_ADDRESS_FORMAT_TYPE_DATA = 0x02;
const CKB2019_ADDRESS_FORMAT_TYPE_TYPE = 0x04;

/**
* @deprecated please migrate to {@link encodeToAddress}, the short format address will be removed in the future
* @param script
Expand Down Expand Up @@ -141,9 +153,13 @@ export function generateAddress(
data.push(1, scriptTemplate.SHORT_ID);
data.push(...hexToByteArray(script.args));
} else {
if (script.hashType === "type") data.push(0x04);
else if (script.hashType === "data") data.push(0x02);
else throw new Error(`Invalid hashType ${script.hashType}`);
if (script.hashType === "type") {
data.push(CKB2019_ADDRESS_FORMAT_TYPE_TYPE);
} else if (script.hashType === "data") {
data.push(CKB2019_ADDRESS_FORMAT_TYPE_DATA);
} else {
throw new Error(`Invalid hashType ${script.hashType}`);
}

data.push(...hexToByteArray(script.codeHash));
data.push(...hexToByteArray(script.args));
Expand Down Expand Up @@ -514,3 +530,5 @@ export function objectToTransactionSkeleton(
});
return txSkeleton;
}

export { refreshTypeIdCellDeps } from "./refresh";
26 changes: 26 additions & 0 deletions packages/helpers/src/refresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TransactionSkeletonType } from "./";
import type { ResolveLatestOutPointsOfTypeIds } from "@ckb-lumos/config-manager/lib/refresh";

/**
* Refresh cellDeps of txSkeleton
* @example
* const resolve = createRpcResolver(rpc)
* // refresh the cell deps
* let txSkeleton = await refreshTypeIdCellDeps(txSkeleton, { resolve })
* @param txSkeleton
* @param resolve
*/
export async function refreshTypeIdCellDeps(
txSkeleton: TransactionSkeletonType,
{ resolve }: { resolve: ResolveLatestOutPointsOfTypeIds }
): Promise<TransactionSkeletonType> {
const outPoints = txSkeleton.cellDeps
.map(({ outPoint }) => outPoint)
.toArray();

const latestOutPoints = await resolve(outPoints);

return txSkeleton.update("cellDeps", (cellDeps) =>
cellDeps.map((cellDep, i) => ({ ...cellDep, outPoint: latestOutPoints[i] }))
);
}
48 changes: 48 additions & 0 deletions packages/helpers/tests/refresh.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from "ava";
import { refreshTypeIdCellDeps } from "../src/refresh";
import { TransactionSkeleton } from "../src";
import { CellDep } from "@ckb-lumos/base";
import { bytes } from "@ckb-lumos/codec";
import { randomBytes } from "node:crypto";

test("refreshTypeIdCellDeps", async (t) => {
const outdatedCellDep: CellDep = {
outPoint: { index: "0x0", txHash: bytes.hexify(randomBytes(32)) },
depType: "code",
};
const latestCellDep: CellDep = {
outPoint: { index: "0x0", txHash: bytes.hexify(randomBytes(32)) },
depType: "code",
};

const normalCellDep: CellDep = {
outPoint: { index: "0x0", txHash: bytes.hexify(randomBytes(32)) },
depType: "code",
};

const txSkeleton = TransactionSkeleton({}).asMutable();
txSkeleton.update("cellDeps", (cellDeps) =>
cellDeps.push(outdatedCellDep, normalCellDep)
);
await refreshTypeIdCellDeps(txSkeleton, {
resolve: (outPoints) =>
outPoints.map((outPoint) => {
if (outPoint.txHash === outdatedCellDep.outPoint.txHash) {
return latestCellDep.outPoint;
}
return outPoint;
}),
});

t.is(
txSkeleton.cellDeps.get(0)?.outPoint.txHash,
latestCellDep.outPoint.txHash
);

t.is(
txSkeleton.cellDeps.get(1)?.outPoint.txHash,
normalCellDep.outPoint.txHash
);

t.is(txSkeleton.cellDeps.size, 2);
});
1 change: 1 addition & 0 deletions packages/lumos/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
createTransactionFromSkeleton,
sealTransaction,
TransactionSkeleton,
refreshTypeIdCellDeps,
type TransactionSkeletonType,
type TransactionSkeletonInterface,
type TransactionSkeletonObject,
Expand Down
Loading