Skip to content

Commit

Permalink
chore: restore helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Jan 14, 2025
1 parent 0c52c86 commit ecef056
Show file tree
Hide file tree
Showing 41 changed files with 1,638 additions and 452 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PRIVATE_KEY=
CHAIN_ID=84532
MAINNET_CHAIN_ID=11155111
MAINNET_CHAIN_ID=10
RPC_URL=
BUNDLER_URL=
BICONOMY_SDK_DEBUG=false
Expand Down
49 changes: 49 additions & 0 deletions src/sdk/account/decorators/buildBalanceInstructions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { Address, Chain, LocalAccount } from "viem"
import { base } from "viem/chains"
import { beforeAll, describe, expect, it } from "vitest"
import { toNetwork } from "../../../test/testSetup"
import type { NetworkConfig } from "../../../test/testUtils"
import { type MeeClient, createMeeClient } from "../../clients/createMeeClient"
import { mcUSDC } from "../../constants/tokens"
import {
type MultichainSmartAccount,
toMultichainNexusAccount
} from "../toMultiChainNexusAccount"
import { buildBalanceInstructions } from "./buildBalanceInstructions"

describe("mee:buildBalanceInstruction", () => {
let network: NetworkConfig
let eoaAccount: LocalAccount
let paymentChain: Chain
let paymentToken: Address
let mcNexus: MultichainSmartAccount
let meeClient: MeeClient

beforeAll(async () => {
network = await toNetwork("MAINNET_FROM_ENV_VARS")

paymentChain = network.chain
paymentToken = network.paymentToken!
eoaAccount = network.account!

mcNexus = await toMultichainNexusAccount({
chains: [base, paymentChain],
signer: eoaAccount
})

meeClient = createMeeClient({ account: mcNexus })
})

it("should adjust the account balance", async () => {
const instructions = await buildBalanceInstructions({
account: mcNexus,
amount: BigInt(1000),
token: mcUSDC,
chain: base
})

expect(instructions.length).toBeGreaterThan(0)
expect(instructions[0]).toHaveProperty("calls")
expect(instructions[0].calls.length).toBeGreaterThan(0)
})
})
52 changes: 52 additions & 0 deletions src/sdk/account/decorators/buildBalanceInstructions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Chain, erc20Abi } from "viem"
import type { Instruction } from "../../clients/decorators/mee/getQuote"
import type { BaseMultichainSmartAccount } from "../toMultiChainNexusAccount"
import type { MultichainContract } from "../utils/getMultichainContract"
import buildBridgeInstructions from "./buildBridgeInstructions"
import { getUnifiedERC20Balance } from "./getUnifiedERC20Balance"

export type BuildBalanceInstructionParams = {
/** Optional smart account to execute the transaction. If not provided, uses the client's default account */
account: BaseMultichainSmartAccount
/** The amount of tokens to require */
amount: bigint
/** The token to require */
token: MultichainContract<typeof erc20Abi>
/** The chain to require the token on */
chain: Chain
}

/**
* Makes sure that the user has enough funds on the selected chain before filling the
* supertransaction. Bridges funds from other chains if needed.
*
* @param client - The Mee client to use
* @param params - The parameters for the balance requirement
* @returns Instructions for any required bridging operations
* @example
* const instructions = await buildBalanceInstruction(client, {
* amount: BigInt(1000),
* token: mcUSDC,
* chain: base
* })
*/

export const buildBalanceInstructions = async (
params: BuildBalanceInstructionParams
): Promise<Instruction[]> => {
const { amount, token, chain, account } = params
const unifiedBalance = await getUnifiedERC20Balance({
mcToken: token,
account
})
const { instructions } = await buildBridgeInstructions({
account,
amount: amount,
toChain: chain,
unifiedBalance
})

return instructions
}

export default buildBalanceInstructions
59 changes: 59 additions & 0 deletions src/sdk/account/decorators/buildBridgeInstructions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Address, Chain, LocalAccount } from "viem"
import { base } from "viem/chains"
import { beforeAll, describe, expect, it } from "vitest"
import { toNetwork } from "../../../test/testSetup"
import type { NetworkConfig } from "../../../test/testUtils"
import { type MeeClient, createMeeClient } from "../../clients/createMeeClient"
import { mcUSDC } from "../../constants/tokens"
import {
type MultichainSmartAccount,
toMultichainNexusAccount
} from "../toMultiChainNexusAccount"
import { AcrossPlugin } from "../utils/acrossPlugin"
import buildBridgeInstructions from "./buildBridgeInstructions"
import { getUnifiedERC20Balance } from "./getUnifiedERC20Balance"

describe("mee:buildBridgeInstructions", () => {
let network: NetworkConfig
let eoaAccount: LocalAccount
let paymentChain: Chain
let paymentToken: Address
let mcNexus: MultichainSmartAccount
let meeClient: MeeClient

beforeAll(async () => {
network = await toNetwork("MAINNET_FROM_ENV_VARS")

paymentChain = network.chain
paymentToken = network.paymentToken!
eoaAccount = network.account!

mcNexus = await toMultichainNexusAccount({
chains: [base, paymentChain],
signer: eoaAccount
})

meeClient = createMeeClient({ account: mcNexus })
})

it("should call the bridge with a unified balance", async () => {
const unifiedBalance = await mcNexus.getUnifiedERC20Balance(mcUSDC)
const payload = await buildBridgeInstructions({
account: mcNexus,
amount: 1n,
bridgingPlugins: [AcrossPlugin],
toChain: base,
unifiedBalance
})

expect(payload).toHaveProperty("meta")
expect(payload).toHaveProperty("instructions")
expect(payload.instructions.length).toBeGreaterThan(0)
expect(payload.meta.bridgingInstructions.length).toBeGreaterThan(0)
expect(payload.meta.bridgingInstructions[0]).toHaveProperty("userOp")
expect(payload.meta.bridgingInstructions[0].userOp).toHaveProperty("calls")
expect(
payload.meta.bridgingInstructions[0].userOp.calls.length
).toBeGreaterThan(0)
})
})
Loading

0 comments on commit ecef056

Please sign in to comment.