# Cosmos Snap Provider

##

## [@leapwallet/**cosmos-snap-provider**](https://www.npmjs.com/package/@leapwallet/cosmos-snap-provider)

The **`cosmos-snap-provider`** is specifically designed for seamless integration of the Leap Cosmos Snap with the [CosmJS](https://github.com/cosmos/cosmjs) client.

### Installation

```bash
npm install @leapwallet/cosmos-snap-provider
```

### **or**

```bash
yarn add @leapwallet/cosmos-snap-provider
```

### **Methods**

### **cosmjsOfflineSigner**

If you're already employing cosmjs libraries for transaction signing, **`cosmjsOfflineSigner`** is recommended. It functions as an offline signer with existing cosmwasm clients. Before utilizing it as an offline signer, verify that the dapp is connected to the Snap.

**Usage:**

```jsx

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 = ""; // Populate with an RPC URL corresponding to the given chainId

const stargateClient = await SigningStargateClient.connectWithSigner(
  rpcUrl,
  offlineSigner,
  {
    gasPrice: GasPrice.fromString("0.0025ujuno"),
  }
);
```

### **getSnap**

This method is used to detect if the Leap Cosmos Snap is installed within the user's browser instance of Metamask.

**Usage:**

```jsx
import { getSnap } from '@leapwallet/cosmos-snap-provider';

const snapInstalled = await getSnap(); // Returns true if the snap is already installed
```

### **connectSnap**

**`connectSnap`** facilitates the connection to the Leap Cosmos Snap. If the Snap isn't installed, this function triggers its installation and establishes a connection.

**Usage:**

```javascript
import { getSnap, connectSnap } from '@leapwallet/cosmos-snap-provider';

const snapInstalled = await getSnap();
if (!snapInstalled) {
  connectSnap(); // Initiates installation if not already present
}
```

### **getKey**

**`getKey`** fetches the chain address corresponding to a specific chainId. Ensure the Snap is connected with the dapp before invoking this function.

```javascript
const key = await getKey(chainId);
```

### SuggestChain

Utilize the **`suggestChain`** method to suggest any chains of coin type 118.

**Usage:**

```
import { suggestChain } from '@leapwallet/cosmos-snap-provider';
await suggestChain({
  chainId: 'coreum-mainnet-1',
  chainName: 'coreum',
  bech32Config: {
    bech32PrefixAccAddr: 'core',
  },
  bip44: {
    coinType: 990,
  },
  feeCurrencies: [
    {
      coinDenom: "ucore",
      coinMinimalDenom: "ucore",
      coinDecimals: 6,
      coinGeckoId: "coreum",
      gasPriceStep: {
        low: 0.0625,
        average: 0.5,
        high: 62.5,
      },
    },
  ],
});
```
