Skip to content

Commit

Permalink
Merge pull request #49 from massalabs/feature/localhost
Browse files Browse the repository at this point in the history
added localnet default url configuration
  • Loading branch information
BatiGencho authored Nov 8, 2022
2 parents 573c822 + 99634be commit e05c51a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ const customClient: Client = await ClientFactory.createCustomClient(
);
```

Please note that connecting to a locally running node could be easily done using the factory method:

```ts
const testnetClient: Client = await ClientFactory.createDefaultClient(
DefaultProviderUrls.LOCALNET,
baseAccount
);
```

Once there is an initialized client instance, it is straightforward to call methods on it:

```ts
Expand Down
22 changes: 19 additions & 3 deletions src/web3/ClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,36 @@ import { IClientConfig } from "../interfaces/IClientConfig";
export enum DefaultProviderUrls {
MAINNET = "https://massa.net/api/v2",
TESTNET = "https://test.massa.net/api/v2",
LABNET = "https://labnet.massa.net/api/v2"
LABNET = "https://labnet.massa.net/api/v2",
LOCALNET = "http://127.0.0.1",
}

/** Massa Web3 Client Factory for easy initialization */
export class ClientFactory {

/** Factory Method for easy initializing a client using a default provider */
public static async createDefaultClient(provider: DefaultProviderUrls, retryStrategyOn: boolean = true, baseAccount?: IAccount): Promise<Client> {
let publicProviderUrl = provider.toString();
let privateProviderUrl = provider.toString();
switch (provider) {
// in the case of localnet append specific default ports to url
case DefaultProviderUrls.LOCALNET: {
publicProviderUrl = `${publicProviderUrl}:33034`;
privateProviderUrl = `${privateProviderUrl}:33035`;
break;
}
// all other networks should be public only access
default: {
break;
}
}

const providers = new Array({
url: provider,
url: publicProviderUrl,
type: ProviderType.PUBLIC
} as IProvider,
{
url: provider,
url: privateProviderUrl,
type: ProviderType.PRIVATE
} as IProvider);

Expand Down

0 comments on commit e05c51a

Please sign in to comment.