-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,638 additions
and
452 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
49 changes: 49 additions & 0 deletions
49
src/sdk/account/decorators/buildBalanceInstructions.test.ts
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,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) | ||
}) | ||
}) |
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,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
59
src/sdk/account/decorators/buildBridgeInstructions.test.ts
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,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) | ||
}) | ||
}) |
Oops, something went wrong.