-
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
16 changed files
with
355 additions
and
171 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
src/sdk/account/decorators/buildBalanceInstructions.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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,115 @@ | ||
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 { | ||
Instruction, | ||
SupertransactionLike | ||
} from "../../clients/decorators/mee/getQuote" | ||
import { mcUSDC } from "../../constants/tokens" | ||
import { | ||
type MultichainSmartAccount, | ||
toMultichainNexusAccount | ||
} from "../toMultiChainNexusAccount" | ||
import { buildInstructions } from "./buildInstructions" | ||
|
||
describe("mee:buildInstructions", () => { | ||
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 use the default action while building instructions", async () => { | ||
const instructions = await buildInstructions({ | ||
account: mcNexus, | ||
action: { | ||
type: "DEFAULT", | ||
parameters: [ | ||
{ | ||
calls: [ | ||
{ | ||
to: "0x0000000000000000000000000000000000000000", | ||
gasLimit: 50000n, | ||
value: 0n | ||
} | ||
], | ||
chainId: 8453 | ||
} | ||
] | ||
} | ||
}) | ||
|
||
expect(instructions).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"calls": [ | ||
{ | ||
"gasLimit": 50000n, | ||
"to": "0x0000000000000000000000000000000000000000", | ||
"value": 0n, | ||
}, | ||
], | ||
"chainId": 8453, | ||
}, | ||
] | ||
`) | ||
expect(instructions.length).toBeGreaterThan(0) | ||
}) | ||
|
||
it("should use the bridge action while building instructions", async () => { | ||
const initialInstructions = await buildInstructions({ | ||
account: mcNexus, | ||
action: { | ||
type: "BRIDGE", | ||
parameters: { | ||
amount: BigInt(1000), | ||
mcToken: mcUSDC, | ||
chain: base | ||
} | ||
} | ||
}) | ||
|
||
const instructions = await buildInstructions({ | ||
currentInstructions: initialInstructions, | ||
account: mcNexus, | ||
action: { | ||
type: "DEFAULT", | ||
parameters: [ | ||
{ | ||
calls: [ | ||
{ | ||
to: "0x0000000000000000000000000000000000000000", | ||
gasLimit: 50000n, | ||
value: 0n | ||
} | ||
], | ||
chainId: 8453 | ||
} | ||
] | ||
} | ||
}) | ||
|
||
expect(instructions.length).toBe(2) | ||
expect(instructions[0].calls.length).toBe(2) // Bridge instructions generates two calls | ||
expect(instructions[1].calls.length).toBe(1) // Default instruction in this case generates one call | ||
}) | ||
}) |
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,78 @@ | ||
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 BridgeInstructionsForBridgeAction = { | ||
/** The amount of tokens to require */ | ||
amount: bigint | ||
/** The token to require */ | ||
mcToken: MultichainContract<typeof erc20Abi> | ||
/** The chain to require the token on */ | ||
chain: Chain | ||
} | ||
|
||
/** | ||
* Parameters for querying bridge operations | ||
*/ | ||
export type BuildInstructionsParams = { | ||
/** The multichain smart account to check balances for */ | ||
account: BaseMultichainSmartAccount | ||
/** The chain to build instructions for */ | ||
action: DefaultBuildAction | BridgeBuildAction | ||
/** The current instructions */ | ||
currentInstructions?: Instruction[] | ||
} | ||
|
||
type DefaultBuildAction = { | ||
type: "DEFAULT" | ||
parameters: Instruction[] | Instruction | ||
} | ||
|
||
type BridgeBuildAction = { | ||
type: "BRIDGE" | ||
parameters: BridgeInstructionsForBridgeAction | ||
} | ||
|
||
/** | ||
* 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 buildInstructions(client, { | ||
* amount: BigInt(1000), | ||
* mcToken: mcUSDC, | ||
* chain: base | ||
* }) | ||
*/ | ||
|
||
export const buildInstructions = async ( | ||
params: BuildInstructionsParams | ||
): Promise<Instruction[]> => { | ||
const { account, action, currentInstructions = [] } = params | ||
|
||
switch (action.type) { | ||
case "BRIDGE": { | ||
const { amount, mcToken, chain } = action.parameters | ||
const unifiedBalance = await getUnifiedERC20Balance({ mcToken, account }) | ||
const { instructions } = await buildBridgeInstructions({ | ||
account, | ||
amount: amount, | ||
toChain: chain, | ||
unifiedBalance | ||
}) | ||
return [...currentInstructions, ...instructions] | ||
} | ||
default: | ||
return Array.isArray(action.parameters) | ||
? [...currentInstructions, ...action.parameters] | ||
: [...currentInstructions, action.parameters] | ||
} | ||
} | ||
|
||
export default buildInstructions |
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
Oops, something went wrong.