diff --git a/examples/02-simple-transfer-with-inputs/abis/ERC20.json b/examples/02-simple-transfer-with-inputs/abis/ERC20.json new file mode 100644 index 0000000..405d6b3 --- /dev/null +++ b/examples/02-simple-transfer-with-inputs/abis/ERC20.json @@ -0,0 +1,222 @@ +[ + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_from", + "type": "address" + }, + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [ + { + "name": "", + "type": "uint8" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "balance", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + }, + { + "name": "_spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "fallback" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + } +] diff --git a/examples/02-simple-transfer-with-inputs/tests/task.spec.ts b/examples/02-simple-transfer-with-inputs/tests/task.spec.ts index 6fc1ea7..41bfd26 100644 --- a/examples/02-simple-transfer-with-inputs/tests/task.spec.ts +++ b/examples/02-simple-transfer-with-inputs/tests/task.spec.ts @@ -1,6 +1,11 @@ import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -21,7 +26,11 @@ describe('Task', () => { const calls: ContractCallMock[] = [ { - request: { to: inputs.token, chainId: inputs.chainId, fnSelector: '0x313ce567' }, // decimals + request: { + to: inputs.token, + chainId: inputs.chainId, + fnSelector: ERC20Interface.getFunction('decimals')!.selector, + }, response: { value: '6', abiType: 'uint8' }, }, ] diff --git a/examples/03-transfer-balance-threshold/tests/task.spec.ts b/examples/03-transfer-balance-threshold/tests/task.spec.ts index 0d23f5d..e92510d 100644 --- a/examples/03-transfer-balance-threshold/tests/task.spec.ts +++ b/examples/03-transfer-balance-threshold/tests/task.spec.ts @@ -1,6 +1,11 @@ import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -25,29 +30,18 @@ describe('Task', () => { request: { to: inputs.token, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf` - params: [ - { - value: inputs.recipient, - abiType: 'address', - }, - ], - }, - response: { - value: balance, - abiType: 'uint256', + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, + params: [{ value: inputs.recipient, abiType: 'address' }], }, + response: { value: balance, abiType: 'uint256' }, }, { request: { to: inputs.token, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, ] diff --git a/examples/04-transfer-balance-threshold-with-oracles/tests/task.spec.ts b/examples/04-transfer-balance-threshold-with-oracles/tests/task.spec.ts index 81095b5..d83dcf8 100644 --- a/examples/04-transfer-balance-threshold-with-oracles/tests/task.spec.ts +++ b/examples/04-transfer-balance-threshold-with-oracles/tests/task.spec.ts @@ -1,6 +1,11 @@ import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, GetPriceMock, runTask, Transfer } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -22,10 +27,7 @@ describe('Task', () => { const prices: GetPriceMock[] = [ { - request: { - token: inputs.token, - chainId: inputs.chainId, - }, + request: { token: inputs.token, chainId: inputs.chainId }, response: ['1000000000000000000'], // 1 token = 1 USD }, ] @@ -35,29 +37,18 @@ describe('Task', () => { request: { to: inputs.token, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf`, - params: [ - { - value: inputs.recipient, - abiType: 'address', - }, - ], - }, - response: { - value: balance, - abiType: 'uint256', + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, + params: [{ value: inputs.recipient, abiType: 'address' }], }, + response: { value: balance, abiType: 'uint256' }, }, { request: { to: inputs.token, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, ] diff --git a/examples/05-invest-aave-idle-balance/tests/task.spec.ts b/examples/05-invest-aave-idle-balance/tests/task.spec.ts index 67c6de3..c2b3b85 100644 --- a/examples/05-invest-aave-idle-balance/tests/task.spec.ts +++ b/examples/05-invest-aave-idle-balance/tests/task.spec.ts @@ -4,10 +4,12 @@ import { expect } from 'chai' import { Interface } from 'ethers' import AavePool from '../abis/AavePool.json' +import AaveToken from '../abis/AaveToken.json' import ERC20Abi from '../abis/ERC20.json' -const ERC20Interface = new Interface(ERC20Abi) const AavePoolInterface = new Interface(AavePool) +const AaveTokenInterface = new Interface(AaveToken) +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -31,17 +33,11 @@ describe('Task', () => { const prices: GetPriceMock[] = [ { - request: { - token: inputs.aToken, - chainId: inputs.chainId, - }, + request: { token: inputs.aToken, chainId: inputs.chainId }, response: ['1000000000000000000'], // 1 aOptUSDC = 1 USD }, { - request: { - token: underlyingToken, - chainId: inputs.chainId, - }, + request: { token: underlyingToken, chainId: inputs.chainId }, response: ['1000000000000000000'], // 1 USDC = 1 USD }, ] @@ -52,85 +48,59 @@ describe('Task', () => { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0xb16a19de', // `UNDERLYING_ASSET_ADDRESS` - }, - response: { - value: underlyingToken, - abiType: 'address', + fnSelector: AaveTokenInterface.getFunction('UNDERLYING_ASSET_ADDRESS')!.selector, }, + response: { value: underlyingToken, abiType: 'address' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x7535d246', // `POOL` - }, - response: { - value: aavePool, - abiType: 'address', + fnSelector: AaveTokenInterface.getFunction('POOL')!.selector, }, + response: { value: aavePool, abiType: 'address' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x95d89b41', // `symbol` - }, - response: { - value: 'aOptUSDC', - abiType: 'string', + fnSelector: ERC20Interface.getFunction('symbol')!.selector, }, + response: { value: 'aOptUSDC', abiType: 'string' }, }, // USDC { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf` - params: [ - { - value: inputs.smartAccount, - abiType: 'address', - }, - ], - }, - response: { - value: balance, - abiType: 'uint256', + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, + params: [{ value: inputs.smartAccount, abiType: 'address' }], }, + response: { value: balance, abiType: 'uint256' }, }, { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x95d89b41', // `symbol` - }, - response: { - value: 'USDC', - abiType: 'string', + fnSelector: ERC20Interface.getFunction('symbol')!.selector, }, + response: { value: 'USDC', abiType: 'string' }, }, ] diff --git a/examples/06-withdraw-from-aave-balance-threshold/tests/task.spec.ts b/examples/06-withdraw-from-aave-balance-threshold/tests/task.spec.ts index ea4e076..1a1b9bb 100644 --- a/examples/06-withdraw-from-aave-balance-threshold/tests/task.spec.ts +++ b/examples/06-withdraw-from-aave-balance-threshold/tests/task.spec.ts @@ -1,6 +1,13 @@ import { OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, GetPriceMock, runTask, Swap } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import AaveToken from '../abis/AaveToken.json' +import ERC20Abi from '../abis/ERC20.json' + +const AaveTokenInterface = new Interface(AaveToken) +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -23,17 +30,11 @@ describe('Task', () => { const prices: GetPriceMock[] = [ { - request: { - token: inputs.aToken, - chainId: inputs.chainId, - }, + request: { token: inputs.aToken, chainId: inputs.chainId }, response: ['1000000000000000000'], // 1 aOptUSDC = 1 USD }, { - request: { - token: underlyingToken, - chainId: inputs.chainId, - }, + request: { token: underlyingToken, chainId: inputs.chainId }, response: ['1000000000000000000'], // 1 USDC = 1 USD }, ] @@ -44,91 +45,60 @@ describe('Task', () => { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0xb16a19de', // `UNDERLYING_ASSET_ADDRESS` - }, - response: { - value: underlyingToken, - abiType: 'address', + fnSelector: AaveTokenInterface.getFunction('UNDERLYING_ASSET_ADDRESS')!.selector, }, + response: { value: underlyingToken, abiType: 'address' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf` - params: [ - { - value: context.user!, - abiType: 'address', - }, - ], - }, - response: { - value: userBalance, - abiType: 'uint256', + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, + params: [{ value: context.user!, abiType: 'address' }], }, + response: { value: userBalance, abiType: 'uint256' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, { request: { to: inputs.aToken, chainId: inputs.chainId, - fnSelector: '0x95d89b41', // `symbol` - }, - response: { - value: 'aOptUSDC', - abiType: 'string', + fnSelector: ERC20Interface.getFunction('symbol')!.selector, }, + response: { value: 'aOptUSDC', abiType: 'string' }, }, // USDC { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf` - params: [ - { - value: inputs.recipient, - abiType: 'address', - }, - ], - }, - response: { - value: recipientBalance, - abiType: 'uint256', + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, + params: [{ value: inputs.recipient, abiType: 'address' }], }, + response: { value: recipientBalance, abiType: 'uint256' }, }, { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, { request: { to: underlyingToken, chainId: inputs.chainId, - fnSelector: '0x95d89b41', // `symbol` - }, - response: { - value: 'USDC', - abiType: 'string', + fnSelector: ERC20Interface.getFunction('symbol')!.selector, }, + response: { value: 'USDC', abiType: 'string' }, }, ] diff --git a/examples/07-withdraw-from-aave-swap-and-transfer/abis/ERC20.json b/examples/07-withdraw-from-aave-swap-and-transfer/abis/ERC20.json new file mode 100644 index 0000000..405d6b3 --- /dev/null +++ b/examples/07-withdraw-from-aave-swap-and-transfer/abis/ERC20.json @@ -0,0 +1,222 @@ +[ + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_from", + "type": "address" + }, + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [ + { + "name": "", + "type": "uint8" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "balance", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + }, + { + "name": "_spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "fallback" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + } +] diff --git a/examples/07-withdraw-from-aave-swap-and-transfer/tests/task.spec.ts b/examples/07-withdraw-from-aave-swap-and-transfer/tests/task.spec.ts index eab24c0..9aa2a69 100644 --- a/examples/07-withdraw-from-aave-swap-and-transfer/tests/task.spec.ts +++ b/examples/07-withdraw-from-aave-swap-and-transfer/tests/task.spec.ts @@ -10,6 +10,13 @@ import { Transfer, } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import AavePool from '../abis/AavePool.json' +import ERC20Abi from '../abis/ERC20.json' + +const AavePoolInterface = new Interface(AavePool) +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -36,7 +43,7 @@ describe('Task', () => { const calls: ContractCallMock[] = [ { - request: { to: tokens.USDT, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: tokens.USDT, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '6', abiType: 'uint8' }, }, ] @@ -125,8 +132,9 @@ describe('Task', () => { describe('when all relevant tokens are not present', () => { describe('when there is only aUSDC in the smart account', () => { + const amount = '1000000' const relevantTokens = buildRelevantTokens({ - aUsdcSmartAccountBalance: '1000000', + aUsdcSmartAccountBalance: amount, usdcUserBalance: '0', aUsdcUserBalance: '0', }) @@ -141,6 +149,15 @@ describe('Task', () => { expect(intents[0].op).to.equal(OpType.EvmCall) expect(intents[0].user).to.equal(inputs.smartAccount) + + const expectedData = AavePoolInterface.encodeFunctionData('withdraw(address,uint256,address)', [ + tokens.USDC, + amount, + context.user, + ]) + expect(intents[0].calls[0].target).to.be.equal('0x794a61358d6845594f94dc1db02a252b5b4814ad') + expect(intents[0].calls[0].value).to.be.equal('0') + expect(intents[0].calls[0].data).to.be.equal(expectedData) }) }) diff --git a/examples/08-relevant-tokens-query/tests/task.spec.ts b/examples/08-relevant-tokens-query/tests/task.spec.ts index 2ac7820..4617c2d 100644 --- a/examples/08-relevant-tokens-query/tests/task.spec.ts +++ b/examples/08-relevant-tokens-query/tests/task.spec.ts @@ -1,6 +1,11 @@ import { NATIVE_TOKEN_ADDRESS, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { ContractCallMock, GetRelevantTokensMock, runTask, Transfer } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -22,11 +27,11 @@ describe('Task', () => { const calls: ContractCallMock[] = [ { - request: { chainId, to: USDC, fnSelector: '0x313ce567' }, + request: { chainId, to: USDC, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '6', abiType: 'uint8' }, }, { - request: { chainId, to: USDC, fnSelector: '0x95d89b41' }, + request: { chainId, to: USDC, fnSelector: ERC20Interface.getFunction('symbol')!.selector }, response: { value: 'USDC', abiType: 'string' }, }, ] diff --git a/examples/09-subgraph-query/tests/task.spec.ts b/examples/09-subgraph-query/tests/task.spec.ts index f641604..c0ea6a7 100644 --- a/examples/09-subgraph-query/tests/task.spec.ts +++ b/examples/09-subgraph-query/tests/task.spec.ts @@ -1,6 +1,11 @@ import { OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, runTask, SubgraphQueryMock, Swap } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -39,35 +44,26 @@ describe('Task', () => { request: { to: inputs.tokenIn, chainId: inputs.chainId, - fnSelector: '0x70a08231', // `balanceOf` + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, params: [{ value: context.user!, abiType: 'address' }], }, - response: { - value: balance, - abiType: 'uint256', - }, + response: { value: balance, abiType: 'uint256' }, }, { request: { to: inputs.tokenIn, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '6', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '6', abiType: 'uint8' }, }, { request: { to: inputs.tokenOut, chainId: inputs.chainId, - fnSelector: '0x313ce567', // `decimals` - }, - response: { - value: '18', - abiType: 'uint8', + fnSelector: ERC20Interface.getFunction('decimals')!.selector, }, + response: { value: '18', abiType: 'uint8' }, }, ] diff --git a/examples/10-rebalancing-tokens/tests/task.spec.ts b/examples/10-rebalancing-tokens/tests/task.spec.ts index 892ab87..198ae61 100644 --- a/examples/10-rebalancing-tokens/tests/task.spec.ts +++ b/examples/10-rebalancing-tokens/tests/task.spec.ts @@ -1,13 +1,20 @@ -import { OpType, randomEvmAddress } from '@mimicprotocol/sdk' +import { Chains, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { ContractCallMock, runTask, Swap } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' + const chainId = Chains.Optimism + const context = { user: randomEvmAddress(), - settlers: [{ address: randomEvmAddress(), chainId: 10 }], + settlers: [{ address: randomEvmAddress(), chainId }], timestamp: Date.now(), } @@ -16,7 +23,7 @@ describe('Task', () => { const DAI = randomEvmAddress() // 18 decimals const inputs = { - chainId: 10, + chainId, tokenA: WBTC, tokenB: WETH, tokenC: DAI, @@ -31,43 +38,52 @@ describe('Task', () => { { request: { to: WBTC, - chainId: 10, - fnSelector: '0x70a08231', // balanceOf + chainId, + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, params: [{ value: context.user, abiType: 'address' }], }, response: { value: balanceWBTC, abiType: 'uint256' }, }, - { request: { to: WBTC, chainId: 10, fnSelector: '0x313ce567' }, response: { value: '8', abiType: 'uint8' } }, // decimals + { + request: { to: WBTC, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, + response: { value: '8', abiType: 'uint8' }, + }, // WETH { request: { to: WETH, - chainId: 10, - fnSelector: '0x70a08231', + chainId, + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, params: [{ value: context.user, abiType: 'address' }], }, response: { value: balanceWETH, abiType: 'uint256' }, }, - { request: { to: WETH, chainId: 10, fnSelector: '0x313ce567' }, response: { value: '18', abiType: 'uint8' } }, + { + request: { to: WETH, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, + response: { value: '18', abiType: 'uint8' }, + }, // DAI { request: { to: DAI, - chainId: 10, - fnSelector: '0x70a08231', + chainId, + fnSelector: ERC20Interface.getFunction('balanceOf')!.selector, params: [{ value: context.user, abiType: 'address' }], }, response: { value: balanceDAI, abiType: 'uint256' }, }, - { request: { to: DAI, chainId: 10, fnSelector: '0x313ce567' }, response: { value: '18', abiType: 'uint8' } }, + { + request: { to: DAI, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, + response: { value: '18', abiType: 'uint8' }, + }, ] describe('when there are some balances', () => { // Prices: BTC=$60k, ETH=$3k, DAI=$1 — all with 1e18 USD precision const prices = [ - { request: { token: WBTC, chainId: 10 }, response: ['60000000000000000000000'] }, // 60000 * 1e18 - { request: { token: WETH, chainId: 10 }, response: ['3000000000000000000000'] }, // 3000 * 1e18 - { request: { token: DAI, chainId: 10 }, response: ['1000000000000000000'] }, // 1 * 1e18 + { request: { token: WBTC, chainId }, response: ['60000000000000000000000'] }, // 60000 * 1e18 + { request: { token: WETH, chainId }, response: ['3000000000000000000000'] }, // 3000 * 1e18 + { request: { token: DAI, chainId }, response: ['1000000000000000000'] }, // 1 * 1e18 ] describe('when rebalancing is needed (ETH surplus → BTC & DAI deficits)', () => { diff --git a/examples/11-automated-refunds/tests/task.spec.ts b/examples/11-automated-refunds/tests/task.spec.ts index 7f71b22..2c16d47 100644 --- a/examples/11-automated-refunds/tests/task.spec.ts +++ b/examples/11-automated-refunds/tests/task.spec.ts @@ -1,6 +1,11 @@ import { Chains, fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -23,11 +28,11 @@ describe('Task', () => { const calls: ContractCallMock[] = [ { - request: { to: inputs.token, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: inputs.token, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '6', abiType: 'uint8' }, }, { - request: { to: inputs.token, chainId, fnSelector: '0x95d89b41' }, // `symbol` + request: { to: inputs.token, chainId, fnSelector: ERC20Interface.getFunction('symbol')!.selector }, response: { value: 'USDC', abiType: 'string' }, }, ] diff --git a/examples/12-dollar-cost-averaging/abis/ERC20.json b/examples/12-dollar-cost-averaging/abis/ERC20.json new file mode 100644 index 0000000..405d6b3 --- /dev/null +++ b/examples/12-dollar-cost-averaging/abis/ERC20.json @@ -0,0 +1,222 @@ +[ + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_from", + "type": "address" + }, + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [ + { + "name": "", + "type": "uint8" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "balance", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + }, + { + "name": "_spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "fallback" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + } +] diff --git a/examples/12-dollar-cost-averaging/tests/task.spec.ts b/examples/12-dollar-cost-averaging/tests/task.spec.ts index 94a248d..1d46c4b 100644 --- a/examples/12-dollar-cost-averaging/tests/task.spec.ts +++ b/examples/12-dollar-cost-averaging/tests/task.spec.ts @@ -1,6 +1,11 @@ import { Chains, fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, GetPriceMock, runTask, Swap } from '@mimicprotocol/test-ts' import { expect } from 'chai' +import { Interface } from 'ethers' + +import ERC20Abi from '../abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Task', () => { const taskDir = './build' @@ -38,20 +43,20 @@ describe('Task', () => { const calls: ContractCallMock[] = [ // USDC { - request: { to: USDC, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: USDC, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '6', abiType: 'uint8' }, }, { - request: { to: USDC, chainId, fnSelector: '0x95d89b41' }, // `symbol` + request: { to: USDC, chainId, fnSelector: ERC20Interface.getFunction('symbol')!.selector }, response: { value: 'USDC', abiType: 'string' }, }, // WETH { - request: { to: WETH, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: WETH, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '18', abiType: 'uint8' }, }, { - request: { to: WETH, chainId, fnSelector: '0x95d89b41' }, // `symbol` + request: { to: WETH, chainId, fnSelector: ERC20Interface.getFunction('symbol')!.selector }, response: { value: 'WETH', abiType: 'string' }, }, ] diff --git a/examples/13-bridge-and-invest-aave/tests/bridge.spec.ts b/examples/13-bridge-and-invest-aave/tests/bridge.spec.ts index 9b54e39..2b2c4a4 100644 --- a/examples/13-bridge-and-invest-aave/tests/bridge.spec.ts +++ b/examples/13-bridge-and-invest-aave/tests/bridge.spec.ts @@ -1,7 +1,11 @@ import { Chains, fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk' import { Context, ContractCallMock, Inputs, runTask, Swap } from '@mimicprotocol/test-ts' import { expect } from 'chai' -import { AbiCoder, keccak256, toUtf8Bytes } from 'ethers' +import { AbiCoder, Interface, keccak256, toUtf8Bytes } from 'ethers' + +import ERC20Abi from '../src/abis/ERC20.json' + +const ERC20Interface = new Interface(ERC20Abi) describe('Bridge', () => { const taskDir = './build/bridge' @@ -34,15 +38,23 @@ describe('Bridge', () => { const calls: ContractCallMock[] = [ { - request: { to: sourceUsdc, chainId: sourceChain, fnSelector: '0x313ce567' }, // `decimals` + request: { to: sourceUsdc, chainId: sourceChain, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: decimals.toString(), abiType: 'uint8' }, }, { - request: { to: destinationUsdc, chainId: destinationChain, fnSelector: '0x313ce567' }, // `decimals` + request: { + to: destinationUsdc, + chainId: destinationChain, + fnSelector: ERC20Interface.getFunction('decimals')!.selector, + }, response: { value: decimals.toString(), abiType: 'uint8' }, }, { - request: { to: inputs.feeToken, chainId: destinationChain, fnSelector: '0x313ce567' }, // `decimals` + request: { + to: inputs.feeToken, + chainId: destinationChain, + fnSelector: ERC20Interface.getFunction('decimals')!.selector, + }, response: { value: decimals.toString(), abiType: 'uint8' }, }, ] diff --git a/examples/13-bridge-and-invest-aave/tests/invest.spec.ts b/examples/13-bridge-and-invest-aave/tests/invest.spec.ts index ef66a44..6a2f7d5 100644 --- a/examples/13-bridge-and-invest-aave/tests/invest.spec.ts +++ b/examples/13-bridge-and-invest-aave/tests/invest.spec.ts @@ -16,8 +16,8 @@ import AavePool from '../src/abis/AavePool.json' import ERC20Abi from '../src/abis/ERC20.json' import SettlerAbi from '../src/abis/Settler.json' -const ERC20Interface = new Interface(ERC20Abi) const AavePoolInterface = new Interface(AavePool) +const ERC20Interface = new Interface(ERC20Abi) const SettlerInterface = new Interface(SettlerAbi) describe('Invest', () => { @@ -60,11 +60,11 @@ describe('Invest', () => { const calls: ContractCallMock[] = [ { - request: { to: USDC, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: USDC, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: decimals.toString(), abiType: 'uint8' }, }, { - request: { to: inputs.feeToken, chainId, fnSelector: '0x313ce567' }, // `decimals` + request: { to: inputs.feeToken, chainId, fnSelector: ERC20Interface.getFunction('decimals')!.selector }, response: { value: '18', abiType: 'uint8' }, }, ]