@reservoir0x/gelato-adapter
Advanced tools
Comparing version
import { ReservoirWallet } from "@reservoir0x/reservoir-sdk"; | ||
import { Signer } from "ethers/lib/ethers"; | ||
export const adaptGelatoRelayer: (signer: Signer, gelatoApiKey: string) => ReservoirWallet; | ||
export const adaptGelatoRelayer: (signer: Signer, gelatoApiKey?: string, gelatoProxyApiUrl?: string) => ReservoirWallet; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -49,3 +49,4 @@ var $gDyoy$gelatonetworkrelaysdk = require("@gelatonetwork/relay-sdk"); | ||
const $c92b1cb25cf90b00$export$f2ecb312e21d09e0 = (signer, gelatoApiKey)=>{ | ||
const $c92b1cb25cf90b00$export$f2ecb312e21d09e0 = (signer, gelatoApiKey, gelatoProxyApiUrl)=>{ | ||
if (!gelatoApiKey && !gelatoProxyApiUrl) throw new Error("You must supply either an apiKey or a proxy API url."); | ||
return { | ||
@@ -79,4 +80,26 @@ address: async ()=>{ | ||
try { | ||
const relayResponse = await relay.sponsoredCallERC2771(request, signer.provider, gelatoApiKey); | ||
const { taskId: taskId } = relayResponse; | ||
let relayResponse; | ||
if (gelatoApiKey) relayResponse = await relay.sponsoredCallERC2771(request, signer.provider, gelatoApiKey); | ||
else if (gelatoProxyApiUrl) { | ||
const signatureData = await relay.getSignatureDataERC2771(request, signer.provider, (0, $gDyoy$gelatonetworkrelaysdk.ERC2771Type).SponsoredCall); | ||
// userNonce must be a number to be parsed into JSON | ||
const signatureDataFormatted = { | ||
...signatureData, | ||
struct: { | ||
...signatureData.struct, | ||
userNonce: Number(signatureData.struct.userNonce) | ||
} | ||
}; | ||
const response = await fetch(gelatoProxyApiUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
signatureData: signatureDataFormatted | ||
}) | ||
}); | ||
relayResponse = await response.json(); | ||
} else throw new Error("You must supply either an apiKey or a proxy API url."); | ||
const { taskId: taskId } = relayResponse; | ||
const txHash = await (0, $1820cd20ca922dc9$export$9813dd697f9dfeee)(taskId); | ||
@@ -83,0 +106,0 @@ return txHash; |
@@ -1,2 +0,2 @@ | ||
import {GelatoRelay as $fFq2h$GelatoRelay} from "@gelatonetwork/relay-sdk"; | ||
import {GelatoRelay as $fFq2h$GelatoRelay, ERC2771Type as $fFq2h$ERC2771Type} from "@gelatonetwork/relay-sdk"; | ||
import {arrayify as $fFq2h$arrayify} from "ethers/lib/utils"; | ||
@@ -33,3 +33,4 @@ | ||
const $11dde14a9583bb83$export$f2ecb312e21d09e0 = (signer, gelatoApiKey)=>{ | ||
const $11dde14a9583bb83$export$f2ecb312e21d09e0 = (signer, gelatoApiKey, gelatoProxyApiUrl)=>{ | ||
if (!gelatoApiKey && !gelatoProxyApiUrl) throw new Error("You must supply either an apiKey or a proxy API url."); | ||
return { | ||
@@ -63,4 +64,26 @@ address: async ()=>{ | ||
try { | ||
const relayResponse = await relay.sponsoredCallERC2771(request, signer.provider, gelatoApiKey); | ||
const { taskId: taskId } = relayResponse; | ||
let relayResponse; | ||
if (gelatoApiKey) relayResponse = await relay.sponsoredCallERC2771(request, signer.provider, gelatoApiKey); | ||
else if (gelatoProxyApiUrl) { | ||
const signatureData = await relay.getSignatureDataERC2771(request, signer.provider, (0, $fFq2h$ERC2771Type).SponsoredCall); | ||
// userNonce must be a number to be parsed into JSON | ||
const signatureDataFormatted = { | ||
...signatureData, | ||
struct: { | ||
...signatureData.struct, | ||
userNonce: Number(signatureData.struct.userNonce) | ||
} | ||
}; | ||
const response = await fetch(gelatoProxyApiUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
signatureData: signatureDataFormatted | ||
}) | ||
}); | ||
relayResponse = await response.json(); | ||
} else throw new Error("You must supply either an apiKey or a proxy API url."); | ||
const { taskId: taskId } = relayResponse; | ||
const txHash = await (0, $ef2b7e154aa38549$export$9813dd697f9dfeee)(taskId); | ||
@@ -67,0 +90,0 @@ return txHash; |
{ | ||
"name": "@reservoir0x/gelato-adapter", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "An adapter used to enhance a ethers wallet adapter with the gelato SDK for use in the @reservoir0x/reservoir-sdk to enable gasless transactions", | ||
@@ -42,3 +42,3 @@ "source": "src/index.ts", | ||
"peerDependencies": { | ||
"@reservoir0x/reservoir-sdk": "1.2.1", | ||
"@reservoir0x/reservoir-sdk": "1.2.2", | ||
"ethers": "^5.6.1", | ||
@@ -45,0 +45,0 @@ "viem": "~1.0.6" |
103
README.md
@@ -1,4 +0,4 @@ | ||
<h3 align="center">Reservoir Ethers Wallet Adapter</h3> | ||
<h1 align="center">Reservoir Gelato Wallet Adapter</h1> | ||
### Installation | ||
## Installation | ||
@@ -15,7 +15,39 @@ ``` | ||
### Usage | ||
## Usage | ||
To use the adapter pass in your ethers signer and gelato api key. In return receive a normalized ReservoirWallet object: | ||
There are two ways to use this adapter: with a Gelato API key or a Gelato API proxy URL. | ||
If you are relaying transactions for a public protocol like Seaport, you might prefer to use the Gelato SDK on the backend to not expose your API keys as these will give anyone the power to relay their transactions. In this case, you should use the Gelato API proxy URL parameter. You will have to build your own backend API proxy, there is a basic example [here](/sdk-demo/app/api/relay/route.ts). | ||
Otherwise, if you are relaying transactions and don't mind your API keys being public, you can pass the Gelato API keys into the adapter directly on the front-end. | ||
To use the adapter pass in your ethers signer and Gelato API key / Gelato API proxy URL. In return receive a normalized ReservoirWallet object: | ||
### Gelato API key | ||
<h4>Sequence Diagram</h4> | ||
```mermaid | ||
sequenceDiagram | ||
actor U as User | ||
box rgba(255, 255, 230, 0.1) API key lives here | ||
participant C as Client | ||
end | ||
participant G as Gelato | ||
U->>C: Click relay | ||
activate C | ||
C->>U: Prompt user to sign data | ||
U->>C: Sign data | ||
deactivate C | ||
C->>G: Request relay with signed data | ||
activate G | ||
G->>C: Return taskId | ||
deactivate G | ||
``` | ||
<h4>Example adapter integration</h4> | ||
```typescript | ||
import { getClient } from '@reservoir0x/reservoir-sdk' | ||
@@ -45,6 +77,67 @@ import { adaptGelatoRelayer } from '@reservoir0x/gelato-adapter' | ||
In the code snippet above we use the wagmi `useSigner` method, which is not required, you can create your ethers signer however you wish. We then adapt the signer to the ReservoirWallet object and pass this into any of the SDK methods. Here we pass it into the buyToken method along with the other required parameters. In order for the purchase to be successful you'll need the following: | ||
### Gelato API proxy URL | ||
You will have to build your own backend API proxy, there is a basic example [here](/sdk-demo/app/api/relay/route.ts). | ||
<h4>Sequence Diagram</h4> | ||
```mermaid | ||
sequenceDiagram | ||
actor U as User | ||
participant C as Client | ||
box rgba(255, 255, 230, 0.1) API key lives here | ||
participant P as Gelato API proxy | ||
end | ||
participant G as Gelato | ||
U->>C: Click relay | ||
activate C | ||
C->>U: Prompt user to sign data | ||
U->>C: Sign data | ||
deactivate C | ||
C->>P: Send signed data | ||
activate P | ||
P->>G: Request relay with signed data | ||
activate G | ||
G->>P: Return taskId | ||
deactivate G | ||
P->>C: Return taskId | ||
deactivate P | ||
``` | ||
<h4>Example adapter integration</h4> | ||
```typescript | ||
import { getClient } from '@reservoir0x/reservoir-sdk' | ||
import { adaptGelatoRelayer } from '@reservoir0x/gelato-adapter' | ||
import { useSigner } from 'wagmi' | ||
... | ||
const { data: signer } = useSigner() | ||
const apiProxyUrl = "/api/relay" | ||
const wallet = adaptGelatoRelayer(signer, undefined, apiProxyUrl) | ||
getClient().actions.buyToken({ | ||
items: [ | ||
{ | ||
collection: '0xd8560c88d1dc85f9ed05b25878e366c49b68bef9', | ||
}, | ||
], | ||
options: { | ||
usePermit: true, | ||
currency: '0x07865c6e87b9f70255377e024ace6630c1eaa37f', | ||
}, | ||
wallet: adaptGelatoRelayer(signer, undefined, '/api/relay'), | ||
onProgress: () => {}, | ||
}) | ||
``` | ||
In the code snippets above we use the wagmi `useSigner` method, which is not required, you can create your ethers signer however you wish. We then adapt the signer to the ReservoirWallet object and pass this into any of the SDK methods. Here we pass it into the buyToken method along with the other required parameters. In order for the purchase to be successful you'll need the following: | ||
- Configure your gelato application to whitelist Reservoir's [PermitProxy](https://github.com/reservoirprotocol/indexer/blob/b82284e67c9995f39d8274a93e73e61bd08fa6a3/packages/contracts/deployments.json#L260). | ||
- Pass usePermit: true to make the API return calldata that routes to the permit proxy | ||
- Make sure the erc20 used for purchase supports permit (USDC, etc) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29367
39.88%198
30.26%142
189.8%