npm.io
0.41.0 • Published 6 months ago

@rainbow-me/swaps

Licence
GPL-3.0
Version
0.41.0
Deps
14
Size
842 kB
Vulns
0
Weekly
0

Rainbow Swaps Image

Rainbow Swaps SDK

npm version License: GPL v3

Swap aggregator SDK used in Rainbow Wallet.

Installation

yarn add @rainbow-me/swaps
npm install @rainbow-me/swaps
pnpm add @rainbow-me/swaps

Usage

Get a quote for a pair
import { getQuote } from '@rainbow-me/swaps';

const quote = await getQuote({
  source,             // optional, "1inch" or "0x"
  chainId,            // numeric chain id
  fromAddress,        // address of the wallet to execute the swap from
  destReceiver,       // optional, address to receive the output tokens
  sellTokenAddress,   // address of the input token
  buyTokenAddress,    // address of the output token
  sellAmount,         // amount of the input token (required if not passing buyAmount)
  buyAmount,          // amount of the output token (required if not passing sellAmount)
  slippage,           // max slippage percentage allowed
});
Estimate gas for the swap
import { getQuoteExecutionDetails } from '@rainbow-me/swaps';

const { params, method, methodArgs } = getQuoteExecutionDetails(
  quote,                     // quote returned from getQuote
  { from: quote.from },      // transaction options
  provider                   // ethers provider
);

const estimatedGas = await method(methodArgs);
Execute swap for a given quote
import { fillQuote } from '@rainbow-me/swaps';

const hash = await fillQuote(
  quote,                // quote returned from getQuote
  transactionOptions,   // gasLimit, maxFeePerGas, maxPriorityFeePerGas, nonce, value, from
  permit,               // true if you want to use the permit
  chainId               // numeric chain id
);
Prepare fill quote transaction
import { prepareFillQuote } from '@rainbow-me/swaps';

const transaction = await prepareFillQuote(
  quote,                // quote returned from getQuote
  transactionOptions,   // gasLimit, maxFeePerGas, maxPriorityFeePerGas, nonce, value, from
  wallet,               // ethers signer
  permit,               // true if you want to use the permit
  chainId               // numeric chain id
);

// Returns a populated transaction object that can be sent later
const hash = await wallet.sendTransaction(transaction);

Features

  • Token to ETH swaps
  • Token to Token swaps
  • ETH to Token swaps
  • Use of permit when supported (to avoid an extra approval)
  • Supported chains: Mainnet, Optimism, Arbitrum, Polygon, Base, and more

Development

nvm use
yarn install
yarn build
yarn test
yarn lint

License

Licensed under the GPL-3.0 License.