Skip to content

Commit

Permalink
Merge pull request #38 from fileverse/update-pkg-to-support-gnosis
Browse files Browse the repository at this point in the history
Add support for gnosis
  • Loading branch information
nadeem-fileverse committed Apr 2, 2024
2 parents aaaed2f + 9108878 commit 79abd15
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-rocks-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fileverse/heartbit-react": major
"@fileverse/heartbit-core": major
---

Adds support for Gnosis chain
120 changes: 81 additions & 39 deletions packages/heartbit-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,72 +30,114 @@ The SDK utilizes `coreOption` to configure the network for user interactions.

```javascript
const coreSDK = new HeartBitCore({
chain: "0xaa36a7",
chain: "0xaa36a7",
});
```

### Example Usage
### Example Usage - mintHeartBit (Signed Mint)

This minimal example demonstrates how to use `HeartBitCore` for minting and querying data.

```javascript
async function main() {
const message = "Hello World!";
const signature = "0x...signed message";
const startTime = 1706898250;
const endTime = 1706898251;
const hash = "Hello World"; // This is an identifier for the token, if this hash changes you mint a new token in that case

// Mint HeartBit

await coreSDK.mintHeartBit({
message,
signature,
startTime,
endTime,
hash,
});

// Get TotalSupply for a hash

const totalSupply = await coreSDK.getTotalHeartBitCountByHash({ hash });

// Get Total Mints By User
const address = "0x...ethaddress";
const mintsByUser = await coreSDK.getHeartBitByUser({
hash,
address,
});
const message = "Hello World!";
const signature = "0x...signed message";
const startTime = 1706898250;
const endTime = 1706898251;
const hash = "ipfs://somecid"; // unique identifier for token. eg ipfs://somecid in that case

// Mint HeartBit

await coreSDK.mintHeartBit({
message,
signature,
startTime,
endTime,
hash,
});

// Get TotalSupply for a hash

const totalSupply = await coreSDK.getTotalHeartBitCountByHash({ hash });

// Get Total Mints By User
const address = "0x...ethaddress";
const mintsByUser = await coreSDK.getHeartBitByUser({
hash,
address,
});
}
```

[Here](https://codesandbox.io/p/devbox/heartbit-core-sdk-example-37h7hw) is a link to a working example using `HeartBitCore`.

### Example Usage - unSignedMintHeartBit (Unsigned Mint)

This minimal example demonstrates how to use `HeartBitCore` for minting heartbit without signature.

```javascript
async function main() {
const startTime = 1706898250;
const endTime = 1706898251;
const hash = "ipfs://cid"; // unique identifier for token. eg ipfs://somecid that case
const apiKey = "hello";
const address = "0x...ethaddress";

// Mint HeartBit

await coreSDK.unSignedMintHeartBit({
startTime,
endTime,
hash,
address,
apiKey,
});

// Get TotalSupply for a hash

const totalSupply = await coreSDK.getTotalHeartBitCountByHash({ hash });

// Get Total Mints By User
const mintsByUser = await coreSDK.getHeartBitByUser({
hash,
address,
});
}
```

### Interfaces

```javascript
type SupportedChain = "0xaa36a7" | "0x2105";
type SupportedChain = "0xaa36a7" | "0x2105" | "0x64";

interface HeartBitCoreOptions {
chain: SupportedChain;
rpcUrl?: string;
chain: SupportedChain;
rpcUrl?: string;
}

interface TotalHeartBitCountArgs {
hash: string; // keccak256 hash of a string
hash: string; // unique identifier for token. eg ipfs://somecid
}

interface HeartBitCountByUserArgs {
hash: string; // keccak256 hash of a string
address: string; // ethereum wallet address
hash: string; // unique identifier for token. eg ipfs://somecid
address: string; // ethereum wallet address
}

interface MintHeartBitArgs {
message: string;
signature: string;
startTime: number; // in seconds
endTime: number; // in seconds
hash: string; // keccak256 hash of a string
message: string;
signature: string;
startTime: number; // in seconds
endTime: number; // in seconds
hash: string; // unique identifier for token. eg ipfs://somecid
}

interface UnSignedMintArgs {
startTime: number;
endTime: number;
hash: string; // unique identifier for token. eg ipfs://somecid
address: string; // target wallet address to which heartbits should be minted
apiKey: string; // Api Key
}
```

Expand Down
16 changes: 9 additions & 7 deletions packages/heartbit-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
"version": "1.0.0",
"description": "",
"type": "module",
"main": "./dist/esm/index.ts",
"types": "./dist/types/index.d.ts",
"typings": "./dist/types/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"typings": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"default": "./dist/esm/index.js"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "pnpm run clean && pnpm run build:esm+types",
"build:esm+types": "tsc --project tsconfig.build.json --outDir ./dist/esm --declaration --declarationMap --declarationDir ./dist/types",
"build": "pnpm run clean && tsup",
"clean": "rm -rf dist",
"test:build": "publint --strict",
"typecheck": "tsc --noEmit"
Expand All @@ -27,5 +26,8 @@
"license": "ISC",
"dependencies": {
"ethers": "^6.10.0"
},
"devDependencies": {
"tsup": "^8.0.2"
}
}
11 changes: 9 additions & 2 deletions packages/heartbit-core/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ import type { HeartBitConfig } from "../types/index.js";
export const PUBLIC_RPC_URL_MAP = {
sepolia: "https://rpc.ankr.com/eth_sepolia",
base: "https://rpc.ankr.com/base",
gnosis: "https://rpc.ankr.com/gnosis",
};

export const HEART_BIT_CONFIG: HeartBitConfig = {
"0xaa36a7": {
chainId: 11155111,
backendApi: "https://sepolia-heartbit.fileverse.io/signed-mint",
relayerUrl: "https://sepolia-heartbit.fileverse.io",
contractAddress: "0x47E3fd3331a89822A980DA7Fe51592bD6f900FE6",
publicRPCUrl: PUBLIC_RPC_URL_MAP.sepolia,
},
"0x2105": {
chainId: 8453,
backendApi: "https://base-heartbit.fileverse.io/signed-mint",
relayerUrl: "https://base-heartbit.fileverse.io",
contractAddress: "0x5290B2e25c98015cE80b43C5c5CfBd01aA372E04",
publicRPCUrl: PUBLIC_RPC_URL_MAP.base,
},
"0x64": {
chainId: 100,
relayerUrl: "http://gnosis-heartbit.fileverse.io",
contractAddress: "0xD9De9EdE4EFB6088a257C6AdB21619dE656C0863",
publicRPCUrl: PUBLIC_RPC_URL_MAP.gnosis,
},
};

export const CONTRACT_ABI = [
Expand Down
25 changes: 22 additions & 3 deletions packages/heartbit-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
MintHeartBitArgs,
SupportedChain,
HeartBitCountByUserArgs,
UnSignedMintArgs,
} from "./types";

import {
Expand All @@ -16,7 +17,7 @@ import { getMinterContract, getHashedString } from "./utils";

export class HeartBitCore {
chain: SupportedChain;
#backendApi: string;
#relayerUrl: string;
#contract: Contract;
#rpcProvider: JsonRpcProvider;

Expand All @@ -26,15 +27,15 @@ export class HeartBitCore {
if (!chain) throw new Error("Chain is required");

this.chain = chain;
this.#backendApi = HEART_BIT_CONFIG[chain].backendApi;
this.#relayerUrl = HEART_BIT_CONFIG[chain].relayerUrl;
this.#rpcProvider = new JRPCProvider(
rpcUrl || HEART_BIT_CONFIG[chain].publicRPCUrl
);
this.#contract = getMinterContract(chain, this.#rpcProvider);
}

async mintHeartBit(opts: MintHeartBitArgs) {
const response = await fetch(this.#backendApi, {
const response = await fetch(this.#relayerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -48,6 +49,23 @@ export class HeartBitCore {
return data;
}

async unSignedMintHeartBit(opts: UnSignedMintArgs) {
const { apiKey, ...restArgs } = opts;
const response = await fetch(this.#relayerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey || "",
},
body: JSON.stringify({
...restArgs,
}),
});

const data = await response.json();
return data;
}

async getTotalHeartBitCountByHash(
opts: TotalHeartBitCountArgs
): Promise<number> {
Expand Down Expand Up @@ -81,4 +99,5 @@ export {
type HeartBitCountByUserArgs,
type MintHeartBitArgs,
type TotalHeartBitCountArgs,
type UnSignedMintArgs,
};
12 changes: 10 additions & 2 deletions packages/heartbit-core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type SupportedChain = "0xaa36a7" | "0x2105";
export type SupportedChain = "0xaa36a7" | "0x2105" | "0x64";

export type ChainConfig = {
chainId: number;
backendApi: string;
relayerUrl: string;
publicRPCUrl: string;
contractAddress: string;
};
Expand All @@ -19,6 +19,14 @@ export interface MintHeartBitArgs {
hash: string;
}

export interface UnSignedMintArgs {
startTime: number;
endTime: number;
hash: string;
address: string;
apiKey: string;
}

export interface TotalHeartBitCountArgs {
hash: string;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/heartbit-core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ export const getMinterContract = (

return new Contract(contractAddress, CONTRACT_ABI, rpcProvider);
};

export const sleep = (ms = 1000): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
3 changes: 2 additions & 1 deletion packages/heartbit-core/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compilerOptions": {
"sourceMap": true,
"esModuleInterop": true,
"declaration": true
"declaration": true,
"incremental": false
}
}
12 changes: 12 additions & 0 deletions packages/heartbit-core/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from "tsup";

export default defineConfig({
target: "es2020",
format: ["cjs", "esm"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
outDir: "dist",
entryPoints: ["src/index.ts"],
});
2 changes: 1 addition & 1 deletion packages/heartbit-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ interface HeartBitProps
}


type SupportedChain = "0xaa36a7" | "0x2105";
type SupportedChain = "0xaa36a7" | "0x2105" | "0x64";
interface HeartBitCoreOptions {
chain: SupportedChain;
rpcUrl?: string;
Expand Down
Loading

0 comments on commit 79abd15

Please sign in to comment.