@paraswap/sdk
Advanced tools
Comparing version 5.6.0-alpha.3 to 5.6.0-alpha.4
@@ -1,6 +0,6 @@ | ||
export { constructFetcher as constructAxiosFetcher } from './axios'; | ||
export { constructFetcher as constructFetchFetcher } from './fetch'; | ||
export { constructContractCaller as constructEthersContractCaller } from './ethers'; | ||
export { constructContractCaller as constructWeb3ContractCaller } from './web3'; | ||
export { constructFetcher as constructAxiosFetcher } from './fetchers/axios'; | ||
export { constructFetcher as constructFetchFetcher } from './fetchers/fetch'; | ||
export { constructContractCaller as constructEthersContractCaller, EthersProviderDeps, } from './providers/ethers'; | ||
export { constructContractCaller as constructWeb3ContractCaller, Web3UnpromiEvent, } from './providers/web3'; | ||
export { isFetcherError } from './misc'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,21 +0,29 @@ | ||
import type { Contract, ContractFunction, PopulatedTransaction, BigNumber } from 'ethers'; | ||
import type { Contract as EthersContract, ContractFunction as EthersContractFunction, PopulatedTransaction as EthersPopulatedTransaction, BigNumber as EthersBigNumber } from 'ethers'; | ||
import type { ContractSendMethod as Web3ContractSendMethod, Contract as Web3Contract } from 'web3-eth-contract'; | ||
import type { AxiosError } from 'axios'; | ||
export declare type ContractWithMethod<T extends string> = Contract & { | ||
readonly [method in T]: ContractFunction; | ||
export declare type EthersContractWithMethod<T extends string> = EthersContract & { | ||
readonly [method in T]: EthersContractFunction; | ||
} & { | ||
readonly functions: { | ||
[method in T]: ContractFunction; | ||
[method in T]: EthersContractFunction; | ||
}; | ||
readonly callStatic: { | ||
[method in T]: ContractFunction; | ||
[method in T]: EthersContractFunction; | ||
}; | ||
readonly estimateGas: { | ||
[method in T]: ContractFunction<BigNumber>; | ||
[method in T]: EthersContractFunction<EthersBigNumber>; | ||
}; | ||
readonly populateTransaction: { | ||
[method in T]: ContractFunction<PopulatedTransaction>; | ||
[method in T]: EthersContractFunction<EthersPopulatedTransaction>; | ||
}; | ||
}; | ||
export declare function contractHasMethods<T extends string>(contract: Contract, ...methods: T[]): contract is ContractWithMethod<T>; | ||
export declare function assertContractHasMethods<T extends string>(contract: Contract, ...methods: T[]): asserts contract is ContractWithMethod<T>; | ||
export declare function ethersContractHasMethods<T extends string>(contract: EthersContract, ...methods: T[]): contract is EthersContractWithMethod<T>; | ||
export declare function assertEthersContractHasMethods<T extends string>(contract: EthersContract, ...methods: T[]): asserts contract is EthersContractWithMethod<T>; | ||
export declare type Web3ContractWithMethod<T extends string> = Web3Contract & { | ||
methods: { | ||
[method in T]: Web3ContractSendMethod; | ||
}; | ||
}; | ||
export declare function web3ContractHasMethods<T extends string>(contract: Web3Contract, ...methods: T[]): contract is Web3ContractWithMethod<T>; | ||
export declare function assertWeb3ContractHasMethods<T extends string>(contract: Web3Contract, ...methods: T[]): asserts contract is Web3ContractWithMethod<T>; | ||
export declare const objectToFilledEntries: <T extends Record<string, unknown>>(object: T) => [string, string][]; | ||
@@ -22,0 +30,0 @@ export declare const constructSearchString: <U extends Record<string, unknown>>(queryOptions: U) => `?${string}` | ''; |
@@ -1,25 +0,21 @@ | ||
import { constructApproveToken, ApproveTokenFunctions } from './approve'; | ||
import { constructGetBalances, GetBalancesFunctions } from './balance'; | ||
import { constructGetSpender, GetSpenderFunctions } from './spender'; | ||
import { constructGetAdapters, AdaptersFunctions } from './adapters'; | ||
import { constructGetRate, GetRateFunctions } from './rates'; | ||
import { constructGetTokens, constructToken, GetTokensFunctions, Token, PriceString } from './token'; | ||
import { BuildTxFunctions, constructBuildTx } from './transaction'; | ||
import { constructEthersContractCaller, constructWeb3ContractCaller, constructAxiosFetcher, constructFetchFetcher, isFetcherError } from './helpers'; | ||
import { ConstructBaseInput, ConstructFetchInput, ConstructProviderFetchInput, Address } from './types'; | ||
import { UnionToIntersection } from 'ts-essentials'; | ||
export type { TransactionParams } from './transaction'; | ||
import { constructApproveToken, ApproveTokenFunctions } from './methods/approve'; | ||
import { constructGetBalances, GetBalancesFunctions, isAllowance, Allowance } from './methods/balance'; | ||
import { constructGetSpender, GetSpenderFunctions } from './methods/spender'; | ||
import { constructGetAdapters, GetAdaptersFunctions } from './methods/adapters'; | ||
import { constructGetRate, GetRateFunctions } from './methods/rates'; | ||
import { constructGetTokens, GetTokensFunctions } from './methods/token'; | ||
import { BuildTxFunctions, constructBuildTx } from './methods/transaction'; | ||
import { constructEthersContractCaller, constructWeb3ContractCaller, constructAxiosFetcher, constructFetchFetcher, isFetcherError, EthersProviderDeps } from './helpers'; | ||
import { ConstructFetchInput, ConstructProviderFetchInput, Address, AddressOrSymbol, Token, PriceString, TxHash, TxSendOverrides } from './types'; | ||
export type { TransactionParams } from './methods/transaction'; | ||
export type { Web3UnpromiEvent } from './helpers'; | ||
export * from './constants'; | ||
export { constructApproveToken, constructGetBalances, // getAllowances is inside | ||
constructGetSpender, // this was getTokenTransferProxy | ||
constructGetTokens, constructBuildTx, constructEthersContractCaller, constructWeb3ContractCaller, constructAxiosFetcher, constructFetchFetcher, constructToken, constructGetAdapters, constructGetRate, isFetcherError, }; | ||
export type { ApproveTokenFunctions, GetBalancesFunctions, GetSpenderFunctions, GetTokensFunctions, BuildTxFunctions, ConstructFetchInput, ConstructProviderFetchInput, AdaptersFunctions as ConstructAdaptersFunctions, Token, Address, PriceString, }; | ||
export declare type SDKConfig = ConstructProviderFetchInput & ConstructFetchInput; | ||
export declare type AllSDKMethods = GetBalancesFunctions & GetTokensFunctions & GetSpenderFunctions & ApproveTokenFunctions & BuildTxFunctions & AdaptersFunctions & GetRateFunctions; | ||
declare type AnyFunction = (...args: any[]) => any; | ||
declare type SDKFunction<T extends ConstructBaseInput> = (config: T) => Record<string, AnyFunction>; | ||
declare type IntersectionOfReturns<Funcs extends AnyFunction[]> = UnionToIntersection<ReturnType<Funcs[number]>>; | ||
export declare const constructPartialSDK: <T extends ConstructBaseInput, Funcs extends [SDKFunction<T>, ...SDKFunction<T>[]]>(config: T, ...funcs: Funcs) => UnionToIntersection<ReturnType<Funcs[number]>>; | ||
export declare const constructSDK: (config: SDKConfig) => AllSDKMethods; | ||
constructGetTokens, constructBuildTx, constructEthersContractCaller, constructWeb3ContractCaller, constructAxiosFetcher, constructFetchFetcher, constructGetAdapters, constructGetRate, isFetcherError, isAllowance, }; | ||
export type { Allowance, EthersProviderDeps, ApproveTokenFunctions, GetBalancesFunctions, GetSpenderFunctions, GetTokensFunctions, GetAdaptersFunctions, GetRateFunctions, BuildTxFunctions, ConstructFetchInput, ConstructProviderFetchInput, Token, Address, AddressOrSymbol, PriceString, TxHash, TxSendOverrides, }; | ||
export { SDKConfig, constructPartialSDK } from './sdk/partial'; | ||
export { AllSDKMethods, constructFullSDK } from './sdk/full'; | ||
export { SDKFetchMethods, constructSimpleSDK } from './sdk/simple'; | ||
export { ParaSwap } from './legacy'; | ||
//# sourceMappingURL=index.d.ts.map |
import type { AxiosStatic } from 'axios'; | ||
import type Web3 from 'web3'; | ||
import type { SendOptions } from 'web3-eth-contract'; | ||
import type { Contract as EthersContract } from '@ethersproject/contracts'; | ||
import type { Signer } from '@ethersproject/abstract-signer'; | ||
import type { BaseProvider } from '@ethersproject/providers'; | ||
import type { ContractTransaction } from '@ethersproject/contracts'; | ||
import type { Address, OptimalRate } from 'paraswap-core'; | ||
import { SwapSide } from '../constants'; | ||
import { AllSDKMethods, PriceString } from '..'; | ||
import type { RateOptions } from '../rates'; | ||
import type { BuildOptions, TransactionParams } from '../transaction'; | ||
import type { AddressOrSymbol, Token } from '../token'; | ||
import type { Allowance } from '../balance'; | ||
import type { FetcherFunction } from '../types'; | ||
import { Web3UnpromiEvent, EthersProviderDeps } from '../helpers'; | ||
import type { RateOptions } from '../methods/rates'; | ||
import type { BuildOptions, TransactionParams } from '../methods/transaction'; | ||
import type { AddressOrSymbol, Token, FetcherFunction } from '../types'; | ||
import type { Allowance } from '../methods/balance'; | ||
export declare type APIError = { | ||
@@ -20,6 +18,3 @@ message: string; | ||
}; | ||
interface EthersProviderDeps { | ||
providerOrSigner: BaseProvider | Signer; | ||
Contract: typeof EthersContract; | ||
} | ||
declare type TxResponse = Web3UnpromiEvent | ContractTransaction; | ||
/** @deprecated */ | ||
@@ -34,7 +29,8 @@ export declare class ParaSwap { | ||
fetch?: typeof fetch | undefined; | ||
sdk: Partial<AllSDKMethods>; | ||
sdk: Partial<AllSDKMethods<TxResponse>>; | ||
fetcher: FetcherFunction; | ||
constructor(network?: number, apiURL?: string, web3Provider?: Web3 | undefined, ethersDeps?: EthersProviderDeps | undefined, // need to be a provider with signer for approve requests | ||
account?: string | undefined, axios?: AxiosStatic | undefined, fetch?: typeof globalThis.fetch | undefined); | ||
private handleAPIError; | ||
private static handleAPIError; | ||
private static extractHasFromTxResponse; | ||
setWeb3Provider(web3Provider: Web3, account?: string): this; | ||
@@ -41,0 +37,0 @@ setEthersProvider(ethersDeps: EthersProviderDeps, account?: string): this; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("paraswap-core"),e=require("ts-essentials");function r(t,e,r,n,o,a,s){try{var i=t[a](s),u=i.value}catch(t){return void r(t)}i.done?e(u):Promise.resolve(u).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(o,a){var s=t.apply(e,n);function i(t){r(s,o,a,i,u,"next",t)}function u(t){r(s,o,a,i,u,"throw",t)}i(void 0)}))}}function o(){return o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},o.apply(this,arguments)}function a(t){return a=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},a(t)}function s(t,e){return s=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},s(t,e)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function u(t,e,r){return u=i()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(t,n));return r&&s(o,r.prototype),o},u.apply(null,arguments)}function c(t){var e="function"==typeof Map?new Map:void 0;return c=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return u(t,arguments,a(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,t)},c(t)}function p(t,e){if(null==t)return{};var r,n,o={},a=Object.keys(t);for(n=0;n<a.length;n++)e.indexOf(r=a[n])>=0||(o[r]=t[r]);return o}function h(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var f={exports:{}};!function(t){var e=function(t){var e,r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",s=o.asyncIterator||"@@asyncIterator",i=o.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var o=Object.create((e&&e.prototype instanceof w?e:w).prototype),a=new L(n||[]);return o._invoke=function(t,e,r){var n=h;return function(o,a){if(n===d)throw new Error("Generator is already running");if(n===l){if("throw"===o)throw a;return O()}for(r.method=o,r.arg=a;;){var s=r.delegate;if(s){var i=A(s,r);if(i){if(i===v)continue;return i}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===h)throw n=l,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=d;var u=p(t,e,r);if("normal"===u.type){if(n=r.done?l:f,u.arg===v)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n=l,r.method="throw",r.arg=u.arg)}}}(t,r,a),o}function p(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var h="suspendedStart",f="suspendedYield",d="executing",l="completed",v={};function w(){}function m(){}function g(){}var y={};u(y,a,(function(){return this}));var k=Object.getPrototypeOf,x=k&&k(k(R([])));x&&x!==r&&n.call(x,a)&&(y=x);var b=g.prototype=w.prototype=Object.create(y);function E(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function T(t,e){function r(o,a,s,i){var u=p(t[o],t,a);if("throw"!==u.type){var c=u.arg,h=c.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){r("next",t,s,i)}),(function(t){r("throw",t,s,i)})):e.resolve(h).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,i)}))}i(u.arg)}var o;this._invoke=function(t,n){function a(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(a,a):a()}}function A(t,r){var n=t.iterator[r.method];if(n===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=e,A(t,r),"throw"===r.method))return v;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var o=p(n,t.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,v;var a=o.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,v):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,v)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function R(t){if(t){var r=t[a];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,s=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return s.next=s}}return{next:O}}function O(){return{value:e,done:!0}}return m.prototype=g,u(b,"constructor",g),u(g,"constructor",m),m.displayName=u(g,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===m||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,u(t,i,"GeneratorFunction")),t.prototype=Object.create(b),t},t.awrap=function(t){return{__await:t}},E(T.prototype),u(T.prototype,s,(function(){return this})),t.AsyncIterator=T,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var s=new T(c(e,r,n,o),a);return t.isGeneratorFunction(r)?s:s.next().then((function(t){return t.done?t.value:s.next()}))},E(b),u(b,i,"Generator"),u(b,a,(function(){return this})),u(b,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=R,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(S),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return i.type="throw",i.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var s=this.tryEntries[a],i=s.completion;if("root"===s.tryLoc)return o("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),c=n.call(s,"finallyLoc");if(u&&c){if(this.prev<s.catchLoc)return o(s.catchLoc,!0);if(this.prev<s.finallyLoc)return o(s.finallyLoc)}else if(u){if(this.prev<s.catchLoc)return o(s.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<s.finallyLoc)return o(s.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var s=a?a.completion:{};return s.type=t,s.arg=e,a?(this.method="next",this.next=a.finallyLoc,v):this.complete(s)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),v},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),v}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:R(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),v}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}(f);var d=f.exports,l="https://api.paraswap.io",v=function(t){var e=t.apiURL,r=t.fetcher,o=(void 0===e?l:e)+"/adapters/contracts?network="+t.network,a=function(){var t=n(d.mark((function t(e){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r({url:o,method:"GET",signal:e});case 2:return t.abrupt("return",t.sent.TokenTransferProxy);case 4:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}();return{getSpender:a}},w=[{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"}],m=function(t){var e,r=v(t).getSpender,o=function(){var o=n(d.mark((function n(o,a,s,i){var u;return d.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(void 0===s&&(s={}),n.t0=e,n.t0){n.next=6;break}return n.next=5,r(i);case 5:n.t0=e=n.sent;case 6:return u=n.t0,n.next=9,t.contractCaller({address:a,abi:w,contractMethod:"approve",args:[u,o],overrides:s,static:!1});case 9:return n.abrupt("return",n.sent);case 11:case"end":return n.stop()}}),n)})));return function(t,e,r,n){return o.apply(this,arguments)}}(),a=function(){var t=n(d.mark((function t(e,r){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",Promise.all(r.map((function(t){return o(e,t)}))));case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}();return{approveToken:o,approveTokenBulk:a}},g=["tokenType","mainConnector","connectors","network"],y=function(t){var e=t.tokenType,r=void 0===e?"ERC20":e,n=t.mainConnector,a=void 0===n?"ETH":n,s=t.connectors,i=void 0===s?[]:s,u=t.network,c=void 0===u?1:u,h=p(t,g);return o({tokenType:r,connectors:i.length>0?i:[a],mainConnector:a,network:c},h)},k=function(t){var e=t.apiURL,r=t.fetcher,o=(void 0===e?l:e)+"/tokens/"+t.network,a=function(){var t=n(d.mark((function t(e){var n;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r({url:o,method:"GET",signal:e});case 2:return n=t.sent.tokens.map(y),t.abrupt("return",n);case 5:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}();return{getTokens:a}},x={message:"Not Found"},b=function(t){var e=t.apiURL,r=t.fetcher,o=(void 0===e?l:e)+"/users/tokens/"+t.network,a=function(){var t=n(d.mark((function t(e,n){var a,s;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=o+"/"+e,t.next=3,r({url:a,method:"GET",signal:n});case 3:return s=t.sent.tokens.map(y),t.abrupt("return",s);case 6:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),s=function(){var t=n(d.mark((function t(e,n,a){var s,i;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=o+"/"+e+"/"+n,t.next=3,r({url:s,method:"GET",signal:a});case 3:if((i=t.sent).token){t.next=6;break}return t.abrupt("return",x);case 6:return t.abrupt("return",y(i.token));case 7:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),i=function(){var t=n(d.mark((function t(e,r,n){var o,s;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new Set(r.map((function(t){return t.toLowerCase()}))),t.next=3,a(e,n);case 3:return s=t.sent.filter((function(t){return o.has(t.address.toLowerCase())})).map((function(t){var e=t.allowance;return{tokenAddress:t.address,allowance:void 0===e?"0":e}})),t.abrupt("return",s);case 6:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),u=function(){var t=n(d.mark((function t(e,r,n){var o,a;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,s(e,r,n);case 2:if(!("message"in(o=t.sent))){t.next=5;break}return t.abrupt("return",o);case 5:return t.abrupt("return",{tokenAddress:o.address,allowance:void 0===(a=o.allowance)?"0":a});case 7:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}();return{getBalance:s,getBalances:a,getAllowance:u,getAllowances:i}};function E(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];return r.every((function(e){return"function"==typeof t[e]}))}function T(t){for(var r=arguments.length,n=new Array(r>1?r-1:0),o=1;o<r;o++)n[o-1]=arguments[o];e.assert(E.apply(void 0,[t].concat(n)),"Contract must have methods: "+n.join(", "))}var A,P=function(t){var e=Object.entries(t).filter((function(t){return void 0!==t[1]})).map((function(t){return[t[0],String(t[1])]})),r=new URLSearchParams(e).toString();return r&&"?"+r},S=function(t){return t instanceof L},L=function(t){var e,r;function n(e){var r,o=e.code,a=e.request,s=e.response,i=e.isAxiosError,u=e.message;if((r=t.call(this)||this).code=void 0,r.status=void 0,r.request=void 0,r.response=void 0,r.isAxiosError=!1,Error.captureStackTrace&&Error.captureStackTrace(h(r),n),r.name=r.constructor.name,r.code=o,r.request=a,r.isAxiosError=i,!s)return r.message=u,h(r);r.response=s;var c=s.data;return r.status=s.status,r.message=(null==c?void 0:c.error)||u,r}return r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,s(e,r),n}(c(Error)),R=function(t){var e=t.apiURL,r=void 0===e?l:e,o=t.network,a=t.fetcher;function s(){return(s=n(d.mark((function t(e,n){var s,i;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=P({network:o,namesOnly:e&&"namesOnly"in e?e.namesOnly:void 0}),i=r+"/adapters"+("list"===(null==e?void 0:e.type)?"/list":"")+s,t.next=4,a({url:i,method:"GET",signal:n});case 4:return t.abrupt("return",t.sent);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}return{getAdapters:function(t,e){return s.apply(this,arguments)}}},O=["srcToken","destToken"],j=["route"],_=["options"],C=["excludePricingMethods","excludeContractMethods","includeContractMethods","adapterVersion","partner","includeDEXS","excludeDEXS"];!function(t){t.megapath="megapath",t.multipath="multipath",t.simplepath="simplepath"}(A||(A={}));var M=function(t){var e=t.apiURL,r=t.network,a=t.fetcher,s=(void 0===e?l:e)+"/prices",i=function(){var t=n(d.mark((function t(e,n){var i,u,c,h,f,l;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=e.srcToken,u=e.destToken,c=p(e,O),h=B(c),f=P(o({srcToken:i,destToken:u,network:r},h)),l=s+"/"+f,t.next=6,a({url:l,method:"GET",signal:n});case 6:return t.abrupt("return",t.sent.priceRoute);case 8:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),u=function(){var t=n(d.mark((function t(e,n){var i,u,c,h,f,l;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i=e.route,u=p(e,j),!(i.length<2)){t.next=3;break}throw new Error("Invalid Route");case 3:return c=B(u),h=i.join("-"),f=P(o({route:h,network:r},c)),l=s+"/"+f,t.next=9,a({url:l,method:"GET",signal:n});case 9:return t.abrupt("return",t.sent.priceRoute);case 11:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}();return{getRate:i,getRateByRoute:u}};function B(t){var e=t.options,r=void 0===e?{}:e,n=p(t,_),a=r.excludePricingMethods,s=r.excludeContractMethods,i=r.includeContractMethods,u=r.adapterVersion,c=r.partner,h=void 0===c?"paraswap.io":c,f=r.includeDEXS,d=r.excludeDEXS,l=p(r,C);return I(f),I(d),o({version:u,excludePricingMethods:null==a?void 0:a.join(","),excludeContractMethods:null==s?void 0:s.join(","),includeContractMethods:null==i?void 0:i.join(","),partner:h,includeDEXS:f,excludeDEXS:d},n,l)}function I(t){if(t&&!t.split(",").length)throw new Error("Invalid DEX list")}var D=function(r){var o=r.apiURL,a=r.fetcher,s=(void 0===o?l:o)+"/transactions/"+r.network,i=function(){var r=n(d.mark((function r(n,o,i){var u,c,p,h;return d.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0===o&&(o={}),c=(u=n.priceRoute.side)===t.SwapSide.SELL?"Source Amount Mismatch":"Destination Amount Mismatch",e.assert((void 0,void 0,void 0,d=(f={queryParams:{srcAmount:n.srcAmount,destAmount:n.destAmount},side:u,priceRoute:n.priceRoute}).queryParams,l=f.priceRoute,(v=f.side===t.SwapSide.SELL?[d.srcAmount,l.srcAmount]:[d.destAmount,l.destAmount])[0]===v[1]),c),p=P(o),h={url:s+"/"+p,method:"POST",data:n,signal:i},r.next=9,a(h);case 9:return r.abrupt("return",r.sent);case 11:case"end":return r.stop()}var f,d,l,v}),r)})));return function(t,e,n){return r.apply(this,arguments)}}();return{buildTx:i}},G=function(t){return function(){var e=n(d.mark((function e(r){return d.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.request(r);case 3:return e.abrupt("return",e.sent.data);case 8:if(e.prev=8,e.t0=e.catch(0),!t.isAxiosError(e.t0)){e.next=12;break}throw new L(e.t0);case 12:throw e.t0;case 13:case"end":return e.stop()}}),e,null,[[0,8]])})));return function(t){return e.apply(this,arguments)}}()},U=function(t){return function(){var e=n(d.mark((function e(r){var n,o,a,s,i,u,c;return d.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,n=r.url,a=r.signal,s="POST"===(o=r.method)?JSON.stringify(r.data):null,i="POST"===o&&s?{"Content-Type":"application/json"}:void 0,e.next=6,t(n,{method:o,body:s,signal:a,headers:i});case 6:return u=e.sent,e.next=9,u.json();case 9:if(c=e.sent,u.ok){e.next=12;break}throw new L({code:String(u.status),response:{data:c,status:u.status,statusText:u.statusText,headers:Object.fromEntries(u.headers.entries()),config:{url:n,method:o}},message:u.statusText,isAxiosError:!1});case 12:return e.abrupt("return",c);case 15:if(e.prev=15,e.t0=e.catch(0),!(e.t0 instanceof L)){e.next=19;break}throw e.t0;case 19:throw new L(e.t0);case 20:case"end":return e.stop()}}),e,null,[[0,15]])})));return function(t){return e.apply(this,arguments)}}()},F=["block","gas"],z=["gas","from"],N=function(t,r){var a=t.providerOrSigner,s=t.Contract,i=function(){var t=n(d.mark((function t(n){var i,u,c,h,f,l,v,w,m,g,y,k,x,b,E,A,P,S,L,R;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!n.static){t.next=8;break}return u=n.contractMethod,c=n.args,h=n.overrides,T(f=new s(n.address,n.abi,a),u),l=h.block,v=h.gas,w=p(h,F),m=o({},w,{blockTag:l,gasLimit:v}),t.abrupt("return",(i=f.callStatic)[u].apply(i,c.concat([m])));case 8:return e.assert(r,"account must be specified to create a signer"),e.assert(q(a)||X(a),"ethers must be an instance of Signer or JsonRpcProvider to create a signer"),g=n.address,y=n.abi,k=n.contractMethod,x=n.args,b=n.overrides,E="getSigner"in a?a.getSigner(r):a,T(A=new s(g,y,E),k),P=b.gas,S=p(b,z),L=o({},S,{gasLimit:P}),R=L,t.next=19,A[k].apply(A,x.concat([R]));case 19:return t.abrupt("return",t.sent.hash);case 21:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}();return i};function q(t){return function(t){return"_isProvider"in t&&t._isProvider}(t)&&"getSigner"in t}function X(t){return"_isSigner"in t&&t._isSigner}var J=["block","gas"],K=["gas","from"],V=function(t,r){var a=function(){var a=n(d.mark((function n(a){var s,i,u,c,h,f,l,v,w,m,g,y,k,x,b,E,A,P,S;return d.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(e.assert(t.currentProvider,"web3.currentProvider is not set"),!a.static){n.next=8;break}return u=a.contractMethod,c=a.args,h=a.overrides,T((f=new t.eth.Contract(a.abi,a.address)).methods,u),l=h.gas,v=p(h,J),w=o({},v,{gas:l}),n.abrupt("return",(i=f.methods)[u].apply(i,c).call(w));case 8:return m=a.contractMethod,g=a.args,y=a.overrides,T((k=new t.eth.Contract(a.abi,a.address)).methods,m),x=y.gas,b=y.from,E=p(y,K),e.assert(A=b||r,"from is required"),P=o({},E,{from:A,gas:x}),S=(s=k.methods)[m].apply(s,g),n.abrupt("return",new Promise((function(t,e){S.send(P,(function(r,n){r?e(r):t(n)}))})));case 17:case"end":return n.stop()}}),n)})));return function(t){return a.apply(this,arguments)}}();return a},W=function(){function r(t,r,n,o,a,s,i){void 0===t&&(t=1),void 0===r&&(r=l),this.network=void 0,this.apiURL=void 0,this.web3Provider=void 0,this.ethersDeps=void 0,this.account=void 0,this.axios=void 0,this.fetch=void 0,this.sdk={},this.fetcher=void 0,this.network=t,this.apiURL=r,this.web3Provider=n,this.ethersDeps=o,this.account=a,this.axios=s,this.fetch=i;var u=s?G(s):i?U(i):null;if(e.assert(u,"at least one fetcher is needed"),this.fetcher=u,n||o){var c=o?N(o,a):n?V(n,a):null;c&&(this.sdk=H({fetcher:u,contractCaller:c,apiURL:r,network:t}))}else this.sdk=Y({fetcher:u,apiURL:r,network:t},b,k,v,D,R,M)}var o=r.prototype;return o.handleAPIError=function(t){if(!S(t))return{message:"Unknown error: "+t};if(!t.response)return{message:t.message};var e=t.response,r=e.data;return{status:e.status,message:r.error,data:r}},o.setWeb3Provider=function(t,e){var r=V(t,e);return this.sdk=H({fetcher:this.fetcher,contractCaller:r,apiURL:this.apiURL,network:this.network}),this.web3Provider=t,this.ethersDeps=void 0,this.account=e,this},o.setEthersProvider=function(t,e){var r=N(t,e);return this.sdk=H({fetcher:this.fetcher,contractCaller:r,apiURL:this.apiURL,network:this.network}),this.web3Provider=void 0,this.ethersDeps=t,this.account=e,this},o.getTokens=function(){var t=n(d.mark((function t(){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getTokens,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getTokens();case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),o.getAdapters=function(){var t=n(d.mark((function t(){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getAdapters,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAdapters({type:"object"});case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),o.getRateByRoute=function(){var r=n(d.mark((function r(n,o,a,s,i,u,c){return d.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(void 0===s&&(s=t.SwapSide.SELL),e.assert(this.sdk.getRateByRoute,"sdk must be initialized with a fetcher"),!(n.length<2)){r.next=4;break}return r.abrupt("return",{message:"Invalid Route"});case 4:return r.prev=4,r.next=7,this.sdk.getRateByRoute({route:n,amount:o,userAddress:a,side:s,options:i,srcDecimals:u,destDecimals:c});case 7:return r.abrupt("return",r.sent);case 10:return r.prev=10,r.t0=r.catch(4),r.abrupt("return",this.handleAPIError(r.t0));case 13:case"end":return r.stop()}}),r,this,[[4,10]])})));return function(t,e,n,o,a,s,i){return r.apply(this,arguments)}}(),o.getRate=function(){var r=n(d.mark((function r(n,o,a,s,i,u,c,p){return d.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0===i&&(i=t.SwapSide.SELL),void 0===u&&(u={}),e.assert(this.sdk.getRate,"sdk must be initialized with a fetcher"),r.prev=3,r.next=6,this.sdk.getRate({srcToken:n,destToken:o,amount:a,userAddress:s,side:i,options:u,srcDecimals:c,destDecimals:p});case 6:return r.abrupt("return",r.sent);case 9:return r.prev=9,r.t0=r.catch(3),r.abrupt("return",this.handleAPIError(r.t0));case 12:case"end":return r.stop()}}),r,this,[[3,9]])})));return function(t,e,n,o,a,s,i,u){return r.apply(this,arguments)}}(),o.buildTx=function(){var t=n(d.mark((function t(r,n,o,a,s,i,u,c,p,h,f,l,v,w,m){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===f&&(f={}),e.assert(this.sdk.buildTx,"sdk must be initialized with a fetcher"),t.prev=2,t.next=5,this.sdk.buildTx({srcToken:r,destToken:n,srcAmount:o,destAmount:a,priceRoute:s,userAddress:i,partner:u,partnerAddress:c,partnerFeeBps:p,receiver:h,srcDecimals:l,destDecimals:v,permit:w,deadline:m},f);case 5:return t.abrupt("return",t.sent);case 8:return t.prev=8,t.t0=t.catch(2),t.abrupt("return",this.handleAPIError(t.t0));case 11:case"end":return t.stop()}}),t,this,[[2,8]])})));return function(e,r,n,o,a,s,i,u,c,p,h,f,d,l,v){return t.apply(this,arguments)}}(),o.getTokenTransferProxy=function(){var t=n(d.mark((function t(r){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getSpender,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getSpender();case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e){return t.apply(this,arguments)}}(),o.getAllowances=function(){var t=n(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getAllowances,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAllowances(r,n);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e,r){return t.apply(this,arguments)}}(),o.getAllowance=function(){var t=n(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getAllowance,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAllowance(r,n);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e,r){return t.apply(this,arguments)}}(),o.approveTokenBulk=function(){var t=n(d.mark((function t(r,n,o,a){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.approveTokenBulk,"sdk must be initialized with a provider"),t.prev=1,t.next=4,this.sdk.approveTokenBulk(r,o);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e,r,n,o){return t.apply(this,arguments)}}(),o.approveToken=function(){var t=n(d.mark((function t(r,n,o,a,s){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.approveToken,"sdk must be initialized with a provider"),t.prev=1,t.next=4,this.sdk.approveToken(r,o);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e,r,n,o,a){return t.apply(this,arguments)}}(),o.getMarketNames=function(){var t=n(d.mark((function t(){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getAdapters,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAdapters({type:"list",namesOnly:!0});case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),o.getBalance=function(){var t=n(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getBalance,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getBalance(r,n);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e,r){return t.apply(this,arguments)}}(),o.getBalances=function(){var t=n(d.mark((function t(r){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e.assert(this.sdk.getBalances,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getBalances(r);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",this.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(e){return t.apply(this,arguments)}}(),r}(),Y=function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];var o=r.reduce((function(e,r){var n=r(t);return Object.assign(e,n)}),{});return o},H=function(t){return Y(t,b,k,v,m,D,R,M)};Object.defineProperty(exports,"ContractMethod",{enumerable:!0,get:function(){return t.ContractMethod}}),Object.defineProperty(exports,"SwapSide",{enumerable:!0,get:function(){return t.SwapSide}}),exports.API_URL=l,exports.ParaSwap=W,exports.constructApproveToken=m,exports.constructAxiosFetcher=G,exports.constructBuildTx=D,exports.constructEthersContractCaller=N,exports.constructFetchFetcher=U,exports.constructGetAdapters=R,exports.constructGetBalances=b,exports.constructGetRate=M,exports.constructGetSpender=v,exports.constructGetTokens=k,exports.constructPartialSDK=Y,exports.constructSDK=H,exports.constructToken=y,exports.constructWeb3ContractCaller=V,exports.isFetcherError=S; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("paraswap-core"),r=require("ts-essentials");function e(t,r,e,n,a,o,s){try{var i=t[o](s),u=i.value}catch(t){return void e(t)}i.done?r(u):Promise.resolve(u).then(n,a)}function n(t){return function(){var r=this,n=arguments;return new Promise((function(a,o){var s=t.apply(r,n);function i(t){e(s,a,o,i,u,"next",t)}function u(t){e(s,a,o,i,u,"throw",t)}i(void 0)}))}}function a(){return a=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},a.apply(this,arguments)}function o(t){return o=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},o(t)}function s(t,r){return s=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t},s(t,r)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function u(t,r,e){return u=i()?Reflect.construct:function(t,r,e){var n=[null];n.push.apply(n,r);var a=new(Function.bind.apply(t,n));return e&&s(a,e.prototype),a},u.apply(null,arguments)}function c(t){var r="function"==typeof Map?new Map:void 0;return c=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,e)}function e(){return u(t,arguments,o(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),s(e,t)},c(t)}function p(t,r){if(null==t)return{};var e,n,a={},o=Object.keys(t);for(n=0;n<o.length;n++)r.indexOf(e=o[n])>=0||(a[e]=t[e]);return a}function f(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var h={exports:{}};!function(t){var r=function(t){var r,e=Object.prototype,n=e.hasOwnProperty,a="function"==typeof Symbol?Symbol:{},o=a.iterator||"@@iterator",s=a.asyncIterator||"@@asyncIterator",i=a.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var a=Object.create((r&&r.prototype instanceof w?r:w).prototype),o=new L(n||[]);return a._invoke=function(t,r,e){var n=f;return function(a,o){if(n===l)throw new Error("Generator is already running");if(n===d){if("throw"===a)throw o;return O()}for(e.method=a,e.arg=o;;){var s=e.delegate;if(s){var i=P(s,e);if(i){if(i===v)continue;return i}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if(n===f)throw n=d,e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n=l;var u=p(t,r,e);if("normal"===u.type){if(n=e.done?d:h,u.arg===v)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n=d,e.method="throw",e.arg=u.arg)}}}(t,e,o),a}function p(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f="suspendedStart",h="suspendedYield",l="executing",d="completed",v={};function w(){}function m(){}function y(){}var g={};u(g,o,(function(){return this}));var k=Object.getPrototypeOf,x=k&&k(k(R([])));x&&x!==e&&n.call(x,o)&&(g=x);var b=y.prototype=w.prototype=Object.create(g);function E(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function T(t,r){function e(a,o,s,i){var u=p(t[a],t,o);if("throw"!==u.type){var c=u.arg,f=c.value;return f&&"object"==typeof f&&n.call(f,"__await")?r.resolve(f.__await).then((function(t){e("next",t,s,i)}),(function(t){e("throw",t,s,i)})):r.resolve(f).then((function(t){c.value=t,s(c)}),(function(t){return e("throw",t,s,i)}))}i(u.arg)}var a;this._invoke=function(t,n){function o(){return new r((function(r,a){e(t,n,r,a)}))}return a=a?a.then(o,o):o()}}function P(t,e){var n=t.iterator[e.method];if(n===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=r,P(t,e),"throw"===e.method))return v;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var a=p(n,t.iterator,e.arg);if("throw"===a.type)return e.method="throw",e.arg=a.arg,e.delegate=null,v;var o=a.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=r),e.delegate=null,v):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,v)}function A(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function S(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function R(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var a=-1,s=function e(){for(;++a<t.length;)if(n.call(t,a))return e.value=t[a],e.done=!1,e;return e.value=r,e.done=!0,e};return s.next=s}}return{next:O}}function O(){return{value:r,done:!0}}return m.prototype=y,u(b,"constructor",y),u(y,"constructor",m),m.displayName=u(y,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===m||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,y):(t.__proto__=y,u(t,i,"GeneratorFunction")),t.prototype=Object.create(b),t},t.awrap=function(t){return{__await:t}},E(T.prototype),u(T.prototype,s,(function(){return this})),t.AsyncIterator=T,t.async=function(r,e,n,a,o){void 0===o&&(o=Promise);var s=new T(c(r,e,n,a),o);return t.isGeneratorFunction(e)?s:s.next().then((function(t){return t.done?t.value:s.next()}))},E(b),u(b,i,"Generator"),u(b,o,(function(){return this})),u(b,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=R,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(S),!t)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=r)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function a(n,a){return i.type="throw",i.arg=t,e.next=n,a&&(e.method="next",e.arg=r),!!a}for(var o=this.tryEntries.length-1;o>=0;--o){var s=this.tryEntries[o],i=s.completion;if("root"===s.tryLoc)return a("end");if(s.tryLoc<=this.prev){var u=n.call(s,"catchLoc"),c=n.call(s,"finallyLoc");if(u&&c){if(this.prev<s.catchLoc)return a(s.catchLoc,!0);if(this.prev<s.finallyLoc)return a(s.finallyLoc)}else if(u){if(this.prev<s.catchLoc)return a(s.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<s.finallyLoc)return a(s.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var a=this.tryEntries[e];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev<a.finallyLoc){var o=a;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=r&&r<=o.finallyLoc&&(o=null);var s=o?o.completion:{};return s.type=t,s.arg=r,o?(this.method="next",this.next=o.finallyLoc,v):this.complete(s)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),v},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),S(e),v}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var a=n.arg;S(e)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:R(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=r),v}},t}(t.exports);try{regeneratorRuntime=r}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=r:Function("r","regeneratorRuntime = r")(r)}}(h);var l=h.exports,d="https://api.paraswap.io",v=function(t){var r=t.apiURL,e=t.fetcher,a=(void 0===r?d:r)+"/adapters/contracts?network="+t.network,o=function(){var t=n(l.mark((function t(r){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e({url:a,method:"GET",signal:r});case 2:return t.abrupt("return",t.sent.TokenTransferProxy);case 4:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{getSpender:o}},w=[{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"}],m=function(t){var r,e=v(t).getSpender,a=function(){var a=n(l.mark((function n(a,o,s,i){var u;return l.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(void 0===s&&(s={}),n.t0=r,n.t0){n.next=6;break}return n.next=5,e(i);case 5:n.t0=r=n.sent;case 6:return u=n.t0,n.next=9,t.contractCaller.transactCall({address:o,abi:w,contractMethod:"approve",args:[u,a],overrides:s});case 9:return n.abrupt("return",n.sent);case 11:case"end":return n.stop()}}),n)})));return function(t,r,e,n){return a.apply(this,arguments)}}(),o=function(){var t=n(l.mark((function t(r,e){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",Promise.all(e.map((function(t){return a(r,t)}))));case 1:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}();return{approveToken:a,approveTokenBulk:o}},y=["tokenType","mainConnector","connectors","network"],g=function(t){var r=t.tokenType,e=void 0===r?"ERC20":r,n=t.mainConnector,o=void 0===n?"ETH":n,s=t.connectors,i=void 0===s?[]:s,u=t.network,c=void 0===u?1:u,f=p(t,y);return a({tokenType:e,connectors:i.length>0?i:[o],mainConnector:o,network:c},f)},k={message:"Not Found"},x=function(t){var r=t.apiURL,e=t.fetcher,a=(void 0===r?d:r)+"/users/tokens/"+t.network,o=function(){var t=n(l.mark((function t(r,n){var o,s;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=a+"/"+r,t.next=3,e({url:o,method:"GET",signal:n});case 3:return s=t.sent.tokens.map(g),t.abrupt("return",s);case 6:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),s=function(){var t=n(l.mark((function t(r,n,o){var s,i;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=a+"/"+r+"/"+n,t.next=3,e({url:s,method:"GET",signal:o});case 3:if((i=t.sent).token){t.next=6;break}return t.abrupt("return",k);case 6:return t.abrupt("return",g(i.token));case 7:case"end":return t.stop()}}),t)})));return function(r,e,n){return t.apply(this,arguments)}}(),i=function(){var t=n(l.mark((function t(r,e,n){var a,s;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=new Set(e.map((function(t){return t.toLowerCase()}))),t.next=3,o(r,n);case 3:return s=t.sent.filter((function(t){return a.has(t.address.toLowerCase())})).map((function(t){var r=t.allowance;return{tokenAddress:t.address,allowance:void 0===r?"0":r}})),t.abrupt("return",s);case 6:case"end":return t.stop()}}),t)})));return function(r,e,n){return t.apply(this,arguments)}}(),u=function(){var t=n(l.mark((function t(r,e,n){var a,o;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,s(r,e,n);case 2:if(!("message"in(a=t.sent))){t.next=5;break}return t.abrupt("return",a);case 5:return t.abrupt("return",{tokenAddress:a.address,allowance:void 0===(o=a.allowance)?"0":o});case 7:case"end":return t.stop()}}),t)})));return function(r,e,n){return t.apply(this,arguments)}}();return{getBalance:s,getBalances:o,getAllowance:u,getAllowances:i}};function b(t){for(var r=arguments.length,e=new Array(r>1?r-1:0),n=1;n<r;n++)e[n-1]=arguments[n];return e.every((function(r){return"function"==typeof t[r]}))}function E(t){for(var e=arguments.length,n=new Array(e>1?e-1:0),a=1;a<e;a++)n[a-1]=arguments[a];r.assert(b.apply(void 0,[t].concat(n)),"Contract must have methods: "+n.join(", "))}function T(t){for(var r=arguments.length,e=new Array(r>1?r-1:0),n=1;n<r;n++)e[n-1]=arguments[n];return e.every((function(r){return"function"==typeof t.methods[r]}))}function P(t){for(var e=arguments.length,n=new Array(e>1?e-1:0),a=1;a<e;a++)n[a-1]=arguments[a];r.assert(T.apply(void 0,[t].concat(n)),"Contract must have methods: "+n.join(", "))}var A,S=function(t){var r=Object.entries(t).filter((function(t){return void 0!==t[1]})).map((function(t){return[t[0],String(t[1])]})),e=new URLSearchParams(r).toString();return e&&"?"+e},L=function(t){return t instanceof R},R=function(t){var r,e;function n(r){var e,a=r.code,o=r.request,s=r.response,i=r.isAxiosError,u=r.message;if((e=t.call(this)||this).code=void 0,e.status=void 0,e.request=void 0,e.response=void 0,e.isAxiosError=!1,Error.captureStackTrace&&Error.captureStackTrace(f(e),n),e.name=e.constructor.name,e.code=a,e.request=o,e.isAxiosError=i,!s)return e.message=u,f(e);e.response=s;var c=s.data;return e.status=s.status,e.message=(null==c?void 0:c.error)||u,e}return e=t,(r=n).prototype=Object.create(e.prototype),r.prototype.constructor=r,s(r,e),n}(c(Error)),O=function(t){var r=t.apiURL,e=void 0===r?d:r,a=t.network,o=t.fetcher;function s(){return(s=n(l.mark((function t(r,n){var s,i;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=S({network:a,namesOnly:r&&"namesOnly"in r?r.namesOnly:void 0}),i=e+"/adapters"+("list"===(null==r?void 0:r.type)?"/list":"")+s,t.next=4,o({url:i,method:"GET",signal:n});case 4:return t.abrupt("return",t.sent);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}return{getAdapters:function(t,r){return s.apply(this,arguments)}}},C=["srcToken","destToken"],j=["route"],_=["options"],M=["excludePricingMethods","excludeContractMethods","includeContractMethods","adapterVersion","partner","includeDEXS","excludeDEXS"];!function(t){t.megapath="megapath",t.multipath="multipath",t.simplepath="simplepath"}(A||(A={}));var B=function(t){var r=t.apiURL,e=t.network,o=t.fetcher,s=(void 0===r?d:r)+"/prices",i=function(){var t=n(l.mark((function t(r,n){var i,u,c,f,h,d;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=r.srcToken,u=r.destToken,c=p(r,C),f=D(c),h=S(a({srcToken:i,destToken:u,network:e},f)),d=s+"/"+h,t.next=6,o({url:d,method:"GET",signal:n});case 6:return t.abrupt("return",t.sent.priceRoute);case 8:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),u=function(){var t=n(l.mark((function t(r,n){var i,u,c,f,h,d;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i=r.route,u=p(r,j),!(i.length<2)){t.next=3;break}throw new Error("Invalid Route");case 3:return c=D(u),f=i.join("-"),h=S(a({route:f,network:e},c)),d=s+"/"+h,t.next=9,o({url:d,method:"GET",signal:n});case 9:return t.abrupt("return",t.sent.priceRoute);case 11:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}();return{getRate:i,getRateByRoute:u}};function D(t){var r=t.options,e=void 0===r?{}:r,n=p(t,_),o=e.excludePricingMethods,s=e.excludeContractMethods,i=e.includeContractMethods,u=e.adapterVersion,c=e.partner,f=void 0===c?"paraswap.io":c,h=e.includeDEXS,l=e.excludeDEXS,d=p(e,M);return I(h),I(l),a({version:u,excludePricingMethods:null==o?void 0:o.join(","),excludeContractMethods:null==s?void 0:s.join(","),includeContractMethods:null==i?void 0:i.join(","),partner:f,includeDEXS:h,excludeDEXS:l},n,d)}function I(t){if(t&&!t.split(",").length)throw new Error("Invalid DEX list")}var U=function(t){var r=t.apiURL,e=t.fetcher,a=(void 0===r?d:r)+"/tokens/"+t.network,o=function(){var t=n(l.mark((function t(r){var n;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e({url:a,method:"GET",signal:r});case 2:return n=t.sent.tokens.map(g),t.abrupt("return",n);case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{getTokens:o}},G=function(e){var a=e.apiURL,o=e.fetcher,s=(void 0===a?d:a)+"/transactions/"+e.network,i=function(){var e=n(l.mark((function e(n,a,i){var u,c,p,f;return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return void 0===a&&(a={}),c=(u=n.priceRoute.side)===t.SwapSide.SELL?"Source Amount Mismatch":"Destination Amount Mismatch",r.assert((void 0,void 0,void 0,l=(h={queryParams:{srcAmount:n.srcAmount,destAmount:n.destAmount},side:u,priceRoute:n.priceRoute}).queryParams,d=h.priceRoute,(v=h.side===t.SwapSide.SELL?[l.srcAmount,d.srcAmount]:[l.destAmount,d.destAmount])[0]===v[1]),c),p=S(a),f={url:s+"/"+p,method:"POST",data:n,signal:i},e.next=9,o(f);case 9:return e.abrupt("return",e.sent);case 11:case"end":return e.stop()}var h,l,d,v}),e)})));return function(t,r,n){return e.apply(this,arguments)}}();return{buildTx:i}},F=function(t){return function(){var r=n(l.mark((function r(e){return l.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,r.next=3,t.request(e);case 3:return r.abrupt("return",r.sent.data);case 8:if(r.prev=8,r.t0=r.catch(0),!t.isAxiosError(r.t0)){r.next=12;break}throw new R(r.t0);case 12:throw r.t0;case 13:case"end":return r.stop()}}),r,null,[[0,8]])})));return function(t){return r.apply(this,arguments)}}()},z=function(t){return function(){var r=n(l.mark((function r(e){var n,a,o,s,i,u,c;return l.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,n=e.url,o=e.signal,s="POST"===(a=e.method)?JSON.stringify(e.data):null,i="POST"===a&&s?{"Content-Type":"application/json"}:void 0,r.next=6,t(n,{method:a,body:s,signal:o,headers:i});case 6:return u=r.sent,r.next=9,u.json();case 9:if(c=r.sent,u.ok){r.next=12;break}throw new R({code:String(u.status),response:{data:c,status:u.status,statusText:u.statusText,headers:Object.fromEntries(u.headers.entries()),config:{url:n,method:a}},message:u.statusText,isAxiosError:!1});case 12:return r.abrupt("return",c);case 15:if(r.prev=15,r.t0=r.catch(0),!(r.t0 instanceof R)){r.next=19;break}throw r.t0;case 19:throw new R(r.t0);case 20:case"end":return r.stop()}}),r,null,[[0,15]])})));return function(t){return r.apply(this,arguments)}}()},N=["block","gas"],q=["gas","from"],X=function(t,e){var o=t.ethersProviderOrSigner,s=t.EthersContract,i=function(){var t=n(l.mark((function t(r){var e,n,i,u,c,f,h,d,v;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=r.contractMethod,i=r.args,u=r.overrides,E(c=new s(r.address,r.abi,o),n),f=u.block,h=u.gas,d=p(u,N),v=a({},d,{blockTag:f,gasLimit:h}),t.abrupt("return",(e=c.callStatic)[n].apply(e,i.concat([v])));case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),u=function(){var t=n(l.mark((function t(n){var i,u,c,f,h,d,v,w,m,y,g;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(e,"account must be specified to create a signer"),r.assert(H(o)||K(o),"ethers must be an instance of Signer or JsonRpcProvider to create a signer"),i=n.address,u=n.abi,c=n.contractMethod,f=n.args,h=n.overrides,d="getSigner"in o?o.getSigner(e):o,E(v=new s(i,u,d),c),w=h.gas,m=p(h,q),y=a({},m,{gasLimit:w}),g=y,t.next=11,v[c].apply(v,f.concat([g]));case 11:return t.abrupt("return",t.sent);case 13:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{staticCall:i,transactCall:u}};function H(t){return function(t){return"_isProvider"in t&&t._isProvider}(t)&&"getSigner"in t}function K(t){return"_isSigner"in t&&t._isSigner}var J=["block","gas"],V=["gas","from"],W=function(t,e){var o=function(){var e=n(l.mark((function e(n){var o,s,i,u,c,f,h,d;return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r.assert(t.currentProvider,"web3.currentProvider is not set"),s=n.contractMethod,i=n.args,u=n.overrides,P(c=new t.eth.Contract(n.abi,n.address),s),f=u.gas,h=p(u,J),d=a({},h,{gas:f}),e.abrupt("return",(o=c.methods)[s].apply(o,i).call(d));case 7:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),s=function(){var o=n(l.mark((function n(o){var s,i,u,c,f,h,d,v,w,m,y,g,k;return l.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return r.assert(t.currentProvider,"web3.currentProvider is not set"),i=o.contractMethod,u=o.args,c=o.overrides,P(f=new t.eth.Contract(o.abi,o.address),i),h=c.gas,d=c.from,v=p(c,V),r.assert(w=d||e,"from is required"),m=a({},v,{from:w,gas:h}),y=(s=f.methods)[i].apply(s,u),g=y.send(m),k={on:g.on.bind(g),once:g.once.bind(g)},n.abrupt("return",k);case 12:case"end":return n.stop()}}),n)})));return function(t){return o.apply(this,arguments)}}();return{staticCall:o,transactCall:s}},Y=function(t){for(var r=arguments.length,e=new Array(r>1?r-1:0),n=1;n<r;n++)e[n-1]=arguments[n];var a=e.reduce((function(r,e){var n=e(t);return Object.assign(r,n)}),{});return a},Q=function(t){return Y(t,x,U,v,m,G,O,B)},Z=function(){function e(t,e,n,a,o,s,i){void 0===t&&(t=1),void 0===e&&(e=d),this.network=void 0,this.apiURL=void 0,this.web3Provider=void 0,this.ethersDeps=void 0,this.account=void 0,this.axios=void 0,this.fetch=void 0,this.sdk={},this.fetcher=void 0,this.network=t,this.apiURL=e,this.web3Provider=n,this.ethersDeps=a,this.account=o,this.axios=s,this.fetch=i;var u=s?F(s):i?z(i):null;if(r.assert(u,"at least one fetcher is needed"),this.fetcher=u,n||a){var c=a?X(a,o):n?W(n,o):null;c&&(this.sdk=Q({fetcher:u,contractCaller:c,apiURL:e,network:t}))}else this.sdk=Y({fetcher:u,apiURL:e,network:t},x,U,v,G,O,B)}e.handleAPIError=function(t){if(!L(t))return{message:"Unknown error: "+t};if(!t.response)return{message:t.message};var r=t.response,e=r.data;return{status:r.status,message:e.error,data:e}},e.extractHasFromTxResponse=function(){var t=n(l.mark((function t(r){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!("once"in r)){t.next=2;break}return t.abrupt("return",new Promise((function(t,e){r.once("transactionHash",t),r.once("error",e)})));case 2:return t.next=4,r;case 4:return t.abrupt("return",t.sent.hash);case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();var a=e.prototype;return a.setWeb3Provider=function(t,r){var e=W(t,r);return this.sdk=Q({fetcher:this.fetcher,contractCaller:e,apiURL:this.apiURL,network:this.network}),this.web3Provider=t,this.ethersDeps=void 0,this.account=r,this},a.setEthersProvider=function(t,r){var e=X(t,r);return this.sdk=Q({fetcher:this.fetcher,contractCaller:e,apiURL:this.apiURL,network:this.network}),this.web3Provider=void 0,this.ethersDeps=t,this.account=r,this},a.getTokens=function(){var t=n(l.mark((function t(){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getTokens,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getTokens();case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),a.getAdapters=function(){var t=n(l.mark((function t(){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getAdapters,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAdapters({type:"object"});case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),a.getRateByRoute=function(){var a=n(l.mark((function n(a,o,s,i,u,c,p){return l.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(void 0===i&&(i=t.SwapSide.SELL),r.assert(this.sdk.getRateByRoute,"sdk must be initialized with a fetcher"),!(a.length<2)){n.next=4;break}return n.abrupt("return",{message:"Invalid Route"});case 4:return n.prev=4,n.next=7,this.sdk.getRateByRoute({route:a,amount:o,userAddress:s,side:i,options:u,srcDecimals:c,destDecimals:p});case 7:return n.abrupt("return",n.sent);case 10:return n.prev=10,n.t0=n.catch(4),n.abrupt("return",e.handleAPIError(n.t0));case 13:case"end":return n.stop()}}),n,this,[[4,10]])})));return function(t,r,e,n,o,s,i){return a.apply(this,arguments)}}(),a.getRate=function(){var a=n(l.mark((function n(a,o,s,i,u,c,p,f){return l.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return void 0===u&&(u=t.SwapSide.SELL),void 0===c&&(c={}),r.assert(this.sdk.getRate,"sdk must be initialized with a fetcher"),n.prev=3,n.next=6,this.sdk.getRate({srcToken:a,destToken:o,amount:s,userAddress:i,side:u,options:c,srcDecimals:p,destDecimals:f});case 6:return n.abrupt("return",n.sent);case 9:return n.prev=9,n.t0=n.catch(3),n.abrupt("return",e.handleAPIError(n.t0));case 12:case"end":return n.stop()}}),n,this,[[3,9]])})));return function(t,r,e,n,o,s,i,u){return a.apply(this,arguments)}}(),a.buildTx=function(){var t=n(l.mark((function t(n,a,o,s,i,u,c,p,f,h,d,v,w,m,y){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===d&&(d={}),r.assert(this.sdk.buildTx,"sdk must be initialized with a fetcher"),t.prev=2,t.next=5,this.sdk.buildTx({srcToken:n,destToken:a,srcAmount:o,destAmount:s,priceRoute:i,userAddress:u,partner:c,partnerAddress:p,partnerFeeBps:f,receiver:h,srcDecimals:v,destDecimals:w,permit:m,deadline:y},d);case 5:return t.abrupt("return",t.sent);case 8:return t.prev=8,t.t0=t.catch(2),t.abrupt("return",e.handleAPIError(t.t0));case 11:case"end":return t.stop()}}),t,this,[[2,8]])})));return function(r,e,n,a,o,s,i,u,c,p,f,h,l,d,v){return t.apply(this,arguments)}}(),a.getTokenTransferProxy=function(){var t=n(l.mark((function t(n){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getSpender,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getSpender();case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(r){return t.apply(this,arguments)}}(),a.getAllowances=function(){var t=n(l.mark((function t(n,a){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getAllowances,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAllowances(n,a);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(r,e){return t.apply(this,arguments)}}(),a.getAllowance=function(){var t=n(l.mark((function t(n,a){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getAllowance,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAllowance(n,a);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(r,e){return t.apply(this,arguments)}}(),a.approveTokenBulk=function(){var t=n(l.mark((function t(n,a,o,s){var i;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.approveTokenBulk,"sdk must be initialized with a provider"),t.prev=1,t.next=4,this.sdk.approveTokenBulk(n,o);case 4:return i=t.sent,t.next=7,Promise.all(i.map(e.extractHasFromTxResponse));case 7:return t.abrupt("return",t.sent);case 10:return t.prev=10,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 13:case"end":return t.stop()}}),t,this,[[1,10]])})));return function(r,e,n,a){return t.apply(this,arguments)}}(),a.approveToken=function(){var t=n(l.mark((function t(n,a,o,s,i){var u;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.approveToken,"sdk must be initialized with a provider"),t.prev=1,t.next=4,this.sdk.approveToken(n,o);case 4:return u=t.sent,t.next=7,e.extractHasFromTxResponse(u);case 7:return t.abrupt("return",t.sent);case 10:return t.prev=10,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 13:case"end":return t.stop()}}),t,this,[[1,10]])})));return function(r,e,n,a,o){return t.apply(this,arguments)}}(),a.getMarketNames=function(){var t=n(l.mark((function t(){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getAdapters,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getAdapters({type:"list",namesOnly:!0});case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(){return t.apply(this,arguments)}}(),a.getBalance=function(){var t=n(l.mark((function t(n,a){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getBalance,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getBalance(n,a);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(r,e){return t.apply(this,arguments)}}(),a.getBalances=function(){var t=n(l.mark((function t(n){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r.assert(this.sdk.getBalances,"sdk must be initialized with a fetcher"),t.prev=1,t.next=4,this.sdk.getBalances(n);case 4:return t.abrupt("return",t.sent);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",e.handleAPIError(t.t0));case 10:case"end":return t.stop()}}),t,this,[[1,7]])})));return function(r){return t.apply(this,arguments)}}(),e}();Object.defineProperty(exports,"ContractMethod",{enumerable:!0,get:function(){return t.ContractMethod}}),Object.defineProperty(exports,"SwapSide",{enumerable:!0,get:function(){return t.SwapSide}}),exports.API_URL=d,exports.ParaSwap=Z,exports.constructApproveToken=m,exports.constructAxiosFetcher=F,exports.constructBuildTx=G,exports.constructEthersContractCaller=X,exports.constructFetchFetcher=z,exports.constructFullSDK=Q,exports.constructGetAdapters=O,exports.constructGetBalances=x,exports.constructGetRate=B,exports.constructGetSpender=v,exports.constructGetTokens=U,exports.constructPartialSDK=Y,exports.constructSimpleSDK=function(t,r){var e="axios"in t?F(t.axios):z(t.fetch);if(!r)return Y({apiURL:t.apiURL,network:t.network,fetcher:e},x,U,v,G,O,B);var a=function(t){if("ethersProviderOrSigner"in t){var r=X(t,t.account),e=r.staticCall,a=r.transactCall,o=function(){var t=n(l.mark((function t(r){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,a(r);case 2:return t.abrupt("return",t.sent.hash);case 4:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{staticCall:e,transactCall:o}}var s=W(t.web3,t.account),i=s.staticCall,u=s.transactCall,c=function(){var t=n(l.mark((function t(r){var e;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,u(r);case 2:return e=t.sent,t.abrupt("return",new Promise((function(t,r){e.once("transactionHash",t),e.once("error",r)})));case 4:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();return{staticCall:i,transactCall:c}}(r);return Q({apiURL:t.apiURL,network:t.network,fetcher:e,contractCaller:a})},exports.constructWeb3ContractCaller=W,exports.isAllowance=function(t){return"allowance"in t},exports.isFetcherError=L; | ||
//# sourceMappingURL=sdk.cjs.production.min.js.map |
import type { JsonFragment } from '@ethersproject/abi'; | ||
import { OptimalRate } from 'paraswap-core'; | ||
import { Address, Token, TxHash } from './token'; | ||
export type { Address, Token, TxHash }; | ||
import { Address, AddressOrSymbol, PriceString, Token, TxHash } from './helpers/token'; | ||
export type { Address, AddressOrSymbol, PriceString, Token, TxHash }; | ||
export interface ConstructBaseInput { | ||
@@ -41,17 +41,19 @@ apiURL?: string; | ||
contractMethod: T; | ||
static: boolean; | ||
args: any[]; | ||
} | ||
interface ContractCallStaticInput<T extends string> extends ContractCallInput<T> { | ||
static: true; | ||
overrides: StaticCallOverrides; | ||
} | ||
interface ContractCallTransactionInput<T extends string> extends ContractCallInput<T> { | ||
static: false; | ||
overrides: TxSendOverrides; | ||
} | ||
export declare type ContractCallerFunction = <T, M extends string = string>(params: ContractCallTransactionInput<M> | ContractCallStaticInput<M>) => Promise<T>; | ||
export interface ConstructProviderFetchInput extends ConstructFetchInput { | ||
contractCaller: ContractCallerFunction; | ||
export declare type StaticContractCallerFn = <T, M extends string = string>(params: ContractCallStaticInput<M>) => Promise<T>; | ||
export declare type TransactionContractCallerFn<T> = <M extends string = string>(params: ContractCallTransactionInput<M>) => Promise<T>; | ||
export interface ContractCallerFunctions<T> { | ||
staticCall: StaticContractCallerFn; | ||
transactCall: TransactionContractCallerFn<T>; | ||
} | ||
export interface ConstructProviderFetchInput<T, D extends keyof ContractCallerFunctions<T> = keyof ContractCallerFunctions<T>> extends ConstructFetchInput { | ||
contractCaller: Pick<ContractCallerFunctions<T>, D>; | ||
} | ||
export declare type TokenFromApi = Pick<Token, 'address' | 'decimals' | 'symbol' | 'balance' | 'allowance'>; | ||
@@ -58,0 +60,0 @@ export interface TokensApiResponse { |
{ | ||
"name": "@paraswap/sdk", | ||
"version": "5.6.0-alpha.3", | ||
"version": "5.6.0-alpha.4", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/sdk.esm.js", |
151
README.md
@@ -26,3 +26,97 @@ <p align="center"> | ||
### Import the necessary functions | ||
There are multiple ways to use ParaSwap SDK, ranging from a simple construct-and-use approach to a fully composable _bring what you need_ approach which allows for advanced tree-shaking and minimizes bundle size. | ||
### Simple SDK | ||
Can be created by providing `network` and either `axios` or `window.fetch` (or alternative `fetch` implementation). The resulting SDK will be able to use all methods that query the API. | ||
```ts | ||
import { constructSimpleSDK } from '@paraswap/sdk'; | ||
import axios from 'axios'; | ||
// construct minimal SDK with fetcher only | ||
const paraSwapMin = constructSimpleSDK({network: 1, axios}); | ||
// or | ||
const paraSwapMin = constructSimpleSDK({network: 1, fetch: window.fetch}); | ||
const ETH = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; | ||
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F'; | ||
async function swapExample() { | ||
const signer: JsonRpcSigner = ... | ||
const senderAddress = signer.address; | ||
const priceRoute = await paraSwapMin.getRate({ | ||
srcToken: ETH, | ||
destToken: DAI, | ||
amount: srcAmount, | ||
userAddress: senderAddress, | ||
side: SwapSide.SELL, | ||
}); | ||
const txParams = await paraSwapMin.buildTx( | ||
{ | ||
srcToken, | ||
destToken, | ||
srcAmount, | ||
destAmount, | ||
priceRoute, | ||
userAddress: senderAddress, | ||
partner: referrer, | ||
}, | ||
{ ignoreChecks: true } | ||
); | ||
const transaction = { | ||
...txParams, | ||
gasPrice: '0x' + new BigNumber(txParams.gasPrice).toString(16), | ||
gasLimit: '0x' + new BigNumber(5000000).toString(16), | ||
value: '0x' + new BigNumber(txParams.value).toString(16), | ||
}; | ||
const txr = await signer.sendTransaction(transaction); | ||
} | ||
async function approveTokenYourselfExample() { | ||
const TransferProxy = await paraSwapMin.getSpender(); | ||
const DAI_CONTRACT = new ethers.Contract(DAI, ERC20_ABI, ethersSignerOrProvider); | ||
const tx = await DAI_CONTRACT.approve(TransferProxy, amountInWei); | ||
const txReceipt = await tx.wait(1); | ||
} | ||
``` | ||
If optional `providerOptions` is provided as the second parameter, then the resulting SDK will also be able to approve Tokens for swap. | ||
```ts | ||
// | ||
// with ethers.js | ||
const providerOptionsEther = { | ||
ethersProviderOrSigner: provider, // JsonRpcProvider | ||
EthersContract: ethers.Contract, | ||
account: senderAddress, | ||
}; | ||
// or with web3.js | ||
const providerOptionsWeb3 = { | ||
web3, // new Web3(...) instance | ||
account: senderAddress, | ||
}; | ||
const paraSwap = constructSimpleSDK({network: 1, axios}, providerOptionsEther); | ||
async function approveTokenExample() { | ||
const txHash = await paraSwap.approveToken(amountInWei, DAI); | ||
// await tx somehow | ||
await provider.waitForTransaction(txHash); | ||
} | ||
``` | ||
### Composed SDK | ||
Import the necessary functions | ||
```typescript | ||
@@ -34,8 +128,8 @@ import { constructSDK, constructAxiosFetcher, constructEthersContractCaller } from '@paraswap/sdk'; | ||
```typescript | ||
const signer = ethers.Wallet.fromMnmemonic('__your_mnemonic__') // or any other signer/provider | ||
const account = '__signer_address__' | ||
const signer = ethers.Wallet.fromMnmemonic('__your_mnemonic__'); // or any other signer/provider | ||
const account = '__signer_address__'; | ||
const contractCaller = constructEthersContractCaller({ | ||
providerOrSigner: signer, | ||
Contract: ethers.Contract, | ||
ethersProviderOrSigner: signer, | ||
EthersContract: ethers.Contract, | ||
}, account); // alternatively constructWeb3ContractCaller | ||
@@ -54,3 +148,12 @@ const fetcher = constructAxiosFetcher(axios); // alternatively constructFetchFetcher | ||
```typescript | ||
const txHash = await paraSwap.approveToken(amount, tokenAddress); | ||
// if created with constructEthersContractCaller | ||
const contractTx: ContractTransaction = await paraSwap.approveToken(amount, tokenAddress); | ||
const txReceipt = await contractTx.wait(); | ||
// if created with constructWeb3ContractCaller | ||
const unpromiEvent: Web3UnpromiEvent = await paraSwap.approveToken(amount, tokenAddress); | ||
const txReceipt = await new Promise<Web3TransactionReceipt>((resolve, reject) => { | ||
unpromiEvent.once('receipt', resolve); | ||
unpromiEvent.once('error', reject); | ||
}) | ||
``` | ||
@@ -64,4 +167,4 @@ | ||
const srcAmount = '1000000000000000000'; //The source amount multiplied by its decimals: 10 ** 18 here | ||
const srcDecimals = 18 | ||
const destDecimals = 18 | ||
const srcDecimals = 18; | ||
const destDecimals = 18; | ||
@@ -85,11 +188,11 @@ const priceRoute = await paraSwap.getRate( | ||
const srcToken = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; | ||
const srcDecimals = 18 | ||
const srcDecimals = 18; | ||
const srcAmount = '1000000000000000000'; // The source amount multiplied by its decimals | ||
const destToken = '0xcAfE001067cDEF266AfB7Eb5A286dCFD277f3dE5'; | ||
const destDecimals = 18 | ||
const destAmount = priceRoute.destAmount // price route being output of paraSwap.getRate() | ||
const destDecimals = 18; | ||
const destAmount = priceRoute.destAmount; // price route being output of paraSwap.getRate() | ||
const senderAddress = '__sender_address__'; // mandatory | ||
const receiver = '__receiver_address__'; // optional: for swap and transfer | ||
const partnerAddress = '__fee_receiver_address__' // optional: for permission-less monetization | ||
const partnerFeeBps = 50 // optional: fee in base point, for permission-less monetization | ||
const partnerAddress = '__fee_receiver_address__'; // optional: for permission-less monetization | ||
const partnerFeeBps = 50; // optional: fee in base point, for permission-less monetization | ||
@@ -128,3 +231,3 @@ | ||
const fetcher = constructFetchFetcher(window.fetch) | ||
const fetcher = constructFetchFetcher(window.fetch); | ||
@@ -134,5 +237,5 @@ const minParaSwap = constructPartialSDK({ | ||
fetcher, | ||
}, constructGetRate, constructGetBalances) | ||
}, constructGetRate, constructGetBalances); | ||
const priceRoute = await minParaSwap.getRate(params) | ||
const priceRoute = await minParaSwap.getRate(params); | ||
const allowance = await minParaSwap.getAllowance(userAddress, tokenAddress); | ||
@@ -145,8 +248,8 @@ ``` | ||
```typescript | ||
import { ParaSwap } from '@paraswap/sdk' | ||
import axios from 'axios' | ||
import Web3 from 'web3' | ||
import { ParaSwap } from '@paraswap/sdk'; | ||
import axios from 'axios'; | ||
import Web3 from 'web3'; | ||
const web3Provider = new Web3(window.ethereum) | ||
const account = '__user_address__' | ||
const web3Provider = new Web3(window.ethereum); | ||
const account = '__user_address__'; | ||
@@ -160,3 +263,3 @@ const paraswap = new ParaSwap( | ||
axios | ||
) | ||
); | ||
@@ -168,3 +271,3 @@ ``` | ||
```typescript | ||
import { ParaSwap } from '@paraswap/sdk' | ||
import { ParaSwap } from '@paraswap/sdk'; | ||
@@ -179,3 +282,3 @@ const paraswap = new ParaSwap( | ||
window.fetch | ||
) | ||
); | ||
@@ -182,0 +285,0 @@ ``` |
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import axios from 'axios'; | ||
import { ethers } from 'ethers'; | ||
import { constructPartialSDK, constructSDK } from '..'; | ||
import { constructGetAdapters } from '../adapters'; | ||
import { | ||
constructPartialSDK, | ||
constructFullSDK, | ||
constructGetAdapters, | ||
constructEthersContractCaller, | ||
constructAxiosFetcher, | ||
} from '../helpers'; | ||
} from '..'; | ||
@@ -15,7 +16,7 @@ const fetcher = constructAxiosFetcher(axios); | ||
const contractCaller = constructEthersContractCaller({ | ||
providerOrSigner: provider, | ||
Contract: ethers.Contract, | ||
ethersProviderOrSigner: provider, | ||
EthersContract: ethers.Contract, | ||
}); | ||
const paraswap = constructSDK({ | ||
const paraswap = constructFullSDK({ | ||
apiURL: '', | ||
@@ -29,2 +30,7 @@ network: 1, | ||
// type Promise<ContractTransaction> | ||
const txResponse = paraswap.approveToken('1', '0x...'); | ||
// type Promise<ContractTransaction[]> | ||
const txResponses = paraswap.approveTokenBulk('1', ['0x...']); | ||
const part1 = constructPartialSDK( | ||
@@ -31,0 +37,0 @@ { apiURL: '', network: 1, fetcher }, |
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import axios from 'axios'; | ||
import Web3 from 'web3'; | ||
import { constructSDK } from '..'; | ||
import { constructAxiosFetcher } from '../helpers'; | ||
import { constructContractCaller } from '../helpers/web3'; | ||
import { | ||
constructFullSDK, | ||
constructAxiosFetcher, | ||
constructWeb3ContractCaller, | ||
} from '..'; | ||
const fetcher = constructAxiosFetcher(axios); | ||
const web3 = new Web3(Web3.givenProvider); | ||
const contractCaller = constructContractCaller(web3); | ||
const contractCaller = constructWeb3ContractCaller(web3); | ||
const paraswap = constructSDK({ | ||
const paraswap = constructFullSDK({ | ||
network: 1, | ||
@@ -19,3 +21,4 @@ fetcher, | ||
async function main() { | ||
const txHash = await paraswap.approveToken( | ||
// type Web3UnpromiEvent | ||
const eventfulTxResponse = await paraswap.approveToken( | ||
'1000000000000000000', | ||
@@ -25,5 +28,7 @@ '0xcafe001067cDEF266AfB7Eb5A286dCFD277f3dE5' | ||
console.log('watch for tx', txHash); | ||
eventfulTxResponse.once('transactionHash', (txHash) => | ||
console.log('watch for tx', txHash) | ||
); | ||
} | ||
main(); |
@@ -1,5 +0,11 @@ | ||
export { constructFetcher as constructAxiosFetcher } from './axios'; | ||
export { constructFetcher as constructFetchFetcher } from './fetch'; | ||
export { constructContractCaller as constructEthersContractCaller } from './ethers'; | ||
export { constructContractCaller as constructWeb3ContractCaller } from './web3'; | ||
export { constructFetcher as constructAxiosFetcher } from './fetchers/axios'; | ||
export { constructFetcher as constructFetchFetcher } from './fetchers/fetch'; | ||
export { | ||
constructContractCaller as constructEthersContractCaller, | ||
EthersProviderDeps, | ||
} from './providers/ethers'; | ||
export { | ||
constructContractCaller as constructWeb3ContractCaller, | ||
Web3UnpromiEvent, | ||
} from './providers/web3'; | ||
export { isFetcherError } from './misc'; |
import type { | ||
Contract, | ||
ContractFunction, | ||
PopulatedTransaction, | ||
BigNumber, | ||
Contract as EthersContract, | ||
ContractFunction as EthersContractFunction, | ||
PopulatedTransaction as EthersPopulatedTransaction, | ||
BigNumber as EthersBigNumber, | ||
} from 'ethers'; | ||
import type { | ||
ContractSendMethod as Web3ContractSendMethod, | ||
Contract as Web3Contract, | ||
} from 'web3-eth-contract'; | ||
import { assert } from 'ts-essentials'; | ||
@@ -11,27 +15,29 @@ | ||
export type ContractWithMethod<T extends string> = Contract & { | ||
readonly [method in T]: ContractFunction; | ||
export type EthersContractWithMethod<T extends string> = EthersContract & { | ||
readonly [method in T]: EthersContractFunction; | ||
} & { | ||
readonly functions: { [method in T]: ContractFunction }; | ||
readonly functions: { [method in T]: EthersContractFunction }; | ||
readonly callStatic: { [method in T]: ContractFunction }; | ||
readonly estimateGas: { [method in T]: ContractFunction<BigNumber> }; | ||
readonly callStatic: { [method in T]: EthersContractFunction }; | ||
readonly estimateGas: { | ||
[method in T]: EthersContractFunction<EthersBigNumber>; | ||
}; | ||
readonly populateTransaction: { | ||
[method in T]: ContractFunction<PopulatedTransaction>; | ||
[method in T]: EthersContractFunction<EthersPopulatedTransaction>; | ||
}; | ||
}; | ||
export function contractHasMethods<T extends string>( | ||
contract: Contract, | ||
export function ethersContractHasMethods<T extends string>( | ||
contract: EthersContract, | ||
...methods: T[] | ||
): contract is ContractWithMethod<T> { | ||
): contract is EthersContractWithMethod<T> { | ||
return methods.every((method) => typeof contract[method] === 'function'); | ||
} | ||
export function assertContractHasMethods<T extends string>( | ||
contract: Contract, | ||
export function assertEthersContractHasMethods<T extends string>( | ||
contract: EthersContract, | ||
...methods: T[] | ||
): asserts contract is ContractWithMethod<T> { | ||
): asserts contract is EthersContractWithMethod<T> { | ||
assert( | ||
contractHasMethods(contract, ...methods), | ||
ethersContractHasMethods(contract, ...methods), | ||
`Contract must have methods: ${methods.join(', ')}` | ||
@@ -41,2 +47,25 @@ ); | ||
export type Web3ContractWithMethod<T extends string> = Web3Contract & { | ||
methods: { [method in T]: Web3ContractSendMethod }; | ||
}; | ||
export function web3ContractHasMethods<T extends string>( | ||
contract: Web3Contract, | ||
...methods: T[] | ||
): contract is Web3ContractWithMethod<T> { | ||
return methods.every( | ||
(method) => typeof contract.methods[method] === 'function' | ||
); | ||
} | ||
export function assertWeb3ContractHasMethods<T extends string>( | ||
contract: Web3Contract, | ||
...methods: T[] | ||
): asserts contract is Web3ContractWithMethod<T> { | ||
assert( | ||
web3ContractHasMethods(contract, ...methods), | ||
`Contract must have methods: ${methods.join(', ')}` | ||
); | ||
} | ||
export const objectToFilledEntries = <T extends Record<string, unknown>>( | ||
@@ -43,0 +72,0 @@ object: T |
102
src/index.ts
@@ -1,15 +0,17 @@ | ||
import { constructApproveToken, ApproveTokenFunctions } from './approve'; | ||
import { constructGetBalances, GetBalancesFunctions } from './balance'; | ||
import { constructGetSpender, GetSpenderFunctions } from './spender'; | ||
import { constructGetAdapters, AdaptersFunctions } from './adapters'; | ||
import { constructGetRate, GetRateFunctions } from './rates'; | ||
import { | ||
constructGetTokens, | ||
constructToken, | ||
GetTokensFunctions, | ||
Token, | ||
PriceString, | ||
} from './token'; | ||
import { BuildTxFunctions, constructBuildTx } from './transaction'; | ||
constructApproveToken, | ||
ApproveTokenFunctions, | ||
} from './methods/approve'; | ||
import { | ||
constructGetBalances, | ||
GetBalancesFunctions, | ||
isAllowance, | ||
Allowance, | ||
} from './methods/balance'; | ||
import { constructGetSpender, GetSpenderFunctions } from './methods/spender'; | ||
import { constructGetAdapters, GetAdaptersFunctions } from './methods/adapters'; | ||
import { constructGetRate, GetRateFunctions } from './methods/rates'; | ||
import { constructGetTokens, GetTokensFunctions } from './methods/token'; | ||
import { BuildTxFunctions, constructBuildTx } from './methods/transaction'; | ||
import { | ||
constructEthersContractCaller, | ||
@@ -20,12 +22,17 @@ constructWeb3ContractCaller, | ||
isFetcherError, | ||
EthersProviderDeps, | ||
} from './helpers'; | ||
import { | ||
ConstructBaseInput, | ||
ConstructFetchInput, | ||
ConstructProviderFetchInput, | ||
Address, | ||
AddressOrSymbol, | ||
Token, | ||
PriceString, | ||
TxHash, | ||
TxSendOverrides, | ||
} from './types'; | ||
import { UnionToIntersection } from 'ts-essentials'; | ||
export type { TransactionParams } from './transaction'; | ||
export type { TransactionParams } from './methods/transaction'; | ||
export type { Web3UnpromiEvent } from './helpers'; | ||
export * from './constants'; | ||
@@ -46,11 +53,11 @@ | ||
constructFetchFetcher, | ||
constructToken, | ||
constructGetAdapters, | ||
constructGetRate, | ||
isFetcherError, | ||
isAllowance, | ||
}; | ||
// @TODO missing fro parity with older SDK: | ||
// getRate, getRateByRoute | ||
export type { | ||
Allowance, | ||
EthersProviderDeps, | ||
ApproveTokenFunctions, | ||
@@ -60,62 +67,19 @@ GetBalancesFunctions, | ||
GetTokensFunctions, | ||
GetAdaptersFunctions, | ||
GetRateFunctions, | ||
BuildTxFunctions, | ||
ConstructFetchInput, | ||
ConstructProviderFetchInput, | ||
AdaptersFunctions as ConstructAdaptersFunctions, | ||
Token, | ||
Address, | ||
AddressOrSymbol, | ||
PriceString, | ||
TxHash, | ||
TxSendOverrides, | ||
}; | ||
export type SDKConfig = ConstructProviderFetchInput & ConstructFetchInput; | ||
export { SDKConfig, constructPartialSDK } from './sdk/partial'; | ||
export { AllSDKMethods, constructFullSDK } from './sdk/full'; | ||
export { SDKFetchMethods, constructSimpleSDK } from './sdk/simple'; | ||
export type AllSDKMethods = GetBalancesFunctions & | ||
GetTokensFunctions & | ||
GetSpenderFunctions & | ||
ApproveTokenFunctions & | ||
BuildTxFunctions & | ||
AdaptersFunctions & | ||
GetRateFunctions; | ||
type AnyFunction = (...args: any[]) => any; | ||
type SDKFunction<T extends ConstructBaseInput> = ( | ||
config: T | ||
) => Record<string, AnyFunction>; | ||
type IntersectionOfReturns<Funcs extends AnyFunction[]> = UnionToIntersection< | ||
ReturnType<Funcs[number]> | ||
>; | ||
export const constructPartialSDK = < | ||
T extends ConstructBaseInput, | ||
Funcs extends [SDKFunction<T>, ...SDKFunction<T>[]] | ||
>( | ||
config: T, // config is auto-inferred to cover the used functions | ||
...funcs: Funcs | ||
): IntersectionOfReturns<Funcs> => { | ||
const sdkFuncs = funcs.reduce<Partial<IntersectionOfReturns<Funcs>>>( | ||
(accum, func) => { | ||
const sdkSlice = func(config); | ||
return Object.assign(accum, sdkSlice); | ||
}, | ||
{} | ||
); | ||
return sdkFuncs as IntersectionOfReturns<Funcs>; | ||
}; | ||
export const constructSDK = (config: SDKConfig): AllSDKMethods => | ||
// include all available functions | ||
constructPartialSDK( | ||
config, | ||
constructGetBalances, | ||
constructGetTokens, | ||
constructGetSpender, | ||
constructApproveToken, | ||
constructBuildTx, | ||
constructGetAdapters, | ||
constructGetRate | ||
); | ||
export { ParaSwap } from './legacy'; |
import type { AxiosStatic } from 'axios'; | ||
import type Web3 from 'web3'; | ||
import type { SendOptions } from 'web3-eth-contract'; | ||
import type { Contract as EthersContract } from '@ethersproject/contracts'; | ||
import type { Signer } from '@ethersproject/abstract-signer'; | ||
import type { BaseProvider } from '@ethersproject/providers'; | ||
import type { ContractTransaction } from '@ethersproject/contracts'; | ||
import type { Address, OptimalRate } from 'paraswap-core'; | ||
@@ -21,3 +17,3 @@ | ||
constructGetRate, | ||
constructSDK, | ||
constructFullSDK, | ||
PriceString, | ||
@@ -32,9 +28,10 @@ } from '..'; | ||
isFetcherError, | ||
Web3UnpromiEvent, | ||
EthersProviderDeps, | ||
} from '../helpers'; | ||
import type { RateOptions } from '../rates'; | ||
import type { BuildOptions, TransactionParams } from '../transaction'; | ||
import type { AddressOrSymbol, Token } from '../token'; | ||
import type { Allowance } from '../balance'; | ||
import type { FetcherFunction } from '../types'; | ||
import type { RateOptions } from '../methods/rates'; | ||
import type { BuildOptions, TransactionParams } from '../methods/transaction'; | ||
import type { AddressOrSymbol, Token, FetcherFunction } from '../types'; | ||
import type { Allowance } from '../methods/balance'; | ||
@@ -48,10 +45,7 @@ export type APIError = { | ||
interface EthersProviderDeps { | ||
providerOrSigner: BaseProvider | Signer; | ||
Contract: typeof EthersContract; | ||
} | ||
type TxResponse = Web3UnpromiEvent | ContractTransaction; | ||
/** @deprecated */ | ||
export class ParaSwap { | ||
sdk: Partial<AllSDKMethods> = {}; | ||
sdk: Partial<AllSDKMethods<TxResponse>> = {}; | ||
fetcher: FetcherFunction; | ||
@@ -98,7 +92,12 @@ | ||
if (contractCaller) { | ||
this.sdk = constructSDK({ fetcher, contractCaller, apiURL, network }); | ||
this.sdk = constructFullSDK<TxResponse>({ | ||
fetcher, | ||
contractCaller, | ||
apiURL, | ||
network, | ||
}); | ||
} | ||
} | ||
private handleAPIError(e: unknown): APIError { | ||
private static handleAPIError(e: unknown): APIError { | ||
// @CONSIDER if some errors should not be replaced | ||
@@ -118,11 +117,15 @@ if (!isFetcherError(e)) { | ||
// private checkDexList(dexs?: string): void { | ||
// if (dexs) { | ||
// const targetDEXs = dexs.split(','); | ||
private static async extractHasFromTxResponse( | ||
txResponse: TxResponse | ||
): Promise<string> { | ||
if ('once' in txResponse) { | ||
return new Promise<string>((resolve, reject) => { | ||
txResponse.once('transactionHash', resolve); | ||
txResponse.once('error', reject); | ||
}); | ||
} | ||
// if (!targetDEXs.length) { | ||
// throw new Error('Invalid DEX list'); | ||
// } | ||
// } | ||
// } | ||
const { hash } = await txResponse; | ||
return hash; | ||
} | ||
@@ -133,3 +136,3 @@ setWeb3Provider(web3Provider: Web3, account?: string): this { | ||
this.sdk = constructSDK({ | ||
this.sdk = constructFullSDK({ | ||
fetcher, | ||
@@ -152,3 +155,3 @@ contractCaller, | ||
this.sdk = constructSDK({ | ||
this.sdk = constructFullSDK({ | ||
fetcher, | ||
@@ -173,3 +176,3 @@ contractCaller, | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -183,3 +186,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -215,3 +218,3 @@ } | ||
// including Error('Invalid DEX list') | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -243,3 +246,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -287,3 +290,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -297,3 +300,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -310,3 +313,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -323,3 +326,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -342,5 +345,12 @@ } | ||
// @TODO allow to pass Web3 specific sendOptions ({from: userAddress}) | ||
return await this.sdk.approveTokenBulk(amount, tokenAddresses); | ||
const txResponses = await this.sdk.approveTokenBulk( | ||
amount, | ||
tokenAddresses | ||
); | ||
return await Promise.all( | ||
txResponses.map(ParaSwap.extractHasFromTxResponse) | ||
); | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -361,5 +371,7 @@ } | ||
// @TODO allow to pass Web3 specific sendOptions ({from: userAddress}) | ||
return await this.sdk.approveToken(amount, tokenAddress); | ||
const txResponse = await this.sdk.approveToken(amount, tokenAddress); | ||
return await ParaSwap.extractHasFromTxResponse(txResponse); | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -373,3 +385,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -386,3 +398,3 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
@@ -396,5 +408,5 @@ } | ||
} catch (e) { | ||
return this.handleAPIError(e); | ||
return ParaSwap.handleAPIError(e); | ||
} | ||
} | ||
} |
import type { JsonFragment } from '@ethersproject/abi'; | ||
import { OptimalRate } from 'paraswap-core'; | ||
import { Address, Token, TxHash } from './token'; | ||
import { | ||
Address, | ||
AddressOrSymbol, | ||
PriceString, | ||
Token, | ||
TxHash, | ||
} from './helpers/token'; | ||
export type { Address, Token, TxHash }; | ||
export type { Address, AddressOrSymbol, PriceString, Token, TxHash }; | ||
@@ -51,3 +57,2 @@ export interface ConstructBaseInput { | ||
contractMethod: T; | ||
static: boolean; | ||
args: any[]; | ||
@@ -58,3 +63,2 @@ } | ||
extends ContractCallInput<T> { | ||
static: true; | ||
overrides: StaticCallOverrides; | ||
@@ -65,14 +69,25 @@ } | ||
extends ContractCallInput<T> { | ||
static: false; | ||
overrides: TxSendOverrides; | ||
} | ||
export type ContractCallerFunction = <T, M extends string = string>( | ||
params: ContractCallTransactionInput<M> | ContractCallStaticInput<M> | ||
// may have to type result T differently if we ever use staticCalls in SDK | ||
export type StaticContractCallerFn = <T, M extends string = string>( | ||
params: ContractCallStaticInput<M> | ||
) => Promise<T>; | ||
export type TransactionContractCallerFn<T> = <M extends string = string>( | ||
params: ContractCallTransactionInput<M> | ||
) => Promise<T>; | ||
export interface ConstructProviderFetchInput extends ConstructFetchInput { | ||
contractCaller: ContractCallerFunction; | ||
export interface ContractCallerFunctions<T> { | ||
staticCall: StaticContractCallerFn; | ||
transactCall: TransactionContractCallerFn<T>; | ||
} | ||
export interface ConstructProviderFetchInput< | ||
T, | ||
D extends keyof ContractCallerFunctions<T> = keyof ContractCallerFunctions<T> | ||
> extends ConstructFetchInput { | ||
contractCaller: Pick<ContractCallerFunctions<T>, D>; | ||
} | ||
export type TokenFromApi = Pick< | ||
@@ -79,0 +94,0 @@ Token, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
789312
88
7336
277