Skip to content

Commit

Permalink
fix: updated mint and transfer to allow using own lock hashes for min…
Browse files Browse the repository at this point in the history
…ting PUDT and default wallet
  • Loading branch information
Alive24 committed Feb 8, 2025
1 parent dd03ec6 commit 5494293
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
23 changes: 12 additions & 11 deletions packages/examples/src/udt/mint.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ccc } from "@ckb-ccc/ccc";
import { render, signer } from "@ckb-ccc/playground";

// NOTE: Please use ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2jk6pyw9vlnfakx7vp4t5lxg0lzvvsp3c5adflu as the signer
const executor = new ccc.ssri.ExecutorJsonRpc("http://localhost:9090");

const signerAddress = await signer.getRecommendedAddress();
const {script: signerLock} = await ccc.Address.fromString(signerAddress, signer.client);

const pudtScriptCell = await signer.client.findSingletonCellByType({
// TypeID Code Hash. Don't change
codeHash:
Expand All @@ -20,13 +22,13 @@ const pudtCodeHash = pudtScriptCell.cellOutput.type?.hash();
if (!pudtCodeHash) {
throw new Error("PUDT code hash not found");
}
const pudtType = {
const signerPudtType = {
codeHash: pudtCodeHash,
hashType: "type",
args: "0x02c93173368ec56f72ec023f63148461b80e7698eddd62cbd9dbe31a13f2b330",
args: signerLock.hash()
};

const pudt = new ccc.udt.Udt(pudtScriptCell.outPoint, pudtType, {
const signerPudt = new ccc.udt.Udt(pudtScriptCell.outPoint, signerPudtType, {
executor,
});

Expand All @@ -47,8 +49,8 @@ const { script: lockB } = await ccc.Address.fromString(
signer.client,
);

const pudtMintTx = (
await pudt.mint(signer, [
const signerPudtMintTx = (
await signerPudt.mint(signer, [
{
to: lockA,
amount: 1000000000000000000,
Expand All @@ -60,10 +62,9 @@ const pudtMintTx = (
])
).res;

await render(pudtMintTx);
await signerPudtMintTx.completeFeeBy(signer);
await render(signerPudtMintTx);

await pudtMintTx.completeFeeBy(signer);
const pudtMintTxHash = await signer.sendTransaction(pudtMintTx);
const signerPudtMintTxHash = await signer.sendTransaction(signerPudtMintTx);

console.log(pudtMintTxHash);
// "0xfb3edb43a9eb79ae875a06cc17140717a1beea1fb394219797b2ce1c8cf144d3"
console.log(signerPudtMintTxHash);
37 changes: 23 additions & 14 deletions packages/examples/src/udt/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ccc } from "@ckb-ccc/ccc";
import { render, signer } from "@ckb-ccc/playground";

// NOTE: Please use ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgtlcnzzna2tqst7jw78egjpujn7hdxpackjmmdp as the signer
// NOTE: Disconnect your wallet will be running this example with the default wallet with some balance of the a specific PUDT token (though you won't be able to send the transaction). If you want to use your own wallet and your own PUDT, please use the `mint.ts` example to mint some of your own PUDT tokens to your wallet first.
const executor = new ccc.ssri.ExecutorJsonRpc("http://localhost:9090");

const signerAddress = await signer.getRecommendedAddress();
const { script: signerLock } = await ccc.Address.fromString(signerAddress, signer.client);

const pudtScriptCell = await signer.client.findSingletonCellByType({
// TypeID Code Hash. Don't change
codeHash:
Expand All @@ -20,13 +23,17 @@ const pudtCodeHash = pudtScriptCell.cellOutput.type?.hash();
if (!pudtCodeHash) {
throw new Error("PUDT code hash not found");
}
const pudtType = {

// Note: If using the default wallet, we will instead use another predefined owner lock hash for the PUDT.

const pudtOwnerLockHash = signerLock.hash() === "0x0ac59c147c592cd50958875ad50291f1c2f34025142c72526a9f5fb62f1b37b2" ? "0x02c93173368ec56f72ec023f63148461b80e7698eddd62cbd9dbe31a13f2b330" : signerLock.hash();
const signerPudtType = {
codeHash: pudtCodeHash,
hashType: "type",
args: "0x02c93173368ec56f72ec023f63148461b80e7698eddd62cbd9dbe31a13f2b330",
args: pudtOwnerLockHash,
};

const pudt = new ccc.udt.Udt(pudtScriptCell.outPoint, pudtType, {
const signerPudt = new ccc.udt.Udt(pudtScriptCell.outPoint, signerPudtType, {
executor,
});

Expand Down Expand Up @@ -55,8 +62,8 @@ const { script: lockB } = await ccc.Address.fromString(
signer.client,
);

let pudtTransferTx = (
await pudt.transfer(signer, [
let signerPudtTransferTx = (
await signerPudt.transfer(signer, [
{
to: lockA,
amount: 100,
Expand All @@ -68,18 +75,20 @@ let pudtTransferTx = (
])
).res;

await render(pudtTransferTx);
await render(signerPudtTransferTx);

signerPudtTransferTx = await signerPudt.completeBy(signerPudtTransferTx, signer);
await signerPudtTransferTx.completeFeeBy(signer);
await render(signerPudtTransferTx);

pudtTransferTx = await pudt.completeBy(pudtTransferTx, signer);
await pudtTransferTx.completeFeeBy(signer);
const pudtTransferTxHash = await signer.sendTransaction(pudtTransferTx);
const signerPudtTransferTxHash = await signer.sendTransaction(signerPudtTransferTx);

console.log(pudtTransferTxHash);
console.log(signerPudtTransferTxHash);
// "0x20d2f9456b3cd1bf21b32bcffcb91dac68e0cf63e24b496a3a06420080dd08b6"

// NOTE: As PUDT is in fact a pauseable UDT which paused transactions, though we instantiated it as a UDT, it still pauses transactions when the script is paused.
// NOTE: As PUDT is in fact a pauseable UDT which pauses transactions, though we instantiated it as a UDT, it still pauses transactions when the script is paused.
let shouldPudtPauseTx = (
await pudt.transfer(signer, [
await signerPudt.transfer(signer, [
{
to: pausedReceiverScript,
amount: 1000,
Expand All @@ -91,7 +100,7 @@ let shouldPudtPauseTx = (
])
).res;

shouldPudtPauseTx = await pudt.completeBy(shouldPudtPauseTx, signer);
shouldPudtPauseTx = await signerPudt.completeBy(shouldPudtPauseTx, signer);
await shouldPudtPauseTx.completeFeeBy(signer);

// This would fail. Catches the error.
Expand Down

0 comments on commit 5494293

Please sign in to comment.