Integrate Operations
Stake B3TR with Referral
import find from "lodash.find";
import ABI_ERC20 from "path/to/erc20.json";
import ABI_VEDELEGATE from "path/to/VeDelegateV2.json";
import type { Connex } from "@vechain/connex";
type Receipt = Connex.Thor.Transaction.Receipt | null;
async function stakeB3TRWithReferral(amount: string, referral: string) {
// Parameters:
// - amount: The amount of B3TR to stake (in standard units, not wei)
// - referral: The referral ID (typically the app ID)
const baseUnit = 10n ** BigInt(TOKEN_B3TR.decimals);
const amountInWei = (BigInt(amount) * baseUnit).toString();
// Approve B3TR spending
const approveB3trMethod = connex.thor
.account(TOKEN_B3TR.address)
.method(find(ABI_ERC20, { name: "approve" }));
const approveB3trClause = approveB3trMethod.asClause(VEDELEGATE_CONTRACT_ADDRESS, amountInWei);
// Stake B3TR with referral
const stakeB3trMethod = connex.thor
.account(VEDELEGATE_CONTRACT_ADDRESS)
.method(find(ABI_VEDELEGATE, { name: "depositB3TRWithReferral" }));
const stakeB3trClause = stakeB3trMethod.asClause(amountInWei, referral);
try {
const tx = await connex.vendor
.sign("tx", [approveB3trClause, stakeB3trClause])
.comment(`Stake ${amount} B3TR with referral`)
.request();
// Wait for transaction confirmation
const receipt: Receipt = await poll(() => connex.thor.transaction(tx.txid).getReceipt());
if (receipt) {
const isSuccess = receipt.reverted === false;
return {
success: isSuccess,
txid: tx.txid,
receipt
};
} else {
throw new Error(`Failed to fetch receipt for transaction: ${tx.txid}`);
}
} catch (err) {
console.error("Staking failed:", err);
throw err;
}
}Unstake B3TR
Claim Reward
Transaction Status Management
Error Handling
Last updated