@goat-sdk/plugin-zilliqa
Version:
<div align="center"> <a href="https://github.com/goat-sdk/goat">
2 lines (1 loc) • 3.64 kB
JavaScript
import{a as y,b as m,c}from"./chunk-T7C5FZET.mjs";import{Tool as h}from"@goat-sdk/core";import{ZilliqaWalletClient as i}from"@goat-sdk/wallet-zilliqa";import{fromBech32Address as A,toBech32Address as w,toChecksumAddress as j}from"@zilliqa-js/crypto";import{BN as Z,Long as q,units as b,validation as v}from"@zilliqa-js/util";import*as g from"viem";function p(f,t,e,r){var s=arguments.length,n=s<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,e):r,d;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(f,t,e,r);else for(var u=f.length-1;u>=0;u--)(d=f[u])&&(n=(s<3?d(n):s>3?d(t,e,n):d(t,e))||n);return s>3&&n&&Object.defineProperty(t,e,n),n}y(p,"_ts_decorate");function o(f,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(f,t)}y(o,"_ts_metadata");var l=class{static{y(this,"ZilliqaService")}async convertToBech32(t,e){return w(e.address)}async convertFromBech32(t,e){return A(e.address)}async transferFromEvmAddress(t,e){try{let r=v.isBech32(e.toAddress)?A(e.toAddress):e.toAddress,s=g.getAddress(r),n=g.parseEther(e.amount);return(await t.getEVM().sendTransaction({to:s,value:n})).hash}catch(r){throw new Error(`Failed to send ZIL: ${r}`)}}async transferFromZilliqaAddress(t,e){let r=v.isBech32(e.toAddress)?A(e.toAddress):e.toAddress,s=j(r),n=t.getZilliqa(),u=t.getZilliqaChainId()<<16|1;try{let a=await n.blockchain.createTransaction(n.transactions.new({toAddr:s,amount:b.toQa(e.amount,b.Units.Zil),gasPrice:new Z("2000000000"),gasLimit:new q(50),version:u}));return a.isConfirmed()?`the transfer succeeded with transaction id ${a.id}`:a.isRejected()?"the transfer failed and no ZIL was transferred":"the transfer timed out and probably did not succeed"}catch(a){throw new Error(`an error occurred and the transfer probably did not succeed - ${a}`)}}async getZilliqaAddressBalance(t,e){let r=await t.getZilliqa().blockchain.getBalance(e.address),s="0",n="0",d=null;if(r?.error)if(r.error?.code===-5)d="This account has not yet been created so its balance is 0";else return`An error occurred - ${r.error?.message}`;else{let a=r?.result;s=a?.balance,n=a?.nonce}return{value:b.fromQa(new Z(s),b.Units.Zil),decimals:12,symbol:"ZIL",name:"Zil",inBaseUnits:s,nonce:n,comment:d}}};p([h({description:"Convert addresses from hex to bech32 format"}),o("design:type",Function),o("design:paramtypes",[typeof i>"u"?Object:i,typeof c>"u"?Object:c]),o("design:returntype",Promise)],l.prototype,"convertToBech32",null);p([h({description:"Convert addresses from bech32 to hex format"}),o("design:type",Function),o("design:paramtypes",[typeof i>"u"?Object:i,typeof c>"u"?Object:c]),o("design:returntype",Promise)],l.prototype,"convertFromBech32",null);p([h({description:"Transfer ZIL from an EVM address to another EVM or Zilliqa address, in either hex or bech32 format."}),o("design:type",Function),o("design:paramtypes",[typeof i>"u"?Object:i,typeof m>"u"?Object:m]),o("design:returntype",Promise)],l.prototype,"transferFromEvmAddress",null);p([h({description:"Transfer ZIL from a Zilliqa address to another EVM or Zilliqa address, in either hex or bech32 format."}),o("design:type",Function),o("design:paramtypes",[typeof i>"u"?Object:i,typeof m>"u"?Object:m]),o("design:returntype",Promise)],l.prototype,"transferFromZilliqaAddress",null);p([h({description:"Return the balance of a bech32 address, or an EVM address. A bech32 address starts with 'zil'. An EVM address starts with '0x'"}),o("design:type",Function),o("design:paramtypes",[typeof i>"u"?Object:i,typeof c>"u"?Object:c]),o("design:returntype",Promise)],l.prototype,"getZilliqaAddressBalance",null);export{l as a};