Contract ID: WhbarHelper
unwrapWhbar
Code overview
Recommended gas limit: 1,000,000- JavaScript SDK
Next steps
Wrap HBAR for WHBAR
Deposit HBAR and receive WHBAR at 1:1.
WHBAR overview
How WHBAR works and its security advisory.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Unwrap WHBAR tokens back into native HBAR at a 1:1 ratio through the WhbarHelper unwrapWhbar function, including the required spender allowance setup.
WHBAR contract directly.WHBAR contract.WhbarHelper contract instead.unwrapWhbar
| Parameter Name | Description |
|---|---|
| uint256 wad | WHBAR token amount in its smallest unit |
/// @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);
}
import {
ContractFunctionParameters,
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);
Was this page helpful?