UNPKG

@goat-sdk/plugin-zilliqa

Version:

<div align="center"> <a href="https://github.com/goat-sdk/goat">

2 lines (1 loc) 5.75 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunk6TGFZMSGjs = require('./chunk-6TGFZMSG.js');var _core = require('@goat-sdk/core');var _walletzilliqa = require('@goat-sdk/wallet-zilliqa');var _crypto = require('@zilliqa-js/crypto');var _util = require('@zilliqa-js/util');var _viem = require('viem'); var g = _interopRequireWildcard(_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}_chunk6TGFZMSGjs.a.call(void 0, p,"_ts_decorate");function o(f,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(f,t)}_chunk6TGFZMSGjs.a.call(void 0, o,"_ts_metadata");var l=class{static{_chunk6TGFZMSGjs.a.call(void 0, this,"ZilliqaService")}async convertToBech32(t,e){return _crypto.toBech32Address.call(void 0, e.address)}async convertFromBech32(t,e){return _crypto.fromBech32Address.call(void 0, e.address)}async transferFromEvmAddress(t,e){try{let r=_util.validation.isBech32(e.toAddress)?_crypto.fromBech32Address.call(void 0, 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=_util.validation.isBech32(e.toAddress)?_crypto.fromBech32Address.call(void 0, e.toAddress):e.toAddress,s=_crypto.toChecksumAddress.call(void 0, r),n=t.getZilliqa(),u=t.getZilliqaChainId()<<16|1;try{let a=await n.blockchain.createTransaction(n.transactions.new({toAddr:s,amount:_util.units.toQa(e.amount,_util.units.Units.Zil),gasPrice:new (0, _util.BN)("2000000000"),gasLimit:new (0, _util.Long)(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(_optionalChain([r, 'optionalAccess', _ => _.error]))if(_optionalChain([r, 'access', _2 => _2.error, 'optionalAccess', _3 => _3.code])===-5)d="This account has not yet been created so its balance is 0";else return`An error occurred - ${_optionalChain([r, 'access', _4 => _4.error, 'optionalAccess', _5 => _5.message])}`;else{let a=_optionalChain([r, 'optionalAccess', _6 => _6.result]);s=_optionalChain([a, 'optionalAccess', _7 => _7.balance]),n=_optionalChain([a, 'optionalAccess', _8 => _8.nonce])}return{value:_util.units.fromQa(new (0, _util.BN)(s),_util.units.Units.Zil),decimals:12,symbol:"ZIL",name:"Zil",inBaseUnits:s,nonce:n,comment:d}}};p([_core.Tool.call(void 0, {description:"Convert addresses from hex to bech32 format"}),o("design:type",Function),o("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClient>"u"?Object:_walletzilliqa.ZilliqaWalletClient,typeof _chunk6TGFZMSGjs.c>"u"?Object:_chunk6TGFZMSGjs.c]),o("design:returntype",Promise)],l.prototype,"convertToBech32",null);p([_core.Tool.call(void 0, {description:"Convert addresses from bech32 to hex format"}),o("design:type",Function),o("design:paramtypes",[typeof _walletzilliqa.ZilliqaWalletClient>"u"?Object:_walletzilliqa.ZilliqaWalletClient,typeof _chunk6TGFZMSGjs.c>"u"?Object:_chunk6TGFZMSGjs.c]),o("design:returntype",Promise)],l.prototype,"convertFromBech32",null);p([_core.Tool.call(void 0, {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 _walletzilliqa.ZilliqaWalletClient>"u"?Object:_walletzilliqa.ZilliqaWalletClient,typeof _chunk6TGFZMSGjs.b>"u"?Object:_chunk6TGFZMSGjs.b]),o("design:returntype",Promise)],l.prototype,"transferFromEvmAddress",null);p([_core.Tool.call(void 0, {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 _walletzilliqa.ZilliqaWalletClient>"u"?Object:_walletzilliqa.ZilliqaWalletClient,typeof _chunk6TGFZMSGjs.b>"u"?Object:_chunk6TGFZMSGjs.b]),o("design:returntype",Promise)],l.prototype,"transferFromZilliqaAddress",null);p([_core.Tool.call(void 0, {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 _walletzilliqa.ZilliqaWalletClient>"u"?Object:_walletzilliqa.ZilliqaWalletClient,typeof _chunk6TGFZMSGjs.c>"u"?Object:_chunk6TGFZMSGjs.c]),o("design:returntype",Promise)],l.prototype,"getZilliqaAddressBalance",null);exports.a = l;