import { describe, expect, it } from "bun:test" import { encodeFunctionData, erc20Abi, parseAbi, zeroAddress } from "viem" import { arbitrum } from "viem/chains" import { CallType } from "../actions/createMagicAddress" import { FLEX, TOKEN_ADDRESSES } from "../constants" import type { CreateActionFromArgsParameters } from "../types" import { createCall } from "./createCall" describe("createAction", () => { const target = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831" // usdc on arb const owner = "0x9775137314fe595c943712b0b336327dfa80ae8a" it("should create an action", () => { const expectedData = encodeFunctionData({ abi: erc20Abi, functionName: "transfer", args: [owner, 0n] }) const expectedValue = BigInt(1) const action = createCall({ target: target, value: 1n, actionType: CallType.CALL, abi: erc20Abi, functionName: "transfer", args: [owner, 0n] }) expect(action.target).toBe(target) expect(action.value).toBe(expectedValue) expect(action.callType).toBe(CallType.CALL) expect(action.tokenAddressOffset).toBe(0n) expect(action.tokenAmountOffset).toBe(0n) expect(action.data).toBe(expectedData) }) it("should create an action with DELEGATECALL", () => { const action = createCall({ target: target, value: 0n, actionType: CallType.DELEGATE_CALL, abi: erc20Abi, functionName: "transfer", args: [owner, 0n] }) expect(action.callType).toBe(CallType.DELEGATE_CALL) }) it("should create an action without functionName", () => { const action = createCall({ target: target, value: 0n, actionType: CallType.CALL }) console.log("action", action) const expectedData = "0x" expect(action.target).toBe(target) expect(action.value).toBe(BigInt(0)) expect(action.callType).toBe(CallType.CALL) expect(action.tokenAddressOffset).toBe(0n) expect(action.tokenAmountOffset).toBe(0n) expect(action.data).toBe(expectedData) }) it("should handle FLEX.TOKEN_ADDRESS as target", () => { const action = createCall({ target: FLEX.TOKEN_ADDRESS, value: 0n, actionType: CallType.CALL }) expect(action.target).toBe("0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF") }) it("should handle FLEX.NATIVE_AMOUNT as value", () => { const action = createCall({ target: target, value: FLEX.NATIVE_AMOUNT, actionType: CallType.CALL }) expect(action.value).toBe( BigInt( "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfFfffffFfFFFffffFfFfFfffff" ) ) }) it("should handle FLEX.AMOUNT as args", () => { const action = createCall({ target: target, value: 0n, actionType: CallType.CALL, abi: erc20Abi, functionName: "transfer", args: [owner, FLEX.AMOUNT] }) expect(action.tokenAddressOffset).toBe(0n) expect(action.tokenAmountOffset).toBe(36n) }) it("should handle FLEX.TOKEN_ADDRESS as args", () => { const action = createCall({ target: target, value: 0n, actionType: CallType.CALL, abi: erc20Abi, functionName: "transfer", args: [FLEX.TOKEN_ADDRESS, 0n] }) expect(action.tokenAddressOffset).toBe(4n) expect(action.tokenAmountOffset).toBe(0n) }) it("should throw an error if functionName is not found in abi", () => { expect(() => { createCall({ target: zeroAddress, value: 0n, actionType: CallType.CALL, abi: erc20Abi, // @ts-ignore functionName: "nonExistentFunction", args: [] }) }).toThrow("nonExistentFunction not found in abi") }) })