|
8 | 8 | randomSig, |
9 | 9 | TriggerType, |
10 | 10 | } from '@mimicprotocol/sdk' |
11 | | -import { Call, Context, ContractCallMock, runTask } from '@mimicprotocol/test-ts' |
| 11 | +import { Call, Context, ContractCallMock, Inputs, runTask } from '@mimicprotocol/test-ts' |
12 | 12 | import { expect } from 'chai' |
13 | 13 | import { AbiCoder, concat } from 'ethers' |
14 | 14 |
|
@@ -65,41 +65,76 @@ describe('Invest', () => { |
65 | 65 | }, |
66 | 66 | ] |
67 | 67 |
|
68 | | - it('produces the expected intents', async () => { |
69 | | - const result = await runTask(taskDir, context, { inputs, calls }) |
70 | | - expect(result.success).to.be.true |
71 | | - expect(result.timestamp).to.be.equal(context.timestamp) |
| 68 | + const itThrowsAnError = (context: Context, inputs: Inputs, error: string): void => { |
| 69 | + it('throws an error', async () => { |
| 70 | + const result = await runTask(taskDir, context, { inputs, calls }) |
| 71 | + expect(result.success).to.be.false |
| 72 | + expect(result.intents).to.have.lengthOf(0) |
72 | 73 |
|
73 | | - const intents = result.intents as Call[] |
74 | | - expect(intents).to.have.lengthOf(1) |
| 74 | + expect(result.logs).to.have.lengthOf(1) |
| 75 | + expect(result.logs[0]).to.include(error) |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + describe('when the chain is supported', () => { |
| 80 | + describe('when the trigger is event', () => { |
| 81 | + describe('when the event user is the smart account', () => { |
| 82 | + it('produces the expected intents', async () => { |
| 83 | + const result = await runTask(taskDir, context, { inputs, calls }) |
| 84 | + expect(result.success).to.be.true |
| 85 | + expect(result.timestamp).to.be.equal(context.timestamp) |
| 86 | + |
| 87 | + const intents = result.intents as Call[] |
| 88 | + expect(intents).to.have.lengthOf(1) |
| 89 | + |
| 90 | + expect(intents[0].op).to.be.equal(OpType.EvmCall) |
| 91 | + expect(intents[0].settler).to.be.equal(context.settlers?.[0].address) |
| 92 | + expect(intents[0].user).to.be.equal(inputs.smartAccount) |
| 93 | + expect(intents[0].chainId).to.be.equal(inputs.chainId) |
| 94 | + |
| 95 | + expect(intents[0].maxFees).to.have.lengthOf(1) |
| 96 | + expect(intents[0].maxFees[0].token).to.be.equal(inputs.feeToken) |
| 97 | + expect(intents[0].maxFees[0].amount).to.be.equal(fp(inputs.maxFee, decimals).toString()) |
75 | 98 |
|
76 | | - expect(intents[0].op).to.be.equal(OpType.EvmCall) |
77 | | - expect(intents[0].settler).to.be.equal(context.settlers?.[0].address) |
78 | | - expect(intents[0].user).to.be.equal(inputs.smartAccount) |
79 | | - expect(intents[0].chainId).to.be.equal(inputs.chainId) |
| 99 | + expect(intents[0].calls).to.have.lengthOf(2) |
80 | 100 |
|
81 | | - expect(intents[0].maxFees).to.have.lengthOf(1) |
82 | | - expect(intents[0].maxFees[0].token).to.be.equal(inputs.feeToken) |
83 | | - expect(intents[0].maxFees[0].amount).to.be.equal(fp(inputs.maxFee, decimals).toString()) |
| 101 | + const firstCall = intents[0].calls[0] |
| 102 | + expect(firstCall.target).to.be.equal(USDC) |
| 103 | + expect(firstCall.value).to.be.equal('0') |
84 | 104 |
|
85 | | - expect(intents[0].calls).to.have.lengthOf(2) |
| 105 | + const approveData = AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [aavePool, amount]) |
| 106 | + expect(firstCall.data).to.be.equal(concat(['0x095ea7b3', approveData])) // approve |
86 | 107 |
|
87 | | - const firstCall = intents[0].calls[0] |
88 | | - expect(firstCall.target).to.be.equal(USDC) |
89 | | - expect(firstCall.value).to.be.equal('0') |
| 108 | + const secondCall = intents[0].calls[1] |
| 109 | + expect(secondCall.target).to.be.equal(aavePool) |
| 110 | + expect(secondCall.value).to.be.equal('0') |
90 | 111 |
|
91 | | - const approveData = AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [aavePool, amount]) |
92 | | - expect(firstCall.data).to.be.equal(concat(['0x095ea7b3', approveData])) // approve |
| 112 | + const supplyData = AbiCoder.defaultAbiCoder().encode( |
| 113 | + ['address', 'uint256', 'address', 'uint16'], |
| 114 | + [USDC, amount, smartAccount, 0] |
| 115 | + ) |
| 116 | + expect(secondCall.data).to.be.equal(concat(['0x617ba037', supplyData])) // supply |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('when the event user is not the smart account', () => { |
| 121 | + const inputsOtherSmartAccount = { ...inputs, smartAccount: randomEvmAddress() } |
| 122 | + |
| 123 | + itThrowsAnError(context, inputsOtherSmartAccount, 'Intent user not smart account') |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('when the trigger is not event', () => { |
| 128 | + const cronContext = { ...context, trigger: { ...trigger, type: TriggerType.Cron } } |
| 129 | + |
| 130 | + itThrowsAnError(cronContext, inputs, 'Trigger not event') |
| 131 | + }) |
| 132 | + }) |
93 | 133 |
|
94 | | - const secondCall = intents[0].calls[1] |
95 | | - expect(secondCall.target).to.be.equal(aavePool) |
96 | | - expect(secondCall.value).to.be.equal('0') |
| 134 | + describe('when the chain is not supported', () => { |
| 135 | + const inputsUnsupportedChain = { ...inputs, chainId: Chains.Mainnet } |
97 | 136 |
|
98 | | - const supplyData = AbiCoder.defaultAbiCoder().encode( |
99 | | - ['address', 'uint256', 'address', 'uint16'], |
100 | | - [USDC, amount, smartAccount, 0] |
101 | | - ) |
102 | | - expect(secondCall.data).to.be.equal(concat(['0x617ba037', supplyData])) // supply |
| 137 | + itThrowsAnError(context, inputsUnsupportedChain, 'Invalid chain') |
103 | 138 | }) |
104 | 139 | }) |
105 | 140 |
|
|
0 commit comments