Skip to main content

Methods

Uniswap methods made easier with Unifi.

Unifi supports the following chains for Uniswap

UNISWAP SUPPORTED CHAINS
GOERLI:5
ETHEREUM_MAINNET:1
POLYGON_MAINNET:137
ARBITRUM_MAINNET:42161
OPTIMISM_MAINNET:10
POLYGON_MUMBAI:80001
BINANCE: SOON
CELO: SOON

approve()

await unifi.uniswap.approve({
walletAddress:,//your wallet address
tokenAddress:,//token contract address in the respective chain
amount:,//amount of token to be approved, example "0.5",'20.534","10000"
chain:,//chainId
})

returns transaction object to approve uniswap to handle your tokens. This method is required to be executed before you do unifi.uniswap.swap calls.

If you do not pass amount, then default value is used. The default value is the maximum value possible in solidity 2^256-1.

let unsignedTxn = await unifi.uniswap.approve({
walletAddress: wallet.address,
tokenAddress: "0x70cBa46d2e933030E2f274AE58c951C800548AeF",
amount: "100000",
chainId: 5,
});

This call returns an unsignedTxn object which you can sign with your wallet to submit your transaction

Output of the above will be similar as shown below.

{
data: '0x095ea7b300000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc4500000000000000000000000000000000000000000000000000000000000186a0',
to: '0x70cBa46d2e933030E2f274AE58c951C800548AeF',
nonce: 842,
gasLimit: { type: 'BigNumber', hex: '0x0493e0' },
gasPrice: { type: 'BigNumber', hex: '0x015b3970' }
}

getQuote()

await unifi.uniswap.getQuote({
sellTokenAddress:,//sell Token Contract Address
buyTokenAddress:,//buy Token Contract Address
//you should use either sellTokenAmount or buyTokenAmount NOT BOTH
sellTokenAmount:,//amount of sell Token to be swapped for receiving buy Token
buyTokenAmount:,//amount of buy Token to be received for appropriate sell Token buy Token
//
chainId:,//chainId
});

Get the best Quotes from Uniswap accessing all liquidity present. Returns an Object with all essential information about the swap

  let swapQuote = await unifi.uniswap.getQuote({
walletAddress:wallet.address,
sellTokenAddress:"0xD6DF932A45C0f255f85145f286eA0b292B21C90B",
buyTokenAddress:"0xbFc70507384047Aa74c29Cdc8c5Cb88D0f7213AC",
sellTokenAmount:"10000000",
chainId:137,
}
)

The Output for the above will be similar to this

quote is the expected amount of buyTokens received after swap, if sellTokenAmount is mentioned

quote is the expected amount of sellTokens required for the swap, if buyTokenAmount is mentioned

{
quote: '62.153421426358290464',
gasUsed: '317000',
priceImpact: '100.00',
minimumAmountOut: '61.53804'
}

swap()

await unifi.uniswap.swap({
walletAddress:, //wallet address
sellTokenAddress:,//sell token contract address
buyTokenAddress:,//buy token contract address
//you should use either sellTokenAmount or buyTokenAmount NOT BOTH
sellTokenAmount:,//amount of sell Token to be swapped for receiving buy Token
buyTokenAmount:,//amount of buy Token to be received for appropriate sell Token buy Token
//
recipientAddress:,//address receiving the tokens after swap, in general you can use your wallet address
chainId:,//chainId
});

returns transaction Object for Swaping Tokens, pass in the sellTokenAddress for the token you want to sell and buyTokenAddress for the token you want to buy.

You can either pass sellTokenAmount or buyTokenAmount depending on your need, but please never pass both.

let unsignedTxn = await unifi.uniswap.swap({
walletAddress: wallet.address,
sellTokenAddress: "0x70cBa46d2e933030E2f274AE58c951C800548AeF",
buyTokenAddress: "0x07865c6e87b9f70255377e024ace6630c1eaa37f",
buyTokenAmount: "1000",
recipientAddress: wallet.address,
chainId: 5,
});

This call returns an unsignedTxn object which you can sign with your wallet to submit your transaction

Output of the above will be similar as shown below

{
data: '0x5ae401dc00000000000000000000000000000000000000000000000000000000643c9683000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000010442712a670000000000000000000000000000000000000000000000000000000000989680000000000000000000000000000000000000000000000000001be2e1dedca98f0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000f522c2e707345475da50bd45a4c8061df4de722e000000000000000000000000000000000000000000000000000000000000000300000000000000000000000070cba46d2e933030e2f274ae58c951c800548aef000000000000000000000000b4fbf271143f4fbf7b91a5ded31805e42b2208d600000000000000000000000007865c6e87b9f70255377e024ace6630c1eaa37f00000000000000000000000000000000000000000000000000000000',
to: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
value: { type: 'BigNumber', hex: '0x00' },
from: '0xf522C2E707345475dA50BD45a4C8061df4dE722E',
gasPrice: { type: 'BigNumber', hex: '0x015b3970' },
gasLimit: { type: 'BigNumber', hex: '0x043bfc' },
nonce: 842
}

Sample implementation Below

const Unifi = require('unifi-sdk')
const ethers = require('ethers');
let provider = new ethers.providers.JsonRpcProvider('your-provider-url');
let wallet = new ethers.Wallet('your-private-key',provider);

let apiKey = 'your-api-key';
let unifi = Unifi(apiKey);

async function execute(){

let unsignedTxn = await unifi.uniswap.swap({
walletAddress:wallet.address,
sellTokenAddress:"0x70cBa46d2e933030E2f274AE58c951C800548AeF",
buyTokenAddress:"0x07865c6e87b9f70255377e024ace6630c1eaa37f",
buyTokenAmount:"1000000",
recipientAddress:wallet.address,
chainId:5,
}
)

let sentTxn = await wallet.sendTransaction(unsignedTxn)
let txnReceipt = await sentTxn.wait();
console.log('txnReceipt',txnReceipt)
}

execute()