/ packages / heartbit-core / src / index.ts
index.ts
  1  import { HEART_BIT_CONFIG } from "./constants";
  2  import type {
  3    TotalHeartBitCountArgs,
  4    HeartBitCoreOptions,
  5    MintHeartBitArgs,
  6    SupportedChain,
  7    HeartBitCountByUserArgs,
  8    UnSignedMintArgs,
  9  } from "./types";
 10  
 11  import {
 12    type Contract,
 13    type JsonRpcProvider,
 14    JsonRpcProvider as JRPCProvider,
 15  } from "ethers";
 16  import { getMinterContract, getHashedString } from "./utils";
 17  
 18  export class HeartBitCore {
 19    chain: SupportedChain;
 20    #relayerUrl: string;
 21    #contract: Contract;
 22    #rpcProvider: JsonRpcProvider;
 23  
 24    constructor(opts: HeartBitCoreOptions) {
 25      const { chain, rpcUrl } = opts;
 26  
 27      if (!chain) throw new Error("Chain is required");
 28  
 29      this.chain = chain;
 30      this.#relayerUrl = HEART_BIT_CONFIG[chain].relayerUrl;
 31      this.#rpcProvider = new JRPCProvider(
 32        rpcUrl || HEART_BIT_CONFIG[chain].publicRPCUrl
 33      );
 34      this.#contract = getMinterContract(chain, this.#rpcProvider);
 35    }
 36  
 37    async mintHeartBit(opts: MintHeartBitArgs) {
 38      const response = await fetch(`${this.#relayerUrl}/signed-mint`, {
 39        method: "POST",
 40        headers: {
 41          "Content-Type": "application/json",
 42        },
 43        body: JSON.stringify({
 44          ...opts,
 45        }),
 46      });
 47  
 48      const data = await response.json();
 49      return data;
 50    }
 51  
 52    async unSignedMintHeartBit(opts: UnSignedMintArgs) {
 53      const { apiKey, ...restArgs } = opts;
 54      const response = await fetch(this.#relayerUrl, {
 55        method: "POST",
 56        headers: {
 57          "Content-Type": "application/json",
 58          "x-api-key": apiKey || "",
 59        },
 60        body: JSON.stringify({
 61          ...restArgs,
 62        }),
 63      });
 64  
 65      const data = await response.json();
 66      return data;
 67    }
 68  
 69    async getTotalHeartBitCountByHash(
 70      opts: TotalHeartBitCountArgs
 71    ): Promise<number> {
 72      const { hash } = opts;
 73      const tokenId = await this.getHeartbitHashTokenMap(hash);
 74  
 75      const balance = await this.#contract.totalSupply?.(tokenId);
 76  
 77      return parseInt(balance);
 78    }
 79  
 80    async getHeartbitHashTokenMap(url: string) {
 81      const hashTokenMap = await this.#contract.hashTokenMap?.(url);
 82  
 83      return hashTokenMap;
 84    }
 85  
 86    async getHeartBitByUser(opts: HeartBitCountByUserArgs): Promise<number> {
 87      const { account, hash } = opts;
 88      const tokenId = await this.getHeartbitHashTokenMap(hash);
 89  
 90      const balance = await this.#contract.balanceOf?.(account, tokenId);
 91      return parseInt(balance);
 92    }
 93  }
 94  
 95  export {
 96    getHashedString,
 97    type HeartBitCoreOptions,
 98    type SupportedChain,
 99    type HeartBitCountByUserArgs,
100    type MintHeartBitArgs,
101    type TotalHeartBitCountArgs,
102    type UnSignedMintArgs,
103  };