Git Product home page Git Product logo

eth-permit's Introduction

eth-permit

This package simplifies the process of signing permit messages for Ethereum tokens.

What is permit?

Permit is a technique for metatransaction token transfers. Using permit can allow a contract to use a user's tokens without the user first needing to first to send an approve() transaction.

Permit variations

Permit was first introduced in the Multi-Collateral Dai token contract.

The permit technique is being standardized as part of ERC-2612. This standard (which has already been implemented in projects like Uniswap V2) is slightly different than the implementation used by Dai. Therefore, this library provides functions for signing both types of messages.

Usage

Install the package eth-permit using npm or yarn.

Dai-style permits

import { signDaiPermit } from 'eth-permit';

// Sign message using injected provider (ie Metamask).
// You can replace window.ethereum with any other web3 provider.
const result = await signDaiPermit(window.ethereum, tokenAddress, senderAddress, spender);

await token.methods.permit(senderAddress, spender, result.nonce, result.expiry, true, result.v, result.r, result.s).send({
  from: senderAddress,
});

ERC2612-style permits

import { signERC2612Permit } from 'eth-permit';

const value = web3.utils.toWei('1', 'ether');

// Sign message using injected provider (ie Metamask).
// You can replace window.ethereum with any other web3 provider.
const result = await signERC2612Permit(window.ethereum, tokenAddress, senderAddress, spender, value);

await token.methods.permit(senderAddress, spender, value, result.deadline, result.v, result.r, result.s).send({
  from: senderAddress,
});

Ethers Wallet support

The library now supports Ethers.js Wallet signers:

import { signERC2612Permit } from 'eth-permit';

const value = web3.utils.toWei('1', 'ether');

const wallet = new ethers.Wallet(privateKey, new ethers.providers.JsonRpcProvider(rpcUrl));
const senderAddress = await wallet.getAddress();

const result = await signERC2612Permit(wallet, tokenAddress, senderAddress, spender, value);

await token.methods.permit(senderAddress, spender, value, result.deadline, result.v, result.r, result.s).send({
  from: senderAddress,
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.