SaucerSwap
AppMerchBlog
Developer
Developer
  • Developer Resources
  • REST API
    • Stats
    • Farms
    • Tokens
    • Pools (V1)
    • Pools and Positions (V2)
  • SaucerSwap V1
    • Swap Operations
      • Swap Quote
      • Swap HBAR for Tokens
      • Swap Tokens for Tokens
      • Swap Tokens for HBAR
      • Track Swap Events
    • Liquidity Operations
      • Fetch All Pools
      • Check if a Pool Exists
      • Pool Creation Fee
      • Create a New Pool
      • Get Pool Reserves
      • Track Pool Updates
      • Adding Liquidity
      • Removing Liquidity
  • SaucerSwap V2
    • Swap Operations
      • Swap Quote
      • Swap HBAR for Tokens
      • Swap Tokens for Tokens
      • Swap Tokens for HBAR
      • Track Swap Events
    • Liquidity Operations
      • Fetch all Pools
      • Check if a Pool Exists
      • Fetch Pool Token Ratio
      • Liquidity Position Fee
      • New Liquidity Position
      • Increasing Liquidity
      • Get User Positions
      • Claiming Fees
      • Decreasing Liquidity
  • Staking Operations
    • Single-Sided Staking
    • Yield Farming
  • WHBAR
    • Wrap HBAR for WHBAR
    • Unwrap WHBAR for HBAR
  • Contract Deployments
Powered by GitBook
On this page
  1. WHBAR

Unwrap WHBAR for HBAR

Withdraw Wrapped HBAR (WHBAR) tokens to retrieve HBAR native cryptocurrency at a 1:1 ratio.

PreviousWrap HBAR for WHBAR

Last updated 4 months ago

Contract ID:

Granting a spender allowance to the WhbarHelper contract is required for the contract to transfer the token from the user's wallet to the contract.


Function name: unwrapWhbar

Parameter Name
Description

uint256 wad

WHBAR token amount in its smallest unit

Solidity Function Body
/// @notice Safely unwrap whbar to msg.sender
/// @dev This contract needs an allowance from msg.sender to transfer the whbar token
/// @param wad The amount to unwrap
function unwrapWhbar(uint wad) public {
  require(wad > 0, "WhbarHelper: wad cannot be lt zero");
  // transfer the whbar to this contract
  HederaTokenHelper.safeTransferFrom(whbarToken, msg.sender, address(this), wad);
  
  // approve sending the whbar to the whbar contract
  HederaTokenHelper.safeApprove(whbarToken, whbarContract, wad);
  
  // use withdraw(address src, address dst, uint wad) and use this contract and msg.sender to withdraw
  // to the contract caller
  IWHBAR(whbarContract).withdraw(address(this), msg.sender, wad);
}

Code Overview

⛽ Recommended gas: 1,000,000 gwei (~ $0.085 USD)

Resources:

import { 
  ContractExecuteTransaction,
  TokenAssociateTransaction,
  Hbar,
  HbarUnit,  
  .. 
} from '@hashgraph/sdk';

//Client pre-checks:
// - WhbarHelper contract has spender allowance for WHBAR token

const params = new ContractFunctionParameters();
params.addUint256(withdrawAmount); //amount in token's smallest unit

await new ContractExecuteTransaction()
  .setContractId(whbarHelperContractId)
  .setGas(gasLim)
  .setFunction('unwrapWhbar', params)
  .execute(client);
WhbarHelper
WHBAR
Hedera JavaScript SDK
Associate tokens to an account
Calling a smart contract function