5
5
derivePda ,
6
6
getEvmTokenDecimals ,
7
7
getEvmTotalSupply ,
8
+ normalizeToDecimals ,
8
9
} from '@wormhole-foundation/wormhole-monitor-common' ;
9
10
import { PublicKey } from '@solana/web3.js' ;
10
11
import {
@@ -25,23 +26,6 @@ const cacheBucket = storage.bucket(bucketName);
25
26
const cacheFileName = 'ntt-total-supply-and-locked.json' ;
26
27
const cloudStorageCache = cacheBucket . file ( cacheFileName ) ;
27
28
28
- async function getEvmNormalizedTotalSupply (
29
- network : Network ,
30
- token : string ,
31
- chain : Chain
32
- ) : Promise < number > {
33
- const tokenDecimals = await getEvmTokenDecimals (
34
- rpc . rpcAddress ( network , chain ) ,
35
- NTT_MANAGER_CONTRACT [ network ] [ token ] [ chain ] !
36
- ) ;
37
- const tokenSupply = await getEvmTotalSupply (
38
- rpc . rpcAddress ( network , chain ) ,
39
- NTT_TOKENS [ network ] [ token ] [ chain ] !
40
- ) ;
41
-
42
- return tokenSupply / 10 ** tokenDecimals ;
43
- }
44
-
45
29
async function fetchTotalSupplyAndLocked ( network : Network ) : Promise < NTTTotalSupplyAndLockedData [ ] > {
46
30
const tokens = NTT_MANAGER_CONTRACT [ network ] ;
47
31
const totalSupplyVsLocked : NTTTotalSupplyAndLockedData [ ] = [ ] ;
@@ -51,32 +35,49 @@ async function fetchTotalSupplyAndLocked(network: Network): Promise<NTTTotalSupp
51
35
const programId = new PublicKey ( NTT_MANAGER_CONTRACT [ network ] [ token ] . Solana ! ) ;
52
36
const pda = derivePda ( 'config' , programId ) ;
53
37
const custody = await getCustody ( rpc . rpcAddress ( network , 'Solana' ) , pda . toBase58 ( ) ) ;
54
- const locked = await getCustodyAmount ( rpc . rpcAddress ( network , 'Solana' ) , custody ) ;
38
+ const custodyAmount = await getCustodyAmount ( rpc . rpcAddress ( network , 'Solana' ) , custody ) ;
55
39
56
40
const evmTotalSupply : NTTTotalSupplyAndLockedData [ ] = [ ] ;
57
- let totalSupply = 0 ;
41
+ let cumulativeEvmSupply = 0n ;
58
42
for ( const [ supportedChain ] of Object . entries ( NTT_TOKENS [ network ] [ token ] ) ) {
59
43
if ( supportedChain === 'Solana' ) continue ;
60
- const tokenSupplyNormalized = await getEvmNormalizedTotalSupply (
61
- network ,
62
- token ,
63
- supportedChain as Chain
44
+ const tokenSupply = await getEvmTotalSupply (
45
+ rpc . rpcAddress ( network , supportedChain as Chain ) ,
46
+ NTT_TOKENS [ network ] [ token ] [ supportedChain as Chain ] !
47
+ ) ;
48
+
49
+ const tokenDecimals = await getEvmTokenDecimals (
50
+ rpc . rpcAddress ( network , supportedChain as Chain ) ,
51
+ NTT_MANAGER_CONTRACT [ network ] [ token ] [ supportedChain as Chain ] !
64
52
) ;
65
53
66
54
evmTotalSupply . push ( {
67
55
tokenName : token ,
68
56
chain : chainToChainId ( supportedChain as Chain ) ,
69
- totalSupply : tokenSupplyNormalized ,
57
+ totalSupply : {
58
+ amount : tokenSupply . toString ( ) ,
59
+ decimals : tokenDecimals ,
60
+ } ,
70
61
} ) ;
71
62
72
- totalSupply += tokenSupplyNormalized ;
63
+ // Normalize to 18 decimals so prevent potential different decimals from affecting the total supply
64
+ cumulativeEvmSupply += normalizeToDecimals (
65
+ {
66
+ amount : tokenSupply . toString ( ) ,
67
+ decimals : tokenDecimals ,
68
+ } ,
69
+ 18
70
+ ) ;
73
71
}
74
72
75
73
totalSupplyVsLocked . push ( {
76
74
chain : chainToChainId ( 'Solana' ) ,
77
75
tokenName : token ,
78
- amountLocked : locked ,
79
- totalSupply,
76
+ amountLocked : custodyAmount ,
77
+ totalSupply : {
78
+ amount : cumulativeEvmSupply . toString ( ) ,
79
+ decimals : 18 ,
80
+ } ,
80
81
evmTotalSupply,
81
82
} ) ;
82
83
}
@@ -98,7 +99,6 @@ export async function computeTotalSupplyAndLocked(req: any, res: any) {
98
99
const network = assertEnvironmentVariable ( 'NETWORK' ) as Network ;
99
100
const totalSupplyAndLocked = await fetchTotalSupplyAndLocked ( network ) ;
100
101
await cloudStorageCache . save ( JSON . stringify ( totalSupplyAndLocked ) ) ;
101
-
102
102
res . status ( 200 ) . send ( 'Total supply and locked saved' ) ;
103
103
} catch ( e ) {
104
104
console . error ( e ) ;
0 commit comments