getForeignAsset.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { PublicKey } from "@solana/web3.js";
  2. import { ethers } from "ethers";
  3. import { Bridge__factory } from "../ethers-contracts";
  4. import { ChainId } from "../utils";
  5. /**
  6. * Returns a foreign asset address on Ethereum for a provided native chain and asset address, AddressZero if it does not exist
  7. * @param tokenBridgeAddress
  8. * @param provider
  9. * @param originChain
  10. * @param originAsset zero pad to 32 bytes
  11. * @returns
  12. */
  13. export async function getForeignAssetEth(
  14. tokenBridgeAddress: string,
  15. provider: ethers.providers.Web3Provider,
  16. originChain: ChainId,
  17. originAsset: Uint8Array
  18. ) {
  19. const tokenBridge = Bridge__factory.connect(tokenBridgeAddress, provider);
  20. try {
  21. return await tokenBridge.wrappedAsset(originChain, originAsset);
  22. } catch (e) {
  23. return null;
  24. }
  25. }
  26. /**
  27. * Returns a foreign asset address on Solana for a provided native chain and asset address
  28. * @param tokenBridgeAddress
  29. * @param originChain
  30. * @param originAsset zero pad to 32 bytes
  31. * @returns
  32. */
  33. export async function getForeignAssetSol(
  34. tokenBridgeAddress: string,
  35. originChain: ChainId,
  36. originAsset: Uint8Array,
  37. tokenId: Uint8Array
  38. ) {
  39. const { wrapped_address } = await import("../solana/nft/nft_bridge");
  40. const wrappedAddress = wrapped_address(
  41. tokenBridgeAddress,
  42. originAsset,
  43. originChain,
  44. tokenId
  45. );
  46. const wrappedAddressPK = new PublicKey(wrappedAddress);
  47. // we don't require NFT accounts to exist, so don't check them.
  48. return wrappedAddressPK.toString();
  49. }