1
+ /* eslint-env node */
2
+
3
+ import {
4
+ agoric ,
5
+ getContractInfo ,
6
+ pushPrices ,
7
+ getPriceQuote ,
8
+ } from '@agoric/synthetic-chain' ;
9
+ import { retryUntilCondition } from '@agoric/client-utils' ;
10
+
11
+ export const scale6 = x => BigInt ( x * 1_000_000 ) ;
12
+
13
+ /**
14
+ *
15
+ * @param {number } price
16
+ * @param {string } brand
17
+ * @param {Map<any, any> } oraclesByBrand
18
+ * @param {number } roundId
19
+ * @returns {Promise<void> }
20
+ */
21
+ export const verifyPushedPrice = async (
22
+ price ,
23
+ brand ,
24
+ oraclesByBrand ,
25
+ roundId ,
26
+ ) => {
27
+ const pushPriceRetryOpts = {
28
+ maxRetries : 5 , // arbitrary
29
+ retryIntervalMs : 5000 , // in ms
30
+ } ;
31
+
32
+ await pushPrices ( price , brand , oraclesByBrand , roundId ) ;
33
+ console . log ( `Pushing price ${ price } for ${ brand } ` ) ;
34
+
35
+ await retryUntilCondition (
36
+ ( ) => getPriceQuote ( brand ) ,
37
+ res => res === `+${ scale6 ( price ) . toString ( ) } ` ,
38
+ 'price not pushed yet' ,
39
+ {
40
+ log : console . log ,
41
+ setTimeout : global . setTimeout ,
42
+ ...pushPriceRetryOpts ,
43
+ } ,
44
+ ) ;
45
+ console . log ( `Price ${ price } pushed for ${ brand } ` ) ;
46
+ } ;
47
+
48
+ /**
49
+ *
50
+ * @param {string } brand
51
+ * @returns {Promise<number> }
52
+ */
53
+ export const getPriceFeedRoundId = async brand => {
54
+ const latestRoundPath = `published.priceFeed.${ brand } -USD_price_feed.latestRound` ;
55
+ const latestRound = await getContractInfo ( latestRoundPath , {
56
+ agoric,
57
+ prefix : '' ,
58
+ } ) ;
59
+
60
+ console . log ( latestRoundPath , latestRound ) ;
61
+ return Number ( latestRound . roundId ) ;
62
+ } ;
63
+
64
+ /**
65
+ * Copy from https://github.com/Agoric/agoric-sdk/blob/745f2a82cc94e246f98dd1bd69cb679b608a7170/a3p-integration/proposals/p%3Aupgrade-19/test-lib/psm-lib.js#L277
66
+ *
67
+ * Checking IST balances can be tricky because of the execution fee mentioned in
68
+ * https://github.com/Agoric/agoric-sdk/issues/6525. So we first check for
69
+ * equality, but if that fails we recheck against an assumption that a fee of
70
+ * the default "minFeeDebit" has been charged.
71
+ *
72
+ * @param {import('ava').ExecutionContext } t
73
+ * @param {number } actualBalance
74
+ * @param {number } expectedBalance
75
+ */
76
+ export const tryISTBalances = async ( t , actualBalance , expectedBalance ) => {
77
+ const firstTry = await t . try ( tt => {
78
+ tt . is ( actualBalance , expectedBalance ) ;
79
+ } ) ;
80
+ if ( firstTry . passed ) {
81
+ firstTry . commit ( ) ;
82
+ return ;
83
+ }
84
+
85
+ firstTry . discard ( ) ;
86
+ t . log ( 'tryISTBalances assuming no batched IST fee' , ...firstTry . errors ) ;
87
+ // See golang/cosmos/x/swingset/types/default-params.go
88
+ // and `ChargeBeans` in golang/cosmos/x/swingset/keeper/keeper.go.
89
+ const minFeeDebit = 200_000 ;
90
+ t . is ( actualBalance + minFeeDebit , expectedBalance ) ;
91
+ } ;
0 commit comments