1
1
import axios from "axios" ;
2
+ import { ErrContractQuery , ErrNetworkProvider } from "../errors" ;
2
3
import { AccountOnNetwork , GuardianData } from "./accounts" ;
3
4
import { defaultAxiosConfig } from "./config" ;
4
- import { EsdtContractAddress , BaseUserAgent } from "./constants" ;
5
+ import { BaseUserAgent , EsdtContractAddress } from "./constants" ;
5
6
import { ContractQueryRequest } from "./contractQueryRequest" ;
6
7
import { ContractQueryResponse } from "./contractQueryResponse" ;
7
- import { ErrContractQuery , ErrNetworkProvider } from "./errors" ;
8
8
import { IAddress , IContractQuery , INetworkProvider , IPagination , ITransaction , ITransactionNext } from "./interface" ;
9
9
import { NetworkConfig } from "./networkConfig" ;
10
10
import { NetworkGeneralStatistics } from "./networkGeneralStatistics" ;
11
+ import { NetworkProviderConfig } from "./networkProviderConfig" ;
11
12
import { NetworkStake } from "./networkStake" ;
12
13
import { NetworkStatus } from "./networkStatus" ;
13
14
import { DefinitionOfFungibleTokenOnNetwork , DefinitionOfTokenCollectionOnNetwork } from "./tokenDefinitions" ;
14
15
import { FungibleTokenOfAccountOnNetwork , NonFungibleTokenOfAccountOnNetwork } from "./tokens" ;
15
16
import { TransactionOnNetwork , prepareTransactionForBroadcasting } from "./transactions" ;
16
17
import { TransactionStatus } from "./transactionStatus" ;
17
18
import { extendUserAgent } from "./userAgent" ;
18
- import { NetworkProviderConfig } from "./networkProviderConfig" ;
19
19
20
20
// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
21
21
export class ProxyNetworkProvider implements INetworkProvider {
22
22
private url : string ;
23
23
private config : NetworkProviderConfig ;
24
- private userAgentPrefix = `${ BaseUserAgent } /proxy`
24
+ private userAgentPrefix = `${ BaseUserAgent } /proxy` ;
25
25
26
26
constructor ( url : string , config ?: NetworkProviderConfig ) {
27
27
this . url = url ;
@@ -65,40 +65,57 @@ export class ProxyNetworkProvider implements INetworkProvider {
65
65
return accountGuardian ;
66
66
}
67
67
68
- async getFungibleTokensOfAccount ( address : IAddress , _pagination ?: IPagination ) : Promise < FungibleTokenOfAccountOnNetwork [ ] > {
68
+ async getFungibleTokensOfAccount (
69
+ address : IAddress ,
70
+ _pagination ?: IPagination ,
71
+ ) : Promise < FungibleTokenOfAccountOnNetwork [ ] > {
69
72
let url = `address/${ address . bech32 ( ) } /esdt` ;
70
73
let response = await this . doGetGeneric ( url ) ;
71
74
let responseItems : any [ ] = Object . values ( response . esdts ) ;
72
75
// Skip NFTs / SFTs.
73
- let responseItemsFiltered = responseItems . filter ( item => ! item . nonce ) ;
74
- let tokens = responseItemsFiltered . map ( item => FungibleTokenOfAccountOnNetwork . fromHttpResponse ( item ) ) ;
76
+ let responseItemsFiltered = responseItems . filter ( ( item ) => ! item . nonce ) ;
77
+ let tokens = responseItemsFiltered . map ( ( item ) => FungibleTokenOfAccountOnNetwork . fromHttpResponse ( item ) ) ;
75
78
76
79
// TODO: Fix sorting
77
80
tokens . sort ( ( a , b ) => a . identifier . localeCompare ( b . identifier ) ) ;
78
81
return tokens ;
79
82
}
80
83
81
- async getNonFungibleTokensOfAccount ( address : IAddress , _pagination ?: IPagination ) : Promise < NonFungibleTokenOfAccountOnNetwork [ ] > {
84
+ async getNonFungibleTokensOfAccount (
85
+ address : IAddress ,
86
+ _pagination ?: IPagination ,
87
+ ) : Promise < NonFungibleTokenOfAccountOnNetwork [ ] > {
82
88
let url = `address/${ address . bech32 ( ) } /esdt` ;
83
89
let response = await this . doGetGeneric ( url ) ;
84
90
let responseItems : any [ ] = Object . values ( response . esdts ) ;
85
91
// Skip fungible tokens.
86
- let responseItemsFiltered = responseItems . filter ( item => item . nonce >= 0 ) ;
87
- let tokens = responseItemsFiltered . map ( item => NonFungibleTokenOfAccountOnNetwork . fromProxyHttpResponse ( item ) ) ;
92
+ let responseItemsFiltered = responseItems . filter ( ( item ) => item . nonce >= 0 ) ;
93
+ let tokens = responseItemsFiltered . map ( ( item ) =>
94
+ NonFungibleTokenOfAccountOnNetwork . fromProxyHttpResponse ( item ) ,
95
+ ) ;
88
96
89
97
// TODO: Fix sorting
90
98
tokens . sort ( ( a , b ) => a . identifier . localeCompare ( b . identifier ) ) ;
91
99
return tokens ;
92
100
}
93
101
94
- async getFungibleTokenOfAccount ( address : IAddress , tokenIdentifier : string ) : Promise < FungibleTokenOfAccountOnNetwork > {
102
+ async getFungibleTokenOfAccount (
103
+ address : IAddress ,
104
+ tokenIdentifier : string ,
105
+ ) : Promise < FungibleTokenOfAccountOnNetwork > {
95
106
let response = await this . doGetGeneric ( `address/${ address . bech32 ( ) } /esdt/${ tokenIdentifier } ` ) ;
96
107
let tokenData = FungibleTokenOfAccountOnNetwork . fromHttpResponse ( response . tokenData ) ;
97
108
return tokenData ;
98
109
}
99
110
100
- async getNonFungibleTokenOfAccount ( address : IAddress , collection : string , nonce : number ) : Promise < NonFungibleTokenOfAccountOnNetwork > {
101
- let response = await this . doGetGeneric ( `address/${ address . bech32 ( ) } /nft/${ collection } /nonce/${ nonce . valueOf ( ) } ` ) ;
111
+ async getNonFungibleTokenOfAccount (
112
+ address : IAddress ,
113
+ collection : string ,
114
+ nonce : number ,
115
+ ) : Promise < NonFungibleTokenOfAccountOnNetwork > {
116
+ let response = await this . doGetGeneric (
117
+ `address/${ address . bech32 ( ) } /nft/${ collection } /nonce/${ nonce . valueOf ( ) } ` ,
118
+ ) ;
102
119
let tokenData = NonFungibleTokenOfAccountOnNetwork . fromProxyHttpResponseByNonce ( response . tokenData ) ;
103
120
return tokenData ;
104
121
}
@@ -133,7 +150,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
133
150
}
134
151
135
152
async sendTransactions ( txs : ( ITransaction | ITransactionNext ) [ ] ) : Promise < string [ ] > {
136
- const data = ( txs ) . map ( ( tx ) => prepareTransactionForBroadcasting ( tx ) ) ;
153
+ const data = txs . map ( ( tx ) => prepareTransactionForBroadcasting ( tx ) ) ;
137
154
138
155
const response = await this . doPostGeneric ( "transaction/send-multiple" , data ) ;
139
156
const hashes = Array ( txs . length ) . fill ( null ) ;
@@ -163,7 +180,10 @@ export class ProxyNetworkProvider implements INetworkProvider {
163
180
164
181
async getDefinitionOfFungibleToken ( tokenIdentifier : string ) : Promise < DefinitionOfFungibleTokenOnNetwork > {
165
182
let properties = await this . getTokenProperties ( tokenIdentifier ) ;
166
- let definition = DefinitionOfFungibleTokenOnNetwork . fromResponseOfGetTokenProperties ( tokenIdentifier , properties ) ;
183
+ let definition = DefinitionOfFungibleTokenOnNetwork . fromResponseOfGetTokenProperties (
184
+ tokenIdentifier ,
185
+ properties ,
186
+ ) ;
167
187
return definition ;
168
188
}
169
189
@@ -173,7 +193,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
173
193
let queryResponse = await this . queryContract ( {
174
194
address : EsdtContractAddress ,
175
195
func : "getTokenProperties" ,
176
- getEncodedArguments : ( ) => [ encodedIdentifier ]
196
+ getEncodedArguments : ( ) => [ encodedIdentifier ] ,
177
197
} ) ;
178
198
179
199
let properties = queryResponse . getReturnDataParts ( ) ;
0 commit comments