Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring "src" of "sdk-network-providers" (with history) #484

Merged
merged 184 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
184 commits
Select commit Hold shift + click to select a range
cdc8d55
Sketch interface INetworkProvider, proxy implementation.
andreibancioiu Feb 21, 2022
cbe64af
Sketch implementation.
andreibancioiu Feb 21, 2022
cc9324b
WIP.
andreibancioiu Feb 21, 2022
807fa8c
WIP. Tests.
andreibancioiu Feb 21, 2022
4ab793e
WIP.
andreibancioiu Feb 22, 2022
a9b7f6d
WIP.
andreibancioiu Feb 22, 2022
8baba73
Simplifications.
andreibancioiu Feb 22, 2022
1b97a67
WIP.
andreibancioiu Feb 22, 2022
3976cff
Contract queries. WIP.
andreibancioiu Feb 23, 2022
281eafb
Logs, results, refactor, WIP.
andreibancioiu Feb 23, 2022
97581a8
SCRs, tests.
andreibancioiu Feb 23, 2022
4958a0c
Fix after self-review.
andreibancioiu Feb 23, 2022
86480d6
Fix after self-review.
andreibancioiu Feb 23, 2022
66fee80
Fix after review.
andreibancioiu Feb 24, 2022
790a352
Fix remaining imports.
andreibancioiu Mar 14, 2022
41084c6
Refactor contract results (breaking change).
andreibancioiu Mar 20, 2022
be7ba70
Merge pull request #159 from ElrondNetwork/better-interactions
andreibancioiu Mar 24, 2022
9b645b6
Preparatory refactoring.
andreibancioiu Apr 5, 2022
49f2cdc
Fix circular dependency.
andreibancioiu Apr 5, 2022
6da4cfc
Move interfaces to where they are used.
andreibancioiu Apr 5, 2022
5e24e20
Make ./networkProvider less dependent on outside definitions.
andreibancioiu Apr 5, 2022
3c57234
Prepare extraction of networkProvider. WIP.
andreibancioiu Apr 5, 2022
3041339
Continue decoupling of package networkProvider.
andreibancioiu Apr 5, 2022
f6cbf03
Continue decoupling.
andreibancioiu Apr 5, 2022
2aa4f2c
Continue decoupling.
andreibancioiu Apr 5, 2022
52dfa31
Fix after self review.
andreibancioiu Apr 5, 2022
f1efc27
Fixes (upon running the tests).
andreibancioiu Apr 6, 2022
68c2946
Tests and fixes.
andreibancioiu Apr 6, 2022
dd13690
Remove deprecated status (fix after review).
andreibancioiu Apr 6, 2022
12231f0
Merge pull request #180 from ElrondNetwork/tx-on-network
andreibancioiu Apr 6, 2022
8e8032c
Merge branch 'main' into account-on-network
andreibancioiu Apr 6, 2022
22640f3
Move "accountOnNetwork" to "networkProvider". Further decoupling.
andreibancioiu Apr 6, 2022
d647294
Move networkStatus, networkStake to networkProvider package.
andreibancioiu Apr 6, 2022
e119bd2
Merge pull request #181 from ElrondNetwork/account-on-network
andreibancioiu Apr 6, 2022
8f6327e
Merge branch 'main' into network-obj
andreibancioiu Apr 6, 2022
9622091
Move "stats" to networkProvider.
andreibancioiu Apr 6, 2022
5edfba1
Move "networkConfig" to networkProvider.
andreibancioiu Apr 6, 2022
97793ed
Merge pull request #182 from ElrondNetwork/network-obj
andreibancioiu Apr 6, 2022
10945a9
Move "queryResponse" to "networkProvider".
andreibancioiu Apr 6, 2022
65e4970
Fix after review.
andreibancioiu Apr 6, 2022
2c51b24
Merge pull request #184 from ElrondNetwork/query-response
andreibancioiu Apr 6, 2022
3a3bbfe
Move old providers to networkProvider package.
andreibancioiu Apr 6, 2022
97394cd
Adjust implementation of old providers.
andreibancioiu Apr 7, 2022
1a49eb6
Rename files.
andreibancioiu Apr 7, 2022
c451acf
Remove deprecated API provider.
andreibancioiu Apr 7, 2022
669dbe5
Add missing fields on NFT token.
andreibancioiu Apr 7, 2022
a66f6a0
Move deprecated proxy provider to contractWrappers (it depends on it).
andreibancioiu Apr 7, 2022
0b964bb
Remove some interfaces.
andreibancioiu Apr 7, 2022
bb34980
More specific error for vm queries (as before).
andreibancioiu Apr 7, 2022
82c0bdc
Merge pull request #185 from ElrondNetwork/move-old-providers
andreibancioiu Apr 7, 2022
3944eaa
Move files one-level up.
andreibancioiu Apr 7, 2022
0344d43
Fix / improve tests.
andreibancioiu Apr 7, 2022
7c8bc18
Linting.
andreibancioiu Apr 7, 2022
ec43045
Merge pull request #1 from ElrondNetwork/make-it-compile
andreibancioiu Apr 7, 2022
528c863
Add factory of network providers.
andreibancioiu Apr 7, 2022
be398c3
Merge pull request #2 from ElrondNetwork/add-factory
andreibancioiu Apr 7, 2022
dbca42e
Add IPagination interface, instead of Pagination.
andreibancioiu Apr 7, 2022
c172f07
Removed transactionOnNetwork.getAllEvents().
andreibancioiu Apr 7, 2022
b34ebfb
Fix interfaces.
andreibancioiu Apr 7, 2022
2718f8f
Fix exports.
andreibancioiu Apr 7, 2022
d653546
Fix after extraction: TransactionEvent.
andreibancioiu Apr 8, 2022
e0ea5f6
Fix exports.
andreibancioiu Apr 8, 2022
019b9a5
Merge pull request #7 from ElrondNetwork/fixes-upon-extraction
andreibancioiu Apr 8, 2022
4c2212e
Fix DTOs.
andreibancioiu Apr 8, 2022
d8fac6f
Handle completion: API vs. Proxy.
andreibancioiu Apr 8, 2022
bcd8fbc
Simplify.
andreibancioiu Apr 8, 2022
5cef3fe
Cleanup.
andreibancioiu Apr 8, 2022
b99a17f
Merge pull request #9 from ElrondNetwork/handle-completion
andreibancioiu Apr 8, 2022
1fb0dcf
Formatting etc.
andreibancioiu Apr 9, 2022
f13e771
Fix exports.
andreibancioiu Apr 9, 2022
8004195
Simplify interfaces etc.
andreibancioiu Apr 9, 2022
a311e1c
Adjust config.
andreibancioiu Apr 9, 2022
e2dc631
Simplification (string instead of AccountBalance).
andreibancioiu Apr 9, 2022
9a41acf
Further simplification (transaction payload).
andreibancioiu Apr 10, 2022
4b69eed
Simplification (contract return code is now a string).
andreibancioiu Apr 10, 2022
9e1f86c
Bring query.toHttpRequest() logic in network-providers.
andreibancioiu Apr 10, 2022
7768419
Simplification: string instead of IHash.
andreibancioiu Apr 10, 2022
cccfe8e
Merge pull request #8 from ElrondNetwork/fixes-upon-extraction
andreibancioiu Apr 11, 2022
deed7aa
Merge branch 'main' into improvements-04-09
andreibancioiu Apr 11, 2022
b9c400c
Merge pull request #10 from ElrondNetwork/improvements-04-09
andreibancioiu Apr 11, 2022
a6d1310
Account.balance as bigNumber.
andreibancioiu Apr 12, 2022
2cc0af3
Rename class (Bech32Address to Address).
andreibancioiu Apr 12, 2022
86aa807
Implement ProxyNetworkProvider.getDefinitionOfFungibleToken().
andreibancioiu Apr 12, 2022
9740713
Implement ProxyNetworkProvider.getDefinitionOfTokenCollection().
andreibancioiu Apr 12, 2022
64634c9
Update src/accounts.ts
andreibancioiu Apr 13, 2022
07962a1
Fix after review.
andreibancioiu Apr 13, 2022
75e6eea
Merge pull request #11 from ElrondNetwork/improvements-04-12
andreibancioiu Apr 13, 2022
1092457
Less console.debug(). Update readme.
andreibancioiu Apr 18, 2022
a69489d
Merge pull request #13 from ElrondNetwork/less-console-debug
andreibancioiu Apr 18, 2022
36583fe
Sketch fix for assets.
andreibancioiu Apr 29, 2022
e35cf0c
DefinitionOfTokenCollectionOnNetwork update
schimih Jun 14, 2022
622fd51
fix
schimih Jun 14, 2022
041788b
Fix (remove) logs, bump version.
andreibancioiu Jul 1, 2022
f1f5cf9
Merge pull request #18 from ElrondNetwork/fix-logs
andreibancioiu Jul 1, 2022
98b8b07
Increase default axios timeout.
andreibancioiu Jul 18, 2022
bf2b16e
Merge pull request #20 from ElrondNetwork/increase-default-timeout
andreibancioiu Jul 19, 2022
2ff1f4b
add more infos for tokens api andpoints
ccorcoveanu Aug 4, 2022
425155a
property rename - fix after review
ccorcoveanu Aug 9, 2022
4c4499b
Merge pull request #22 from ElrondNetwork/api-endpoints
ccorcoveanu Aug 9, 2022
0fc73e5
pairs endpoint implementation
ccorcoveanu Aug 16, 2022
fbc06ec
reverted test
ccorcoveanu Aug 19, 2022
9572232
added new method on the interface
ccorcoveanu Aug 19, 2022
a23e7d9
removed method from interface
ccorcoveanu Aug 19, 2022
0607fae
removed importr
ccorcoveanu Aug 19, 2022
4c6f161
removed importr
ccorcoveanu Aug 19, 2022
084d4e6
Merge pull request #23 from ElrondNetwork/add-pairs-endpoint
ccorcoveanu Aug 19, 2022
9bce67a
pass headers when performing post api calls
tanghel Sep 15, 2022
b175e05
Merge pull request #26 from ElrondNetwork/do-post-headers-fixes
andreibancioiu Sep 15, 2022
d897dda
Add address.toString().
andreibancioiu Oct 18, 2022
df23351
Fix after review.
andreibancioiu Oct 19, 2022
39e29b5
Merge pull request #27 from ElrondNetwork/address-to-string-18
andreibancioiu Oct 19, 2022
4e0a57a
Add proxy method to send transaction in bulk
NiftyWell Nov 7, 2022
1630913
fix sendBulkTransaction api
NiftyWell Nov 7, 2022
add11e3
Fix axios deserialization, ignore "_constructor".
andreibancioiu Nov 14, 2022
92cd4a8
Merge pull request #29 from ElrondNetwork/fix-axios-deserialize
andreibancioiu Nov 14, 2022
86a0496
Merge branch 'main' into tokens-update
schimih Nov 18, 2022
a39cd15
Merge pull request #28 from NiftyWell/main
andreibancioiu Dec 18, 2022
7fb3910
Fix after community PR (send transactions in bulk).
andreibancioiu Dec 19, 2022
9de774e
Unit tests, fix response.
andreibancioiu Dec 19, 2022
07ec29e
Merge pull request #16 from ElrondNetwork/tokens-update
andreibancioiu Dec 19, 2022
4b6fe24
Merge branch 'main' into token-assets
andreibancioiu Dec 19, 2022
a11a24d
Fix comment, formatting etc.
andreibancioiu Dec 19, 2022
e356ceb
Merge pull request #14 from ElrondNetwork/token-assets
andreibancioiu Dec 19, 2022
f16b9c3
Merge pull request #31 from ElrondNetwork/external-12-17
andreibancioiu Dec 20, 2022
5ba0131
Rename Elrond to MultiversX.
andreibancioiu Jan 6, 2023
7eaff97
Merge pull request #34 from multiversx/rebranding-01
andreibancioiu Jan 9, 2023
9429911
Rename erdjs to sdk-js.
andreibancioiu Jan 10, 2023
1e455ea
Merge pull request #35 from multiversx/rebranding-02
andreibancioiu Jan 10, 2023
7e1f47f
Rename package: sdk-network-providers.
andreibancioiu Jan 11, 2023
09faec2
Merge pull request #36 from multiversx/rebranding-03
andreibancioiu Jan 11, 2023
8c92cff
Redo PR 33 (error handling).
andreibancioiu Jan 23, 2023
9bd01f8
Merge pull request #37 from multiversx/redo-33
andreibancioiu Jan 23, 2023
33c5ebc
Implement getGuardianData().
andreibancioiu Apr 13, 2023
930f248
Rename param.
andreibancioiu Apr 13, 2023
af389df
Add "getCurrentGuardianAddress".
andreibancioiu Apr 13, 2023
769819c
Merge pull request #39 from multiversx/get-guardian-data
andreibancioiu Apr 18, 2023
4ba9d93
add `TransactionEventData` class
popenta Jun 26, 2023
b61dd61
fix
popenta Jun 26, 2023
88e409d
rename variable
popenta Jun 26, 2023
550244e
Merge pull request #40 from multiversx/tx-event-data
popenta Jun 26, 2023
f1076f9
new tx status for proxy
popenta Jul 11, 2023
a080032
started to fix tests
popenta Jul 11, 2023
275e180
change variable names to camel case
popenta Jul 11, 2023
66d12e7
Merge branch 'process-status' into default-nft-balance
popenta Jul 11, 2023
b95ca00
fix tests
popenta Jul 11, 2023
a2c07bc
fix after review
popenta Jul 12, 2023
ad3180f
Merge pull request #41 from multiversx/process-status
popenta Jul 12, 2023
539eea6
Merge branch 'feat/proxy-tx-status' into default-nft-balance
popenta Jul 12, 2023
a9e35e4
small fixes & tests refactoring
popenta Jul 12, 2023
94ee687
update test
popenta Jul 12, 2023
19166b4
update test
popenta Jul 12, 2023
996426d
fixes after review
popenta Jul 13, 2023
4b771ad
Merge pull request #42 from multiversx/default-nft-balance
popenta Jul 13, 2023
809494c
fix test
popenta Aug 23, 2023
f029b7f
Merge pull request #44 from multiversx/fix-network-status-test
popenta Aug 23, 2023
e5df06c
Merge pull request #43 from multiversx/feat/proxy-tx-status
popenta Aug 24, 2023
2a18abd
For Sirius: handle logs.events.additionalData.
andreibancioiu Oct 31, 2023
46f3afc
Fix tests.
andreibancioiu Nov 9, 2023
04517d1
Fix tests.
andreibancioiu Nov 9, 2023
0001c39
Merge pull request #49 from multiversx/logs-additional-data-sirius
andreibancioiu Nov 9, 2023
a0393e3
Fix integration test.
andreibancioiu Jan 9, 2024
302dae1
Merge pull request #54 from multiversx/audit-fix-axios
andreibancioiu Jan 10, 2024
bf426bb
add suport for TransactionNext
popenta Feb 16, 2024
ab6f64a
fixes after review
popenta Feb 19, 2024
9ccbae5
Merge pull request #55 from multiversx/add-support-for-tx-next
popenta Feb 20, 2024
fc9cd7e
Add TransactionOnNetwork.function.
andreibancioiu Mar 14, 2024
da92935
Ignore some differences.
andreibancioiu Mar 20, 2024
9fb5a20
Merge pull request #58 from multiversx/function-14
andreibancioiu Mar 21, 2024
445bd03
Export "TransactionEventData".
andreibancioiu Mar 22, 2024
1a604a6
Merge pull request #59 from multiversx/fix-import-22
andreibancioiu Mar 22, 2024
e08524e
Fix tests and update dependency
danielailie Jul 31, 2024
6a09e66
Add empty line
danielailie Jul 31, 2024
4ccd63b
remove only from tests
danielailie Jul 31, 2024
410a9a2
Merge pull request #64 from multiversx/TOOL-216-fix-mapping-for-mappi…
danielailie Jul 31, 2024
3c06d22
Add user agent info
danielailie Aug 26, 2024
a52a34b
Extract duplicated code and extend config
danielailie Aug 27, 2024
1a18343
refactoring, fix typos, remaning
danielailie Aug 27, 2024
25d250a
Add tests
danielailie Aug 27, 2024
8b46cd3
Small renaming
danielailie Aug 27, 2024
d4d1385
Fix tests
danielailie Aug 28, 2024
cbd0611
Merge pull request #67 from multiversx/TOOL-233-extend-user-agent-on-…
danielailie Aug 28, 2024
a3014a7
Fix constructor of ApiNetworkProvider, when config is not provided.
andreibancioiu Aug 30, 2024
6587282
Merge pull request #69 from multiversx/fix-missing-config
andreibancioiu Aug 30, 2024
fd5be7c
Merge remote-tracking branch 'network-providers/main' into merge-netw…
andreibancioiu Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src-network-providers/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/testscommon/**
80 changes: 80 additions & 0 deletions src-network-providers/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import BigNumber from "bignumber.js";
import { IAddress } from "./interface";
import { Address } from "./primitives";

/**
* A plain view of an account, as queried from the Network.
*/
export class AccountOnNetwork {
address: IAddress = new Address("");
nonce: number = 0;
balance: BigNumber = new BigNumber(0);
code: string = "";
userName: string = "";

constructor(init?: Partial<AccountOnNetwork>) {
Object.assign(this, init);
}

static fromHttpResponse(payload: any): AccountOnNetwork {
let result = new AccountOnNetwork();

result.address = new Address(payload["address"] || "");
result.nonce = Number(payload["nonce"] || 0);
result.balance = new BigNumber(payload["balance"] || 0);
result.code = payload["code"] || "";
result.userName = payload["username"] || "";

return result;
}
}

export class GuardianData {
guarded: boolean = false;
activeGuardian?: Guardian;
pendingGuardian?: Guardian;

constructor(init?: Partial<GuardianData>) {
Object.assign(this, init);
}

static fromHttpResponse(response: any): GuardianData {
const result = new GuardianData();

result.guarded = response["guarded"] || false;

if (response["activeGuardian"]) {
result.activeGuardian = Guardian.fromHttpResponse(response["activeGuardian"]);
}

if (response["pendingGuardian"]) {
result.pendingGuardian = Guardian.fromHttpResponse(response["pendingGuardian"]);
}

return result;
}

getCurrentGuardianAddress(): IAddress | undefined {
if (!this.guarded) {
return undefined;
}

return this.activeGuardian?.address;
}
}

class Guardian {
activationEpoch: number = 0;
address: IAddress = new Address("");
serviceUID: string = "";

static fromHttpResponse(responsePart: any): Guardian {
const result = new Guardian();

result.activationEpoch = Number(responsePart["activationEpoch"] || 0);
result.address = new Address(responsePart["address"] || "");
result.serviceUID = responsePart["serviceUID"] || "";

return result;
}
}
229 changes: 229 additions & 0 deletions src-network-providers/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import axios from "axios";
import { AccountOnNetwork, GuardianData } from "./accounts";
import { defaultAxiosConfig, defaultPagination } from "./config";
import { BaseUserAgent } from "./constants";
import { ContractQueryRequest } from "./contractQueryRequest";
import { ContractQueryResponse } from "./contractQueryResponse";
import { ErrContractQuery, ErrNetworkProvider } from "./errors";
import { IAddress, IContractQuery, INetworkProvider, IPagination, ITransaction, ITransactionNext } from "./interface";
import { NetworkConfig } from "./networkConfig";
import { NetworkGeneralStatistics } from "./networkGeneralStatistics";
import { NetworkProviderConfig } from "./networkProviderConfig";
import { NetworkStake } from "./networkStake";
import { NetworkStatus } from "./networkStatus";
import { PairOnNetwork } from "./pairs";
import { Nonce } from "./primitives";
import { ProxyNetworkProvider } from "./proxyNetworkProvider";
import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwork } from "./tokenDefinitions";
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens";
import { TransactionOnNetwork, prepareTransactionForBroadcasting } from "./transactions";
import { TransactionStatus } from "./transactionStatus";
import { extendUserAgent } from "./userAgent";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ApiNetworkProvider implements INetworkProvider {
private url: string;
private config: NetworkProviderConfig;
private backingProxyNetworkProvider;
private userAgentPrefix = `${BaseUserAgent}/api`

constructor(url: string, config?: NetworkProviderConfig) {
this.url = url;
let proxyConfig = this.getProxyConfig(config);
this.config = { ...defaultAxiosConfig, ...config };
this.backingProxyNetworkProvider = new ProxyNetworkProvider(url, proxyConfig);
extendUserAgent(this.userAgentPrefix, this.config);
}

private getProxyConfig(config: NetworkProviderConfig | undefined) {
let proxyConfig = JSON.parse(JSON.stringify(config || {}));
proxyConfig = { ...defaultAxiosConfig, ...proxyConfig };
return proxyConfig;
}

async getNetworkConfig(): Promise<NetworkConfig> {
return await this.backingProxyNetworkProvider.getNetworkConfig();
}

async getNetworkStatus(): Promise<NetworkStatus> {
return await this.backingProxyNetworkProvider.getNetworkStatus();
}

async getNetworkStakeStatistics(): Promise<NetworkStake> {
let response = await this.doGetGeneric("stake");
let networkStake = NetworkStake.fromHttpResponse(response);
return networkStake;
}

async getNetworkGeneralStatistics(): Promise<NetworkGeneralStatistics> {
let response = await this.doGetGeneric("stats");
let stats = NetworkGeneralStatistics.fromHttpResponse(response);
return stats;
}

async getAccount(address: IAddress): Promise<AccountOnNetwork> {
let response = await this.doGetGeneric(`accounts/${address.bech32()}`);
let account = AccountOnNetwork.fromHttpResponse(response);
return account;
}

async getGuardianData(address: IAddress): Promise<GuardianData> {
return await this.backingProxyNetworkProvider.getGuardianData(address);
}

async getFungibleTokensOfAccount(address: IAddress, pagination?: IPagination): Promise<FungibleTokenOfAccountOnNetwork[]> {
pagination = pagination || defaultPagination;

let url = `accounts/${address.bech32()}/tokens?${this.buildPaginationParams(pagination)}`;
let response: any[] = await this.doGetGeneric(url);
let tokens = response.map(item => FungibleTokenOfAccountOnNetwork.fromHttpResponse(item));

// TODO: Fix sorting
tokens.sort((a, b) => a.identifier.localeCompare(b.identifier));
return tokens;
}

async getNonFungibleTokensOfAccount(address: IAddress, pagination?: IPagination): Promise<NonFungibleTokenOfAccountOnNetwork[]> {
pagination = pagination || defaultPagination;

let url = `accounts/${address.bech32()}/nfts?${this.buildPaginationParams(pagination)}`;
let response: any[] = await this.doGetGeneric(url);
let tokens = response.map(item => NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(item));

// TODO: Fix sorting
tokens.sort((a, b) => a.identifier.localeCompare(b.identifier));
return tokens;
}

async getFungibleTokenOfAccount(address: IAddress, tokenIdentifier: string): Promise<FungibleTokenOfAccountOnNetwork> {
let response = await this.doGetGeneric(`accounts/${address.bech32()}/tokens/${tokenIdentifier}`);
let tokenData = FungibleTokenOfAccountOnNetwork.fromHttpResponse(response);
return tokenData;
}

async getNonFungibleTokenOfAccount(address: IAddress, collection: string, nonce: number): Promise<NonFungibleTokenOfAccountOnNetwork> {
let nonceAsHex = new Nonce(nonce).hex();
let response = await this.doGetGeneric(`accounts/${address.bech32()}/nfts/${collection}-${nonceAsHex}`);
let tokenData = NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(response);
return tokenData;
}

async getMexPairs(pagination?: IPagination): Promise<PairOnNetwork[]> {
let url = `mex/pairs`;
if (pagination) {
url = `${url}?from=${pagination.from}&size=${pagination.size}`;
}

let response: any[] = await this.doGetGeneric(url);

return response.map(item => PairOnNetwork.fromApiHttpResponse(item));
}

async getTransaction(txHash: string): Promise<TransactionOnNetwork> {
let response = await this.doGetGeneric(`transactions/${txHash}`);
let transaction = TransactionOnNetwork.fromApiHttpResponse(txHash, response);
return transaction;
}

async getTransactionStatus(txHash: string): Promise<TransactionStatus> {
let response = await this.doGetGeneric(`transactions/${txHash}?fields=status`);
let status = new TransactionStatus(response.status);
return status;
}

async sendTransaction(tx: ITransaction | ITransactionNext): Promise<string> {
const transaction = prepareTransactionForBroadcasting(tx);
const response = await this.doPostGeneric("transactions", transaction);
return response.txHash;
}

async sendTransactions(txs: (ITransaction | ITransactionNext)[]): Promise<string[]> {
return await this.backingProxyNetworkProvider.sendTransactions(txs);
}

async simulateTransaction(tx: ITransaction | ITransactionNext): Promise<any> {
return await this.backingProxyNetworkProvider.simulateTransaction(tx);
}

async queryContract(query: IContractQuery): Promise<ContractQueryResponse> {
try {
let request = new ContractQueryRequest(query).toHttpRequest();
let response = await this.doPostGeneric("query", request);
return ContractQueryResponse.fromHttpResponse(response);
} catch (error: any) {
throw new ErrContractQuery(error);
}
}

async getDefinitionOfFungibleToken(tokenIdentifier: string): Promise<DefinitionOfFungibleTokenOnNetwork> {
let response = await this.doGetGeneric(`tokens/${tokenIdentifier}`);
let definition = DefinitionOfFungibleTokenOnNetwork.fromApiHttpResponse(response);
return definition;
}

async getDefinitionOfTokenCollection(collection: string): Promise<DefinitionOfTokenCollectionOnNetwork> {
let response = await this.doGetGeneric(`collections/${collection}`);
let definition = DefinitionOfTokenCollectionOnNetwork.fromApiHttpResponse(response);
return definition;
}

async getNonFungibleToken(collection: string, nonce: number): Promise<NonFungibleTokenOfAccountOnNetwork> {
let nonceAsHex = new Nonce(nonce).hex();
let response = await this.doGetGeneric(`nfts/${collection}-${nonceAsHex}`);
let token = NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(response);
return token;
}

async doGetGeneric(resourceUrl: string): Promise<any> {
let response = await this.doGet(resourceUrl);
return response;
}

async doPostGeneric(resourceUrl: string, payload: any): Promise<any> {
let response = await this.doPost(resourceUrl, payload);
return response;
}

private buildPaginationParams(pagination: IPagination) {
return `from=${pagination.from}&size=${pagination.size}`;
}

private async doGet(resourceUrl: string): Promise<any> {
let url = `${this.url}/${resourceUrl}`;

try {
let response = await axios.get(url, this.config);
return response.data;
} catch (error) {
this.handleApiError(error, resourceUrl);
}
}

private async doPost(resourceUrl: string, payload: any): Promise<any> {
let url = `${this.url}/${resourceUrl}`;

try {
let response = await axios.post(url, payload, {
...this.config,
headers: {
"Content-Type": "application/json",
...this.config.headers,
},
});
let responsePayload = response.data;
return responsePayload;
} catch (error) {
this.handleApiError(error, resourceUrl);
}
}

private handleApiError(error: any, resourceUrl: string) {
if (!error.response) {
throw new ErrNetworkProvider(resourceUrl, error.toString(), error);
}

const errorData = error.response.data;
const originalErrorMessage = errorData.message || errorData.error || JSON.stringify(errorData);
throw new ErrNetworkProvider(resourceUrl, originalErrorMessage, error);
}
}
18 changes: 18 additions & 0 deletions src-network-providers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IPagination } from "./interface";

const JSONbig = require("json-bigint")({ constructorAction: 'ignore' });

export const defaultAxiosConfig = {
timeout: 5000,
// See: https://github.com/axios/axios/issues/983 regarding transformResponse
transformResponse: [
function (data: any) {
return JSONbig.parse(data);
}
]
};

export const defaultPagination: IPagination = {
from: 0,
size: 100
};
7 changes: 7 additions & 0 deletions src-network-providers/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BigNumber from "bignumber.js";
import { Address } from "./primitives";

export const MaxUint64AsBigNumber = new BigNumber("18446744073709551615");
export const EsdtContractAddress = new Address("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u");
export const BaseUserAgent = "multiversx-sdk"
export const UnknownClientName = "unknown"
21 changes: 21 additions & 0 deletions src-network-providers/contractQueryRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IContractQuery } from "./interface";

export class ContractQueryRequest {
private readonly query: IContractQuery;

constructor(query: IContractQuery) {
this.query = query;
}

toHttpRequest() {
let request: any = {};
let query = this.query;
request.scAddress = query.address.bech32();
request.caller = query.caller?.bech32() ? query.caller.bech32() : undefined;
request.funcName = query.func.toString();
request.value = query.value ? query.value.toString() : undefined;
request.args = query.getEncodedArguments();

return request;
}
}
Loading
Loading