UNPKG

@lucoadam/zebec-wormhole-sdk

Version:

This sdk can be use to transfer assets across chains and to interact with the Zebec's xchain bridge smart contracts for passing message from EVM chain to solana specially to utilize the features of Zebec Streaming and Zebec Multisig Streaming protocol.

80 lines (77 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getForeignAsset = getForeignAsset; exports.getOriginalAsset = getOriginalAsset; exports.getTargetAsset = getTargetAsset; var _ethers = require("ethers"); var _wormholeSdk = require("@certusone/wormhole-sdk"); var _web = require("@solana/web3.js"); var _utils = require("../utils"); /** * Get address of asset for given token address in specified chain * @param signer evm signer * @param sourceTokenAddress token address of source chain * @param sourceChainId source chain id * @param targetChainId target chain id * @returns */ async function getTargetAsset(signer, sourceTokenAddress, sourceChainId, targetChainId) { if (_utils.isBrowser) { (0, _wormholeSdk.setDefaultWasm)("bundler"); } else { (0, _wormholeSdk.setDefaultWasm)("node"); } const originalAsset = await getOriginalAsset(signer, sourceTokenAddress, sourceChainId); if (originalAsset.chainId === targetChainId) { return (0, _wormholeSdk.tryUint8ArrayToNative)(originalAsset.assetAddress, originalAsset.chainId); } const foreignAsset = await getForeignAsset(originalAsset.assetAddress, originalAsset.chainId, targetChainId, signer); return foreignAsset; } /** * Get original asset info for given token address * @param signer evm signer * @param wrappedAddress token address * @param lookupChain chain id of given token address * @returns */ async function getOriginalAsset(signer, wrappedAddress, lookupChain) { let originalAsset; const tokenBridgeAddress = (0, _utils.getTokenBridgeAddressForChain)(lookupChain); if ((0, _wormholeSdk.isEVMChain)(lookupChain)) { originalAsset = await (0, _wormholeSdk.getOriginalAssetEth)(tokenBridgeAddress, signer, wrappedAddress, lookupChain); } else if ((0, _utils.isSolanaChain)(lookupChain)) { originalAsset = await (0, _wormholeSdk.getOriginalAssetSol)(new _web.Connection(_utils.SOLANA_HOST, "confirmed"), tokenBridgeAddress, wrappedAddress); } else { throw new _utils.NotSupportedError("This method currently doesn't support " + (0, _wormholeSdk.toChainName)(lookupChain)); } console.log("Original Asset: ", (0, _wormholeSdk.tryUint8ArrayToNative)(originalAsset.assetAddress, originalAsset.chainId)); return originalAsset; } /** * Get foreign asset for given origin asset in specified target chain * @param originAsset wormhole's 32 byte array of original asset * @param originChain chain id of original asset * @param targetChain foreign chain id to find the asset * @param signer evm signer * @returns asset address */ async function getForeignAsset(originAsset, originChain, targetChain, signer) { const tokenBridgeAddress = (0, _utils.getTokenBridgeAddressForChain)(targetChain); let asset; if ((0, _wormholeSdk.isEVMChain)(targetChain)) { asset = await (0, _wormholeSdk.getForeignAssetEth)(tokenBridgeAddress, signer, originChain, originAsset); } else if ((0, _utils.isSolanaChain)(targetChain)) { asset = await (0, _wormholeSdk.getForeignAssetSolana)(new _web.Connection(_utils.SOLANA_HOST, "confirmed"), tokenBridgeAddress, originChain, originAsset); } else { throw new _utils.NotSupportedError(`This method currently doesn't support ${(0, _wormholeSdk.toChainName)(targetChain)} chain`); } if (asset === _ethers.ethers.constants.AddressZero || !asset) { throw new Error("Given asset is not attested in " + (0, _wormholeSdk.toChainName)(targetChain)); } console.log("foreign asset:", asset); return asset; } //# sourceMappingURL=assets.js.map