|
| 1 | +import { Chains, EvmDynamicCallOperation, OpType, randomEvmAddress, SwapOperation } from '@mimicprotocol/sdk' |
| 2 | +import { EvmCallQueryMock, runFunction, TokenPriceQueryMock } from '@mimicprotocol/test-ts' |
| 3 | +import { expect } from 'chai' |
| 4 | + |
| 5 | +describe('Function', () => { |
| 6 | + const functionDir = './build' |
| 7 | + |
| 8 | + const chainId = Chains.Optimism |
| 9 | + |
| 10 | + const context = { |
| 11 | + user: '0x756f45e3fa69347a9a973a725e3c98bc4db0b5a0', |
| 12 | + settlers: [{ address: '0xdcf1d9d12a0488dfb70a8696f44d6d3bc303963d', chainId }], |
| 13 | + timestamp: Date.now(), |
| 14 | + } |
| 15 | + |
| 16 | + const inputs = { |
| 17 | + tokenIn: randomEvmAddress(), |
| 18 | + tokenOut: randomEvmAddress(), |
| 19 | + chainId, |
| 20 | + amount: '1.5', |
| 21 | + slippageBps: 1, |
| 22 | + smartAccount: randomEvmAddress(), |
| 23 | + recipient: randomEvmAddress(), |
| 24 | + maxFeeUsd: '0.1', |
| 25 | + } |
| 26 | + |
| 27 | + const calls: EvmCallQueryMock[] = [ |
| 28 | + { |
| 29 | + request: { to: inputs.tokenIn, chainId, fnSelector: '0x313ce567' }, // decimals |
| 30 | + response: { value: '6', abiType: 'uint8' }, |
| 31 | + }, |
| 32 | + { |
| 33 | + request: { to: inputs.tokenOut, chainId, fnSelector: '0x313ce567' }, // decimals |
| 34 | + response: { value: '6', abiType: 'uint8' }, |
| 35 | + }, |
| 36 | + ] |
| 37 | + |
| 38 | + const prices: TokenPriceQueryMock[] = [ |
| 39 | + { |
| 40 | + request: { token: { address: inputs.tokenIn, chainId: inputs.chainId } }, |
| 41 | + response: ['1000000000000000000'], // 1 token = 1 USD |
| 42 | + }, |
| 43 | + { |
| 44 | + request: { token: { address: inputs.tokenOut, chainId: inputs.chainId } }, |
| 45 | + response: ['1000000000000000000'], // 1 token = 1 USD |
| 46 | + }, |
| 47 | + ] |
| 48 | + |
| 49 | + it('produces the expected intents', async () => { |
| 50 | + const result = await runFunction(functionDir, context, { inputs, calls, prices }) |
| 51 | + expect(result.success).to.be.true |
| 52 | + expect(result.timestamp).to.be.equal(context.timestamp) |
| 53 | + |
| 54 | + expect(result.intents).to.have.lengthOf(1) |
| 55 | + const intent = result.intents[0] |
| 56 | + |
| 57 | + expect(intent.feePayer).to.be.equal(context.user) |
| 58 | + expect(intent.feePayer).to.be.equal(context.user) |
| 59 | + expect(intent.maxFees).to.have.lengthOf(1) |
| 60 | + expect(intent.maxFees[0].token).to.be.equal('0x0000000000000000000000000000000000000348') |
| 61 | + expect(intent.maxFees[0].amount).to.be.equal('100000000000000000') |
| 62 | + expect(intent.operations).to.have.lengthOf(2) |
| 63 | + expect(intent.settler).to.be.equal(context.settlers[0].address) |
| 64 | + |
| 65 | + const swap = intent.operations[0] as SwapOperation |
| 66 | + const call = intent.operations[1] as EvmDynamicCallOperation |
| 67 | + expect(swap.opType).to.be.equal(OpType.Swap) |
| 68 | + expect(call.opType).to.be.equal(OpType.EvmDynamicCall) |
| 69 | + |
| 70 | + expect(swap.user).to.be.equal(context.user) |
| 71 | + expect(call.user).to.be.equal(inputs.smartAccount) |
| 72 | + |
| 73 | + expect(swap.sourceChain).to.be.equal(inputs.chainId) |
| 74 | + expect(swap.destinationChain).to.be.equal(inputs.chainId) |
| 75 | + expect(call.chainId).to.be.equal(inputs.chainId) |
| 76 | + |
| 77 | + expect(call.calls).to.have.lengthOf(1) |
| 78 | + expect(call.calls[0].target).to.be.equal(inputs.tokenOut) |
| 79 | + expect(call.calls[0].arguments).to.have.lengthOf(2) |
| 80 | + }) |
| 81 | +}) |
0 commit comments