Crates.io | hapi-core |
lib.rs | hapi-core |
version | 0.2.0 |
source | src |
created_at | 2022-05-17 14:20:07.531284 |
updated_at | 2023-01-17 11:54:17.476299 |
description | Core contract of HAPI Protocol |
homepage | https://hapi.one |
repository | https://github.com/HAPIprotocol/hapi-core |
max_upload_size | |
id | 588342 |
size | 72,529 |
HAPI Core contract built on Anchor for Solana. If you want to know more about HAPI Protocol, please visit the official site and our gitbook. If you want to propose any changes to this smart contract, please visit our governance forum. Suggestions for the client library enhancements are welcome.
To install everything you need to work with this project, you'll need to install dependencies as described in Anchor documentation.
The source code of hapi-core program is in ./programs/hapi-core
.
To build the hapi-core program, you need to execute this command:
anchor build
You'll get the following output:
./target/deploy/hapi_core.so
./target/idl/hapi_core.json
./target/types/hapi_core.ts
To test the program, you'll have to run this command:
anchor test
This command starts a local validator, sets up the program on chain and runs a suite of Jest tests against it.
To deploy the program, run this command:
anchor deploy \
--provider.cluster https://api.mainnet-beta.solana.com \
--provider.wallet ~/.config/solana/id.json
Where provider.cluster
is the target node API and provider.wallet
is the path to keypair you want to use to deploy the program.
The Javascript/Typescript client for this program is an NPM package that can be found here: @hapi.one/core-cli.
It's published by this command:
npm publish
Please view the test suite (./tests/hapi-core/**.spec.ts
) to see how can this client be used in NodeJS context.
import { Connection, PublicKey } from "@solana/web3.js";
import { Provider } from "@project-serum/anchor";
import { initHapiCore } from "@hapi.one/core-cli";
// Setup web3 Connection
const connection = new Connection("https://api.mainnet-beta.solana.com");
// Use Phantom wallet provider
const wallet = window.solana;
// Setup Anchor provider
const provider = new Provider(connection, wallet as any);
// hapi-core program ID is a well-known public key
const HAPI_PROGRAM_ID = new PublicKey(
"hapiAwBQLYRXrjGn6FLCgC8FpQd2yWbKMqS6AYZ48g6"
);
// Setup the client
const hapiCore = initHapiCore(HAPI_PROGRAM_ID, provider);
// HAPI community account is a well-known public key
const communityAccount = new PublicKey(
"31gQ11Qsd7dPcnkdCJ2ZGnY2ijRXsvFCPWagpcFxYwem"
);
// Use client to fetch community account data
const communityData = await hapiCore.account.community.fetch(communityAccount);
console.log(communityData);
// Find a PDA for a particular network
const [networkAccount] = await program.pda.findNetworkAddress(
communityAccount,
"solana"
);
// Encode address buffer
const addressEncoded = hapiCore.util.encodeAddress(
"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
"Solana"
);
// Find a PDA for an address, which we want to check
const [addressAccount] = await program.pda.findAddressAddress(
networkAccount,
addressEncoded
);
// Fetch address risk scoring data
const addressData = await hapiCore.account.address.fetch(addressAccount);
console.log(addressData);