npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@reserve-protocol/token-zapper

v3.11.0

Published

A library to help convert tokens into RTokens and back

Downloads

1,044

Readme

Reserve token Zapper

This is a TypeScript library that enables finding the best way to swap between any token and into an RToken for the Reserve protocol.

Installation

You can install the package using npm:

npm install @reserve-protocol/token-zapper

Usage

To use the library, import it in your TypeScript file:

import {
  fromProvider
} from '@reserve-protocol/token-zapper'

// The fromProvider<1> in the fromProvider method, specifies the chainId. 
//   1 for ethereum mainnet
//   8453 for base
//   42161 for arbitrum
const zapperState = await fromProvider<1>(
  provider
)

// Zapper loads asynchroniously, you can wait the initialized promise to wait for it to fully bootstrap
await zapperState.initialized

// The the zapper state needs to be updated with every new block,
// because a lot of internal caching is bound to the current block, and gas price determines which zap is the best for the user
provider.on('block', async (blockNumber) => {
  await zapperState.updateBlockState(
    blockNumber,
    (await provider.getGasPrice()).toBigInt()
  )
})

After this setup, you can use the zapper to find a way to zap into an rToken, or zap redeem it into something else

const yourAddress = "0x....."
const zapTx = await zapperState.zap(
  zapperState.commonTokens.USDC.from(1000.0),
  zapperState.rTokens.eUSD,
  yourAddress
);

// const zapTx = await zapperState.redeem(
//  zapperState.rTokens.eUSD.from(1000.0),
//  zapperState.commonTokens.USDC,
//  yourAddress
// );

// You can get an overview of the zap transaction by describing it:
console.log(zapTx.describe().join("\n"));
Transaction {
  zap: 100000.0 USDC (99995.0 USD) -> 31.282717305975456267 ETH+ (98273.98377038 USD) (+ $183.56379896 USD D.) @ fee: 25.61216338 USD,
  dust: [0.021474740048719004 rETH (73.52053689 USD), 0.000000000000000001 stETH (0.0 stETH), 0.030485670541067423 wstETH (110.04326207 USD)],
  fees: 0.00828157863717086 ETH (25.61216338 USD) (3070865 wei)
  program: [
   cmd 0: // Curve,swap=33259.3 USDC -> 10.707839131756914597 frxETH
   amt_frxETH: uint256 = [curve-router-caller]:delegate.exchange(
      amountIn: uint256 = 33259300000,
      _expected: uint256 = 10600760740439345452,
      router: address = [0x99a5...788f],
      encodedRouterCall: bytes = [len=1666]0x000000000000000000000000a0b86991...00000000000000000000000000000000
   );

   cmd 1:
   bal_frxETH: uint256 = [tok=frxETH].balanceOf(account: address = [this]);

   cmd 2: // UniV3.exactInput(33377.2 USDC -> [USDC -> 0x8ad5...e6D8 -> WETH -> WETH -> 0xa4e0...9613 -> rETH] -> 9.699320281516622756 rETH)
   b: uint256 = [0x32F5...17A9]:delegate.exactInput(
      amountIn: uint256 = 33377200000,
      _expected: uint256 = 9675132450390646140,
      router: address = [0x68b3...Fc45],
      recipient: address = [this],
      path: bytes = [len=258]0xa0b86991c6218b36c1d19d4a2e9eb0ce...00000000000000000000000000000000
   );

   cmd 3: // Enso(33363.5 USDC, enso,lido, 10.743417369406715664 stETH)
   d: bytes[] = [0x80Eb...fB8E].routeSingle(
      tokenIn: address = [tok=USDC],
      amountIn: uint256 = 33363500000,
      commands: bytes32[] = (bytes32[]),
      state: bytes[] = (bytes[])
   );

   cmd 4:
   bal_stETH: uint256 = [tok=stETH].balanceOf(account: address = [this]);

   cmd 5:
   bal_frxETH: uint256 = [tok=frxETH].balanceOf(account: address = [this]);

   cmd 6:
   bal_rETH: uint256 = [tok=rETH].balanceOf(account: address = [this]);

   cmd 7:
   bal_stETH: uint256 = [tok=stETH].balanceOf(account: address = [this]);

   cmd 8:
   f: uint256 = [tok=sfrxETH].deposit(assets: uint256 = bal_frxETH, receiver: address = [this]);

   cmd 9:
   bal_sfrxETH: uint256 = [tok=sfrxETH].balanceOf(account: address = [this]);

   cmd 10:
   h: uint256 = [tok=wstETH].wrap(_stETHAmount: uint256 = bal_stETH);

   cmd 11:
   [this]:delegate.mintMaxRToken(facade: address = [0x81b9...eB3C], token: address = [tok=ETH+], recipient: address = [0x..yourAddress]);

   [...] // More commands to collect any leftover dust. (In the future we hope to reduce it or at least unwrap the returned tokens)
  ],
}

To execute your zap you just need to use the parametres in the zapTx:

const {
  to,
  data,
  value
} = zapTx.transaction.tx;

const signer = new ethers.Wallet([YOUR_PRIVATE_KEY], provider)
const { to, data, value } = zapTx.transaction.tx
const resp = await signer.sendTransaction({
  to,
  data,
  value
})
console.log("Tx pending, hash: ", resp.hash);
const receipt = await resp.wait(1)
console.log("Your zap was" + receipt.status === 1 ? "successfull!" : "reverted")

console.log("See it here: https://etherscan.io/tx/" + resp.hash);

Contributing

Contributions to this project are always welcome! Here are a few ways you can help:

Report bugs or issues by opening a new issue on the GitHub repository.
Implement new features by opening a pull request on the GitHub repository.
Improve the documentation by suggesting edits or additions.

Before submitting a pull request, please make sure your changes pass the existing tests and add new tests if necessary.

License

This project is licensed under the Blue Oak Model License - see the LICENSE file for details.