-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add examples for transfer and withdrawal tx which uses paym…
…aster
- Loading branch information
1 parent
7c24ae2
commit ec02d3f
Showing
24 changed files
with
233 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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,44 @@ | ||
import { Provider, types, Wallet, utils } from "zksync-ethers"; | ||
import { ethers } from "ethers"; | ||
|
||
const provider = Provider.getDefaultProvider(types.Network.Sepolia); | ||
const ethProvider = ethers.getDefaultProvider("sepolia"); | ||
const PRIVATE_KEY = process.env.PRIVATE_KEY; | ||
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); | ||
|
||
const tokenAddress = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free | ||
const paymasterAddress = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token | ||
|
||
/* | ||
This example demonstrates how to use a paymaster to facilitate fee payment with an ERC20 token. | ||
The user initiates a transfer transaction that is configured to be paid with an ERC20 token through the paymaster. | ||
During transaction execution, the paymaster receives the ERC20 token from the user and covers the transaction fee using ETH. | ||
*/ | ||
async function main() { | ||
const receiver = "0x81E9D85b65E9CC8618D85A1110e4b1DF63fA30d9"; | ||
|
||
console.log(`Account1 balance before transfer: ${await wallet.getBalance()}`); | ||
console.log(`Account2 balance before transfer: ${await provider.getBalance(receiver)}`); | ||
|
||
const tx = await wallet.transfer({ | ||
to: receiver, | ||
amount: ethers.parseEther("0.01"), | ||
paymasterParamas: utils.getPaymasterParams(paymasterAddress, { | ||
type: "ApprovalBased", | ||
token: tokenAddress, | ||
minimalAllowance: 1, | ||
innerInput: new Uint8Array(), | ||
}), | ||
}); | ||
const receipt = await tx.wait(); | ||
console.log(`Tx: ${receipt.hash}`); | ||
|
||
console.log(`Account1 balance after transfer: ${await wallet.getBalance()}`); | ||
console.log(`Account2 balance after transfer: ${await provider.getBalance(receiver)}`); | ||
} | ||
|
||
main() | ||
.then() | ||
.catch((error) => { | ||
console.log(`Error: ${error}`); | ||
}); |
File renamed without changes.
This file contains 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,45 @@ | ||
import { Provider, types, utils, Wallet } from "zksync-ethers"; | ||
import { ethers } from "ethers"; | ||
|
||
const provider = Provider.getDefaultProvider(types.Network.Sepolia); | ||
const ethProvider = ethers.getDefaultProvider("sepolia"); | ||
const PRIVATE_KEY = process.env.PRIVATE_KEY; | ||
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); | ||
|
||
const tokenAddress = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free | ||
const paymasterAddress = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token | ||
|
||
/* | ||
This example demonstrates how to use a paymaster to facilitate fee payment with an ERC20 token. | ||
The user initiates a withdrawal transaction that is configured to be paid with an ERC20 token through the paymaster. | ||
During transaction execution, the paymaster receives the ERC20 token from the user and covers the transaction fee using ETH. | ||
*/ | ||
async function main() { | ||
console.log(`L2 balance before withdraw: ${await wallet.getBalance()}`); | ||
console.log(`L1 balance before withdraw: ${await wallet.getBalanceL1()}`); | ||
|
||
const tx = await wallet.withdraw({ | ||
token: utils.ETH_ADDRESS, | ||
to: await wallet.getAddress(), | ||
amount: ethers.parseEther("0.00020"), | ||
paymasterParamas: utils.getPaymasterParams(paymasterAddress, { | ||
type: "ApprovalBased", | ||
token: tokenAddress, | ||
minimalAllowance: 1, | ||
innerInput: new Uint8Array(), | ||
}), | ||
}); | ||
const receipt = await tx.wait(); | ||
console.log(`Tx: ${receipt.hash}`); | ||
|
||
// The duration for submitting a withdrawal transaction to L1 can last up to 24 hours. For additional information, | ||
// please refer to the documentation: https://era.zksync.io/docs/reference/troubleshooting/withdrawal-delay.html. | ||
// Once the withdrawal transaction is submitted on L1, it needs to be finalized. | ||
// To learn more about how to achieve this, please take a look at the 04_finalize_withdraw.ts script. | ||
} | ||
|
||
main() | ||
.then() | ||
.catch((error) => { | ||
console.log(`Error: ${error}`); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,50 @@ | ||
import { Provider, types, Wallet, utils } from "zksync-ethers"; | ||
import { ethers } from "ethers"; | ||
|
||
const provider = Provider.getDefaultProvider(types.Network.Sepolia); | ||
const ethProvider = ethers.getDefaultProvider("sepolia"); | ||
const PRIVATE_KEY = process.env.PRIVATE_KEY; | ||
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); | ||
|
||
const tokenAddress = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free | ||
const paymasterAddress = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token | ||
|
||
/* | ||
This example demonstrates how to use a paymaster to facilitate fee payment with an ERC20 token. | ||
The user initiates a token transfer transaction that is configured to be paid with an ERC20 token through the paymaster. | ||
During transaction execution, the paymaster receives the ERC20 token from the user and covers the transaction fee using ETH. | ||
*/ | ||
async function main() { | ||
const token = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1"; | ||
const receiver = "0x81E9D85b65E9CC8618D85A1110e4b1DF63fA30d9"; | ||
|
||
console.log(`Account1 balance before transfer: ${await wallet.getBalance(token)}`); | ||
console.log( | ||
`Account2 balance before transfer: ${await provider.getBalance(receiver, "latest", token)}`, | ||
); | ||
|
||
const tx = await wallet.transfer({ | ||
token: token, | ||
to: receiver, | ||
amount: 5, | ||
paymasterParamas: utils.getPaymasterParams(paymasterAddress, { | ||
type: "ApprovalBased", | ||
token: tokenAddress, | ||
minimalAllowance: 1, | ||
innerInput: new Uint8Array(), | ||
}), | ||
}); | ||
const receipt = await tx.wait(); | ||
console.log(`Tx: ${receipt.hash}`); | ||
|
||
console.log(`Account1 balance after transfer: ${await wallet.getBalance(token)}`); | ||
console.log( | ||
`Account2 balance after transfer: ${await provider.getBalance(receiver, "latest", token)}`, | ||
); | ||
} | ||
|
||
main() | ||
.then() | ||
.catch((error) => { | ||
console.log(`Error: ${error}`); | ||
}); |
File renamed without changes.
This file contains 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,47 @@ | ||
import { Provider, types, Wallet, utils } from "zksync-ethers"; | ||
import { ethers } from "ethers"; | ||
|
||
const provider = Provider.getDefaultProvider(types.Network.Sepolia); | ||
const ethProvider = ethers.getDefaultProvider("sepolia"); | ||
const PRIVATE_KEY = process.env.PRIVATE_KEY; | ||
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); | ||
|
||
const tokenAddress = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free | ||
const paymasterAddress = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token | ||
|
||
/* | ||
This example demonstrates how to use a paymaster to facilitate fee payment with an ERC20 token. | ||
The user initiates a token withdrawal transaction that is configured to be paid with an ERC20 token through the paymaster. | ||
During transaction execution, the paymaster receives the ERC20 token from the user and covers the transaction fee using ETH. | ||
*/ | ||
async function main() { | ||
const token = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1"; | ||
|
||
console.log(`L2 balance before withdrawal: ${await wallet.getBalance()}`); | ||
console.log(`L1 balance before withdrawal: ${await wallet.getBalanceL1()}`); | ||
|
||
const tx = await wallet.withdraw({ | ||
token: token, | ||
to: await wallet.getAddress(), | ||
amount: 5, | ||
paymasterParamas: utils.getPaymasterParams(paymasterAddress, { | ||
type: "ApprovalBased", | ||
token: tokenAddress, | ||
minimalAllowance: 1, | ||
innerInput: new Uint8Array(), | ||
}), | ||
}); | ||
const receipt = await tx.wait(); | ||
console.log(`Tx: ${receipt.hash}`); | ||
|
||
// The duration for submitting a withdrawal transaction to L1 can last up to 24 hours. For additional information, | ||
// please refer to the documentation: https://era.zksync.io/docs/reference/troubleshooting/withdrawal-delay.html. | ||
// Once the withdrawal transaction is submitted on L1, it needs to be finalized. | ||
// To learn more about how to achieve this, please take a look at the 04_finalize_withdraw.ts script. | ||
} | ||
|
||
main() | ||
.then() | ||
.catch((error) => { | ||
console.log(`Error: ${error}`); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 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