|
2 | 2 | sidebar_position: 3 |
3 | 3 | --- |
4 | 4 |
|
5 | | -# Provider object 🔌 connect to the network |
| 5 | +# RpcProvider object 🔌 connect to the network |
6 | 6 |
|
7 | 7 | The first thing to do is to define with which network you want to interact. |
8 | 8 |
|
9 | | -With the Provider object, you define which network to use. |
| 9 | +Then you need to select a node. A node is a safe way to connect with the Starknet blockchain. You can use: |
10 | 10 |
|
11 | | -```typescript |
12 | | -import {Provider} from 'starknet'; |
13 | | -``` |
14 | | - |
15 | | -## Connect your DAPP to Starknet mainnet |
16 | | - |
17 | | -```typescript |
18 | | -const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_MAIN } }) |
19 | | -``` |
20 | | - |
21 | | -## Connect your DAPP to Starknet testnet |
22 | | - |
23 | | -```typescript |
24 | | -const provider = new Provider({ sequencer: { network: constants.NetworkName.SN_GOERLI } }) // for testnet |
25 | | -``` |
| 11 | +- a node supplied by a node provider - it can be free or not; it can have limitations or not; it can have WebSocket support or not. |
| 12 | + > RPC node providers are for example Infura, Alchemy, Blast, Nethermind, Lava, Chainstack... |
| 13 | +- your own node, located on your local computer or in your local network. |
| 14 | + > you can spin up your own node with Pathfinder, Juno, Papyrus, Deoxys, ... |
| 15 | +- a local development node, that simulates a Starknet network. Useful for devs to perform quick tests without spending precious fee token. |
| 16 | + > Main development devnets are Starknet-devnet-rs, Madara, ... |
26 | 17 |
|
27 | | -## Connect your DAPP to Starknet devnet |
| 18 | +With the RpcProvider object, you define the Starknet Rpc node to use. |
28 | 19 |
|
29 | 20 | ```typescript |
30 | | -const provider = new Provider({ sequencer: { baseUrl:"http://127.0.0.1:5050"} }); |
| 21 | +import {RpcProvider} from 'starknet'; |
31 | 22 | ``` |
32 | 23 |
|
33 | | -> If you have customized host and port during starknet-devnet initialization, adapt in accordance to your script. |
34 | | -
|
35 | | -## Connect your DAPP to a private Starknet network |
36 | | - |
37 | | -If necessary you can have full control of the network access (for example, for your company's private test network): |
38 | | - |
39 | | -```typescript |
40 | | -const provider = new Provider({ |
41 | | - sequencer: { |
42 | | - baseUrl: 'https://mynetwork.mycompany.io', |
43 | | - feederGatewayUrl: 'feeder_gateway', |
44 | | - gatewayUrl: 'gateway', |
45 | | - } |
46 | | -}) |
47 | | -``` |
48 | | - |
49 | | -## Connect your DAPP to a Starknet node |
50 | | - |
51 | | -### Pathfinder |
52 | | - |
53 | | -For a local [Pathfinder](https://github.com/eqlabs/pathfinder) node: |
| 24 | +## Connect your DAPP to an RPC node provider |
54 | 25 |
|
55 | | -```typescript |
56 | | -const provider = new Provider({ rpc: { nodeUrl: '127.0.0.1:9545/rpc/v0.4' } }) |
57 | | -``` |
| 26 | +### Default Rpc node |
58 | 27 |
|
59 | | -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`). |
60 | | -You can connect with: |
| 28 | +If you don't want to use a specific node, or to handle an API key, you can use by default: |
61 | 29 |
|
62 | 30 | ```typescript |
63 | | -const provider = new Provider({ rpc: { nodeUrl: '192.168.1.99:9545/rpc/v0.4' } }) |
| 31 | +const myProvider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_GOERLI }); |
| 32 | +const myProvider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_MAIN }); |
| 33 | +// or |
| 34 | +const myProvider = new RpcProvider(); // Goerli |
64 | 35 | ``` |
65 | 36 |
|
66 | | -### Juno |
67 | | - |
68 | | -Initialize the provider with: |
| 37 | +> when using this syntax, a random public node will be selected. |
69 | 38 |
|
70 | | -```typescript |
71 | | -const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:6060' }); |
72 | | -``` |
| 39 | +Using a specific nodeUrl is the better approach, as such a node will have fewer limitations and will be less crowded. |
73 | 40 |
|
74 | | -### Other node clients |
| 41 | +Some examples of RpcProvider instantiation to connect to RPC node providers: |
75 | 42 |
|
76 | | -Other examples (some need a secret key): |
77 | | - |
78 | | -**Mainnet:** |
| 43 | +### Mainnet |
79 | 44 |
|
80 | 45 | ```typescript |
81 | 46 | // Infura node rpc for Mainnet: |
82 | 47 | const providerInfuraMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.infura.io/v3/' + infuraKey }); |
83 | 48 | // Blast node rpc for Mainnet: |
84 | | -const providerBlastMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.blastapi.io/' + blastKey + "/rpc/v0.4" }); |
| 49 | +const providerBlastMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.blastapi.io/' + blastKey + "/rpc/v0.5" }); |
85 | 50 | // Lava node rpc for Mainnet: |
86 | 51 | const providerMainnetLava = new RpcProvider({ nodeUrl: "https://g.w.lavanet.xyz:443/gateway/strk/rpc-http/" + lavaMainnetKey }); |
87 | 52 | // Alchemy node rpc for Mainnet: |
88 | | -const providerAlchemyMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/v2/' + alchemyKey }); |
| 53 | +const providerAlchemyMainnet = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0.5/' + alchemyKey }); |
| 54 | +// Public Nethermind node rpc 0.5.1 for Mainnet: |
| 55 | +const providerMainnetNethermindPublic = new RpcProvider({ nodeUrl: "https://free-rpc.nethermind.io/mainnet-juno/v0_5" }); |
89 | 56 | ``` |
90 | 57 |
|
91 | | -**Testnet:** |
| 58 | +> Take care to safely manage your API key. It's a confidential item! |
| 59 | +
|
| 60 | +### Testnet |
92 | 61 |
|
93 | 62 | ```typescript |
94 | 63 | // Infura node rpc for Testnet: |
95 | 64 | const providerInfuraTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.infura.io/v3/' + infuraKey }); |
96 | 65 | // Blast node rpc for Testnet: |
97 | | -const providerBlastTestnet = new RpcProvider({ nodeUrl: 'https://starknet-testnet.blastapi.io/' + blastKey + "/rpc/v0.4" }); |
| 66 | +const providerBlastTestnet = new RpcProvider({ nodeUrl: 'https://starknet-testnet.blastapi.io/' + blastKey + "/rpc/v0.5" }); |
98 | 67 | // Alchemy node rpc for Testnet: |
99 | | -const providerAlchemyTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.g.alchemy.com/v2/' + alchemyKey }); |
| 68 | +const providerAlchemyTestnet = new RpcProvider({ nodeUrl: 'https://starknet-goerli.g.alchemy.com/starknet/version/rpc/v0.5/' + alchemyKey }); |
| 69 | +// Public Nethermind node rpc for Testnet: |
| 70 | +const providerTestnetNethermindPublic = new RpcProvider({ nodeUrl: "https://free-rpc.nethermind.io/testnet-juno/v0_5" }); |
100 | 71 | ``` |
101 | 72 |
|
102 | | -## Specific methods |
| 73 | +## Connect to your own node |
103 | 74 |
|
104 | | -Some methods are available only if connected to a sequencer, and some others are available only if connected to a node (using RPC). |
| 75 | +### Pathfinder |
| 76 | + |
| 77 | +For a local [Pathfinder](https://github.com/eqlabs/pathfinder) node: |
105 | 78 |
|
106 | | -### Specific sequencer methods |
| 79 | +```typescript |
| 80 | +const provider = new RpcProvider({ nodeUrl: '127.0.0.1:9545/rpc/v0.5' }); |
| 81 | +``` |
107 | 82 |
|
108 | | -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: |
| 83 | +Your node can be located in your local network (example: Pathfinder node running on a computer in your network, launched with this additional option: `--http-rpc 0.0.0.0:9545`). |
| 84 | +You can connect with: |
109 | 85 |
|
110 | 86 | ```typescript |
111 | | -import { SequencerProvider, constants } from "starknet"; |
112 | | -const provider = new SequencerProvider({ baseUrl: constants.BaseUrl.SN_GOERLI }); // for testnet |
113 | | -const responseEstimateMessageFee = await provider.estimateMessageFee(.....) |
| 87 | +const provider = new RpcProvider({ nodeUrl: '192.168.1.99:9545/rpc/v0.5' }) |
114 | 88 | ``` |
115 | 89 |
|
116 | | -### Specific RPC methods |
| 90 | +### Juno |
117 | 91 |
|
118 | | -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: |
| 92 | +For a local [Juno](https://github.com/NethermindEth/juno) node initialize the provider with: |
119 | 93 |
|
120 | 94 | ```typescript |
121 | | -import { RpcProvider } from "starknet"; |
122 | | -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 |
123 | | -const pendingTx = await providerRPC.getPendingTransactions(); |
| 95 | +const provider = new RpcProvider({ nodeUrl: 'http://127.0.0.1:6060/v0_5' }); |
124 | 96 | ``` |
125 | 97 |
|
126 | | -RPC providers are for example Infura, Alchemy, Chainstack... Or you can spin up your own Pathfinder node! |
| 98 | +> If Juno is running on a separate computer in your local network, don't forget to add the option `--http-host 0.0.0.0` when launching Juno. |
| 99 | +
|
| 100 | +## Devnet |
127 | 101 |
|
128 | | -For example, to connect to Alchemy with your personal API key: |
| 102 | +Example of a connection to a local development node, with Starknet-devnet-rs: |
129 | 103 |
|
130 | 104 | ```typescript |
131 | | -const providerRPC = new RpcProvider({ nodeUrl: 'https://starknet-mainnet.g.alchemy.com/v2/' + alchemyKey}); |
| 105 | +const provider = new RpcProvider({ nodeUrl: "http://127.0.0.1:5050/rpc" }); |
132 | 106 | ``` |
| 107 | + |
| 108 | +> If you have customized host and port during starknet-devnet initialization, adapt in accordance to your script. |
0 commit comments