@@ -36,6 +36,13 @@ const as_of_args = [
3636 new BQFunctionArgument ( 'migration_id' , INT64 ) ,
3737] ;
3838
39+ const time_window_args = [
40+ new BQFunctionArgument ( 'start_record_time' , TIMESTAMP ) ,
41+ new BQFunctionArgument ( 'start_migration_id' , INT64 ) ,
42+ new BQFunctionArgument ( 'up_to_record_time' , TIMESTAMP ) ,
43+ new BQFunctionArgument ( 'up_to_migration_id' , INT64 ) ,
44+ ] ;
45+
3946const rewardsStruct = new BQStruct ( [
4047 { name : 'appRewardAmount' , type : BIGNUMERIC } ,
4148 { name : 'validatorRewardAmount' , type : BIGNUMERIC } ,
@@ -136,7 +143,7 @@ const in_time_window = new BQScalarFunction(
136143 WHEN start_record_time IS NOT NULL AND start_migration_id IS NOT NULL THEN
137144 (migration_id > start_migration_id
138145 OR (migration_id = start_migration_id
139- AND record_time >= UNIX_MICROS(start_record_time)))
146+ AND record_time > UNIX_MICROS(start_record_time)))
140147 AND (migration_id < up_to_migration_id
141148 OR (migration_id = up_to_migration_id
142149 AND record_time <= UNIX_MICROS(up_to_record_time)))
@@ -167,12 +174,23 @@ const migration_id_at_time = new BQScalarFunction(
167174 `
168175 -- Given a record time, find the latest migration ID that was active at that time. Takes the lowest ID that has updates
169176 -- after the given time, therefore if the timestamp is during a migration, it will return the older migration ID.
170- (SELECT
171- MIN(migration_id)
172- FROM
173- ((SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_creates\`) UNION DISTINCT
174- (SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_exercises\`))
175- WHERE record_time > UNIX_MICROS(as_of_record_time))
177+ -- If no updates exist after the given time, returns the migration id of the last update.
178+ IFNULL
179+ (
180+ -- Try to find the lowest migration ID that has updates after the given time.
181+ (SELECT
182+ MIN(migration_id)
183+ FROM
184+ ((SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_creates\`) UNION ALL
185+ (SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_exercises\`))
186+ WHERE record_time > UNIX_MICROS(as_of_record_time)),
187+ -- If none exists, return the migration ID of the last update.
188+ (SELECT migration_id FROM
189+ (
190+ (SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_creates\`) UNION ALL
191+ (SELECT record_time, migration_id FROM \`$$SCAN_DATASET$$.scan_sv_1_update_history_exercises\`)
192+ ) ORDER BY record_time DESC LIMIT 1)
193+ )
176194 `
177195) ;
178196
@@ -301,7 +319,7 @@ const choice_result_TransferSummary = new BQScalarFunction(
301319
302320const minted = new BQScalarFunction (
303321 'minted' ,
304- as_of_args ,
322+ time_window_args ,
305323 rewardsStruct ,
306324 `
307325 (SELECT
@@ -331,29 +349,13 @@ const minted = new BQScalarFunction(
331349 OR (e.choice = 'TransferPreapproval_Renew'
332350 AND e.template_id_entity_name = 'TransferPreapproval'))
333351 AND e.template_id_module_name = 'Splice.AmuletRules'
334- AND \`$$FUNCTIONS_DATASET$$.up_to_time\`(as_of_record_time, migration_id,
352+ AND \`$$FUNCTIONS_DATASET$$.in_time_window\`(
353+ start_record_time, start_migration_id,
354+ up_to_record_time, up_to_migration_id,
335355 e.record_time, e.migration_id))
336356 `
337357) ;
338358
339- const total_minted = new BQScalarFunction (
340- 'total_minted' ,
341- as_of_args ,
342- BIGNUMERIC ,
343- `
344- (SELECT
345- SUM(mint_amount)
346- FROM
347- UNNEST(ARRAY[
348- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).appRewardAmount,
349- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).validatorRewardAmount,
350- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).svRewardAmount,
351- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).unclaimedActivityRecordAmount
352- ]) AS mint_amount
353- )
354- `
355- ) ;
356-
357359const transferresult_fees = new BQScalarFunction (
358360 'transferresult_fees' ,
359361 [ new BQFunctionArgument ( 'tr_json' , json ) ] ,
@@ -401,10 +403,7 @@ const result_burn = new BQScalarFunction(
401403
402404const burned = new BQScalarFunction (
403405 'burned' ,
404- [
405- new BQFunctionArgument ( 'as_of_record_time' , TIMESTAMP ) ,
406- new BQFunctionArgument ( 'migration_id_arg' , INT64 ) ,
407- ] ,
406+ time_window_args ,
408407 BIGNUMERIC ,
409408 `
410409 (SELECT SUM(fees)
@@ -423,7 +422,9 @@ const burned = new BQScalarFunction(
423422 OR (e.choice = 'TransferPreapproval_Renew'
424423 AND e.template_id_entity_name = 'TransferPreapproval'))
425424 AND e.template_id_module_name = 'Splice.AmuletRules'
426- AND \`$$FUNCTIONS_DATASET$$.up_to_time\`(as_of_record_time, migration_id_arg,
425+ AND \`$$FUNCTIONS_DATASET$$.in_time_window\`(
426+ start_record_time, start_migration_id,
427+ up_to_record_time, up_to_migration_id,
427428 e.record_time, e.migration_id))
428429 UNION ALL (-- Purchasing ANS Entries
429430 SELECT
@@ -441,8 +442,10 @@ const burned = new BQScalarFunction(
441442 AND e.template_id_module_name = 'Splice.Wallet.Subscriptions'
442443 AND c.template_id_module_name = 'Splice.Amulet'
443444 AND c.template_id_entity_name = 'Amulet'
444- AND \`$$FUNCTIONS_DATASET$$.up_to_time\`(as_of_record_time, migration_id_arg,
445- e.record_time, e.migration_id)
445+ AND \`$$FUNCTIONS_DATASET$$.in_time_window\`(
446+ start_record_time, start_migration_id,
447+ up_to_record_time, up_to_migration_id,
448+ e.record_time, e.migration_id)
446449 AND c.record_time != -62135596800000000)))
447450 `
448451) ;
@@ -565,12 +568,11 @@ const all_stats = new BQTableFunction(
565568 new BQColumn ( 'unlocked' , BIGNUMERIC ) ,
566569 new BQColumn ( 'current_supply_total' , BIGNUMERIC ) ,
567570 new BQColumn ( 'unminted' , BIGNUMERIC ) ,
568- new BQColumn ( 'minted_app_rewards' , BIGNUMERIC ) ,
569- new BQColumn ( 'minted_validator_rewards' , BIGNUMERIC ) ,
570- new BQColumn ( 'minted_sv_rewards' , BIGNUMERIC ) ,
571- new BQColumn ( 'minted_unclaimed_activity_records' , BIGNUMERIC ) ,
572- new BQColumn ( 'burned' , BIGNUMERIC ) ,
573- new BQColumn ( 'monthly_burn' , BIGNUMERIC ) ,
571+ new BQColumn ( 'daily_mint_app_rewards' , BIGNUMERIC ) ,
572+ new BQColumn ( 'daily_mint_validator_rewards' , BIGNUMERIC ) ,
573+ new BQColumn ( 'daily_mint_sv_rewards' , BIGNUMERIC ) ,
574+ new BQColumn ( 'daily_mint_unclaimed_activity_records' , BIGNUMERIC ) ,
575+ new BQColumn ( 'daily_burn' , BIGNUMERIC ) ,
574576 new BQColumn ( 'num_amulet_holders' , INT64 ) ,
575577 new BQColumn ( 'num_active_validators' , INT64 ) ,
576578 new BQColumn ( 'average_tps' , FLOAT64 ) ,
@@ -584,12 +586,36 @@ const all_stats = new BQTableFunction(
584586 \`$$FUNCTIONS_DATASET$$.unlocked\`(as_of_record_time, migration_id) as unlocked,
585587 \`$$FUNCTIONS_DATASET$$.locked\`(as_of_record_time, migration_id) + \`$$FUNCTIONS_DATASET$$.unlocked\`(as_of_record_time, migration_id) as current_supply_total,
586588 \`$$FUNCTIONS_DATASET$$.unminted\`(as_of_record_time, migration_id) as unminted,
587- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).appRewardAmount as minted_app_rewards,
588- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).validatorRewardAmount as minted_validator_rewards,
589- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).svRewardAmount as minted_sv_rewards,
590- \`$$FUNCTIONS_DATASET$$.minted\`(as_of_record_time, migration_id).unclaimedActivityRecordAmount as minted_unclaimed_activity_records,
591- IFNULL(\`$$FUNCTIONS_DATASET$$.burned\`(as_of_record_time, migration_id), 0) as burned,
592- IFNULL(\`$$FUNCTIONS_DATASET$$.burned\`(as_of_record_time, migration_id) - \`$$FUNCTIONS_DATASET$$.burned\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 30 DAY), \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 30 DAY))), 0) as monthly_burn,
589+ \`$$FUNCTIONS_DATASET$$.minted\`(
590+ TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR),
591+ \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR)),
592+ as_of_record_time,
593+ migration_id).appRewardAmount
594+ AS daily_mint_app_rewards,
595+ \`$$FUNCTIONS_DATASET$$.minted\`(
596+ TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR),
597+ \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR)),
598+ as_of_record_time,
599+ migration_id).validatorRewardAmount
600+ AS daily_mint_validator_rewards,
601+ \`$$FUNCTIONS_DATASET$$.minted\`(
602+ TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR),
603+ \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR)),
604+ as_of_record_time, migration_id).svRewardAmount
605+ AS daily_mint_sv_rewards,
606+ \`$$FUNCTIONS_DATASET$$.minted\`(
607+ TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR),
608+ \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR)),
609+ as_of_record_time,
610+ migration_id).unclaimedActivityRecordAmount
611+ AS daily_mint_unclaimed_activity_records,
612+ IFNULL(
613+ \`$$FUNCTIONS_DATASET$$.burned\`(
614+ TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR),
615+ \`$$FUNCTIONS_DATASET$$.migration_id_at_time\`(TIMESTAMP_SUB(as_of_record_time, INTERVAL 24 HOUR)),
616+ as_of_record_time,
617+ migration_id),
618+ 0) AS daily_burn,
593619 \`$$FUNCTIONS_DATASET$$.num_amulet_holders\`(as_of_record_time, migration_id) as num_amulet_holders,
594620 \`$$FUNCTIONS_DATASET$$.num_active_validators\`(as_of_record_time, migration_id) as num_active_validators,
595621 IFNULL(\`$$FUNCTIONS_DATASET$$.average_tps\`(as_of_record_time, migration_id), 0.0) as average_tps,
@@ -637,12 +663,11 @@ const days_with_missing_stats = new BQTableFunction(
637663 AND unlocked IS NOT NULL
638664 AND current_supply_total IS NOT NULL
639665 AND unminted IS NOT NULL
640- AND minted_app_rewards IS NOT NULL
641- AND minted_validator_rewards IS NOT NULL
642- AND minted_sv_rewards IS NOT NULL
643- AND minted_unclaimed_activity_records IS NOT NULL
644- AND burned IS NOT NULL
645- AND monthly_burn IS NOT NULL
666+ AND daily_mint_app_rewards IS NOT NULL
667+ AND daily_mint_validator_rewards IS NOT NULL
668+ AND daily_mint_sv_rewards IS NOT NULL
669+ AND daily_mint_unclaimed_activity_records IS NOT NULL
670+ AND daily_burn IS NOT NULL
646671 AND num_amulet_holders IS NOT NULL
647672 AND num_active_validators IS NOT NULL
648673 AND average_tps IS NOT NULL
@@ -681,7 +706,6 @@ export const allScanFunctions = [
681706 TransferSummary_minted ,
682707 choice_result_TransferSummary ,
683708 minted ,
684- total_minted ,
685709 transferresult_fees ,
686710 result_burn ,
687711 burned ,
0 commit comments