@cowprotocol/permit-utils
Collection of utils for handling token permits.
Installation
yarn add @cowprotocol/permit-utils
Usage
getTokenPermitInfo
import { getTokenPermitInfo } from "@cowprotocol/permit-utils"
const permitInfo = await getTokenPermitInfo({
spender, tokenAddress, tokenName, chainId, provider
})
getPermitUtilsInstance
import { getPermitUtilsInstance } from "@cowprotocol/permit-utils"
const staticEip2612PermitUtils = getPermitUtilsInstance(chainId, provider)
const accountEip2612PermitUtils = getPermitUtilsInstance(chainId, provider, account)
generatePermitHook
import { generatePermitHook } from "@cowprotocol/permit-utils"
const hookData = await generatePermitHook({
chainId,
inputToken,
spender,
provider,
permitInfo,
eip2162Utils,
account,
nonce
})
checkIsCallDataAValidPermit
import { checkIsCallDataAValidPermit } from "@cowprotocol/permit-utils"
const isCallDataAValidPermit = await checkIsCallDataAValidPermit(
account,
chainId,
eip2612Utils,
tokenAddress,
tokenName,
callData,
permitInfo
)
Full flow example
To illustrate, we'll show the flow of placing an order with a permit hook using the utils in this lib.
- Check whether a given token is permittable
- If it is, generate a permit hook
- Add permit hook to order's appData
- Periodically check whether the permit is still valid
import { checkIsCallDataAValidPermit, generatePermitHook, getPermitUtilsInstance, getTokenPermitInfo } from '@cowprotocol/permit-utils'
import { stringifyDeterministic } from '@cowprotocol/app-data'
import { OrderBookApi } from '@cowprotocol/cow-sdk'
const permitInfo = await getTokenPermitInfo({
spender, tokenAddress, tokenName, chainId, provider
})
if (!permitInfo) {
return
}
const eip2612Utils = getPermitUtilsInstance(chainId, provider, account)
const nonce = await eip2162Utils.getTokenNonce(inputToken.address, account)
const hookData = await generatePermitHook({
chainId,
inputToken,
spender,
provider,
permitInfo,
eip2612Utils,
account,
nonce
})
const appData = { version: '0.10.0', metadata: { hooks: { pre: [hookData] } } }
const fullAppData = await stringifyDeterministic(appData)
const orderBookApi = new OrderBookApi()
const orderId = await orderBookApi.sendOrder(
{
...orderParameters,
quoteId,
appData: fullAppData
},
{ chainId }
)
const callData = hookData.callData
const isCallDataAValidPermit = await checkIsCallDataAValidPermit(
account,
chainId,
eip2612Utils,
tokenAddress,
tokenName,
callData,
permitInfo
)