Skip to content

Commit

Permalink
Merge pull request #505 from multiversx/TOOL-264-bring-sdk-wallet-int…
Browse files Browse the repository at this point in the history
…o-sdk-core-optional-dependencies

Make axios and bls optional
  • Loading branch information
danielailie authored Oct 15, 2024
2 parents 10c93ed + df8d1ad commit fe5f1b4
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 44 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ npm install --global browserify
npm install esmify --no-save
```

## Optional Dependencies

### axios

This package can make HTTP requests using `axios`, which is not bundled by default. If you plan to use the API network provider or Proxy network provider, make sure to install `axios`:

```bash
npm install axios
```

### @multiversx/sdk-bls-wasm

This package requires `@multiversx/sdk-bls-wasm` for BLS (Boneh-Lynn-Shacham) cryptographic functions, which is not bundled by default. If you plan to use BLS functionality, make sure to install this optional dependency:

```bash
npm install @multiversx/sdk-bls-wasm
```

### Building the library

In order to compile the library, run the following:
Expand Down
46 changes: 25 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"dependencies": {
"@multiversx/sdk-transaction-decoder": "1.0.2",
"@multiversx/sdk-bls-wasm": "0.3.5",
"json-bigint": "1.0.0",
"bech32": "1.1.4",
"bip39": "3.1.0",
Expand Down Expand Up @@ -75,7 +74,10 @@
},
"peerDependencies": {
"bignumber.js": "^9.0.1",
"protobufjs": "^7.2.6",
"axios": "^1.7.4"
"protobufjs": "^7.2.6"
},
"optionalDependencies": {
"axios": "^1.7.4",
"@multiversx/sdk-bls-wasm": "0.3.5"
}
}
8 changes: 5 additions & 3 deletions src/networkProviders/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { ErrContractQuery, ErrNetworkProvider } from "../errors";
import { getAxios } from "../utils";
import { numberToPaddedHex } from "../utils.codec";
import { AccountOnNetwork, GuardianData } from "./accounts";
import { defaultAxiosConfig, defaultPagination } from "./config";
Expand All @@ -26,12 +26,14 @@ export class ApiNetworkProvider implements INetworkProvider {
private config: NetworkProviderConfig;
private backingProxyNetworkProvider;
private userAgentPrefix = `${BaseUserAgent}/api`;
private axios: any;

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

Expand Down Expand Up @@ -205,7 +207,7 @@ export class ApiNetworkProvider implements INetworkProvider {
const url = `${this.url}/${resourceUrl}`;

try {
const response = await axios.get(url, this.config);
const response = await this.axios.default.get(url, this.config);
return response.data;
} catch (error) {
this.handleApiError(error, resourceUrl);
Expand All @@ -216,7 +218,7 @@ export class ApiNetworkProvider implements INetworkProvider {
const url = `${this.url}/${resourceUrl}`;

try {
const response = await axios.post(url, payload, {
const response = await this.axios.default.post(url, payload, {
...this.config,
headers: {
"Content-Type": "application/json",
Expand Down
8 changes: 5 additions & 3 deletions src/networkProviders/proxyNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { ErrContractQuery, ErrNetworkProvider } from "../errors";
import { getAxios } from "../utils";
import { AccountOnNetwork, GuardianData } from "./accounts";
import { defaultAxiosConfig } from "./config";
import { BaseUserAgent, EsdtContractAddress } from "./constants";
Expand All @@ -22,10 +22,12 @@ export class ProxyNetworkProvider implements INetworkProvider {
private url: string;
private config: NetworkProviderConfig;
private userAgentPrefix = `${BaseUserAgent}/proxy`;
private axios: any;

constructor(url: string, config?: NetworkProviderConfig) {
this.url = url;
this.config = { ...defaultAxiosConfig, ...config };
this.axios = getAxios();
extendUserAgent(this.userAgentPrefix, this.config);
}

Expand Down Expand Up @@ -227,7 +229,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
const url = `${this.url}/${resourceUrl}`;

try {
const response = await axios.get(url, this.config);
const response = await this.axios.default.get(url, this.config);
const payload = response.data.data;
return payload;
} catch (error) {
Expand All @@ -239,7 +241,7 @@ export class ProxyNetworkProvider implements INetworkProvider {
const url = `${this.url}/${resourceUrl}`;

try {
const response = await axios.post(url, payload, {
const response = await this.axios.default.post(url, payload, {
...this.config,
headers: {
"Content-Type": "application/json",
Expand Down
16 changes: 9 additions & 7 deletions src/testutils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PathLike } from "fs";
import BigNumber from "bignumber.js";
import * as fs from "fs";
import { SmartContract } from "../smartcontracts/smartContract";
import { PathLike } from "fs";
import { IChainID, IGasLimit } from "../interface";
import { Code } from "../smartcontracts/code";
import { SmartContract } from "../smartcontracts/smartContract";
import { AbiRegistry, TypedValue } from "../smartcontracts/typesystem";
import { Transaction } from "../transaction";
import { TransactionWatcher } from "../transactionWatcher";
import { IChainID, IGasLimit } from "../interface";
import { getAxios } from "../utils";
import { TestWallet } from "./wallets";
import axios, { AxiosResponse } from "axios";
import BigNumber from "bignumber.js";

export async function prepareDeployment(obj: {
deployer: TestWallet;
Expand Down Expand Up @@ -41,7 +41,8 @@ export async function prepareDeployment(obj: {

export async function loadContractCode(path: PathLike): Promise<Code> {
if (isOnBrowserTests()) {
let response: AxiosResponse<ArrayBuffer> = await axios.get(path.toString(), {
const axios = await getAxios();
let response: any = await axios.default.get(path.toString(), {
responseType: "arraybuffer",
transformResponse: [],
headers: {
Expand All @@ -60,7 +61,8 @@ export async function loadContractCode(path: PathLike): Promise<Code> {

export async function loadAbiRegistry(path: PathLike): Promise<AbiRegistry> {
if (isOnBrowserTests()) {
let response: AxiosResponse = await axios.get(path.toString());
const axios = await getAxios();
let response: any = await axios.default.get(path.toString());
return AbiRegistry.create(response.data);
}

Expand Down
5 changes: 3 additions & 2 deletions src/testutils/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from "axios";
import * as fs from "fs";
import * as path from "path";
import { Account } from "../account";
import { Address } from "../address";
import { IAddress } from "../interface";
import { IAccountOnNetwork } from "../interfaceOfNetwork";
import { getAxios } from "../utils";
import { UserSecretKey, UserSigner } from "./../wallet";
import { readTestFile } from "./files";
import { isOnBrowserTests } from "./utils";
Expand Down Expand Up @@ -82,7 +82,8 @@ async function readTestWalletFileContents(name: string): Promise<string> {
}

async function downloadTextFile(url: string) {
let response = await axios.get(url, { responseType: "text", transformResponse: [] });
const axios = await getAxios();
let response = await axios.default.get(url, { responseType: "text", transformResponse: [] });
let text = response.data.toString();
return text;
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ export function isEmpty(value: { isEmpty?: () => boolean; length?: number }): bo

return value.length === 0;
}

export function getAxios() {
try {
return require("axios");
} catch (error) {
throw new Error("axios is required but not installed. Please install axios to make network requests.");
}
}
Loading

0 comments on commit fe5f1b4

Please sign in to comment.