-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connect): add wallet client docs
- Loading branch information
Showing
5 changed files
with
264 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
# Wallet Client [One API to rull them all.] | ||
|
||
If you missed a Wallet Client from Viem, you can use MinaJS's Wallet Client to interact with Mina wallets. It's a one API that wraps over various wallet types and connectivities. | ||
|
||
## Client properties | ||
|
||
- `network`: Network type. `devnet` or `mainnet`. | ||
- `account`: Account object compatible with `toAccount` format. `string` or `Account` object. If not provided, the client will use the last injected wallet. | ||
- `providerSource`: Provider source. `klesia` (JSON-RPC account), `window.mina` (last injected wallet), `string` (slug of specific browser wallet). Default is `window.mina`. | ||
|
||
## Initialize client | ||
|
||
```ts twoslash | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const client = createWalletClient({ network: "devnet" }); | ||
``` | ||
|
||
## Querying the data | ||
|
||
### From injected wallet | ||
|
||
Query from the last injected wallet (using `window.mina`): | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const balance = await client.getBalance(); | ||
``` | ||
|
||
Query from a specific browser wallet (using slug of a specific injected wallet): | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ | ||
account, | ||
network: "devnet", | ||
providerSource: 'pallad' | ||
}); | ||
|
||
const balance = await client.getBalance(); | ||
``` | ||
|
||
### From Klesia JSON-RPC | ||
|
||
Query data from our [Klesia JSON-RPC](/klesia): | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ | ||
account, | ||
network: "devnet", | ||
providerSource: 'klesia' | ||
}); | ||
|
||
const balance = await client.getBalance(); | ||
``` | ||
|
||
## Wallet queries | ||
|
||
### Get balance | ||
|
||
Query current wallet's balance. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const balance = await client.getBalance(); | ||
``` | ||
|
||
### Get accounts | ||
|
||
Query wallet addresses of an injected wallet. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const balance = await client.getAccounts(); | ||
``` | ||
|
||
### Get transaction count | ||
|
||
Query the number of transactions of a wallet (used as nonce). | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const transactionCount = await client.getTransactionCount(); | ||
``` | ||
|
||
### Get chain ID | ||
|
||
Query the chain ID of the network. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const chainId = await client.getChainId(); | ||
``` | ||
|
||
### Get estimated fees | ||
|
||
Query the estimated fees of a transaction. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect' | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const fees = await client.estimateFees(); | ||
``` | ||
|
||
## Wallet commands | ||
|
||
If you provide a local wallet, Wallet Client will sign with it, otherwise, it will use the injected wallet. | ||
|
||
### Sign a message | ||
|
||
Sign a message with either the injected wallet or a local wallet. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect'; | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ network: "devnet" }); | ||
|
||
const signature = await client.signMessage({ message: 'Hello World' }) | ||
``` | ||
|
||
### Sign a transaction | ||
|
||
Sign a transaction with either the injected wallet or a local wallet. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect'; | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const signature = await client.signTransaction({ | ||
transaction: { | ||
nonce: 1n, | ||
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", | ||
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", | ||
amount: 3000000000n, | ||
fee: 100000000n, | ||
} | ||
}) | ||
``` | ||
|
||
### Sign fields | ||
|
||
Sign fields of a transaction with either the injected wallet or a local wallet. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect'; | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const signature = await client.signFields({ | ||
fields: [1n, 2n, 3n] | ||
}) | ||
``` | ||
|
||
### Create a nullifier | ||
|
||
Create a nullifier with either the injected wallet or a local wallet. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect'; | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const nullifier = await client.createNullifier({ | ||
message: [1n, 2n, 3n] | ||
}); | ||
``` | ||
|
||
### Prepare a transaction request | ||
|
||
There is a helper method to prepare a transaction request that lacks either nonce or fee. This will fetch the correct nonce for you and estimate a `medium` fee that ensures the transaction is included in the next block. | ||
|
||
```ts twoslash | ||
import { toAccount } from "@mina-js/accounts"; | ||
import { createWalletClient } from '@mina-js/connect'; | ||
|
||
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5'); | ||
const client = createWalletClient({ account, network: "devnet" }); | ||
|
||
const transactionRequest = await client.prepareTransactionRequest({ | ||
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", | ||
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", | ||
amount: 3000000000n, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./store"; | ||
export * from "./events"; | ||
export * from "./client"; | ||
export type { MinaProviderDetail } from "@mina-js/providers"; |