BaconSDK Class
Constructor
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()
Initializes the SDK and connects to the blockchain network.
Example:
reward()
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()
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()
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()
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.openedissue.closedpull_request.openedpull_request.mergedpull_request.reviewedrelease.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()
Creates a new BACON token on the blockchain.
Example:
const tokenId = await bacon.createToken({
name: 'BACON',
symbol: 'BACON',
decimals: 8,
supply: 21000000
});
mint()
Mints new BACON tokens (requires mint authority).
Example:
burn()
Burns BACON tokens from the connected wallet.
Example:
getSupply()
Gets the current token supply information.
Example:
const supply = await bacon.getSupply();
console.log(`Total: ${supply.total}, Circulating: ${supply.circulating}`);
User Management
registerUser()
Links a GitHub username to a wallet address.
Example:
getUser()
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()
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()
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()
Gets transaction history for an address.
Example:
const history = await bacon.getTransactionHistory('address', {
limit: 50,
offset: 0,
type: 'reward'
});
exportReport()
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);
});