-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PRO-1923 - Flare Network Support (#43)
* added flare network, example for token paymaster * updated package version
- Loading branch information
1 parent
e0fc1bc
commit c287343
Showing
5 changed files
with
151 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { ethers, utils } from 'ethers'; | ||
import { PrimeSdk } from '../src'; | ||
import { printOp } from '../src/sdk/common/OperationUtils'; | ||
import * as dotenv from 'dotenv'; | ||
import { sleep } from '../src/sdk/common'; | ||
import { ERC20_ABI } from '../src/sdk/helpers/abi/ERC20_ABI'; | ||
|
||
dotenv.config(); | ||
|
||
const recipient = '0x80a1874E1046B1cc5deFdf4D3153838B72fF94Ac'; // recipient wallet address | ||
const value = '0.0001'; // transfer value | ||
|
||
const arka_api_key = ''; | ||
const arka_url = ''; | ||
|
||
async function main() { | ||
// initializing sdk... | ||
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { | ||
chainId: Number(process.env.CHAIN_ID), projectKey: '', | ||
}) | ||
|
||
console.log('address: ', primeSdk.state.walletAddress) | ||
|
||
const entryPointAddress = '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789'; | ||
|
||
// get address of EtherspotWallet... | ||
const address: string = await primeSdk.getCounterFactualAddress(); | ||
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`); | ||
|
||
// get balance of the account address | ||
let balance = await primeSdk.getNativeBalance(); | ||
console.log('balances: ', balance); | ||
|
||
const tokenAddress = "0x0FA8781a83E46826621b3BC094Ea2A0212e71B23"; // USDC Token address on Mumbai | ||
|
||
/** | ||
* The fetching of pimlico erc20 paymaster address is only required for the first time for each specified gas token since we need to approve the tokens to spend | ||
* from the paymaster address on behalf of you. | ||
*/ | ||
const returnedValue = await fetch(`${arka_url}/pimlicoAddress`, { | ||
method: 'POST', | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ "params": [entryPointAddress, { token: "USDC" }, Number(process.env.CHAIN_ID), arka_api_key] }) | ||
}) | ||
.then((res) => { | ||
return res.json() | ||
}).catch((err) => { | ||
console.log(err); | ||
}); | ||
const paymasterAddress = returnedValue.message; | ||
|
||
if (utils.isAddress(paymasterAddress)) { | ||
console.log('Value returned: ', paymasterAddress); // getting paymaster address for the selected gas token | ||
|
||
const erc20Contract = new ethers.Contract(tokenAddress, ERC20_ABI) | ||
const encodedData = erc20Contract.interface.encodeFunctionData('approve', [paymasterAddress, ethers.constants.MaxUint256]) // Infinite Approval | ||
await primeSdk.addUserOpsToBatch({ to: tokenAddress, data: encodedData }); | ||
const approveOp = await primeSdk.estimate(); | ||
console.log(`UserOpHash: ${await printOp(approveOp)}`); | ||
const uoHash1 = await primeSdk.send(approveOp); | ||
console.log(`UserOpHash: ${uoHash1}`); | ||
|
||
// get transaction hash... | ||
console.log('Waiting for transaction...'); | ||
let userOpsReceipt1 = null; | ||
const timeout1 = Date.now() + 60000; // 1 minute timeout | ||
while ((userOpsReceipt1 == null) && (Date.now() < timeout1)) { | ||
await sleep(2); | ||
userOpsReceipt1 = await primeSdk.getUserOpReceipt(uoHash1); | ||
} | ||
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt1); | ||
|
||
// clear the transaction batch | ||
await primeSdk.clearUserOpsFromBatch(); | ||
|
||
// add transactions to the batch | ||
const transactionBatch = await primeSdk.addUserOpsToBatch({ to: recipient, value: ethers.utils.parseEther(value) }); | ||
console.log('transactions: ', transactionBatch); | ||
|
||
// get balance of the account address | ||
balance = await primeSdk.getNativeBalance(); | ||
|
||
console.log('balances: ', balance); | ||
|
||
// estimate transactions added to the batch and get the fee data for the UserOp | ||
const op = await primeSdk.estimate({ url: arka_url, api_key: arka_api_key, context: { token: "USDC", mode: 'erc20' } }); | ||
console.log(`Estimate UserOp: ${await printOp(op)}`); | ||
|
||
// sign the UserOp and sending to the bundler... | ||
const uoHash = await primeSdk.send(op); | ||
console.log(`UserOpHash: ${uoHash}`); | ||
|
||
// get transaction hash... | ||
console.log('Waiting for transaction...'); | ||
let userOpsReceipt = null; | ||
const timeout = Date.now() + 60000; // 1 minute timeout | ||
while ((userOpsReceipt == null) && (Date.now() < timeout)) { | ||
await sleep(2); | ||
userOpsReceipt = await primeSdk.getUserOpReceipt(uoHash); | ||
} | ||
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt); | ||
} else { | ||
console.log('Unable to fetch the paymaster address. Error returned: ', returnedValue) | ||
} | ||
} | ||
|
||
main() | ||
.catch(console.error) | ||
.finally(() => process.exit()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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