@swaptoshi/dex-snapshot
Version:
Library for capturing snapshots of the blockchain state in the Swaptoshi DEX module
187 lines • 8.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDexModuleAssetFromPath = exports.createDexModuleAsset = void 0;
const lisk_db_1 = require("@liskhq/lisk-db");
const cryptography_1 = require("@klayr/cryptography");
const chain_1 = require("@klayr/chain");
const dex_module_1 = require("@swaptoshi/dex-module");
const constants_1 = require("./constants");
const getObservationSubstore = async (db) => {
const observationStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_OBSERVATION_STORE);
const observations = (await observationStore.iterateWithSchema({
gte: Buffer.alloc(22, 0),
lte: Buffer.alloc(22, 255),
}, dex_module_1.observationStoreSchema));
return observations
.sort((a, b) => {
if (!a.key.subarray(0, 20).equals(b.key.subarray(0, 20))) {
return a.key.subarray(0, 20).compare(b.key.subarray(0, 20));
}
return a.key.subarray(20).readUIntBE(0, 2) - b.key.subarray(20).readUIntBE(0, 2);
})
.map(item => ({
...item.value,
poolAddress: cryptography_1.address.getKlayr32AddressFromAddress(item.key.subarray(0, 20)),
index: item.key.subarray(20).readUIntBE(0, 2).toString(),
}));
};
const getPoolSubstore = async (db) => {
const poolStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_POOL_STORE);
const pools = (await poolStore.iterateWithSchema({
gte: Buffer.alloc(20, 0),
lte: Buffer.alloc(20, 255),
}, dex_module_1.poolStoreSchema));
return pools
.sort((a, b) => {
if (!a.value.token0.equals(b.value.token0)) {
return a.value.token0.compare(b.value.token0);
}
if (!a.value.token1.equals(b.value.token1)) {
return a.value.token1.compare(b.value.token1);
}
return parseInt(a.value.fee, 10) - parseInt(b.value.fee, 10);
})
.map(item => ({
...item.value,
token0: item.value.token0.toString('hex'),
token1: item.value.token1.toString('hex'),
}));
};
const getPositionInfoSubstore = async (db) => {
const positionInfoStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_POSITION_INFO_STORE);
const positionInfos = (await positionInfoStore.iterateWithSchema({
gte: Buffer.alloc(52, 0),
lte: Buffer.alloc(52, 255),
}, dex_module_1.positionInfoStoreSchema));
return positionInfos
.sort((a, b) => {
if (!a.key.subarray(0, 20).equals(b.key.subarray(0, 20))) {
return a.key.subarray(0, 20).compare(b.key.subarray(0, 20));
}
if (!a.key.subarray(20).equals(b.key.subarray(20))) {
return a.key.subarray(20).compare(b.key.subarray(20));
}
return 0;
})
.map(item => ({
...item.value,
poolAddress: cryptography_1.address.getKlayr32AddressFromAddress(item.key.subarray(0, 20)),
key: item.key.subarray(20).toString('hex'),
}));
};
const getPositionManagerSubstore = async (db) => {
const positionManagerStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_POSITION_MANAGER_STORE);
const positionManagers = (await positionManagerStore.iterateWithSchema({
gte: Buffer.alloc(4, 0),
lte: Buffer.alloc(4, 255),
}, dex_module_1.positionManagerStoreSchema));
return positionManagers
.sort((a, b) => a.value.poolAddress.compare(b.value.poolAddress))
.map(item => ({
...item.value,
poolAddress: cryptography_1.address.getKlayr32AddressFromAddress(item.value.poolAddress),
}));
};
const getSupportedTokenSubstore = async (db) => {
const supportedTokenStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_SUPPORTED_TOKEN_STORE);
try {
const suppoertedTokens = await supportedTokenStore.getWithSchema(Buffer.alloc(0), dex_module_1.supportedTokenStoreSchema);
suppoertedTokens.supported.map(t => t.toString('hex'));
return [
{
supportAll: suppoertedTokens.supportAll,
supported: suppoertedTokens.supported.sort((a, b) => a.compare(b)).map(t => t.toString('hex')),
},
];
}
catch {
return [];
}
};
const getTickBitmapSubstore = async (db) => {
const tickBitmapStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_TICK_BITMAP_STORE);
const tickBitmaps = (await tickBitmapStore.iterateWithSchema({
gte: Buffer.alloc(22, 0),
lte: Buffer.alloc(22, 255),
}, dex_module_1.tickBitmapStoreSchema));
return tickBitmaps
.sort((a, b) => {
if (!a.key.subarray(0, 20).equals(b.key.subarray(0, 20))) {
return a.key.subarray(0, 20).compare(b.key.subarray(0, 20));
}
return a.key.subarray(20).readUIntBE(0, 2) - b.key.subarray(20).readUIntBE(0, 2);
})
.map(item => ({
...item.value,
poolAddress: cryptography_1.address.getKlayr32AddressFromAddress(item.key.subarray(0, 20)),
index: item.key.subarray(20).readUIntBE(0, 2).toString(),
}));
};
const getTickInfoSubstore = async (db) => {
const tickInfoStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_TICK_INFO_STORE);
const tickInfos = (await tickInfoStore.iterateWithSchema({
gte: Buffer.alloc(23, 0),
lte: Buffer.alloc(23, 255),
}, dex_module_1.tickInfoStoreSchema));
return tickInfos
.sort((a, b) => {
if (!a.key.subarray(0, 20).equals(b.key.subarray(0, 20))) {
return a.key.subarray(0, 20).compare(b.key.subarray(0, 20));
}
return a.key.subarray(20).readUIntBE(0, 3) - b.key.subarray(20).readUIntBE(0, 3);
})
.map(item => ({
...item.value,
poolAddress: cryptography_1.address.getKlayr32AddressFromAddress(item.key.subarray(0, 20)),
tick: item.key.subarray(20).readUIntBE(0, 3).toString(),
}));
};
const getTokenSymbolSubstore = async (db) => {
const tokenSymbolStore = new chain_1.StateStore(db, constants_1.DB_PREFIX_DEX_TOKEN_SYMBOL_STORE);
const tokenSymbols = (await tokenSymbolStore.iterateWithSchema({
gte: Buffer.alloc(8, 0),
lte: Buffer.alloc(8, 255),
}, dex_module_1.tokenSymbolStoreSchema));
return tokenSymbols
.sort((a, b) => a.key.compare(b.key))
.map(item => ({
...item.value,
tokenId: item.key.toString('hex'),
}));
};
const getDexModuleEntry = (observationSubstore, poolSubstore, positionInfoSubstore, positionManagerSubstore, supportedTokenSubstore, tickBitmapSubstore, tickInfoSubstore, tokenSymbolSubstore) => {
const genesisObj = {
observationSubstore,
poolSubstore,
positionInfoSubstore,
positionManagerSubstore,
supportedTokenSubstore,
tickBitmapSubstore,
tickInfoSubstore,
tokenSymbolSubstore,
};
return {
module: dex_module_1.MODULE_NAME_DEX,
data: genesisObj,
schema: dex_module_1.dexGenesisStoreSchema,
};
};
const createDexModuleAsset = async (db) => {
const observationSubstore = await getObservationSubstore(db);
const poolSubstore = await getPoolSubstore(db);
const positionInfoSubstore = await getPositionInfoSubstore(db);
const positionManagerSubstore = await getPositionManagerSubstore(db);
const supportedTokenSubstore = await getSupportedTokenSubstore(db);
const tickBitmapSubstore = await getTickBitmapSubstore(db);
const tickInfoSubstore = await getTickInfoSubstore(db);
const tokenSymbolSubstore = await getTokenSymbolSubstore(db);
const dexModuleAssets = getDexModuleEntry(observationSubstore, poolSubstore, positionInfoSubstore, positionManagerSubstore, supportedTokenSubstore, tickBitmapSubstore, tickInfoSubstore, tokenSymbolSubstore);
return dexModuleAssets;
};
exports.createDexModuleAsset = createDexModuleAsset;
const createDexModuleAssetFromPath = async (path) => {
const db = new lisk_db_1.StateDB(path);
return (0, exports.createDexModuleAsset)(db);
};
exports.createDexModuleAssetFromPath = createDexModuleAssetFromPath;
//# sourceMappingURL=dex.js.map