-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9e9315
commit 9aa804a
Showing
1 changed file
with
30 additions
and
21 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 |
---|---|---|
@@ -1,50 +1,59 @@ | ||
# cosmos-snap-provider | ||
|
||
## Usage | ||
Cosmos Snap provider has been created for directly using the [Leap Cosmos Snap](https://www.npmjs.com/package/@leapwallet/metamask-cosmos-snap) with cosmos js client. | ||
|
||
## Methods | ||
|
||
### getSnap | ||
This method helps to identify whether leap cosmos snap is installed or not in the metamask instance installed in user browser | ||
|
||
|
||
```typescript | ||
import { getSnap, connectSnap, getKey } from '@leapwallet/cosmos-snap-provider' | ||
const snapInstalled = await getSnap() // return true if already installed | ||
``` | ||
|
||
async function connect(){ | ||
//check if snap is installed | ||
const snapInstalled = await getSnap() | ||
if(!snapInstalled) { | ||
// Install snap if not already installed | ||
connectSnap() | ||
} | ||
|
||
} | ||
### connectSnap | ||
Connect snap lets you connect to leap cosmos snap if installed else it will trigger the installation and connects to the snap. | ||
|
||
```typescript | ||
import { getSnap, connectSnap, getKey } from '@leapwallet/cosmos-snap-provider' | ||
|
||
async function getAccount(){ | ||
await connect() | ||
const chainId = 'cosmoshub-4' | ||
const key = await getKey(chainId) | ||
return key | ||
// check if snap is installed | ||
const snapInstalled = await getSnap() | ||
if(!snapInstalled) { | ||
// Install snap if not already installed | ||
connectSnap() | ||
} | ||
``` | ||
|
||
### getKey | ||
GetKey helps in getting the chain address for the particular chainId. This should be called once the snap is been connected with the dapp. | ||
|
||
```typescript | ||
const key = await getKey(chainId) | ||
``` | ||
|
||
## Usage with cosmjs | ||
### cosmjsOfflineSigner | ||
|
||
The best way to use the provider if you already using the cosmjs libraries for signing is by using it as an offline signer with existing cosmwasm clients. | ||
Make sure the dapp connected to snap before using it as an offline signer. | ||
|
||
```typescript | ||
import { SigningStargateClient } from '@cosmjs/cosmwasm-stargate' | ||
import { GasPrice } from '@cosmjs/stargate' | ||
|
||
|
||
import { cosmjsOfflineSigner } from '@leapwallet/cosmos-snap-provider' | ||
|
||
|
||
|
||
const offlineSigner = new cosmjsOfflineSigner(chainId); | ||
const accounts = await offlineSigner.getAccounts(); | ||
const rpcUrl = "" // Replace with a RPC URL for the given chainId | ||
const rpcUrl = "" // Replace with an RPC URL for the given chainId | ||
const stargateClient = await SigningStargateClient.connectWithSigner( | ||
rpcUrl, | ||
offlineSigner, | ||
{ | ||
gasPrice: GasPrice.fromString("0.0025ujuno"), | ||
} | ||
) | ||
|
||
``` | ||
|