Skip to main content

Installation

To add the Kohin Js to your project, run the following commands:

1. Go to the Directory: Navigate into the project directory with cd project.
2. Install kohin JS: Run below command to install the kohin js in your project.

npm install kohin-js

Singleton Kohin SDK Initialization

To avoid repeating the initialization code every time you need to use the Kohin JS, we recommend creating a singleton file (kohinInstance.ts) to handle the initialization. This way, you only need to configure the SDK once, and the instance can be reused throughout your application.

#Create kohinInstance.ts

This file will contain the logic to initialize the Kohin JS with your credentials and configuration. It will then export the initialized Kohin instance so that it can be imported in any other file where it's needed.

Step 1: Initialize the Kohin JS
Create a file called kohinInstance.ts in your project (e.g., in the src folder).

// src/kohinInstance.ts

import { Kohin } from "kohin-js";

// Initialize Kohin with your access credentials and RPC URL
const kohin = new Kohin({
accessKey: "your-access-key", // Replace with your unique access key
secretKey: "your-secret-key", // Replace with your secret authentication key
envMode: "test", // Options: 'test', 'preProd', 'prod' (set according to environment)
affiliateAddress: "your-affiliate-address", // Replace with your affiliate address
rpcUrl: "https://your-rpc-url.com", // (Optional) Replace with your blockchain RPC URL
});

// Export the instance to be used in other files
export default kohin;

Explanation:

  • accessKey: Your unique API access key.
  • secretKey: Your secret authentication key.
  • envMode: The environment mode (e.g., test, preProd, or prod).
  • affiliateAddress: The required affiliate address for tracking.
  • rpcUrl: The URL for connecting to the blockchain network.--(optional)

This file will now manage the initialization and export the kohin instance to be used across the application.

Note: Ensure the accessKey, secretKey, affiliateAddress and rpcUrl are kept secure and used in the appropriate environment.


Pass WalletClient Instance

When using kohin-js, the appropriate walletClient needs to be passed based on the authentication method:

  • If Web3 authentication (isWeb3Auth) is enabled, use walletClient.data.
  • If Privi authentication is enabled (isWalletConnected):
    • Use socialClient for social wallet authentication.
    • Otherwise, use walletClient.data for connected wallets (e.g., MetaMask).
  • If none of the above conditions are met, default to walletClient.data.

Example Scenarios

The approveAmount method in kohin-js is used to approve a specific token amount for processing. The wallet client provided depends on the authentication method.

1. Web3Auth Enabled

Authenticate and approve the amount using the Web3Auth wallet.

import { useWalletClient } from "wagmi";

const walletClient = useWalletClient();
const amountApprovalResponse = await kohin.approveAmount({
amount: 56.68,
walletClient: walletClient.data,
});

2. Privi Authentication (Social Login or Connected Wallet)

For users authenticated via Privi:

  • If using a social wallet, pass socialClient.
  • Otherwise, pass walletClient.data for connected wallets (e.g., MetaMask).
import { useWalletClient } from "wagmi";
import {
useAAWalletClient,
useAccount,
} from "@azuro-org/sdk-social-aa-connector"; // if you are using azuro's sdk

const walletClient = useWalletClient();
const aaClient = useAAWalletClient();
const { isAAWallet } = useAccount();

// Use aaClient for social login; otherwise, use walletClient.data
const selectedWalletClient = isAAWallet ? aaClient : walletClient.data;

const amountApprovalResponse = await kohin.approveAmount({
amount: 56.68,
walletClient: selectedWalletClient,
});

3. Default Wallet (No Authentication)

When no specific authentication is enabled.

import { useWalletClient } from "wagmi";

const walletClient = useWalletClient();
const amountApprovalResponse = await kohin.approveAmount({
amount: 56.68,
walletClient: walletClient.data,
});

Conclusion

The kohin-js dynamically selects the appropriate wallet client based on the user's authentication method, ensuring seamless transaction approvals.

Next Steps: Integrate Kohin SDK Methods

With the Kohin SDK initialized, you can now start integrating various API & Contract methods into your application. This will allow you to interact with the blockchain network and utilize the full capabilities of the Kohin SDK.

Refers to API documentation for detailed information on available methods and their usage.

Happy coding!