@ickb/order
Version:
UDT Limit Order utilities built on top of CCC
449 lines • 17.4 kB
JavaScript
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
import { ccc, mol } from "@ckb-ccc/core";
import { CheckedInt32LE, union, gcd, max, } from "@ickb/utils";
let Ratio = (() => {
let _classDecorators = [mol.codec(mol.struct({
ckbScale: mol.Uint64,
udtScale: mol.Uint64,
}))];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = mol.Entity.Base();
var Ratio = _classThis = class extends _classSuper {
constructor(ckbScale, udtScale) {
super();
Object.defineProperty(this, "ckbScale", {
enumerable: true,
configurable: true,
writable: true,
value: ckbScale
});
Object.defineProperty(this, "udtScale", {
enumerable: true,
configurable: true,
writable: true,
value: udtScale
});
}
static from(ratio) {
if (ratio instanceof Ratio) {
return ratio;
}
const { ckbScale, udtScale } = ratio;
return new Ratio(ckbScale, udtScale);
}
validate() {
if (!this.isEmpty() && !this.isPopulated()) {
throw Error("Ratio invalid: not empty, not populated");
}
}
isValid() {
try {
this.validate();
return true;
}
catch {
return false;
}
}
isEmpty() {
return this.ckbScale === 0n && this.udtScale === 0n;
}
isPopulated() {
return this.ckbScale > 0n && this.udtScale > 0n;
}
static empty() {
return new Ratio(0n, 0n);
}
compare(other) {
if (this.udtScale == other.udtScale) {
return Number(this.ckbScale - other.ckbScale);
}
if (this.ckbScale == other.ckbScale) {
return Number(other.udtScale - this.udtScale);
}
return Number(this.ckbScale * other.udtScale - other.ckbScale * this.udtScale);
}
applyFee(isCkb2Udt, fee, feeBase) {
if (fee >= feeBase) {
throw Error("Fee too big respectfully to feeBase");
}
if (fee === 0n) {
return this;
}
let { ckbScale: aScale, udtScale: bScale } = this;
if (!isCkb2Udt) {
[aScale, bScale] = [bScale, aScale];
}
aScale *= feeBase - fee;
bScale *= feeBase;
const g = gcd(aScale, bScale);
aScale /= g;
bScale /= g;
const maxBitLen = max(aScale.toString(2).length, bScale.toString(2).length);
if (maxBitLen > 64) {
const shift = BigInt(maxBitLen - 64);
aScale >>= shift;
bScale >>= shift;
}
return Ratio.from({
ckbScale: isCkb2Udt ? aScale : bScale,
udtScale: isCkb2Udt ? bScale : aScale,
});
}
convert(isCkb2Udt, amount, mustCeil) {
if (!this.isPopulated()) {
throw Error("Invalid midpoint ExchangeRatio");
}
if (amount === 0n) {
return 0n;
}
let { ckbScale: aScale, udtScale: bScale } = this;
if (!isCkb2Udt) {
[aScale, bScale] = [bScale, aScale];
}
return (amount * aScale + (mustCeil ? bScale - 1n : 0n)) / bScale;
}
};
__setFunctionName(_classThis, "Ratio");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
Ratio = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return Ratio = _classThis;
})();
export { Ratio };
let Info = (() => {
let _classDecorators = [mol.codec(mol.struct({
ckbToUdt: Ratio,
udtToCkb: Ratio,
ckbMinMatchLog: mol.Uint8,
}))];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = mol.Entity.Base();
var Info = _classThis = class extends _classSuper {
constructor(ckbToUdt, udtToCkb, ckbMinMatchLog) {
super();
Object.defineProperty(this, "ckbToUdt", {
enumerable: true,
configurable: true,
writable: true,
value: ckbToUdt
});
Object.defineProperty(this, "udtToCkb", {
enumerable: true,
configurable: true,
writable: true,
value: udtToCkb
});
Object.defineProperty(this, "ckbMinMatchLog", {
enumerable: true,
configurable: true,
writable: true,
value: ckbMinMatchLog
});
}
static from(info) {
if (info instanceof Info) {
return info;
}
const { ckbToUdt, udtToCkb, ckbMinMatchLog } = info;
return new Info(Ratio.from(ckbToUdt), Ratio.from(udtToCkb), Number(ckbMinMatchLog));
}
static create(isCkb2Udt, ratioLike, ckbMinMatchLog = Info.ckbMinMatchLogDefault()) {
return Info.from({
ckbToUdt: isCkb2Udt ? ratioLike : Ratio.empty(),
udtToCkb: isCkb2Udt ? Ratio.empty() : ratioLike,
ckbMinMatchLog,
});
}
validate() {
if (this.ckbMinMatchLog < 0 || this.ckbMinMatchLog > 64) {
throw Error("ckbMinMatchLog invalid");
}
if (this.ckbToUdt.isEmpty()) {
if (this.udtToCkb.isPopulated()) {
return;
}
else {
throw Error("ckbToUdt is Empty, but udtToCkb is not Populated");
}
}
if (this.udtToCkb.isEmpty()) {
if (this.ckbToUdt.isPopulated()) {
return;
}
else {
throw Error("udtToCkb is Empty, but ckbToUdt is not Populated");
}
}
if (!this.ckbToUdt.isPopulated() || !this.udtToCkb.isPopulated()) {
throw Error("One ratio is invalid, so not Empty and not Populated");
}
if (this.ckbToUdt.ckbScale * this.udtToCkb.udtScale <
this.ckbToUdt.udtScale * this.udtToCkb.ckbScale) {
throw Error("udtToCkb and ckbToUdt allow order value to be extracted");
}
}
isValid() {
try {
this.validate();
return true;
}
catch {
return false;
}
}
getCkbMinMatch() {
return 1n << BigInt(this.ckbMinMatchLog);
}
isCkb2Udt() {
return this.ckbToUdt.isPopulated();
}
isUdt2Ckb() {
return this.udtToCkb.isPopulated();
}
isDualRatio() {
return this.isCkb2Udt() && this.isUdt2Ckb();
}
ckb2UdtCompare(other) {
return this.ckbToUdt.compare(other.ckbToUdt);
}
udt2CkbCompare(other) {
return other.udtToCkb.compare(this.udtToCkb);
}
static ckbMinMatchLogDefault() {
return 33;
}
};
__setFunctionName(_classThis, "Info");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
Info = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return Info = _classThis;
})();
export { Info };
let Relative = (() => {
let _classDecorators = [mol.codec(mol.struct({
padding: mol.Byte32,
distance: CheckedInt32LE,
}))];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = mol.Entity.Base();
var Relative = _classThis = class extends _classSuper {
constructor(padding, distance) {
super();
Object.defineProperty(this, "padding", {
enumerable: true,
configurable: true,
writable: true,
value: padding
});
Object.defineProperty(this, "distance", {
enumerable: true,
configurable: true,
writable: true,
value: distance
});
}
static from(relative) {
if (relative instanceof Relative) {
return relative;
}
const { padding, distance } = relative;
return new Relative(ccc.bytesFrom(padding), ccc.numFrom(distance));
}
static create(distance) {
return new Relative(Relative.padding(), distance);
}
static padding() {
return new Uint8Array(32);
}
validate() {
if (this.padding.length != 32 || this.padding.some((x) => x !== 0)) {
throw Error("Relative master invalid, non standard padding");
}
}
isValid() {
try {
this.validate();
return true;
}
catch {
return false;
}
}
};
__setFunctionName(_classThis, "Relative");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
Relative = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return Relative = _classThis;
})();
export { Relative };
export const MasterCodec = union({
relative: Relative,
absolute: ccc.OutPoint,
});
function masterFrom(master) {
const { type, value } = master;
if (type === "relative") {
return { type, value: Relative.from(value) };
}
else if (type === "absolute") {
return { type, value: ccc.OutPoint.from(value) };
}
else {
throw Error(`Invalid type ${String(type)}, not relative, not absolute`);
}
}
function masterValidate(master) {
const { type, value } = master;
if (type === "relative") {
value.validate();
}
else if (type === "absolute") {
if (!/^0x[0-9a-f]{64}$/i.test(value.txHash) || value.index < 0) {
throw Error("OutPoint invalid");
}
}
else {
throw Error(`Invalid type ${String(type)}, not relative, not absolute`);
}
}
let OrderData = (() => {
let _classDecorators = [mol.codec(mol.struct({
udtValue: mol.Uint128,
master: MasterCodec,
info: Info,
}))];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = mol.Entity.Base();
var OrderData = _classThis = class extends _classSuper {
constructor(udtValue, master, info) {
super();
Object.defineProperty(this, "udtValue", {
enumerable: true,
configurable: true,
writable: true,
value: udtValue
});
Object.defineProperty(this, "master", {
enumerable: true,
configurable: true,
writable: true,
value: master
});
Object.defineProperty(this, "info", {
enumerable: true,
configurable: true,
writable: true,
value: info
});
}
static from(data) {
if (data instanceof OrderData) {
return data;
}
const { udtValue, master, info } = data;
return new OrderData(ccc.numFrom(udtValue), masterFrom(master), Info.from(info));
}
validate() {
if (this.udtValue < 0) {
throw Error("udtValue invalid, negative");
}
masterValidate(this.master);
this.info.validate();
}
isValid() {
try {
this.validate();
return true;
}
catch {
return false;
}
}
isMint() {
return this.master.type === "relative";
}
getMaster(current) {
const { type, value } = this.master;
if (type === "relative") {
return new ccc.OutPoint(current.txHash, current.index + value.distance);
}
else if (type === "absolute") {
return value;
}
else {
throw Error(`Invalid type ${String(type)}, not relative, not absolute`);
}
}
};
__setFunctionName(_classThis, "OrderData");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
OrderData = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return OrderData = _classThis;
})();
export { OrderData };
//# sourceMappingURL=entities.js.map