@bancor/carbon-sdk
Advanced tools
Comparing version 0.0.67-DEV to 0.0.68-DEV
@@ -1,337 +0,7 @@ | ||
import { PopulatedTransaction } from '@ethersproject/contracts'; | ||
import { Api } from './chain'; | ||
import { DecimalFetcher, EncodedStrategy, Action, Strategy, StrategyUpdate, Rate, TokenPair, Config, TradeData, TypedEventEmitter, CacheEvents, SerializableMatchAction } from './types'; | ||
import { TokensTradedEventObject, TradeActionStruct } from './abis/types/CarbonController'; | ||
import { BigNumberish, PayableOverrides } from 'ethers'; | ||
/** | ||
* Enum representing options for the marginal price parameter of the function. | ||
*/ | ||
declare enum MarginalPriceOptions { | ||
/** Indicates that the marginal price should be reset to its default value. */ | ||
reset = "RESET", | ||
/** Indicates that the marginal price should be maintained at its current value. */ | ||
maintain = "MAINTAIN" | ||
} | ||
export { PopulatedTransaction, Action, Strategy, StrategyUpdate, TokenPair, TradeActionStruct, PayableOverrides, Config, MarginalPriceOptions, TokensTradedEventObject, TypedEventEmitter, CacheEvents, SerializableMatchAction, EncodedStrategy, Api, }; | ||
declare const Sdk_base: new () => TypedEventEmitter<CacheEvents>; | ||
/** | ||
* The Sdk class is the main entry point for interacting with the Carbon SDK. It provides methods for | ||
* fetching blockchain data (kept up to date by subscribing to events), and creating and executing transactions. | ||
* | ||
* @class | ||
* | ||
* @example | ||
* // To install the SDK, run: | ||
* npm install @bancor/carbon-sdk | ||
* | ||
* // To create an instance of the Sdk class, pass the URL of the JSON-RPC endpoint to the constructor: | ||
* import Sdk from '@bancor/carbon-sdk'; | ||
* const sdk = new Sdk('https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY'); | ||
* You can optionally pass a callback to fetch decimals, these decimals will be cached and used internally by the sdk | ||
*/ | ||
export declare class Sdk extends Sdk_base { | ||
private _api; | ||
private _cache; | ||
private _syncer; | ||
private _tradingFeePPM; | ||
/** | ||
* Creates a new Sdk instance. | ||
* @param {Config} config - SDK config - must include `rpcUrl`, the URL of the JSON-RPC endpoint to connect to. | ||
* @param {function} [decimalFetcher] - A function that takes a token address and | ||
* returns a promise that resolves with the number of decimals for that address. | ||
* If not provided, the SDK will fetch decimals from the chain. | ||
* @constructor | ||
*/ | ||
constructor(config: Config, decimalFetcher?: DecimalFetcher); | ||
static getMatchActions(amountWei: string, tradeByTargetAmount: boolean, ordersMap: { | ||
[orderId: string]: string; | ||
}): SerializableMatchAction[]; | ||
get api(): Api; | ||
/** | ||
* Fetches blockchain data and subscribes to events. | ||
* This method should be called before any other actions are taken with the SDK, | ||
* with the exception of strategy creation, update and delete which don't rely on cached data. | ||
* @returns {Promise<void>} A promise that resolves once the data sync has completed. | ||
* @example | ||
* await sdk.startDataSync(); | ||
*/ | ||
startDataSync(cachedData?: string): Promise<void>; | ||
get isInitialized(): boolean; | ||
/** | ||
* Returns the token pairs that exist in the cache. | ||
* | ||
* @returns {TokenPair[]} An array of token pairs. | ||
* @throws {Error} If `startDataSync` has not been called. | ||
*/ | ||
getCachedPairs(): TokenPair[]; | ||
getCacheDump(): string; | ||
/** | ||
* Returns whether a pair has liquidity | ||
* | ||
* @param {string} sourceToken - address of the token the trade sells. | ||
* @param {string} targetToken - address of the token the trade buys. | ||
* | ||
* @returns {Boolean} true or false. | ||
* @throws {Error} If `startDataSync` has not been called. | ||
* @throws {Error} If no orders have been found. | ||
*/ | ||
hasLiquidityByPair(sourceToken: string, targetToken: string): Promise<boolean>; | ||
/** | ||
* Returns liquidity for a given pair. | ||
* | ||
* @param {string} sourceToken - address of the token the trade sells. | ||
* @param {string} targetToken - address of the token the trade buys. | ||
* | ||
* @returns {Promise<String>} liquidity value as string | ||
* @throws {Error} If `startDataSync` has not been called. | ||
*/ | ||
getLiquidityByPair(sourceToken: string, targetToken: string): Promise<string>; | ||
/** | ||
* Returns the maximum source amount for a given pair. | ||
* This is the sum of all source amounts in the orderbook. | ||
* This number represents the maximum amount that can be traded by source. | ||
* @param {string} sourceToken - address of the token the trade sells. | ||
* @param {string} targetToken - address of the token the trade buys. | ||
* @returns {Promise<String>} maximum source amount as string | ||
* @throws {Error} If `startDataSync` has not been called. | ||
* | ||
*/ | ||
getMaxSourceAmountByPair(sourceToken: string, targetToken: string): Promise<string>; | ||
/** | ||
* Gets the strategies that are owned by the user. | ||
* It does so by reading the voucher token and | ||
* figuring out strategy IDs from them. | ||
* @param {string} user the user who owns the strategies | ||
* @returns {Promise<Strategy[]>} strategies | ||
* | ||
*/ | ||
getUserStrategies(user: string): Promise<Strategy[]>; | ||
/** | ||
* Returns the data needed to process a trade. The data is serializable and can be | ||
* passed to a webworker to be processed in a separate thread. | ||
* `getMatchParams` returns the data for a given source and target token pair. | ||
* you can take the result and use it to call matchBySourceAmount or matchByTargetAmount | ||
* then get the actions and pass them to getTradeDataFromActions | ||
*/ | ||
getMatchParams(sourceToken: string, targetToken: string, amount: string, tradeByTargetAmount: boolean): Promise<{ | ||
orders: { | ||
[orderId: string]: string; | ||
}; | ||
amountWei: string; | ||
sourceDecimals: number; | ||
targetDecimals: number; | ||
}>; | ||
/** | ||
* Returns the off chain match algo results of orders to fulfill in order to complete the trade. | ||
* Each trade action is identified by the ID of the strategy that the trade order belongs to | ||
* and the input amount to place for this order. | ||
* | ||
* The `trade` method will match the specified `amount` of source tokens or target tokens with available orders from the | ||
* blockchain, depending on the value of `tradeByTargetAmount`. It uses the provided `filter` function to filter the available orders. | ||
* The resulting trade actions will be returned in an object, along with the unsigned transaction that can be used to execute | ||
* the trade. | ||
* | ||
* It is up to the user to sign and send the transaction. | ||
* | ||
* @param {string} sourceToken - The source token for the trade. | ||
* @param {string} targetToken - The target token for the trade. | ||
* @param {BigNumber} amount - The amount of source tokens or target tokens to trade, depending on the value of `tradeByTargetAmount`. | ||
* @param {boolean} tradeByTargetAmount - Whether to trade by target amount (`true`) or source amount (`false`). | ||
* @param {(rate: Rate) => boolean} filter - A function to filter the available orders. | ||
* @returns {Object} An object containing the trade actions and the unsigned trade transaction. | ||
* @property {TradeActionStruct[]} tradeActions - An array of trade actions in wei - pass it to `composeTradeTransaction` to create unsigned trade transaction. | ||
* @property {Action[]} actionsTokenRes - An array of trade actions in the resolution of the proper token. | ||
* @property {string} totalInput - Sum of all inputs, in token resolution. | ||
* @property {string} totalOutput - Sum of all outputs, in token resolution. | ||
* @property {BigNumber} effectiveRate - The rate between totalInput and totalOutput | ||
* @property {MatchAction[]} actionsWei - An array of trade actions in wei. | ||
* @throws {Error} If `startDataSync` has not been called. | ||
*/ | ||
getTradeData(sourceToken: string, targetToken: string, amount: string, tradeByTargetAmount: boolean, filter?: (rate: Rate) => boolean): Promise<{ | ||
tradeActions: TradeActionStruct[]; | ||
actionsTokenRes: Action[]; | ||
totalSourceAmount: string; | ||
totalTargetAmount: string; | ||
effectiveRate: string; | ||
actionsWei: SerializableMatchAction[]; | ||
}>; | ||
getTradeDataFromActions(sourceToken: string, targetToken: string, tradeByTargetAmount: boolean, actionsWei: SerializableMatchAction[]): Promise<{ | ||
tradeActions: TradeActionStruct[]; | ||
actionsTokenRes: Action[]; | ||
totalSourceAmount: string; | ||
totalTargetAmount: string; | ||
effectiveRate: string; | ||
actionsWei: SerializableMatchAction[]; | ||
}>; | ||
/** | ||
* Creates an unsigned transaction to fulfill a trade using an array of trade actions. | ||
* Each trade action is identified by the ID of the strategy that the trade order belongs to | ||
* and the input amount to place for this order. | ||
* | ||
* It is up to the user to sign and send the transaction. | ||
* | ||
* @param {string} sourceToken - The source token for the trade. | ||
* @param {string} targetToken - The target token for the trade. | ||
* @param {TradeActionStruct[]} tradeActions - An array of trade actions in wei - as received from `trade`. | ||
* @param {BigNumberish} deadline - Deadline for the trade | ||
* @param {BigNumberish} maxInput - Maximum input for the trade | ||
* @param {Overrides} [overrides] - Optional overrides for the transaction. | ||
* @returns {Promise<PopulatedTransaction>} A promise that resolves to the unsigned trade transaction. | ||
* | ||
* @example | ||
* // calling trade | ||
* const tradeTx = sdk.composeTradeTransaction( | ||
* '0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A', | ||
* '0x6B175474E89094C44Da98b954EedeAC495271d0F', | ||
* false, | ||
* [] | ||
* ); | ||
* | ||
* // Performing the trade by signing and sending the transaction: | ||
* | ||
* // Import the ethers.js library and the relevant wallet provider | ||
* const ethers = require('ethers'); | ||
* const provider = new ethers.providers.Web3Provider(web3.currentProvider); | ||
* | ||
* // Load the private key for the wallet that will sign and send the transaction | ||
* const privateKey = '0x...'; | ||
* const wallet = new ethers.Wallet(privateKey, provider); | ||
* | ||
* // Sign and send the transaction | ||
* const signedTradeTx = await wallet.sign(tradeTx); | ||
* const txReceipt = await provider.sendTransaction(signedTradeTx); | ||
* console.log(txReceipt); | ||
* // { | ||
* // blockHash: '0x...', | ||
* // blockNumber: 12345, | ||
* // ... | ||
* // } | ||
*/ | ||
composeTradeByTargetTransaction(sourceToken: string, targetToken: string, tradeActions: TradeActionStruct[], deadline: BigNumberish, maxInput: string, overrides?: PayableOverrides): Promise<PopulatedTransaction>; | ||
/** | ||
* Creates an unsigned transaction to fulfill a trade using an array of trade actions. | ||
* Each trade action is identified by the ID of the strategy that the trade order belongs to | ||
* and the input amount to place for this order. | ||
* | ||
* It is up to the user to sign and send the transaction. | ||
* | ||
* @param {string} sourceToken - The source token for the trade. | ||
* @param {string} targetToken - The target token for the trade. | ||
* @param {TradeActionStruct[]} tradeActions - An array of trade actions in wei - as received from `trade`. | ||
* @param {BigNumberish} deadline - Deadline for the trade | ||
* @param {BigNumberish} minReturn - Minimum return for the trade | ||
* @param {Overrides} [overrides] - Optional overrides for the transaction. | ||
* @returns {Promise<PopulatedTransaction>} A promise that resolves to the unsigned trade transaction. | ||
* | ||
* @example | ||
* // calling trade | ||
* const tradeTx = sdk.composeTradeTransaction( | ||
* '0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A', | ||
* '0x6B175474E89094C44Da98b954EedeAC495271d0F', | ||
* false, | ||
* [] | ||
* ); | ||
* | ||
* // Performing the trade by signing and sending the transaction: | ||
* | ||
* // Import the ethers.js library and the relevant wallet provider | ||
* const ethers = require('ethers'); | ||
* const provider = new ethers.providers.Web3Provider(web3.currentProvider); | ||
* | ||
* // Load the private key for the wallet that will sign and send the transaction | ||
* const privateKey = '0x...'; | ||
* const wallet = new ethers.Wallet(privateKey, provider); | ||
* | ||
* // Sign and send the transaction | ||
* const signedTradeTx = await wallet.sign(tradeTx); | ||
* const txReceipt = await provider.sendTransaction(signedTradeTx); | ||
* console.log(txReceipt); | ||
* // { | ||
* // blockHash: '0x...', | ||
* // blockNumber: 12345, | ||
* // ... | ||
* // } | ||
*/ | ||
composeTradeBySourceTransaction(sourceToken: string, targetToken: string, tradeActions: TradeActionStruct[], deadline: BigNumberish, minReturn: string, overrides?: PayableOverrides): Promise<PopulatedTransaction>; | ||
/** | ||
* Creates an unsigned transaction to create a strategy for buying and selling tokens of `baseToken` for price in `quoteToken` per 1 `baseToken`. | ||
* | ||
* The `createBuySellStrategy` method creates a strategy object based on the specified parameters, encodes it according to the | ||
* format used by the smart contracts, and returns an unsigned transaction that can be used to create the strategy on the | ||
* blockchain. | ||
* | ||
* It is up to the user to sign and send the transaction. | ||
* | ||
* @param {string} baseToken - The address of the base token for the strategy. | ||
* @param {string} quoteToken - The address of the quote token for the strategy. | ||
* @param {string} buyPriceLow - The minimum buy price for the strategy, in in `quoteToken` per 1 `baseToken`, as a string. | ||
* @param {string} buyPriceHigh - The maximum buy price for the strategy, in `quoteToken` per 1 `baseToken`, as a string. | ||
* @param {string} buyBudget - The maximum budget for buying tokens in the strategy, in `quoteToken`, as a string. | ||
* @param {string} sellPriceLow - The minimum sell price for the strategy, in `quoteToken` per 1 `baseToken`, as a string. | ||
* @param {string} sellPriceHigh - The maximum sell price for the strategy, in `quoteToken` per 1 `baseToken`, as a string. | ||
* @param {string} sellBudget - The maximum budget for selling tokens in the strategy, in `baseToken`, as a string. | ||
* @param {Overrides} [overrides] - Optional overrides for the transaction, such as gas price or nonce. | ||
* @returns {Promise<PopulatedTransaction>} A promise that resolves to the unsigned transaction that can be used to create the strategy. | ||
* * | ||
* @example | ||
* // Import the ethers.js library and the relevant wallet provider | ||
* const ethers = require('ethers'); | ||
* const provider = new ethers.providers.Web3Provider(web3.currentProvider); | ||
* | ||
* // Load the private key for the wallet that will sign and send the transaction | ||
* const privateKey = '0x...'; | ||
* const wallet = new ethers.Wallet(privateKey, provider); | ||
* | ||
* // Call the createBuySellStrategy method to create an unsigned transaction | ||
* const createStrategyTx = sdk.createBuySellStrategy( | ||
* '0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A', | ||
* '0x6B175474E89094C44Da98b954EedeAC495271d0F', | ||
* '0.1', | ||
* '0.2', | ||
* '1', | ||
* '0.5', | ||
* '0.6', | ||
* '2' | ||
* ); | ||
* | ||
* // Sign and send the transaction | ||
* const signedCreateStrategyTx = await wallet.sign(createStrategyTx); | ||
* const txReceipt = await provider.sendTransaction(signedCreateStrategyTx); | ||
*/ | ||
createBuySellStrategy(baseToken: string, quoteToken: string, buyPriceLow: string, buyPriceHigh: string, buyBudget: string, sellPriceLow: string, sellPriceHigh: string, sellBudget: string, overrides?: PayableOverrides): Promise<PopulatedTransaction>; | ||
/** | ||
* | ||
* @param strategyId | ||
* @param encodedStr | ||
* @param param2 | ||
* @param {MarginalPriceOptions | string} marginalPrice - The marginal price parameter. | ||
* Can either be a value from the `MarginalPriceOptions` enum, or a "BigNumberish" string value for advanced users - | ||
* who wish to set the marginal price themselves. | ||
* @param overrides | ||
* @returns | ||
*/ | ||
updateStrategy(strategyId: BigNumberish, encodedStr: string, { buyPriceLow, buyPriceHigh, buyBudget, sellPriceLow, sellPriceHigh, sellBudget, }: StrategyUpdate, buyMarginalPrice?: MarginalPriceOptions | string, sellMarginalPrice?: MarginalPriceOptions | string, overrides?: PayableOverrides): Promise<PopulatedTransaction>; | ||
deleteStrategy(strategyId: BigNumberish): Promise<PopulatedTransaction>; | ||
/** | ||
* Returns liquidity for a given rate. | ||
* | ||
* @param {string} sourceToken - address of the token the trade sells. | ||
* @param {string} targetToken - address of the token the trade buys. | ||
* @param {string[]} rates - the rates for which to find liquidity depth. | ||
* | ||
* @returns {Promise<String[]>} liquidity value as string | ||
* @throws {Error} If `startDataSync` has not been called. | ||
*/ | ||
getRateLiquidityDepthsByPair(sourceToken: string, targetToken: string, rates: string[]): Promise<string[]>; | ||
getMinRateByPair(sourceToken: string, targetToken: string): Promise<string>; | ||
getMaxRateByPair(sourceToken: string, targetToken: string): Promise<string>; | ||
/** | ||
* Returns the last trade for a given pair. | ||
* @param {string} sourceToken - address of the token the trade sells. | ||
* @param {string} targetToken - address of the token the trade buys. | ||
* @returns {TradeData>} last trade for the given pair | ||
*/ | ||
getLastTradeByPair(sourceToken: string, targetToken: string): Promise<TradeData | undefined>; | ||
getAllPairs(): Promise<TokenPair[]>; | ||
} | ||
export * as contractsApi from './contracts-api'; | ||
export * as chainCache from './chain-cache'; | ||
export * as tradeMatcher from './trade-matcher'; | ||
export * as utils from './utils'; | ||
export * as strategyManagement from './strategy-management'; | ||
export * from './common/types'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -5,7 +5,36 @@ { | ||
"source": "src/index.ts", | ||
"version": "0.0.67-DEV", | ||
"version": "0.0.68-DEV", | ||
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades", | ||
"main": "dist/sdk.umd.js", | ||
"module": "dist/sdk.module.mjs", | ||
"exports": "dist/sdk.modern.mjs", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./contracts-api": "./dist/contracts-api/index.js", | ||
"./chain-cache": "./dist/chain-cache/index.js", | ||
"./trade-matcher": "./dist/trade-matcher/index.js", | ||
"./utils": "./dist/utils/index.js", | ||
"./strategy-management": "./dist/strategy-management/index.js" | ||
}, | ||
"files": [ | ||
"dist/" | ||
], | ||
"typesVersions": { | ||
"*": { | ||
"contracts-api": [ | ||
"dist/contracts-api" | ||
], | ||
"chain-cache": [ | ||
"dist/chain-cache" | ||
], | ||
"trade-matcher": [ | ||
"dist/trade-matcher" | ||
], | ||
"utils": [ | ||
"dist/utils" | ||
], | ||
"strategy-management": [ | ||
"dist/strategy-management" | ||
] | ||
} | ||
}, | ||
"types": "dist/index.d.ts", | ||
@@ -16,4 +45,4 @@ "scripts": { | ||
"prebuild": "yarn clean && yarn compile-abis", | ||
"build": "microbundle src/index.ts", | ||
"dev": "microbundle watch", | ||
"build": "rollup -c", | ||
"dev": "rollup -c -w", | ||
"test": "mocha" | ||
@@ -45,2 +74,4 @@ }, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.2", | ||
"@typechain/ethers-v5": "^10.2.0", | ||
@@ -50,8 +81,7 @@ "@types/chai": "^4.3.4", | ||
"@types/node": "^18.15.11", | ||
"@types/proxyquire": "^1.3.28", | ||
"@types/sinon": "^10.0.13", | ||
"chai": "^4.3.7", | ||
"microbundle": "^0.15.1", | ||
"mocha": "^10.1.0", | ||
"proxyquire": "^2.1.3", | ||
"rollup": "^3.20.2", | ||
"rollup-plugin-typescript2": "^0.34.1", | ||
"sinon": "^15.0.3", | ||
@@ -58,0 +88,0 @@ "ts-node": "^10.9.1", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
1037196
15
100
23696
2
1