@ckboost/booster
Version:
Liquidity provider toolkit for CKBoost - Create and manage ck-token liquidity pools on Internet Computer
256 lines (251 loc) • 7.69 kB
JavaScript
import { HttpAgent, Actor } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { createHash } from 'crypto';
// src/ck-testbtc-booster.ts
// src/candid/ck-testbtc/backend.did.js
var idlFactory = ({ IDL }) => {
const BoostId = IDL.Nat;
const BoostStatus = IDL.Variant({
"active": IDL.Null,
"cancelled": IDL.Null,
"pending": IDL.Null,
"completed": IDL.Null
});
const Amount = IDL.Nat;
const Timestamp = IDL.Int;
const Subaccount = IDL.Vec(IDL.Nat8);
const BoostRequest = IDL.Record({
"id": BoostId,
"status": BoostStatus,
"receivedBTC": Amount,
"confirmationsRequired": IDL.Nat,
"owner": IDL.Principal,
"maxFeePercentage": IDL.Float64,
"createdAt": Timestamp,
"subaccount": Subaccount,
"booster": IDL.Opt(IDL.Principal),
"updatedAt": Timestamp,
"btcAddress": IDL.Opt(IDL.Text),
"amount": Amount,
"preferredBooster": IDL.Opt(IDL.Principal)
});
const Result = IDL.Variant({ "ok": BoostRequest, "err": IDL.Text });
const BoosterAccount = IDL.Record({
"availableBalance": Amount,
"owner": IDL.Principal,
"createdAt": Timestamp,
"subaccount": Subaccount,
"updatedAt": Timestamp,
"totalDeposited": Amount
});
const Result_2 = IDL.Variant({ "ok": IDL.Text, "err": IDL.Text });
const Result_1 = IDL.Variant({ "ok": BoosterAccount, "err": IDL.Text });
const Main = IDL.Service({
"acceptBoostRequest": IDL.Func([BoostId], [IDL.Text], []),
"checkBTCDeposit": IDL.Func([BoostId], [Result], []),
"getAllBoostRequests": IDL.Func([], [IDL.Vec(BoostRequest)], ["query"]),
"getAllBoosterAccounts": IDL.Func(
[],
[IDL.Vec(BoosterAccount)],
["query"]
),
"getBoostRequest": IDL.Func([BoostId], [IDL.Opt(BoostRequest)], ["query"]),
"getBoostRequestBTCAddress": IDL.Func([BoostId], [Result_2], []),
"getBoosterAccount": IDL.Func(
[IDL.Principal],
[IDL.Opt(BoosterAccount)],
["query"]
),
"getCanisterPrincipal": IDL.Func([], [IDL.Principal], ["query"]),
"getDirectBTCAddress": IDL.Func([], [IDL.Text], []),
"getPendingBoostRequests": IDL.Func(
[],
[IDL.Vec(BoostRequest)],
["query"]
),
"getUserBoostRequests": IDL.Func(
[IDL.Principal],
[IDL.Vec(BoostRequest)],
["query"]
),
"registerBoostRequest": IDL.Func(
[Amount, IDL.Float64, IDL.Nat, IDL.Opt(IDL.Principal)],
[Result],
[]
),
"registerBoosterAccount": IDL.Func([], [Result_1], []),
"updateBoosterDeposit": IDL.Func([IDL.Principal, Amount], [Result_1], []),
"updateReceivedBTC": IDL.Func([BoostId, Amount], [Result], []),
"withdrawBoosterFunds": IDL.Func([Amount], [IDL.Text], [])
});
return Main;
};
// src/ck-testbtc-booster.ts
var BACKEND_CANISTER_ID = "75egi-7qaaa-aaaao-qj6ma-cai";
var CKBTC_LEDGER_CANISTER_ID = "mc6ru-gyaaa-aaaar-qaaaq-cai";
var ckTestBTCBooster = class {
constructor(mnemonics, host = "https://icp0.io") {
this.identity = this.createIdentityFromMnemonics(mnemonics);
this.agent = new HttpAgent({
host,
identity: this.identity
});
}
/**
* Create identity from mnemonics (your exact logic)
*/
createIdentityFromMnemonics(mnemonics) {
const words = mnemonics.trim().split(/\s+/);
if (words.length !== 12) {
throw new Error("Invalid mnemonics length. Expected 12 words.");
}
const hash = createHash("sha256");
hash.update(words.join(" "));
const seed = hash.digest();
const identity = Ed25519KeyIdentity.generate(seed);
console.log("Raw identity principal:", identity.getPrincipal().toText());
return identity;
}
/**
* Initialize - now uses built-in IDL factory
*/
async initialize() {
this.actor = Actor.createActor(idlFactory, {
agent: this.agent,
canisterId: Principal.fromText(BACKEND_CANISTER_ID)
});
this.ledgerActor = this.createLedgerActor();
}
/**
* Create ICRC-1 ledger actor (your exact logic)
*/
createLedgerActor() {
const icrc1LedgerIDLFactory = ({ IDL }) => {
const Account = IDL.Record({
"owner": IDL.Principal,
"subaccount": IDL.Opt(IDL.Vec(IDL.Nat8))
});
const Tokens = IDL.Nat;
const Memo = IDL.Vec(IDL.Nat8);
const Timestamp = IDL.Nat64;
const TransferArg = IDL.Record({
"to": Account,
"fee": IDL.Opt(Tokens),
"memo": IDL.Opt(Memo),
"from_subaccount": IDL.Opt(IDL.Vec(IDL.Nat8)),
"created_at_time": IDL.Opt(Timestamp),
"amount": Tokens
});
const TransferError = IDL.Variant({
"GenericError": IDL.Record({
"message": IDL.Text,
"error_code": IDL.Nat
}),
"TemporarilyUnavailable": IDL.Null,
"BadBurn": IDL.Record({ "min_burn_amount": Tokens }),
"Duplicate": IDL.Record({ "duplicate_of": IDL.Nat }),
"BadFee": IDL.Record({ "expected_fee": Tokens }),
"CreatedInFuture": IDL.Record({ "ledger_time": Timestamp }),
"TooOld": IDL.Null,
"InsufficientFunds": IDL.Record({ "balance": Tokens })
});
const TransferResult = IDL.Variant({
"Ok": IDL.Nat,
"Err": TransferError
});
return IDL.Service({
"icrc1_balance_of": IDL.Func([Account], [Tokens], ["query"]),
"icrc1_transfer": IDL.Func([TransferArg], [TransferResult], [])
});
};
return Actor.createActor(icrc1LedgerIDLFactory, {
agent: this.agent,
canisterId: Principal.fromText(CKBTC_LEDGER_CANISTER_ID)
});
}
// CKBoost Operations
/**
* Register as booster
*/
async registerBoosterAccount() {
return await this.actor.registerBoosterAccount();
}
/**
* Get booster account
*/
async getBoosterAccount(principal) {
const targetPrincipal = principal || await this.agent.getPrincipal();
return await this.actor.getBoosterAccount(targetPrincipal);
}
/**
* Update booster deposit
*/
async updateBoosterDeposit(principal, amount) {
return await this.actor.updateBoosterDeposit(principal, amount);
}
/**
* Get pending boost requests
*/
async getPendingBoostRequests() {
return await this.actor.getPendingBoostRequests();
}
/**
* Accept boost request
*/
async acceptBoostRequest(requestId) {
return await this.actor.acceptBoostRequest(requestId);
}
// ICRC-1 Operations
/**
* Transfer ckTESTBTC (your exact logic)
*/
async transferCKTESTBTC(transferArgs) {
return await this.ledgerActor["icrc1_transfer"](transferArgs);
}
/**
* Get ckTESTBTC balance
*/
async getCKTESTBTCBalance(account) {
return await this.ledgerActor["icrc1_balance_of"](account);
}
// Utility methods
/**
* Get current principal
*/
async getPrincipal() {
return await this.agent.getPrincipal();
}
/**
* Get backend canister principal
*/
getBackendCanisterPrincipal() {
return Principal.fromText(BACKEND_CANISTER_ID);
}
/**
* Get ledger canister principal
*/
getLedgerCanisterPrincipal() {
return Principal.fromText(CKBTC_LEDGER_CANISTER_ID);
}
/**
* Convert BTC to satoshis
*/
btcToSatoshis(btcAmount) {
return BigInt(btcAmount * 10 ** 8);
}
/**
* Convert satoshis to BTC
*/
satoshisToBTC(satoshis) {
return Number(satoshis) / 10 ** 8;
}
};
// src/index.ts
var ckTESTBTC_CANISTER_IDS = {
CKBOOST_BACKEND: "75egi-7qaaa-aaaao-qj6ma-cai",
CKTESTBTC_LEDGER: "mc6ru-gyaaa-aaaar-qaaaq-cai"
};
export { ckTESTBTC_CANISTER_IDS, ckTestBTCBooster };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map