Using npm:
> npm install @bnb-chain/zkbnb-js-l1-sdk
Using yarn:
> yarn add @bnb-chain/zkbnb-js-l1-sdk
Using pnpm:
> pnpm add @bnb-chain/zkbnb-js-l1-sdk
import { Wallet, Provider, getZkBNBDefaultProvider } from '@bnb-chain/zkbnb-js-l1-sdk';
const rpcEndpoint = 'https://data-seed-prebsc-2-s1.binance.org:8545'; // bsc testnest rpc
const ethWallet = new ethers.Wallet(
'your private key',
new ethers.providers.JsonRpcProvider(rpcEndpoint)
);
const provider = getZkBNBDefaultProvider('bscTestnet'); // bsc or bscTestnet
// or by this method
// const testEndpoint = 'https://testapi.zkbnbchain.org'; // bsc testnest
// const provider = await Provider.newHttpProvider(testEndpoint);
const wallet = await Wallet.fromZkBNBSigner(ethWallet, provider);
// this is used sign message by
const result = await provider.ethMessageSigner().getEthMessageSignature("message");
const address = provider.address();
const assetAddress = await wallet.resolveTokenAddress('asset id');
const assetId = await wallet.resolveTokenId('asset address');
const tokenAddress = 'BEP20 token address';
// Before adding, it is recommended to check whether the asset exists and whether it can be added.
await wallet.addAsset({ tokenAddress });
const isApproved = await wallet.isBEP20DepositsApproved('BEP20 asset address');
const result = await wallet.approveBEP20TokenDeposits('BEP20 address');
// You can check if it is successful approved by the following method
const isApproved = await wallet.isBEP20DepositsApproved("BEP20 address");
const tokenAddress = await wallet.resolveTokenAddress(0);
const result = await wallet.deposit({
to: wallet.address(),
tokenAddress: "0x0000000000000000000000000000000000000000",
amount: ethers.utils.parseEther('0.001'),
});
Deposit funds from the BSC to the zkBNB.
To do the BEP20 token transfer, this token transfer should be approved. User can make BEP20 deposits approved forever using approveBEP20TokenDeposits("token address"), or the user can approve the exact amount (required for a deposit) upon each deposit using approveBEP20TokenDeposits("token address", "exact amount"), but this is not recommended.
const result = await wallet.deposit({
to: wallet.address(),
tokenAddress: 'BEP20 Address',
amount: ethers.utils.parseEther('0.001'),
});
const approveTx = await this.approveForAllERC721TokenDeposits('nft address');
// You can check if it is successful approved by the following method
const isApproved = await this.isERC721DepositsApprovedForAll('nft address');
Deposit NFT from BSC to zkBNB, Only supports the nft created by zkBNB.
To do the NFT transfer, this transfer should be approved by approveForAllERC721TokenDeposits("nft address") once.
const depositResult = await wallet.depositNFT({
to: 'wallet address', // which address to deposit to
tokenAddress: 'nft address',
tokenId: 'nft ID',
});
Withdraw BNB or BEP20 from zkBNB to BSC
const result = await wallet.requestFullExit({
tokenAddress: 'asset address',
accountIndex: 'account index',
});
Withdraw NFT from zkBNB to BSC
const requestResult = await wallet.requestFullExitNft({
tokenId: 'nft ID',
accountIndex: 'account index',
});
const name = 'collection name';
const symbol = 'collection symbol';
const collectionId = 'collection Id';
await wallet.deployAndRegisterNFTFactory({
collectionId,
name,
symbol,
});
const creatorAddress = 'collection creator\'s wallet address';
const collectionId = 1;
// if zero address is returned, it means a dedicated nft address can be bound
const nftAddress = await wallet.getNFTAddress(creatorAddress, collectionId);
const nftContentType = 0; // 0-ipfs, 1-Greenfield
const nftContentHash = 'nft content hash';
const tokenURI = await wallet.getNftTokenURI(nftContentType, nftContentHash);
// Please change the value of the parameter according to the actual situation
const address = 'wallet address';
const assetAddress = 'asset address';
const pendingBalance = await wallet.getPendingBalance(address, assetAddress);
const pendingBalance = await wallet.getTokenBalance('asset address');