/zyfi-paymaster

Paymaster implentation for zkSync Era

Primary LanguageTypeScriptGNU General Public License v3.0GPL-3.0

Zyfi Paymaster

This project was scaffolded with zksync-cli.

Overview

Zyfi allows any protocol on zkSync to integrate their native paymaster technology with a single API call.

The API:

  • Receive the transaction data and the feeToken address
  • Estimate the gas limit of the transactions
  • Fetch the price of the token off-chain
  • Returns the transactions with the added paymaster parameters:
    • The necessary token amount (the user allows to the paymaster only what is needed for each transaction)
    • The block.timestamp expiration of the transaction
    • A signed message that is used to validate that the trasaction was generated by the API

Currently we offer 2 paymasters:

  • ERC20Paymaster: simple implementation where the user can pay with any token and be refunded at the end in the token used

  • ERC20SponsorPaymaster: in addition to the above, a protocol can decide when calling the API to sponsor part or all of the transaction. SponsorshipVault is used as wallet for the protocols.

Please check our documentation.

Benefits

Our off-chain implementation allows the following features:

  • All ERC20 tokens can be supported, with no need of on-chain oracles
  • Protocols can implement any business logic to decide their sponsorship ratio for a given transaction.
  • It's trustless for the user as they can verify the maximum amount they would be spending when signing the transaction (no max allow needed)

Project structure

  • /contracts: smart contracts.
  • /deploy: deployment and contract interaction scripts.
  • /test: test files

Commands

  • npx zksync-cli dev start to start a local node. It can be checked with docker logs --follow zkcli-in-memory-node-zksync-1
  • bun install will install the dependencies.
  • bun run compile will compile the contracts.
  • bun run test will run tests. Check test requirements below.
  • npx zksync-cli dev start can be used to set up an explorer or dapp for local tests

Environment variables

In order to prevent users to leak private keys, this project uses environment variables. It's used to load the wallet private key, required to run the deploy script.

To use it, rename .env.example to .env and enter your private key.

WALLET_PRIVATE_KEY=123cde574ccff....

Local testing

In order to run test, you need to start the zkSync local environment. This project uses the in-memory node as it's only on L2. Begin by installing era-test-node using the command:

cargo install --git https://github.com/matter-labs/era-test-node.git --locked

Rust should install it in the ~/.cargo/bin directory.

To start the node, execute:

yarn local-node

P lease check this section of the docs which contains all the details.

If you do not start the zkSync local environment, the tests will fail with error Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.2)

Links