|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { AbiFunction, Address, Bytes, Hex, Provider } from 'ox' |
| 3 | + |
| 4 | +import { Constants, Config, Context, Payload } from '../../primitives/src/index.js' |
| 5 | +import { State, Wallet } from '../src/index.js' |
| 6 | + |
| 7 | +const SIGNER = '0x1234567890123456789012345678901234567890' as Address.Address |
| 8 | +const TARGET = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd' as Address.Address |
| 9 | + |
| 10 | +const configuration: Config.Config = { |
| 11 | + threshold: 1n, |
| 12 | + checkpoint: 0n, |
| 13 | + topology: { type: 'signer', address: SIGNER, weight: 1n }, |
| 14 | +} |
| 15 | + |
| 16 | +const call: Payload.Call = { |
| 17 | + to: TARGET, |
| 18 | + value: 0n, |
| 19 | + data: '0x', |
| 20 | + gasLimit: 0n, |
| 21 | + delegateCall: false, |
| 22 | + onlyFallback: false, |
| 23 | + behaviorOnError: 'revert', |
| 24 | +} |
| 25 | + |
| 26 | +const payload: Payload.Calls = { |
| 27 | + type: 'call', |
| 28 | + space: 0n, |
| 29 | + nonce: 0n, |
| 30 | + calls: [call], |
| 31 | +} |
| 32 | + |
| 33 | +function providerFor(options: { deployed: boolean; imageHash: Hex.Hex }): Provider.Provider { |
| 34 | + return { |
| 35 | + request: vi.fn(async (request: { method: string; params?: unknown[] }) => { |
| 36 | + switch (request.method) { |
| 37 | + case 'eth_chainId': |
| 38 | + return '0x1' |
| 39 | + |
| 40 | + case 'eth_getCode': |
| 41 | + return options.deployed ? '0x1234' : '0x' |
| 42 | + |
| 43 | + case 'eth_call': { |
| 44 | + const rpcCall = request.params?.[0] as { data?: Hex.Hex } | undefined |
| 45 | + |
| 46 | + if (rpcCall?.data === AbiFunction.encodeData(Constants.GET_IMPLEMENTATION)) { |
| 47 | + return options.deployed ? Hex.padLeft(Context.Dev2.stage2, 32) : '0x' |
| 48 | + } |
| 49 | + |
| 50 | + if (rpcCall?.data === AbiFunction.encodeData(Constants.IMAGE_HASH)) { |
| 51 | + return options.imageHash |
| 52 | + } |
| 53 | + |
| 54 | + return '0x' |
| 55 | + } |
| 56 | + |
| 57 | + default: |
| 58 | + throw new Error(`Unexpected RPC method: ${request.method}`) |
| 59 | + } |
| 60 | + }), |
| 61 | + } as unknown as Provider.Provider |
| 62 | +} |
| 63 | + |
| 64 | +async function createWallet() { |
| 65 | + const stateProvider = new State.Local.Provider() |
| 66 | + const wallet = await Wallet.fromConfiguration(configuration, { stateProvider, context: Context.Dev2 }) |
| 67 | + const imageHash = Hex.from(Config.hashConfiguration(configuration)) |
| 68 | + |
| 69 | + return { wallet, imageHash } |
| 70 | +} |
| 71 | + |
| 72 | +describe('Wallet.buildFeeOptionsTransaction', () => { |
| 73 | + it('targets the wallet execute method when the wallet is deployed', async () => { |
| 74 | + const { wallet, imageHash } = await createWallet() |
| 75 | + const transaction = await wallet.buildFeeOptionsTransaction(providerFor({ deployed: true, imageHash }), payload) |
| 76 | + |
| 77 | + const expectedData = AbiFunction.encodeData(Constants.EXECUTE, [Bytes.toHex(Payload.encode(payload)), '0x0001']) |
| 78 | + |
| 79 | + expect(Address.isEqual(transaction.to, wallet.address)).toBe(true) |
| 80 | + expect(transaction.data).toBe(expectedData) |
| 81 | + }) |
| 82 | + |
| 83 | + it('targets the guest module and prefixes deployment when the wallet is undeployed', async () => { |
| 84 | + const { wallet, imageHash } = await createWallet() |
| 85 | + const deploy = await wallet.buildDeployTransaction() |
| 86 | + const transaction = await wallet.buildFeeOptionsTransaction(providerFor({ deployed: false, imageHash }), payload) |
| 87 | + const decoded = Payload.decode(Bytes.fromHex(transaction.data)) |
| 88 | + |
| 89 | + const expectedExecuteData = AbiFunction.encodeData(Constants.EXECUTE, [ |
| 90 | + Bytes.toHex(Payload.encode(payload)), |
| 91 | + '0x0001', |
| 92 | + ]) |
| 93 | + |
| 94 | + expect(Address.isEqual(transaction.to, Constants.DefaultGuestAddress)).toBe(true) |
| 95 | + expect(decoded.calls).toHaveLength(2) |
| 96 | + expect(Address.isEqual(decoded.calls[0]!.to, deploy.to)).toBe(true) |
| 97 | + expect(decoded.calls[0]!.data).toBe(deploy.data) |
| 98 | + expect(Address.isEqual(decoded.calls[1]!.to, wallet.address)).toBe(true) |
| 99 | + expect(decoded.calls[1]!.data).toBe(expectedExecuteData) |
| 100 | + }) |
| 101 | +}) |
0 commit comments