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

docs: use only RpcProvider #857

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 3 additions & 3 deletions www/docs/guides/compiled_contracts/deployBraavos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
DeployContractResponse,
EstimateFeeDetails,
InvocationsSignerDetails,
Provider,
RawCalldata,
RpcProvider,
constants,
ec,
hash,
Expand Down Expand Up @@ -116,7 +116,7 @@ async function buildBraavosAccountDeployPayload(

export async function estimateBraavosAccountDeployFee(
privateKeyBraavos: BigNumberish,
provider: Provider,
provider: RpcProvider,
{ blockIdentifier, skipValidate }: EstimateFeeDetails = {}
): Promise<bigint> {
const version = hash.feeTransactionVersion;
Expand Down Expand Up @@ -159,7 +159,7 @@ export async function estimateBraavosAccountDeployFee(

export async function deployBraavosAccount(
privateKeyBraavos: BigNumberish,
provider: Provider,
provider: RpcProvider,
max_fee?: BigNumberish
): Promise<DeployContractResponse> {
const nonce = constants.ZERO;
Expand Down
24 changes: 12 additions & 12 deletions www/docs/guides/connect_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ You need 2 pieces of data:
- the private key of this account

```typescript
import { Account, Provider } from "starknet";
import { Account, RpcProvider } from "starknet";
```

## Connect to a pre-deployed account in Starknet-devnet
## Connect to a pre-deployed account in Starknet-devnet-rs

When you launch starknet-devnet, 10 accounts are pre-deployed with 100 dummy ETH in each.
When you launch starknet-devnet-rs, 10 accounts are pre-deployed with 100 dummy ETH in each.

Addresses and private keys are displayed on the console at initialization.

> This data will change at each launch, so to freeze them, launch with: `starknet-devnet --seed 0`.
> This data will change at each launch, so to freeze them, launch with: `cargo run --release -- --seed 0`.

The result for `account #0`:

```bash
Address: 0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a
Public key: 0x7e52885445756b313ea16849145363ccb73fb4ab0440dbac333cf9d13de82b9
Private key: 0xe3e70682c2094cac629f6fbed82c07cd
```text
Address : 0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691
Private key: 0x71d7bb07b9a64f6f78ac4c816aff4da9
Public key : 0x7e52885445756b313ea16849145363ccb73fb4ab0440dbac333cf9d13de82b9
```

Then you can use this code:

```typescript
// initialize provider
const provider = new Provider({ sequencer: { baseUrl:"http://127.0.0.1:5050" } });
const provider = new RpcProvider({ nodeUrl: "http://127.0.0.1:5050/rpc" });
// initialize existing pre-deployed account 0 of Devnet
const privateKey = "0xe3e70682c2094cac629f6fbed82c07cd";
const accountAddress = "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a";
const privateKey = "0x71d7bb07b9a64f6f78ac4c816aff4da9";
const accountAddress = "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691";

const account = new Account(provider, accountAddress, privateKey);
```
Expand Down Expand Up @@ -68,7 +68,7 @@ import * as dotenv from "dotenv";
dotenv.config();

// initialize provider
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } });
const provider = new RpcProvider({ nodeUrl: `${myNodeUrl}` });
// initialize existing account
const privateKey = process.env.OZ_NEW_ACCOUNT_PRIVKEY;
const accountAddress = "0x051158d244c7636dde39ec822873b29e6c9a758c6a9812d005b6287564908667";
Expand Down
7 changes: 4 additions & 3 deletions www/docs/guides/connect_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ You need 2 pieces of data:
- the address of the contract
- the ABI file of the contract (or the compiled/compressed contract file, that includes the abi)

> If you don't have the abi file, the `provider.getClassAt()` and `provider.getClassByHash()` commands will recover the compressed contract file. As these methods generate a significant workload to the sequencer/node, it's recommended to store the result in your computer, to be able to reuse it later without using the provider:
> If you don't have the abi file, the `provider.getClassAt()` and `provider.getClassByHash()` commands will recover the compressed contract file. As these methods generate a significant workload to the sequencer/node, it's recommended to store the result in your computer, to be able to reuse it later without using the provider each time:
penovicp marked this conversation as resolved.
Show resolved Hide resolved

```typescript
import fs from "fs";

const compressedContract = await provider.getClassAt(addrContract);
fs.writeFileSync('./myAbi.json', json.stringify( compressedContract.abi, undefined, 2));
```
Expand All @@ -24,7 +25,7 @@ fs.writeFileSync('./myAbi.json', json.stringify( compressedContract.abi, undefin
## Get the abi from a compiled/compressed file

```typescript
import { Provider, Contract, json } from "starknet";
import { RpcProvider, Contract, json } from "starknet";
```

If you have the compiled/compressed file of the contract, use this code to recover all data, including the ABI:
Expand All @@ -39,7 +40,7 @@ const compiledContract = json.parse(fs.readFileSync("./compiledContracts/test.js

```typescript
// initialize provider
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } });
const provider = new RpcProvider({ nodeUrl: `${myNodeUrl}` });

// initialize deployed contract
const testAddress = "0x7667469b8e93faa642573078b6bf8c790d3a6184b2a1bb39c5c923a732862e1";
Expand Down
128 changes: 52 additions & 76 deletions www/docs/guides/connect_network.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,107 @@
sidebar_position: 3
---

# Provider object 🔌 connect to the network
# RpcProvider object 🔌 connect to the network

The first thing to do is to define with which network you want to interact.

With the Provider object, you define which network to use.
Then you need to select a node. A node is a safe way to connect with the Starknet blockchain. You can use :
penovicp marked this conversation as resolved.
Show resolved Hide resolved

```typescript
import {Provider} from 'starknet';
```

## Connect your DAPP to Starknet mainnet

```typescript
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_MAIN } })
```

## Connect your DAPP to Starknet testnet

```typescript
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } }) // for testnet
```

## Connect your DAPP to Starknet devnet
- a node proposed by a node provider, that can be free or not ; it can have limitations or not ; it can have a WebSocket access or not.
> RPC node providers are for example Infura, Alchemy, Blast, Nethermind, Lava, Chainstack...
- your own node, located in your local computer or in your local network.
> you can spin up your own node with Pathfinder, Juno, Papyrus, Deoxys, ...
- a local development node, that simulates a Starknet network. Useful for devs to perform quick tests without spending precious fee token.
penovicp marked this conversation as resolved.
Show resolved Hide resolved
> Main development devnets are Starknet-devnet-rs, Madara, ...

```typescript
const provider = new Provider({ sequencer: { baseUrl:"http://127.0.0.1:5050"} });
```

> If you have customized host and port during starknet-devnet initialization, adapt in accordance to your script.

## Connect your DAPP to a private Starknet network

If necessary you can have full control of the network access (for example, for your company's private test network):
With the RpcProvider object, you define the Starknet Rpc node to use.

```typescript
const provider = new Provider({
sequencer: {
baseUrl: 'https://mynetwork.mycompany.io',
feederGatewayUrl: 'feeder_gateway',
gatewayUrl: 'gateway',
}
})
import {RpcProvider} from 'starknet';
```

## Connect your DAPP to a Starknet node
## Connect your DAPP to a RPC node provider
penovicp marked this conversation as resolved.
Show resolved Hide resolved

### Pathfinder

For a local [Pathfinder](https://github.com/eqlabs/pathfinder) node:

```typescript
const provider = new Provider({ rpc: { nodeUrl: '127.0.0.1:9545/rpc/v0.4' } })
```
### Default Rpc node

Your node can be located in your local network (example: pathfinder node running on a computer on your network, launched with this additional option: `--http-rpc 0.0.0.0:9545`).
You can connect with:
If you don't want to use a specific node, or to handle an API key, you can use by default :
penovicp marked this conversation as resolved.
Show resolved Hide resolved

```typescript
const provider = new Provider({ rpc: { nodeUrl: '192.168.1.99:9545/rpc/v0.4' } })
const myProvider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_GOERLI });
const myProvider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_MAIN });
// or
const myProvider = new RpcProvider(); // Goerli
```

### Juno
> when using this syntax, a random public node will be selected.

Initialize the provider with:
Using a specific nodeUrl is the better approach, as such a node will have less limitations and will be less crowded.
penovicp marked this conversation as resolved.
Show resolved Hide resolved

```typescript
const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:6060' });
```

### Other node clients

Other examples (some need a secret key):
Some examples of RpcProvider instantiation to connect to RPC node providers:

**Mainnet:**
### Mainnet

```typescript
// Infura node rpc for Mainnet:
const providerInfuraMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.infura.io/v3/' + infuraKey });
// Blast node rpc for Mainnet:
const providerBlastMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.blastapi.io/' + blastKey + "/rpc/v0.4" });
const providerBlastMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.blastapi.io/' + blastKey + "/rpc/v0.5" });
// Lava node rpc for Mainnet:
const providerMainnetLava = new RpcProvider({ nodeUrl: "https://g.w.lavanet.xyz:443/gateway/strk/rpc-http/" + lavaMainnetKey });
// Alchemy node rpc for Mainnet:
const providerAlchemyMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/v2/' + alchemyKey });
const providerAlchemyMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0.5/' + alchemyKey });
// Public Nethermind node rpc 0.5.1 for Mainnet :
penovicp marked this conversation as resolved.
Show resolved Hide resolved
const providerMainnetNethermindPublic = new RpcProvider({ nodeUrl: "https://free-rpc.nethermind.io/mainnet-juno/v0_5" });
```

**Testnet:**
> Take care to safely manage your API key. It's a confidential item!

### Testnet

```typescript
// Infura node rpc for Testnet:
const providerInfuraTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.infura.io/v3/' + infuraKey });
// Blast node rpc for Testnet:
const providerBlastTestnet = new RpcProvider({ nodeUrl: 'https://starknet-testnet.blastapi.io/' + blastKey + "/rpc/v0.4" });
const providerBlastTestnet = new RpcProvider({ nodeUrl: 'https://starknet-testnet.blastapi.io/' + blastKey + "/rpc/v0.5" });
// Alchemy node rpc for Testnet:
const providerAlchemyTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.g.alchemy.com/v2/' + alchemyKey });
const providerAlchemyTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.g.alchemy.com/starknet/version/rpc/v0.5/' + alchemyKey });
// Public Nethermind node rpc for Testnet :
penovicp marked this conversation as resolved.
Show resolved Hide resolved
const providerTestnetNethermindPublic = new RpcProvider({ nodeUrl: "https://free-rpc.nethermind.io/testnet-juno/v0_5" });
```

## Specific methods
## Connect to your own node

### Pathfinder

Some methods are available only if connected to a sequencer, and some others are available only if connected to a node (using RPC).
For a local [Pathfinder](https://github.com/eqlabs/pathfinder) node:

### Specific sequencer methods
```typescript
const provider = new RpcProvider({ nodeUrl: '127.0.0.1:9545/rpc/v0.5' });
```

For example, if you want to estimate the fee of an L1 ➡️ L2 message, you need to use a method that is available only in the sequencer. The class `SequencerProvider` is available for this case:
Your node can be located in your local network (example: pathfinder node running on a computer on your network, launched with this additional option: `--http-rpc 0.0.0.0:9545`).
penovicp marked this conversation as resolved.
Show resolved Hide resolved
You can connect with:

```typescript
import { SequencerProvider, constants } from "starknet";
const provider = new SequencerProvider({ baseUrl: constants.BaseUrl.SN_GOERLI }); // for testnet
const responseEstimateMessageFee = await provider.estimateMessageFee(.....)
const provider = new RpcProvider({ nodeUrl: '192.168.1.99:9545/rpc/v0.5' })
```

### Specific RPC methods
### Juno

For example, if you want to read the list of pending transactions, you need to use a method available from an RPC node. The class `RpcProvider` is available for this case:
Initialize the provider with:
penovicp marked this conversation as resolved.
Show resolved Hide resolved

```typescript
import { RpcProvider } from "starknet";
const providerRPC = new RpcProvider({ nodeUrl: "http://192.168.1.99:9545/rpc/v0.4" }); // for a pathfinder node located in a PC in the local network
const pendingTx = await providerRPC.getPendingTransactions();
const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:6060/v0_5' });
```

RPC providers are for example Infura, Alchemy, Chainstack... Or you can spin up your own Pathfinder node!
> if Juno is running in a separate computer in your local network, don't forget to add the option `--http-host 0.0.0.0` when launching Juno.
penovicp marked this conversation as resolved.
Show resolved Hide resolved

## Devnet

For example, to connect to Alchemy with your personal API key:
Example of a connection to a local development node, with Starknet-devnet-rs:

```typescript
const providerRPC = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/v2/' + alchemyKey});
const provider = new RpcProvider({ nodeUrl: "http://127.0.0.1:5050/rpc" });
```

> If you have customized host and port during starknet-devnet initialization, adapt in accordance to your script.
26 changes: 9 additions & 17 deletions www/docs/guides/create_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Here, we will create a wallet with the Open Zeppelin smart contract v0.5.1. The
This contract is coded in Cairo 0, so it will not survive the upcoming re-genesis of Starknet.

```typescript
import { Account, constants, ec, json, stark, Provider, hash, CallData } from "starknet";
import { Account, constants, ec, json, stark, RpcProvider, hash, CallData } from "starknet";
```

### compute address

```typescript
// connect provider
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } });
const provider = new RpcProvider({ nodeUrl: `${myNodeUrl}` });

// new Open Zeppelin account v0.5.1
// Generate public and private key pair.
Expand Down Expand Up @@ -85,14 +85,6 @@ await provider.waitForTransaction(transaction_hash);
console.log('✅ New OpenZeppelin account created.\n address =', contract_address);
```

> **IMPORTANT:** If this account is based on a Cairo v2 contract (for example OpenZeppelin account 0.7.0 or later), do not forget to add the parameter "1" after the privateKey parameter:

```typescript
const OZaccount = new Account(provider, OZcontractAddress, privateKey, "1");
```

> Take care that this added parameter is a string, NOT a number.

## Create an Argent account

> Level: medium.
Expand All @@ -102,14 +94,14 @@ Here, we will create a wallet with the Argent smart contract v0.2.3. This case i
> If necessary OZ contracts can also be created with a proxy.

```typescript
import { Account, ec, json, stark, Provider, hash, CallData } from "starknet";
import { Account, ec, json, stark, RpcProvider, hash, CallData } from "starknet";
```

### compute address

```typescript
// connect provider
const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } });
const provider = new RpcProvider({ nodeUrl: `${myNodeUrl}` });

//new Argent X account v0.2.3
const argentXproxyClassHash = "0x25ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918";
Expand Down Expand Up @@ -172,7 +164,7 @@ starknet-devnet --seed 0 --fork-network alpha-goerli
Initialization:

```typescript
import { Provider, Account, num, stark } from "starknet";
import { RpcProvider, Account, num, stark } from "starknet";
import { calculateAddressBraavos,
deployBraavosAccount,
estimateBraavosAccountDeployFee
Expand All @@ -195,8 +187,8 @@ const privateKeyBraavos = "0x02e8....e12";
### Compute address

```typescript
// initialize Provider
const providerDevnet = new Provider({ sequencer: { baseUrl: "http://127.0.0.1:5050" } });
// initialize provider
const providerDevnet = new RpcProvider({ nodeUrl: `${myNodeUrl}` });
// address
const BraavosProxyAddress = calculateAddressBraavos(privateKeyBraavos);
console.log('Calculated account address=', BraavosProxyAddress);
Expand Down Expand Up @@ -259,14 +251,14 @@ Here is an example of a customized wallet, including super administrator managem
> launch `starknet-devnet --seed 0` before using this script

```typescript
import { Account, ec, json, stark, Provider, hash, CallData } from "starknet";
import { Account, ec, json, stark, RpcProvider, hash, CallData } from "starknet";
import fs from "fs";
import axios from "axios";
```

```typescript
// connect provider
const provider = new Provider({ sequencer: { network: "http://127.0.0.1:5050" } });
const provider = new RpcProvider({ network: "http://127.0.0.1:5050/rpc" });

// initialize existing predeployed account 0 of Devnet
const privateKey0 = "0xe3e70682c2094cac629f6fbed82c07cd";
Expand Down
Loading