withdraw
API details
To withdraw assets on the Hibit Chain using the API, a signature (sign message) from the source chain wallet is required to authorize the transaction.
Body
chainstringOptional
chainNetworkstring | nullableOptional
messagestring | nullableOptional
signaturestring | nullableOptional
Responses
200
OK
400
Bad Request
401
Unauthorized
403
Forbidden
404
Not Found
500
Internal Server Error
501
Not Implemented
post
POST /v1/withdraw HTTP/1.1
Host: testnetopenapi.hibit.app
Content-Type: application/json
Accept: */*
Content-Length: 74
{
"chain": "text",
"chainNetwork": "text",
"message": "text",
"signature": "text"
}
{
"code": 1,
"message": "text",
"data": {
"txHash": "text"
}
}
Code example
import {
hibitClient,
Chain,
ChainNetwork,
HibitNetwork,
WalletSignatureSchema,
WithdrawInput,
fromSmallestUnit,
MetaMaskWalletApi
} from '@delandlabs/hibit-sdk';
// Set up client options
hibitClient.setOptions({
network: HibitNetwork.Testnet, // or HibitNetwork.Mainnet
hin: BigInt('123456'), // your wallet HIN
proxyKey: 'your_proxy_private_key_hex'
});
// Set wallet API (example: MetaMask)
hibitClient.setWalletApi(new MetaMaskWalletApi());
async function withdrawWithFee() {
const assetId = BigInt('10001');
const assetDecimals = 18;
const amount = 0.01;
const targetChain = Chain.Ethereum;
const targetChainNetwork = ChainNetwork.EvmMainNet;
const address = '0xYourTargetAddress';
// 1. Get withdraw fee info
const feeInfo = await hibitClient.getWithdrawFee({
rootAssetId: assetId,
targetChain,
targetNetwork: targetChainNetwork
});
// 2. Calculate fee
const feeRateDecimal = fromSmallestUnit(feeInfo.feeRate, feeInfo.rateDecimal);
const calculatedFee = amount * Number(feeRateDecimal);
const minFee = Number(fromSmallestUnit(feeInfo.minFee, assetDecimals));
const fee = Math.max(calculatedFee, minFee);
// 3. Get nonce
const nonce = await hibitClient.getNonce();
// 4. Prepare withdraw input
const withdrawInput: WithdrawInput = {
targetChain,
targetChainNetwork,
nonce,
address,
assetId,
assetDecimals,
amount,
fee
};
// 5. Call withdraw
try {
const txHash = await hibitClient.withdraw(withdrawInput);
console.log('Withdraw submitted, txHash:', txHash);
} catch (err) {
console.error('Withdraw failed:', err);
}
}
withdrawWithFee();
SDK
Source code https://github.com/Deland-Labs/hibit-sdk
References
Last updated