2
2
import type { Observable } from 'https://esm.sh/rxjs@7.8.1' ;
3
3
import type { QueryableStorageEntry } from 'https://deno.land/x/polkadot/api-base/types/index.ts' ;
4
4
import type { AccountData , AccountId , AccountIndex , AccountInfo , Address , Balance , Index } from 'https://deno.land/x/polkadot/types/interfaces/index.ts' ;
5
+ import type { FrameSystemAccountInfo , PalletBalancesAccountData } from 'https://deno.land/x/polkadot/types/lookup.ts' ;
5
6
import type { ITuple } from 'https://deno.land/x/polkadot/types/types/index.ts' ;
6
7
import type { DeriveApi , DeriveBalancesAccount , DeriveBalancesAccountData } from '../types.ts' ;
7
8
@@ -13,7 +14,9 @@ import { memo } from '../util/index.ts';
13
14
14
15
type BalanceResult = [ Balance , Balance , Balance , Balance ] ;
15
16
16
- type Result = [ Index , BalanceResult [ ] ] ;
17
+ type Result = [ Index , BalanceResult [ ] , AccountType ] ;
18
+
19
+ interface AccountType { isFrameAccountData : boolean }
17
20
18
21
type DeriveCustomAccount = DeriveApi [ 'derive' ] & Record < string , {
19
22
customAccount ?: DeriveApi [ 'query' ] [ 'balances' ] [ 'account' ]
@@ -23,24 +26,38 @@ function zeroBalance (api: DeriveApi) {
23
26
return api . registry . createType ( 'Balance' ) ;
24
27
}
25
28
26
- function getBalance ( api : DeriveApi , [ freeBalance , reservedBalance , frozenFee , frozenMisc ] : BalanceResult ) : DeriveBalancesAccountData {
29
+ function getBalance ( api : DeriveApi , [ freeBalance , reservedBalance , frozenFeeOrFrozen , frozenMiscOrFlags ] : BalanceResult , accType : AccountType ) : DeriveBalancesAccountData {
27
30
const votingBalance = api . registry . createType ( 'Balance' , freeBalance . toBn ( ) ) ;
28
31
32
+ if ( accType . isFrameAccountData ) {
33
+ return {
34
+ frameSystemAccountInfo : {
35
+ flags : frozenMiscOrFlags ,
36
+ frozen : frozenFeeOrFrozen
37
+ } ,
38
+ freeBalance,
39
+ frozenFee : api . registry . createType ( 'Balance' , 0 ) ,
40
+ frozenMisc : api . registry . createType ( 'Balance' , 0 ) ,
41
+ reservedBalance,
42
+ votingBalance
43
+ } ;
44
+ }
45
+
29
46
return {
30
47
freeBalance,
31
- frozenFee,
32
- frozenMisc,
48
+ frozenFee : frozenFeeOrFrozen ,
49
+ frozenMisc : frozenMiscOrFlags ,
33
50
reservedBalance,
34
51
votingBalance
35
52
} ;
36
53
}
37
54
38
- function calcBalances ( api : DeriveApi , [ accountId , [ accountNonce , [ primary , ...additional ] ] ] : [ AccountId , Result ] ) : DeriveBalancesAccount {
55
+ function calcBalances ( api : DeriveApi , [ accountId , [ accountNonce , [ primary , ...additional ] , accType ] ] : [ AccountId , Result ] ) : DeriveBalancesAccount {
39
56
return objectSpread ( {
40
57
accountId,
41
58
accountNonce,
42
- additional : additional . map ( ( b ) => getBalance ( api , b ) )
43
- } , getBalance ( api , primary ) ) ;
59
+ additional : additional . map ( ( b ) => getBalance ( api , b , accType ) )
60
+ } , getBalance ( api , primary , accType ) ) ;
44
61
}
45
62
46
63
function queryBalancesFree ( api : DeriveApi , accountId : AccountId ) : Observable < Result > {
@@ -51,15 +68,17 @@ function queryBalancesFree (api: DeriveApi, accountId: AccountId): Observable<Re
51
68
] ) . pipe (
52
69
map ( ( [ freeBalance , reservedBalance , accountNonce ] ) : Result => [
53
70
accountNonce ,
54
- [ [ freeBalance , reservedBalance , zeroBalance ( api ) , zeroBalance ( api ) ] ]
71
+ [ [ freeBalance , reservedBalance , zeroBalance ( api ) , zeroBalance ( api ) ] ] ,
72
+ { isFrameAccountData : false }
55
73
] )
56
74
) ;
57
75
}
58
76
59
77
function queryNonceOnly ( api : DeriveApi , accountId : AccountId ) : Observable < Result > {
60
78
const fill = ( nonce : Index ) : Result => [
61
79
nonce ,
62
- [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ]
80
+ [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ] ,
81
+ { isFrameAccountData : false }
63
82
] ;
64
83
65
84
return isFunction ( api . query . system . account )
@@ -80,7 +99,8 @@ function queryBalancesAccount (api: DeriveApi, accountId: AccountId, modules: st
80
99
81
100
const extract = ( nonce : Index , data : AccountData [ ] ) : Result => [
82
101
nonce ,
83
- data . map ( ( { feeFrozen, free, miscFrozen, reserved } ) : BalanceResult => [ free , reserved , feeFrozen , miscFrozen ] )
102
+ data . map ( ( { feeFrozen, free, miscFrozen, reserved } ) : BalanceResult => [ free , reserved , feeFrozen , miscFrozen ] ) ,
103
+ { isFrameAccountData : false }
84
104
] ;
85
105
86
106
// NOTE this is for the first case where we do have instances specified
@@ -103,7 +123,7 @@ function queryBalancesAccount (api: DeriveApi, accountId: AccountId, modules: st
103
123
104
124
function querySystemAccount ( api : DeriveApi , accountId : AccountId ) : Observable < Result > {
105
125
// AccountInfo is current, support old, eg. Edgeware
106
- return api . query . system . account < AccountInfo | ITuple < [ Index , AccountData ] > > ( accountId ) . pipe (
126
+ return api . query . system . account < AccountInfo | FrameSystemAccountInfo | ITuple < [ Index , AccountData ] > > ( accountId ) . pipe (
107
127
map ( ( infoOrTuple ) : Result => {
108
128
const data = ( infoOrTuple as AccountInfo ) . nonce
109
129
? ( infoOrTuple as AccountInfo ) . data
@@ -114,16 +134,30 @@ function querySystemAccount (api: DeriveApi, accountId: AccountId): Observable<R
114
134
if ( ! data || data . isEmpty ) {
115
135
return [
116
136
nonce ,
117
- [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ]
137
+ [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ] ,
138
+ { isFrameAccountData : false }
118
139
] ;
119
140
}
120
141
121
- const { feeFrozen, free, miscFrozen, reserved } = data ;
142
+ const isFrameType = ! ! ( infoOrTuple as FrameSystemAccountInfo ) . data . frozen ;
143
+
144
+ if ( isFrameType ) {
145
+ const { flags, free, frozen, reserved } = ( data as unknown as PalletBalancesAccountData ) ;
122
146
123
- return [
124
- nonce ,
125
- [ [ free , reserved , feeFrozen , miscFrozen ] ]
126
- ] ;
147
+ return [
148
+ nonce ,
149
+ [ [ free , reserved , frozen , flags ] ] ,
150
+ { isFrameAccountData : true }
151
+ ] ;
152
+ } else {
153
+ const { feeFrozen, free, miscFrozen, reserved } = data ;
154
+
155
+ return [
156
+ nonce ,
157
+ [ [ free , reserved , feeFrozen , miscFrozen ] ] ,
158
+ { isFrameAccountData : false }
159
+ ] ;
160
+ }
127
161
} )
128
162
) ;
129
163
}
@@ -165,7 +199,8 @@ export function account (instanceId: string, api: DeriveApi): (address: AccountI
165
199
] )
166
200
: of ( [ api . registry . createType ( 'AccountId' ) , [
167
201
api . registry . createType ( 'Index' ) ,
168
- [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ]
202
+ [ [ zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) , zeroBalance ( api ) ] ] ,
203
+ { isFrameAccountData : false }
169
204
] ] )
170
205
)
171
206
) ,
0 commit comments