-
Notifications
You must be signed in to change notification settings - Fork 2
SDK: Permit2 interactions (stacked on #23) #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "radius-sdk": minor | ||
| --- | ||
|
|
||
| Add Permit2 interactions covering both SignatureTransfer (with optional witness) and AllowanceTransfer: `approvePermit2`, `signPermit2Transfer`, `permit2TransferFrom`, `signPermit2Allowance`, `permit2Permit`, `permit2AllowanceTransferFrom`, the `permit2Actions()` client extension and the EIP-712 helpers, exported from `radius-sdk/client`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Permit2 SignatureTransfer end to end, outside of x402: a payer signs a one-off permit | ||
| // off-chain and a collector pulls the SBC with it. This is the primitive x402 `exact` | ||
| // (Permit2) payments are built on; here the collector is a plain account, not the x402 proxy. | ||
| // | ||
| // Usage: RADIUS_PRIVATE_KEY=<payer key> COLLECTOR_PRIVATE_KEY=<collector key> node permit2-pull.mjs [amount] | ||
| // The payer needs SBC plus a one-time Permit2 approval (sent here if missing; gas comes from | ||
| // SBC via Turnstile). The collector pays gas for the pull, so it needs a little SBC too. | ||
| import { createWalletClient, http } from 'viem'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
| import { erc20Actions, formatTokenAmount, permit2Actions, radiusActions } from 'radius-sdk/client'; | ||
| import { SBC, radiusTestnet } from 'radius-sdk'; | ||
|
|
||
| const amount = process.argv[2] ?? '0.001'; | ||
| const payer = createWalletClient({ account: privateKeyToAccount(process.env.RADIUS_PRIVATE_KEY), chain: radiusTestnet.chain, transport: http() }) | ||
| .extend(radiusActions()).extend(erc20Actions()).extend(permit2Actions()); | ||
| const collector = createWalletClient({ account: privateKeyToAccount(process.env.COLLECTOR_PRIVATE_KEY), chain: radiusTestnet.chain, transport: http() }) | ||
| .extend(radiusActions()).extend(permit2Actions()); | ||
|
|
||
| // 1. One-time: the payer lets Permit2 move its SBC (unlimited, the x402 "one-time gas approval" model). | ||
| if ((await payer.getPermit2Approval({ owner: payer.account.address })) < 10n ** 12n) { | ||
| const tx = await payer.approvePermit2(); | ||
| console.error(`payer approved Permit2 in ${tx.hash} (${tx.status})`); | ||
| } | ||
|
|
||
| // 2. Off-chain: the payer signs a permit for the collector. Nothing is sent; `signed` is plain JSON-able data. | ||
| const signed = await payer.signPermit2Transfer({ amount, spender: collector.account.address }); | ||
| console.error(`payer ${payer.account.address} signed a permit for ${formatTokenAmount(signed.permit.permitted.amount, SBC)} to spender ${signed.spender}, nonce ${signed.permit.nonce}, deadline ${signed.permit.deadline}`); | ||
| console.error(`nonce used before pull: ${await collector.isPermit2NonceUsed({ owner: signed.owner, nonce: signed.permit.nonce })}`); | ||
|
|
||
| // 3. On-chain: the collector pulls the SBC to itself. It could also pull less than the permitted amount. | ||
| const before = (await collector.getTokenBalance({ address: collector.account.address, token: SBC })).atomic; | ||
| const pull = await collector.permit2TransferFrom({ signed, to: collector.account.address }); | ||
| const after = (await collector.getTokenBalance({ address: collector.account.address, token: SBC })).atomic; | ||
| console.error(`collector pulled ${formatTokenAmount(after - before, SBC)} in ${pull.hash} (${pull.status}) ${pull.explorerUrl ?? ''}`); | ||
| console.error(`nonce used after pull: ${await collector.isPermit2NonceUsed({ owner: signed.owner, nonce: signed.permit.nonce })}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EIP-3009 will come next, yes?