-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdeployLogic.ts
More file actions
100 lines (90 loc) · 2.84 KB
/
deployLogic.ts
File metadata and controls
100 lines (90 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { readFileAsBytes } from "@/src/app/utils/(tools)/FileUpload/page";
import { ccc } from "@ckb-ccc/connector-react";
import { ReactNode } from "react";
import { normalizeTypeIdArgs } from "./helpers";
export type Logger = (...args: ReactNode[]) => void;
export async function runDeploy(
signer: ccc.Signer,
file: File,
typeIdArgs: string,
foundCell: ccc.Cell | null,
isAddressMatch: boolean | null,
log: Logger,
error: Logger,
): Promise<string | null> {
const fileBytes = (await readFileAsBytes(file)) as ccc.Bytes;
const { script } = await signer.getRecommendedAddressObj();
let tx: ccc.Transaction;
let typeIdArgsValue: string;
if (typeIdArgs.trim() !== "") {
if (!foundCell) {
error("Type ID cell not found. Please check the Type ID args.");
return null;
}
if (isAddressMatch === false) {
error(
"Cannot update cell: The cell's lock address does not match your wallet address. You cannot unlock this cell.",
);
return null;
}
const normalized = normalizeTypeIdArgs(typeIdArgs);
log("Updating existing Type ID cell...");
tx = ccc.Transaction.from({
inputs: [{ previousOutput: foundCell.outPoint }],
outputs: [
{
...foundCell.cellOutput,
capacity: ccc.Zero,
},
],
outputsData: [fileBytes],
});
typeIdArgsValue = normalized;
} else {
log("Building transaction...");
tx = ccc.Transaction.from({
outputs: [
{
lock: script,
type: await ccc.Script.fromKnownScript(
signer.client,
ccc.KnownScript.TypeId,
"00".repeat(32),
),
},
],
outputsData: [fileBytes],
});
await tx.completeInputsAddOne(signer);
if (!tx.outputs[0].type) {
throw new Error("Unexpected disappeared output");
}
tx.outputs[0].type.args = ccc.hashTypeId(tx.inputs[0], 0);
typeIdArgsValue = tx.outputs[0].type.args;
log("Type ID created:", typeIdArgsValue);
}
await tx.completeFeeBy(signer);
log("Sending transaction...");
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", txHash);
return txHash;
}
/** Burn the selected type_id cell: consume it and send capacity back to the lock (no type script). */
export async function runBurn(
signer: ccc.Signer,
foundCell: ccc.Cell,
log: Logger,
): Promise<string | null> {
const { lock } = foundCell.cellOutput;
const tx = ccc.Transaction.from({
inputs: [{ previousOutput: foundCell.outPoint }],
outputs: [{ lock, capacity: ccc.Zero }],
outputsData: ["0x"],
});
await tx.addCellDepsOfKnownScripts(signer.client, ccc.KnownScript.TypeId);
await tx.completeFeeChangeToOutput(signer, 0);
log("Sending burn transaction...");
const txHash = await signer.sendTransaction(tx);
log("Transaction sent:", txHash);
return txHash;
}