Skip to content

Commit

Permalink
ci: reinclude withdrawal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Apr 11, 2024
1 parent 31b83fa commit 314143e
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 4 deletions.
51 changes: 48 additions & 3 deletions tests/account/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
DEFAULT_ENTRYPOINT_ADDRESS,
ERROR_MESSAGES,
compareChainIds,
createSmartAccountClient
createSmartAccountClient,
NATIVE_TOKEN_ALIAS
} from "../../src/account"
import { type UserOperationStruct, getChain } from "../../src/account"
import { BiconomyAccountAbi } from "../../src/account/abi/SmartAccount"
Expand Down Expand Up @@ -763,11 +764,55 @@ describe("Account: Read", () => {
await smartAccount.getAddress(),
usdt
)
const [usdtBalanceFromSmartAccount] = await smartAccount.getBalances([usdt])
const [usdtBalanceFromSmartAccount, ethBalanceFromSmartAccount] =
await smartAccount.getBalances([usdt])

expect(usdtBalanceFromSmartAccount.amount).toBeGreaterThan(0n)
expect(ethBalanceFromSmartAccount.amount).toBeGreaterThan(0n)
expect(usdtBalanceFromSmartAccount.address).toBe(usdt)
expect(ethBalanceFromSmartAccount.address).toBe(NATIVE_TOKEN_ALIAS)
expect(usdtBalanceFromSmartAccount.chainId).toBe(chainId)
expect(ethBalanceFromSmartAccount.chainId).toBe(chainId)
expect(usdtBalanceFromSmartAccount.decimals).toBe(6)
expect(ethBalanceFromSmartAccount.decimals).toBe(18)
expect(usdtBalanceFromSmartAccount.formattedAmount).toBeTruthy()
expect(ethBalanceFromSmartAccount.formattedAmount).toBeTruthy()
})

test("should error if no recipient exists", async () => {
const usdt: Hex = "0xda5289fcaaf71d52a80a254da614a192b693e977"
const smartAccountOwner = walletClient.account.address

const txs = [
{ address: usdt, amount: BigInt(1), recipient: smartAccountOwner },
{ address: NATIVE_TOKEN_ALIAS, amount: BigInt(1) }
]

expect(usdcBalanceBefore).toBe(usdtBalanceFromSmartAccount.amount)
expect(async () => smartAccount.withdraw(txs)).rejects.toThrow(
ERROR_MESSAGES.NO_RECIPIENT
)
})

test("should error when withdraw all of native token is attempted without an amount explicitly set", async () => {
const smartAccountOwner = walletClient.account.address
expect(async () =>
smartAccount.withdraw(null, smartAccountOwner)
).rejects.toThrow(ERROR_MESSAGES.NATIVE_TOKEN_WITHDRAWAL_WITHOUT_AMOUNT)
}, 6000)

test.concurrent(
"should check native token balance for smartAccount",
async () => {
const [ethBalanceFromSmartAccount] = await smartAccount.getBalances()

expect(ethBalanceFromSmartAccount.amount).toBeGreaterThan(0n)
expect(ethBalanceFromSmartAccount.address).toBe(NATIVE_TOKEN_ALIAS)
expect(ethBalanceFromSmartAccount.chainId).toBe(chainId)
expect(ethBalanceFromSmartAccount.decimals).toBe(18)
},
60000
)

test.concurrent(
"should verify a correct signature through isValidSignature",
async () => {
Expand Down
222 changes: 221 additions & 1 deletion tests/account/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { beforeAll, describe, expect, test } from "vitest"
import {
type BiconomySmartAccountV2,
createSmartAccountClient
createSmartAccountClient,
NATIVE_TOKEN_ALIAS
} from "../../src/account"
import { PaymasterMode } from "../../src/paymaster"
import { testOnlyOnOptimism } from "../setupFiles"
Expand Down Expand Up @@ -298,6 +299,225 @@ describe("Account: Write", () => {
50000
)

test("should withdraw erc20 balances", async () => {
const usdt = "0xda5289fcaaf71d52a80a254da614a192b693e977"

const smartAccountOwner = walletClient.account.address
const smartAccountAddress = await smartAccount.getAddress()
const usdtBalanceOfSABefore = await checkBalance(
publicClient,
smartAccountAddress,
usdt
)
const usdtBalanceOfRecipientBefore = await checkBalance(
publicClient,
smartAccountOwner,
usdt
)

const { wait } = await smartAccount.withdraw([
{ address: usdt, amount: BigInt(1), recipient: smartAccountOwner }
])

const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()

expect(userOpHash).toBeTruthy()
expect(success).toBe("true")

Check failure on line 329 in tests/account/write.test.ts

View workflow job for this annotation

GitHub Actions / coverage

tests/account/write.test.ts > Account: Write > should withdraw erc20 balances

AssertionError: expected 'false' to be 'true' // Object.is equality - Expected + Received - true + false ❯ tests/account/write.test.ts:329:21
expect(transactionHash).toBeTruthy()

const usdtBalanceOfSAAfter = (await checkBalance(
publicClient,
smartAccountAddress,
usdt
)) as bigint
const usdtBalanceOfRecipientAfter = (await checkBalance(
publicClient,
smartAccountOwner,
usdt
)) as bigint

expect(usdtBalanceOfSAAfter - usdtBalanceOfSABefore).toBe(-1n)
expect(usdtBalanceOfRecipientAfter - usdtBalanceOfRecipientBefore).toBe(1n)
}, 15000)

test("should gaslessly withdraw nativeToken", async () => {
const smartAccountOwner = walletClient.account.address

const smartAccountAddress = await smartAccount.getAddress()
const balanceOfSABefore = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientBefore = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint

const { wait } = await smartAccount.withdraw(
[
{
address: NATIVE_TOKEN_ALIAS,
amount: BigInt(1),
recipient: smartAccountAddress
}
],
null,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}
)

const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()

expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
expect(transactionHash).toBeTruthy()

const balanceOfSAAfter = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientAfter = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint

expect(balanceOfSABefore - balanceOfSAAfter).toBe(1n)

Check failure on line 393 in tests/account/write.test.ts

View workflow job for this annotation

GitHub Actions / coverage

tests/account/write.test.ts > Account: Write > should gaslessly withdraw nativeToken

AssertionError: expected 0n to be 1n // Object.is equality - Expected + Received - 1n + 0n ❯ tests/account/write.test.ts:393:50
expect(balanceOfRecipientAfter - balanceOfRecipientBefore).toBe(1n)
}, 12000)

test("should withdraw nativeToken and an erc20 token", async () => {
const usdt = "0xda5289fcaaf71d52a80a254da614a192b693e977"
const smartAccountOwner = walletClient.account.address

const smartAccountAddress = await smartAccount.getAddress()
const balanceOfSABefore = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientBefore = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint
const usdtBalanceOfSABefore = await checkBalance(
publicClient,
smartAccountAddress,
usdt
)
const usdtBalanceOfRecipientBefore = await checkBalance(
publicClient,
smartAccountOwner,
usdt
)

const { wait } = await smartAccount.withdraw(
[
{ address: usdt, amount: BigInt(1) },
{ address: NATIVE_TOKEN_ALIAS, amount: BigInt(1) }
],
smartAccountOwner,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}
)

const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()

expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
expect(transactionHash).toBeTruthy()

const balanceOfSAAfter = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientAfter = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint
const usdtBalanceOfSAAfter = (await checkBalance(
publicClient,
smartAccountAddress,
usdt
)) as bigint
const usdtBalanceOfRecipientAfter = (await checkBalance(
publicClient,
smartAccountOwner,
usdt
)) as bigint

expect(balanceOfSABefore - balanceOfSAAfter).toBe(1n)
expect(balanceOfRecipientAfter - balanceOfRecipientBefore).toBe(1n)
expect(usdtBalanceOfSAAfter - usdtBalanceOfSABefore).toBe(-1n)
expect(usdtBalanceOfRecipientAfter - usdtBalanceOfRecipientBefore).toBe(1n)
}, 60000)

test("should withdraw all native token", async () => {
const smartAccountOwner = walletClient.account.address
const smartAccountAddress = await smartAccount.getAddress()
const balanceOfSABefore = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientBefore = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint

const { wait } = await smartAccount.withdraw(
[] /* null or undefined or [] */,
smartAccountOwner,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED } // Will leave no dust
}
)

const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()

expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
expect(transactionHash).toBeTruthy()

const balanceOfSAAfter = (await checkBalance(
publicClient,
smartAccountAddress
)) as bigint
const balanceOfRecipientAfter = (await checkBalance(
publicClient,
smartAccountOwner
)) as bigint

expect(balanceOfSAAfter).toBe(0n)
expect(balanceOfRecipientAfter).toBe(
balanceOfSABefore + balanceOfRecipientBefore
)

// Teardown: send back the native token to the smart account
const teardownHash = await signer.sendTransaction({

Check failure on line 512 in tests/account/write.test.ts

View workflow job for this annotation

GitHub Actions / coverage

tests/account/write.test.ts > Account: Write > should withdraw all native token

ReferenceError: signer is not defined ❯ tests/account/write.test.ts:512:26
to: smartAccountAddress,
value: balanceOfSABefore,
account,
chain: viemChain
})
expect(teardownHash).toBeTruthy()
}, 60000)

test.skip("should mint an NFT on Mumbai and pay with ERC20 - with preferredToken", async () => {
// const {
// whale: { viemWallet: signer, publicAddress: recipient },
Expand Down

0 comments on commit 314143e

Please sign in to comment.