@uniblock/uniswap v0.4.1
uniblock-sdk uniswap
This sdk can be used to integrate uniswap interactions into your dapp
Setup
yarn install @uniblock/uniswap
or
npm install @uniblock/uniswap
Functions
Note
In examples below providers and signers were declared as follows:
// getting metamask provider and signer using ethers js
let provider;
let signer;
if (window.ethereum) {
await window.ethereum.request({ method: 'eth_requestAccounts' });
provider = await new ethers.providers.Web3Provider(window.ethereum);
signer = await provider.getSigner();
}
To access the functions associated with the SDK you must first create an instance of the Uniswap
class from @uniblock/uniswap
. This is done using our initializeUniswap
function. Given your uniblock api key the function will return an instance of the Uniswap
class. You may then use this object to access the functions of the SDK:
// creating instance of the Uniswap class
const uniswap = await initializeUniswap(uniblockApiKey);
getPrices
gets spot prices for tokens associated with a uniswap pool
Params
parameter | type |
---|---|
poolAddress | string |
provider | ethers provider |
chainId | number |
Return
- Object containing the following
- spotPrice0
- spotPrice0Units
- spotPrice1
- spotPrice1Units
Example
const prices = await uniswap.getPrices(
'0x4d1892f15B03db24b55E73F9801826a56d6f0755', // UNI WETH Pool Address
provider,
5, // Goerli Testnet
);
trade
makes a trade on uniswap
Params
parameter | type |
---|---|
poolAddress | string |
provider | ethers provider |
signer | ethers signer |
chainId | number |
amount | number |
walletAddress | string |
tokenInAddress | string |
slippageInPercent | number where default is 0.5% |
deadlineInMin | number where default is 10 min |
Return
- Object containing transaction data
Example
const res = await uniswap.trade(
'0x4d1892f15B03db24b55E73F9801826a56d6f0755', // UNI WETH Pool Address
provider,
signer,
5, // Goerli Testnet
0.001, // 0.001 WETH
RECIPIENT_WALLET_ADDRESS,
'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6', // WETH Address
5, // 5%
2, // 2 min
);
getLiquidityPositionTokenIds
gets account's liquidity position token ids
Params
parameter | type |
---|---|
walletAddress | string |
chainId | number |
Return
- Array of token ids
Example
const res = await uniswap.getLiquidityPositionTokenIds(
OWNER_WALLET_ADDRESS,
5, // Goerli Testnet
);
getPoolAddress
gets pool address associated with a token pair and fee tier
Params
parameter | type |
---|---|
token0 | string |
token1 | string |
fee | number |
provider | ethers provider |
Return
- Pool address
Example
const res = await uniswap.getPoolAddress(
'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6', // WETH Address
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', // UNI Address
3000, // 0.3% Fee Tier
provider,
);
createNewPool
create a new pool for a pair and fee tier
Params
parameter | type |
---|---|
token0 | string |
token1 | string |
fee | number |
signer | ethers signer |
Return
- Object containing transaction data
Example
const res = await uniswap.createNewPool(
YOUR_TOKEN_0_ADDRESS,
YOUR_TOKEN_1_ADDRESS,
3000, // 0.3% Fee Tier
signer,
);
addLiquidity
adds liquidity to a specified pool, where amount of liquidity is based on quantity of one of the tokens being deposited
Params
parameter | type |
---|---|
poolAddress | string |
provider | ethers provider |
signer | ethers signer |
chainId | number |
tokenInAddress | string |
tokenInAmount | number |
walletAddress | string |
slippageInPercent | number where default is 0.5% |
deadlineInMin | number where default is 10 min |
ticksFromNearestUseableUpper | number where default is 2 ticks |
ticksFromNearestUseableLower | number where default is 2 ticks |
Return
- Object containing transaction data
Example
const res = await uniswap.addLiquidity(
'0x4d1892f15B03db24b55E73F9801826a56d6f0755', // UNI WETH Pool Address
provider,
signer,
5, // Goerli Testnet
'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6', // WETH Address
0.002, // 0.002 WETH
OWNER_WALLET_ADDRESS,
12, // 12%
5, // 5 min
3, // 3 ticks more than nearest useable tick
3, // 3 ticks less than nearest useable tick
);
addLiquidityToExistingPosition
adds liquidity to an existing position, where amount of liquidity is based on quantity of one of the tokens being deposited
Params
parameter | type |
---|---|
poolAddress | string |
provider | ethers provider |
signer | ethers signer |
chainId | number |
tokenInAddress | string |
tokenInAmount | number |
tokenId | number |
slippageInPercent | number where default is 0.5% |
deadlineInMin | number where default is 10 min |
Return
- Object containing transaction data
Example
const res = await uniswap.addLiquidityToExistingPosition(
'0x4d1892f15B03db24b55E73F9801826a56d6f0755', // UNI WETH Pool Address
provider,
signer,
5, // Goerli Testnet
'0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6', // WETH Address
0.005, // 0.005 WETH
22134,
5, // 5%
30, // 30 min
);
removeLiquidity
removes liquidity from an existing position, where percentage of the liquidity removed is specified
Params
parameter | type |
---|---|
poolAddress | string |
provider | ethers provider |
signer | ethers signer |
chainId | number |
tokenId | number |
slippageInPercent | number where default is 0.5% |
liquidityToRemoveInPercent | number where default is 100% |
deadlineInMin | number where default is 10 min |
Return
- Object containing transaction data
Example
const res = await uniswap.removeLiquidity(
'0x4d1892f15B03db24b55E73F9801826a56d6f0755', // UNI WETH Pool Address
provider,
signer,
5, // Goerli Testnet
22134,
5, // 5%
25, // 25%
30, // 30 min
);
claimLiquidity
claims all tokens owed from a liquidity position
Params
parameter | type |
---|---|
signer | ethers signer |
walletAddress | string |
tokenId | number |
Return
- Object containing transaction data
Example
const res = await uniswap.claimLiquidity(
signer,
RECIPIENT_WALLET_ADDRESS,
22134,
);
9 months ago
9 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
8 months ago
9 months ago
9 months ago
8 months ago
8 months ago
8 months ago
10 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago