API Reference

Complete SDK Documentation for BACON Integration

BaconSDK Class

Constructor

new BaconSDK(config: BaconConfig)

Creates a new instance of the BACON SDK.

Parameters:

  • config (BaconConfig): Configuration object

Example:

const bacon = new BaconSDK({
  blockchain: 'bitcoin',
  network: 'mainnet',
  wallet: {
    privateKey: process.env.PRIVATE_KEY
  }
});

Configuration

BaconConfig Interface

interface BaconConfig {
  blockchain: 'bitcoin' | 'solana';
  network: 'mainnet' | 'testnet' | 'devnet' | 'mainnet-beta';
  wallet?: WalletConfig;
  rewards?: RewardConfig;
  rpc?: RPCConfig;
  github?: GitHubConfig;
}

WalletConfig Interface

interface WalletConfig {
  privateKey?: string;
  publicKey?: string;
  address?: string;
  mnemonic?: string;
}

RewardConfig Interface

interface RewardConfig {
  issueOpened?: number;
  issueResolved?: number;
  pullRequestMerged?: number;
  codeReview?: number;
  commitPushed?: number;
  releasePublished?: number;
  [key: string]: number;
}

Core Methods

initialize()

async initialize(): Promise<void>

Initializes the SDK and connects to the blockchain network.

Example:

await bacon.initialize();

reward()

async reward(recipient: string, amount: number, memo?: string): Promise<Transaction>

Sends BACON tokens as a reward to a recipient.

Parameters:

  • recipient (string): Wallet address or GitHub username
  • amount (number): Amount of BACON tokens to send
  • memo (string, optional): Transaction memo

Returns:

Promise that resolves to a Transaction object.

Example:

const tx = await bacon.reward('alice', 100, 'Bug fix reward');
console.log(`Transaction ID: ${tx.id}`);

batchReward()

async batchReward(rewards: Reward[]): Promise<Transaction>

Sends rewards to multiple recipients in a single transaction.

Parameters:

  • rewards (Reward[]): Array of reward objects

Example:

await bacon.batchReward([
  { recipient: 'alice', amount: 50 },
  { recipient: 'bob', amount: 75 },
  { recipient: 'charlie', amount: 100 }
]);

getBalance()

async getBalance(address: string): Promise<number>

Gets the BACON token balance for an address.

Parameters:

  • address (string): Wallet address to check

Returns:

Promise that resolves to the balance as a number.

Example:

const balance = await bacon.getBalance('wallet_address');
console.log(`Balance: ${balance} BACON`);

getTransaction()

async getTransaction(txId: string): Promise<Transaction>

Gets details of a specific transaction.

Parameters:

  • txId (string): Transaction ID

Returns:

Promise that resolves to a Transaction object.

Example:

const tx = await bacon.getTransaction('tx_id');
console.log(`Status: ${tx.status}`);

GitHub Integration

connectGitHub()

async connectGitHub(config: GitHubConfig): Promise<void>

Connects to GitHub webhooks for automatic reward distribution.

Parameters:

  • config (GitHubConfig): GitHub configuration object

Example:

await bacon.connectGitHub({
  repository: 'owner/repo',
  token: process.env.GITHUB_TOKEN,
  webhookSecret: process.env.WEBHOOK_SECRET
});

on()

on(event: string, handler: EventHandler): void

Registers an event handler for GitHub events.

Supported Events:

  • issue.opened
  • issue.closed
  • pull_request.opened
  • pull_request.merged
  • pull_request.reviewed
  • release.published

Example:

bacon.on('issue.opened', async (event) => {
  await bacon.reward(event.user, 10);
});

bacon.on('pull_request.merged', async (event) => {
  await bacon.reward(event.user, 50);
});

start()

start(options?: ServerOptions): void

Starts the webhook server to listen for GitHub events.

Parameters:

  • options (ServerOptions, optional): Server configuration

Example:

bacon.start({ 
  port: 3000,
  path: '/webhook'
});

Token Management

createToken()

async createToken(params: TokenParams): Promise<string>

Creates a new BACON token on the blockchain.

Example:

const tokenId = await bacon.createToken({
  name: 'BACON',
  symbol: 'BACON',
  decimals: 8,
  supply: 21000000
});

mint()

async mint(amount: number, destination: string): Promise<Transaction>

Mints new BACON tokens (requires mint authority).

Example:

await bacon.mint(1000000, treasuryAddress);

burn()

async burn(amount: number): Promise<Transaction>

Burns BACON tokens from the connected wallet.

Example:

await bacon.burn(100);

getSupply()

async getSupply(): Promise<TokenSupply>

Gets the current token supply information.

Example:

const supply = await bacon.getSupply();
console.log(`Total: ${supply.total}, Circulating: ${supply.circulating}`);

User Management

registerUser()

async registerUser(github: string, wallet: string): Promise<User>

Links a GitHub username to a wallet address.

Example:

await bacon.registerUser('alice', 'wallet_address');

getUser()

async getUser(identifier: string): Promise<User>

Gets user information by GitHub username or wallet address.

Example:

const user = await bacon.getUser('alice');
console.log(`Wallet: ${user.wallet}, Balance: ${user.balance}`);

getLeaderboard()

async getLeaderboard(limit?: number): Promise<User[]>

Gets the top contributors by BACON balance.

Example:

const top10 = await bacon.getLeaderboard(10);
top10.forEach((user, i) => {
  console.log(`${i + 1}. ${user.github}: ${user.balance} BACON`);
});

Analytics & Reporting

getStats()

async getStats(): Promise<Statistics>

Gets overall BACON ecosystem statistics.

Example:

const stats = await bacon.getStats();
console.log(`
  Total Rewards: ${stats.totalRewards}
  Active Users: ${stats.activeUsers}
  Transactions: ${stats.transactions}
`);

getTransactionHistory()

async getTransactionHistory(address: string, options?: HistoryOptions): Promise<Transaction[]>

Gets transaction history for an address.

Example:

const history = await bacon.getTransactionHistory('address', {
  limit: 50,
  offset: 0,
  type: 'reward'
});

exportReport()

async exportReport(format: 'json' | 'csv', options?: ReportOptions): Promise<string>

Exports reward distribution report.

Example:

const report = await bacon.exportReport('csv', {
  startDate: '2024-01-01',
  endDate: '2024-12-31'
});

Error Handling

Error Types

class BaconError extends Error {
  code: string;
  details?: any;
}

// Common error codes:
// - INSUFFICIENT_BALANCE
// - INVALID_ADDRESS
// - TRANSACTION_FAILED
// - NETWORK_ERROR
// - UNAUTHORIZED

Error Handling Example

try {
  await bacon.reward('alice', 1000000);
} catch (error) {
  if (error.code === 'INSUFFICIENT_BALANCE') {
    console.error('Not enough tokens in treasury');
  } else if (error.code === 'INVALID_ADDRESS') {
    console.error('Invalid recipient address');
  } else {
    console.error('Unknown error:', error);
  }
}

Events

Event Emitter

The BACON SDK emits various events that you can listen to:

// Transaction events
bacon.on('transaction:pending', (tx) => {
  console.log('Transaction pending:', tx.id);
});

bacon.on('transaction:confirmed', (tx) => {
  console.log('Transaction confirmed:', tx.id);
});

bacon.on('transaction:failed', (tx, error) => {
  console.error('Transaction failed:', error);
});

// Reward events
bacon.on('reward:sent', (reward) => {
  console.log(`Sent ${reward.amount} to ${reward.recipient}`);
});

// Error events
bacon.on('error', (error) => {
  console.error('SDK error:', error);
});

Need More Information?

Check out our guides and examples