Skip to content

Commit

Permalink
fix: minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alive24 committed Jan 8, 2025
1 parent 527786e commit 3cfe531
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions packages/ssri/src/ssri/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ export class Server {
| { cell: ccc.CellLike }
| { transaction: ccc.TransactionLike },
): Promise<ccc.Hex> {
const pathHex = ccc.hashCkb(ccc.bytesFrom(path));
const pathHex = ccc.hashCkb(ccc.bytesFrom(path, "utf8")).slice(0, 18);
const parsedArgsHex = argsHex.map((arg) => ccc.hexFrom(arg));
const payload = {
id: 2,
jsonrpc: "2.0",
method: "run_script_level_code",
params: [
codeOutPoint.txHash,
codeOutPoint.index,
Number(codeOutPoint.index),
[pathHex, ...parsedArgsHex],
],
} as PayloadType;
Expand Down
4 changes: 1 addition & 3 deletions packages/udt/src/udt/advanced.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ccc, mol } from "@ckb-ccc/core";

export const lockArrayCodec = mol.vector(ccc.Script);
import { mol } from "@ckb-ccc/core";

export const amountArrayCodec = mol.vector(mol.Uint128);
79 changes: 42 additions & 37 deletions packages/udt/src/udt/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ccc } from "@ckb-ccc/core";
import { ccc, mol } from "@ckb-ccc/core";
import { ssri } from "@ckb-ccc/ssri";
import { amountArrayCodec, lockArrayCodec } from "./advanced.js";
import { amountArrayCodec } from "./advanced";

/**
* Represents a User Defined Token (UDT) contract compliant with the SSRI protocol.
Expand Down Expand Up @@ -58,8 +58,7 @@ export class UDT extends ssri.Contract {
* @tag Legacy - Supports xUDT legacy behavior.
*/
async name(): Promise<string> {
const hasMethod = (await this.hasMethods(["UDT.name"]))[0];
if (hasMethod) {
try {
const rawResult = await this.ssriServer.callMethod(
"UDT.name",
[],
Expand All @@ -69,7 +68,7 @@ export class UDT extends ssri.Contract {
},
);
return ccc.bytesTo(rawResult, "utf8");
} else {
} catch (_error) {
throw new Error(
"UDT.name method not found and ckb-udt-indexer not implemented yet",
);
Expand All @@ -82,8 +81,7 @@ export class UDT extends ssri.Contract {
* @tag Legacy - Supports xUDT legacy behavior.
*/
async symbol(): Promise<string> {
const hasMethod = (await this.hasMethods(["UDT.symbol"]))[0];
if (hasMethod) {
try {
const rawResult = await this.ssriServer.callMethod(
"UDT.symbol",
[],
Expand All @@ -93,7 +91,7 @@ export class UDT extends ssri.Contract {
},
);
return ccc.bytesTo(rawResult, "utf8");
} else {
} catch (_error) {
throw new Error(
"UDT.symbol method not found and ckb-udt-indexer not implemented yet",
);
Expand All @@ -106,18 +104,17 @@ export class UDT extends ssri.Contract {
* @tag Legacy - Supports xUDT legacy behavior.
*/
async decimals(): Promise<ccc.Num> {
const hasMethod = (await this.hasMethods(["UDT.decimals"]))[0];
if (hasMethod) {
try {
const rawResult = await this.ssriServer.callMethod(
"UDT.decimals",
[],
this.cellDep.outPoint,
{
script: this.type,
},
script: this.type,
},
);
return ccc.numFromBytes(rawResult);
} else {
} catch (_error) {
throw new Error(
"UDT.decimals method not found and ckb-udt-indexer not implemented yet",
);
Expand All @@ -130,18 +127,17 @@ export class UDT extends ssri.Contract {
* @tag Legacy - Supports xUDT legacy behavior.
*/
async balance(cell: ccc.CellLike): Promise<ccc.Num> {
const hasMethod = (await this.hasMethods(["UDT.balance"]))[0];
if (hasMethod) {
try {
const rawResult = await this.ssriServer.callMethod(
"UDT.balance",
[],
this.cellDep.outPoint,
{
cell,
},
);
cell,
},
);
return ccc.numLeFromBytes(ccc.bytesFrom(rawResult));
} else {
} catch (_error) {
return ccc.numLeFromBytes(ccc.bytesFrom(cell.outputData));
}
}
Expand Down Expand Up @@ -211,15 +207,17 @@ export class UDT extends ssri.Contract {
if (toLockArray.length !== toAmountArray.length) {
throw new Error("The number of lock scripts and amounts must match");
}
const hasMethod = (await this.hasMethods(["UDT.transfer"]))[0];
if (hasMethod) {
try {
const txEncodedHex = tx
? ccc.hexFrom(ccc.Transaction.from(tx).toBytes())
: "0x";
const parsedToLockArray = toLockArray.map((lock) =>
ccc.Script.from(lock),
);
const toLockArrayEncoded = lockArrayCodec.encode(parsedToLockArray);

const toLockArrayEncoded = mol.BytesVec.encode(
parsedToLockArray.map((lock) => lock.toBytes()),
);
const toLockArrayEncodedHex = ccc.hexFrom(toLockArrayEncoded);
const toAmountArrayEncoded = amountArrayCodec.encode(toAmountArray);
const toAmountArrayEncodedHex = ccc.hexFrom(toAmountArrayEncoded);
Expand All @@ -233,7 +231,7 @@ export class UDT extends ssri.Contract {
);
const resultDecodedArray = ccc.bytesFrom(rawResult);
return ccc.Transaction.decode(resultDecodedArray);
} else {
} catch (_error) {
let parsedTx: ccc.Transaction;
if (!tx) {
parsedTx = ccc.Transaction.from({
Expand Down Expand Up @@ -276,12 +274,13 @@ export class UDT extends ssri.Contract {
if (toLockArray.length !== toAmountArray.length) {
throw new Error("The number of lock scripts and amounts must match");
}
const hasMethod = (await this.hasMethods(["UDT.mint"]))[0];
if (hasMethod) {
try {
const txEncodedHex = tx
? ccc.hexFrom(ccc.Transaction.from(tx).toBytes())
: "0x";
const toLockArrayEncoded = lockArrayCodec.encode(toLockArray);
const toLockArrayEncoded = mol.BytesVec.encode(
toLockArray.map((lock) => ccc.Script.from(lock).toBytes()),
);
const toLockArrayEncodedHex = ccc.hexFrom(toLockArrayEncoded);
const toAmountArrayEncoded = amountArrayCodec.encode(toAmountArray);
const toAmountArrayEncodedHex = ccc.hexFrom(toAmountArrayEncoded);
Expand All @@ -295,7 +294,7 @@ export class UDT extends ssri.Contract {
);
const rawResultDecoded = ccc.Transaction.decode(rawResult);
return ccc.Transaction.from(rawResultDecoded);
} else {
} catch (_error) {
let parsedTx: ccc.Transaction;
if (!tx) {
parsedTx = ccc.Transaction.from({
Expand Down Expand Up @@ -327,15 +326,21 @@ export class UDT extends ssri.Contract {
* @tag Legacy - Supports xUDT legacy behavior.
*/
async icon(): Promise<ccc.Bytes> {
const rawResult = await this.ssriServer.callMethod(
"UDT.icon",
[],
this.cellDep.outPoint,
{
script: this.type,
},
);
const iconBytes = Buffer.from(ccc.bytesFrom(rawResult));
return iconBytes;
try {
const rawResult = await this.ssriServer.callMethod(
"UDT.icon",
[],
this.cellDep.outPoint,
{
script: this.type,
},
);
const iconBytes = Buffer.from(ccc.bytesFrom(rawResult));
return iconBytes;
} catch (_error) {
throw new Error(
"UDT.icon method not found and ckb-udt-indexer not implemented yet",
);
}
}
}

0 comments on commit 3cfe531

Please sign in to comment.