Skip to content

Commit fdacc98

Browse files
shahafndrortirosh
andauthored
AA-281 L-01 Adding withdrawEth function to TokenPaymaster (eth-infinitism#420)
* Adding withdrawEth function to TokenPaymaster --------- Co-authored-by: Dror Tirosh <dror.tirosh@gmail.com>
1 parent fbb8fbd commit fdacc98

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

contracts/samples/TokenPaymaster.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,8 @@ contract TokenPaymaster is BasePaymaster, UniswapHelper, OracleHelper {
213213
receive() external payable {
214214
emit Received(msg.sender, msg.value);
215215
}
216+
217+
function withdrawEth(address payable recipient, uint256 amount) external onlyOwner {
218+
recipient.call{value: amount}("");
219+
}
216220
}

reports/gas-checker.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
4747
║ token paymaster │ 1 │ 128491 │ │ ║
4848
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
49-
║ token paymaster with diff │ 2 │ │ 6613337154
49+
║ token paymaster with diff │ 2 │ │ 6614537166
5050
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
51-
║ token paymaster │ 10 │ 723923 │ │ ║
51+
║ token paymaster │ 10 │ 723875 │ │ ║
5252
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
53-
║ token paymaster with diff │ 11 │ │ 6619237213
53+
║ token paymaster with diff │ 11 │ │ 6622837249
5454
╚════════════════════════════════╧═══════╧═══════════════╧════════════════╧═════════════════════╝
5555

test/samples/TokenPaymaster.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('TokenPaymaster', function () {
6767
let factory: SimpleAccountFactory
6868
let paymasterAddress: string
6969
let paymaster: TokenPaymaster
70+
let paymasterOwner: string
7071
let callData: string
7172
let token: TestERC20
7273
let weth: TestWrappedNativeToken
@@ -88,7 +89,7 @@ describe('TokenPaymaster', function () {
8889
tokenOracle = await new TestOracle2__factory(ethersSigner).deploy(initialPriceToken, 8)
8990
await weth.deposit({ value: parseEther('1') })
9091
await weth.transfer(testUniswap.address, parseEther('1'))
91-
const owner = await ethersSigner.getAddress()
92+
paymasterOwner = await ethersSigner.getAddress()
9293
const tokenPaymasterConfig: TokenPaymaster.TokenPaymasterConfigStruct = {
9394
priceMaxAge: 86400,
9495
refundPostopCost: 40000,
@@ -120,7 +121,7 @@ describe('TokenPaymaster', function () {
120121
tokenPaymasterConfig,
121122
oracleHelperConfig,
122123
uniswapHelperConfig,
123-
owner
124+
paymasterOwner
124125
)
125126
paymasterAddress = paymaster.address
126127

@@ -132,6 +133,25 @@ describe('TokenPaymaster', function () {
132133
callData = await account.populateTransaction.execute(accountOwner.address, 0, '0x').then(tx => tx.data!)
133134
})
134135

136+
it('Only owner should withdraw eth from paymaster to destination', async function () {
137+
const recipient = accountOwner.address
138+
const amount = 2e18.toString()
139+
const balanceBefore = await ethers.provider.getBalance(paymasterAddress)
140+
await fund(paymasterAddress, '2')
141+
const balanceAfter = await ethers.provider.getBalance(paymasterAddress)
142+
assert.equal(balanceBefore.add(BigNumber.from(amount)).toString(), balanceAfter.toString())
143+
144+
const impersonatedSigner = await ethers.getImpersonatedSigner('0x1234567890123456789012345678901234567890')
145+
const paymasterDifferentSigner = TokenPaymaster__factory.connect(paymasterAddress, impersonatedSigner)
146+
147+
await expect(paymasterDifferentSigner.withdrawEth(paymasterOwner, amount)).to.be.revertedWith('OwnableUnauthorizedAccount')
148+
149+
const recipientBalanceBefore = await ethers.provider.getBalance(recipient)
150+
await paymaster.withdrawEth(recipient, balanceAfter)
151+
const recipientBalanceAfter = await ethers.provider.getBalance(recipient)
152+
assert.equal(recipientBalanceBefore.add(BigNumber.from(amount)).toString(), recipientBalanceAfter.toString())
153+
})
154+
135155
it('paymaster should reject if account does not have enough tokens or allowance', async () => {
136156
const snapshot = await ethers.provider.send('evm_snapshot', [])
137157
let op = await fillUserOp({

0 commit comments

Comments
 (0)