@minswap/internal-sdk
v0.1.2
Published
```ts export function encodeCostModels(costModels: Record<string, number>): Bytes { const sortedCostModelValues: number[] & { encodeCBOR?: any } = Object.entries(costModels) .sort(([key1], [key2]) => { if (key1 < key2) { return -1;
Downloads
129
Keywords
Readme
How to encode cost models
export function encodeCostModels(costModels: Record<string, number>): Bytes {
const sortedCostModelValues: number[] & { encodeCBOR?: any } = Object.entries(costModels)
.sort(([key1], [key2]) => {
if (key1 < key2) {
return -1;
} else if (key1 > key2) {
return 1;
} else {
return 0;
}
})
.map(([_, val]) => val);
sortedCostModelValues.encodeCBOR = cbor.Encoder.encodeIndefinite;
return Bytes.fromHex(cbor.encode(sortedCostModelValues).toString("hex"));
}How to fix circular import
Thanks to https://github.com/acrazing/dpdm - A robust static dependency analyzer for your JavaScript and TypeScript projects.
Example:
- I have tree like:
➜ ido git:(tony/ido-2.2) ✗ tree -L 1 . ├── build-tx.ts ├── ido-v1.ts ├── ido.ts ├── index.ts ├── scripts ├── utils.ts ├── warehouse-v1.ts └── warehouse.ts- I try to run and got error:
check_coin➜ monorepo git:(tony/ido-2.2) ✗ ./packages/backend/scripts/setup-ido.sh mainnet rebuild the script /Users/dzung/minswap/monorepo/packages/sdk/src/ido/ido-v1.ts:9 export class IdoV1Event extends IdoEvent { ^ TypeError: Class extends value undefined is not a constructor or null at invariant (/Users/dzung/minswap/monorepo/packages/sdk/src/ido/ido-v1.ts:9:33) at Object.<anonymous> (/Users/dzung/minswap/monorepo/packages/sdk/src/ido/ido-v1.ts:79:1) at Module._compile (node:internal/modules/cjs/loader:1562:14) at Object.transformer (/Users/dzung/minswap/monorepo/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/register-DpmFHar1.cjs:2:1186) at Module.load (node:internal/modules/cjs/loader:1313:32) at Function._load (node:internal/modules/cjs/loader:1123:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:217:24) at Module.require (node:internal/modules/cjs/loader:1335:12) at require (node:internal/modules/helpers:136:16) Node.js v22.13.0- Code:
# ./src/ido/ido-v1.ts import invariant from "@minswap/tiny-invariant"; import { IdoEvent, IdoOrder, Maybe, type NetworkEnvironment, Result, type Utxo } from ".."; import { IdoWarehouseV1 } from "./warehouse-v1"; export class IdoV1Event extends IdoEvent { }- Use
dpdmto detect circular import:
➜ sdk git:(tony/ido-2.2) ✗ npx dpdm --no-warning --no-tree ./src/ido/ido-v1.ts | grep ido ✔ [130/130] Analyze done! 58) src/index.ts -> src/ido/index.ts -> src/ido/build-tx.ts 59) src/index.ts -> src/ido/index.ts -> src/ido/build-tx.ts -> src/ido/ido.ts 60) src/index.ts -> src/ido/index.ts -> src/ido/build-tx.ts -> src/ido/ido.ts -> src/ido/warehouse.ts 61) src/ido/ido-v1.ts -> src/index.ts -> src/ido/index.ts 62) src/index.ts -> src/ido/index.ts -> src/ido/utils.ts 63) src/index.ts -> src/ido/index.ts -> src/ido/warehouse-v1.ts- Observe: at
61):src/ido/ido-v1.ts -> src/index.ts -> src/ido/index.tsmeans:src/ido/ido-v1.ts -> src/index.ts -> src/ido/index.ts -> src/index.ts -> src/ido/ido-v1.ts - Solution: We need to update direct imports from
src/ido/ido-v1.tstosrc/ido/ido.ts - Updated code:
import invariant from "@minswap/tiny-invariant"; import { Maybe, type NetworkEnvironment, Result, type Utxo } from ".."; import { IdoEvent, IdoOrder } from "./ido"; import { IdoWarehouseV1 } from "./warehouse-v1"; export class IdoV1Event extends IdoEvent { }- Re-run:
➜ monorepo git:(tony/ido-2.2) ✗ ./packages/backend/scripts/setup-ido.sh mainnet rebuild the script setup done
