@aresrpg/sui-checkpoint-reader
Advanced tools
Comparing version 2.0.4 to 2.0.5
{ | ||
"name": "@aresrpg/sui-checkpoint-reader", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
108
src/index.js
@@ -8,3 +8,2 @@ import { setInterval, setTimeout } from 'timers/promises' | ||
unlinkSync, | ||
writeFileSync, | ||
} from 'fs' | ||
@@ -15,2 +14,5 @@ | ||
import suiBcs from './generated/sui-bcs.js' | ||
import standardBcs from './generated/standard-bcs.js' | ||
const Envelope = (name, data, auth_signature) => | ||
@@ -604,18 +606,39 @@ bcs.struct(name, { | ||
function parse_content(contents, bcs) { | ||
if (!contents || !bcs.parse) return | ||
const { | ||
id: { | ||
id: { bytes }, | ||
function parse_content(struct, { contents, known_types }) { | ||
if (!contents) return | ||
function find_nested_bcs({ address, module, name, type_params = [], $kind }) { | ||
const current_bcs = known_types[address]?.[module]?.[name] | ||
if (type_params.length) { | ||
const nested_bcs = type_params.map(({ Struct, ...rest }) => { | ||
if (!Struct) return find_nested_bcs(rest) | ||
return find_nested_bcs(Struct) | ||
}) | ||
return current_bcs(...nested_bcs) | ||
} | ||
if ($kind && $kind !== 'Struct') return bcs[$kind.toLowerCase()]() | ||
return current_bcs | ||
} | ||
const found_bcs = find_nested_bcs(struct) | ||
if (!found_bcs) return | ||
const parsed = found_bcs.parse(new Uint8Array(contents)) | ||
const { id: { id: { bytes = undefined } = {} } = {}, ...rest } = mapper( | ||
parsed, | ||
{ | ||
bytes: b => { | ||
if (Array.isArray(b)) return Buffer.from(b).toString('utf8') | ||
return b | ||
}, | ||
}, | ||
...rest | ||
} = mapper(bcs.parse(new Uint8Array(contents)), { | ||
bytes: b => { | ||
if (Array.isArray(b)) return Buffer.from(b).toString('utf8') | ||
return b | ||
}, | ||
}) | ||
) | ||
return { | ||
id: `0x${bytes}`, | ||
...(bytes && { id: `0x${bytes}` }), | ||
...Object.fromEntries( | ||
@@ -669,19 +692,17 @@ Object.entries(rest).map(([key, value]) => { | ||
if (Other) { | ||
const bcs = known_types[address]?.[module]?.[name] ?? {} | ||
if (bcs) { | ||
const parsed_content = parse_content(contents, bcs) | ||
return { | ||
has_public_transfer, | ||
type: `${address}::${module}::${name}`, | ||
version, | ||
storage_rebate, | ||
owner, | ||
previous_transaction, | ||
...(type_params?.length && { | ||
type_params: `${address}::${module}::${name}<${parsed_type_params}>`, | ||
}), | ||
...(parsed_content && { | ||
contents: parsed_content, | ||
}), | ||
} | ||
const parsed_content = parse_content(Other, { | ||
contents, | ||
known_types, | ||
}) | ||
return { | ||
has_public_transfer, | ||
type: `${address}::${module}::${name}`, | ||
version, | ||
storage_rebate, | ||
owner, | ||
previous_transaction, | ||
...(type_params?.length && { | ||
type_params: `${address}::${module}::${name}<${parsed_type_params}>`, | ||
}), | ||
contents: parsed_content ?? contents, | ||
} | ||
@@ -791,2 +812,9 @@ } | ||
Object.assign(known_types, { | ||
'0x0000000000000000000000000000000000000000000000000000000000000002': | ||
suiBcs, | ||
'0x0000000000000000000000000000000000000000000000000000000000000001': | ||
standardBcs, | ||
}) | ||
// if the lowest known checkpoint is higher than the start checkpoint, we don't need to catch up | ||
@@ -844,10 +872,2 @@ if (lowest_known_checkpoint != null && lowest_known_checkpoint <= from) | ||
async function start_downloading_checkpoints() { | ||
console.log('[system] starting to download checkpoints', { | ||
from, | ||
to: | ||
lowest_known_checkpoint == null | ||
? to | ||
: Math.min(to, lowest_known_checkpoint), | ||
}) | ||
// we will download checkpoints until we reach the lowest known checkpoint or the target checkpoint | ||
@@ -861,2 +881,12 @@ const should_keep_downloading = current => { | ||
if (should_keep_downloading(sync_settings.current_checkpoint)) | ||
console.log('[system] starting to download checkpoints', { | ||
from, | ||
to: | ||
lowest_known_checkpoint == null | ||
? to | ||
: Math.min(to, lowest_known_checkpoint), | ||
}) | ||
else start_listening_for_local_checkpoints() | ||
while (should_keep_downloading(sync_settings.current_checkpoint)) { | ||
@@ -863,0 +893,0 @@ console.log( |
73059
14
1979