1.3.2 • Published 3 years ago
1inch-spot-price-sdk v1.3.2
1inch-spot-price-sdk
About
Typescript module to use Spot Price Aggregator for 1inch supported chains.
1inch-spot-price-sdk
is a TypeScript / Javascript implementation of 1inch's Spot Price Aggregator based on ethers.js library to fetch aggregated on-chain token prices without hassle.
Using this library, you don't need any kind of blockchain knowledge to query dex prices, it would be easy as
import OneInchSpotPrice from '1inch-spot-price-sdk';
// Initialize the sdk
const spotPrice = new OneInchSpotPrice();
// It will query ETH/USDT price from the aggregated liquidity of Ethereum Dexes
spotPrice.getRate('ETH', 'USDT').then(rate => console.log(rate));
Features
- Embedded list of tokens supported by 1inch inside the library (No need to query 1inch api endpoint!)
- Embedded list of chains with public rpc endpoint aggregated from chainid.network & https://chainlist.org/
- Supports web3-providers-axios by default for querying the fastest returned result from public rpc endpoints. (You can also bring your ethers provider as an alternative).
- Returns real-time on-chain dex price rate as string value so that it could be read without any help from an external math library.
Installation
Node.js
npm install 1inch-spot-price-sdk
Documentation
https://ayanamitech.github.io/1inch-spot-price-sdk
Usage
Browser
WARN: We recommend hosting and controlling your own copy for security reasons
<script src="https://cdn.jsdelivr.net/npm/1inch-spot-price-sdk@latest"></script>
<script src="https://unpkg.com/1inch-spot-price-sdk@latest"></script>
Note that it would be helpful to setup the Subresource Integrity hash to ensure that the imported library has the desired codes.
For more info, see https://www.srihash.org/.
Example
// CommonJS
const OneInchSpotPrice = require('1inch-spot-price-sdk');
or
// ModuleJS / TypeScript
import OneInchSpotPrice from '1inch-spot-price-sdk';
// Initialize the sdk
const spotPrice = new OneInchSpotPrice();
// It will query ETH/USDT price from the aggregated liquidity of Ethereum Dexes
spotPrice.getRate('ETH', 'USDT').then(rate => console.log(rate));
With Promise.all()
import OneInchSpotPrice from '1inch-spot-price-sdk';
// Initialize the sdk
const spotPrice = new OneInchSpotPrice();
const getRates = async () => {
// Fetch the config data before calling getRate / getMultiRates
await spotPrice.initialize();
console.log(await Promise.all([
spotPrice.getRate('ETH', 'USDT'),
spotPrice.getRate('WBTC', 'USDT'),
spotPrice.getRate('ETH', 'WBTC')
]));
};
getRates();
Please checkout Type Definition for required parameters and expected output.