You are viewing a single comment's thread from:

RE: Hivepegged Smart Contract

in #hive-engine β€’ 4 days ago

This contract is deployed on the Hive Smart Contract Engine (SSC), and the action is an update of the contract named hivepegged.


🧠 Contract name: hivepegged

πŸ“¦ Action: update

🧾 Written in: JavaScript (Hive Smart Contract Engine format)


πŸ”§ Main Functions Implemented

1. createSSC

Initializes the contract by creating a database table named withdrawals if it doesn't already exist.

actions.createSSC = async () => {
  const tableExists = await api.db.tableExists('withdrawals');
  if (tableExists === false) {
    await api.db.createTable('withdrawals');
  }
};

2. buy

Allows a user to send HIVE to this contract in exchange for a pegged token (like a wrapped/synthetic version of HIVE).

Key steps:
  • Checks if the recipient is the contract owner.
  • Verifies the transaction was signed with the active key.
  • Splits the amount into number and unit (e.g., 1.23 HIVE).
  • Calculates a 0.075% fee with a minimum of 0.0001 HIVE.
  • Sends the tokens from the user to the contract owner using the tokens contract.
  • Calls the initializeWithdrawal function to log the transaction.

3. withdraw

Allows users to request a withdrawal (burn pegged tokens in exchange for real HIVE).

Key steps:
  • Validates that the quantity is a string, not NaN, ≀ 3, and β‰₯ 0.0002.
  • Calculates the 0.075% fee, same as in the buy function.
  • Transfers the pegged tokens back to the contract owner using the tokens.transfer action.
  • Calls initializeWithdrawal to log the withdrawal event.

4. removeWithdrawal

Enables the contract owner to remove a withdrawal entry from the withdrawals table.

actions.removeWithdrawal = async (payload) => {
  const { id, isSignedWithActiveKey } = payload;

  if (api.sender !== api.owner) return;

  if (id && isSignedWithActiveKey) {
    const withdrawal = await api.db.findOne('withdrawals', { id });
    if (withdrawal) {
      await api.db.remove('withdrawals', withdrawal);
    }
  }
};

βš™οΈ Helper Function: initializeWithdrawal

This function saves a withdrawal record to the database using transaction details like ID, quantity, memo, and token symbol.


πŸ’Έ Fee System

  • Fee rate: 0.075% of the amount
  • Minimum fee: 0.0001 HIVE
  • The fee is deducted before sending tokens back to the contract user or logging withdrawals.

πŸ” Security & Validation

  • Ensures all sensitive actions are signed with an active key.
  • Limits withdrawal amounts to ≀ 3 HIVE.
  • Validates quantities and formats carefully.
  • Only the contract owner can delete withdrawal logs.

🧠 Concept

This contract acts as a peg system for HIVE:

  • Users send real HIVE to get a pegged token (SWAP.HIVE).
  • Later, they can redeem their pegged tokens to withdraw real HIVE back.
  • It's essentially a synthetic HIVE gateway with transaction logging and minimal fees.

image.png