diff --git a/bin/build.sh b/bin/build.sh index 6bf2e18..6e9302d 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -2,7 +2,7 @@ # this script requires the jq command $(sudo apt install jq) # first-pass build -forge build +forge build "$@" # calculate the Vault init code hash using the bytecode generated for Vault # shellcheck disable=SC2046 @@ -16,4 +16,4 @@ mkdir gen &> /dev/null echo "export const VAULT_INIT_CODE_HASH='$VAULT_INIT_CODE_HASH';" > gen/vaultHash.js # final build after hash values are set -forge build +forge build "$@" diff --git a/bin/mock.sh b/bin/mock.sh index dc60efd..092c25f 100755 --- a/bin/mock.sh +++ b/bin/mock.sh @@ -5,7 +5,7 @@ #db-migrate up #cd ../contract -./bin/build.sh +#source ./bin/build.sh anvil -f arbitrum_ankr --chain-id 1338 & # todo check anvil result ANVIL_PID=$! @@ -14,7 +14,7 @@ sleep 2 forge script script/Deploy.sol -vvvv --fork-url http://localhost:8545 --broadcast trap_ctrlc() { - echo + echo exiting anvil kill $ANVIL_PID } diff --git a/bin/test.sh b/bin/test.sh new file mode 100755 index 0000000..281d570 --- /dev/null +++ b/bin/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +./bin/build.sh +forge test -vvvv --fork-url arbitrum_ankr diff --git a/src/OrderLib.sol b/src/OrderLib.sol index e246232..e4c618b 100644 --- a/src/OrderLib.sol +++ b/src/OrderLib.sol @@ -4,20 +4,22 @@ pragma abicoder v2; import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; import "./UniswapSwapper.sol"; +import "forge-std/console2.sol"; library OrderLib { + // todo safe math and/or bounds checking uint64 internal constant NO_CHAIN = type(uint64).max; uint64 internal constant NO_OCO = type(uint64).max; - event DexorderPlaced (uint64 startOrderIndex, uint8 numOrders); + event DexorderSwapPlaced (uint64 startOrderIndex, uint8 numOrders); event DexorderSwapFilled (uint64 orderIndex, uint8 trancheIndex, uint256 amountIn, uint256 amountOut); - event DexorderCompleted (uint64 orderIndex); // todo remove? + event DexorderSwapCompleted (uint64 orderIndex); // todo remove? - event DexorderError (uint64 orderIndex, string reason); + event DexorderSwapError (uint64 orderIndex, string reason); // todo If a tranche fails to fill, an order can stay Open forever without any active tranches. maybe replace state with a simple canceled flag enum SwapOrderState { @@ -127,7 +129,6 @@ library OrderLib { OcoGroup[] ocoGroups; // each indexed OCO group is an array of orderIndexes of orders in the oco group. } - function _placeOrder(OrdersInfo storage self, SwapOrder memory order) internal { SwapOrder[] memory orders = new SwapOrder[](1); orders[0] = order; @@ -175,7 +176,7 @@ library OrderLib { status.start = uint32(block.timestamp); status.ocoGroup = ocoGroup; } - emit DexorderPlaced(startIndex,uint8(orders.length)); + emit DexorderSwapPlaced(startIndex,uint8(orders.length)); } @@ -279,7 +280,7 @@ library OrderLib { uint256 remaining = status.order.amount - (status.order.amountIsInput ? status.filledIn : status.filledOut); if( remaining == 0 ) { // todo dust leeway? status.state = SwapOrderState.Filled; - emit DexorderCompleted(orderIndex); + emit DexorderSwapCompleted(orderIndex); if( status.ocoGroup != NO_OCO ) _cancelOco(self, status.ocoGroup); } @@ -298,7 +299,7 @@ library OrderLib { SwapOrderState state = self.orders[orderIndex].state; if( state == SwapOrderState.Open || state == SwapOrderState.Template ) { self.orders[orderIndex].state = SwapOrderState.Canceled; - emit DexorderCompleted(orderIndex); + emit DexorderSwapCompleted(orderIndex); } } } diff --git a/src/Vault.sol b/src/Vault.sol index 734100c..39c6671 100644 --- a/src/Vault.sol +++ b/src/Vault.sol @@ -60,6 +60,10 @@ contract Vault { orderList._placeOrders(orders, ocoMode); } + function swapOrderStatus(uint64 orderIndex) public view returns (OrderLib.SwapOrderStatus memory status) { + return orderList.orders[orderIndex]; + } + function execute(uint64 orderIndex, uint8 tranche_index, OrderLib.PriceProof memory proof) public returns (string memory error) { diff --git a/src/VaultAddress.sol b/src/VaultAddress.sol index 51f70e7..4387633 100644 --- a/src/VaultAddress.sol +++ b/src/VaultAddress.sol @@ -3,13 +3,14 @@ pragma solidity =0.7.6; pragma abicoder v2; import "./Constants.sol"; +import "forge-std/console2.sol"; library VaultAddress { // keccak-256 hash of the Vault's bytecode (not the deployed bytecode but the initialization bytecode) // can paste into: // https://emn178.github.io/online-tools/keccak_256.html - bytes32 internal constant VAULT_INIT_CODE_HASH = 0xdc7f6d1305b2c9951dc98f6a745290e4f2b92bf65d346db11dd284dcfcd67aef; + bytes32 internal constant VAULT_INIT_CODE_HASH = 0x40d5f4d1aa2f505a4b156599c452d0e7df5003430246ca5798a932dc031a9127; // the contract being constructed must not have any constructor arguments or the determinism will be broken. instead, use a callback to // get construction arguments @@ -20,13 +21,14 @@ library VaultAddress { } function computeAddress(address factory, address owner, uint8 num) internal pure returns (address vault) { + bytes32 salt = keccak256(abi.encodePacked(owner,num)); vault = address( uint256( keccak256( abi.encodePacked( hex'ff', factory, - keccak256(abi.encode(owner,num)), + salt, VAULT_INIT_CODE_HASH ) ) diff --git a/src/VaultDeployer.sol b/src/VaultDeployer.sol index 9acca9f..e24cef0 100644 --- a/src/VaultDeployer.sol +++ b/src/VaultDeployer.sol @@ -32,7 +32,7 @@ contract VaultDeployer { function _deployVault(address owner, uint8 num) internal returns (address payable vault) { parameters = Parameters(owner); - vault = address(new Vault{salt: keccak256(abi.encode(owner,num))}()); + vault = address(new Vault{salt: keccak256(abi.encodePacked(owner,num))}()); delete parameters; emit VaultCreated( owner, num ); } diff --git a/test/TestOrder.sol b/test/TestOrder.sol index b5395ba..06a4748 100644 --- a/test/TestOrder.sol +++ b/test/TestOrder.sol @@ -12,6 +12,7 @@ contract TestOrder is MockEnv, Test { // vault gets 100,000 COIN and 100,000 USD function setUp() public { + init(); factory = new Factory(); vault = Vault(factory.deployVault(address(this))); uint256 coinAmount = 100_000 * 10 ** COIN.decimals(); @@ -25,5 +26,4 @@ contract TestOrder is MockEnv, Test { } - } diff --git a/test/TestVault.sol b/test/TestVault.sol index 83f7cf0..8d196e3 100644 --- a/test/TestVault.sol +++ b/test/TestVault.sol @@ -7,14 +7,18 @@ import "../src/VaultAddress.sol"; import "forge-std/Test.sol"; pragma abicoder v2; -contract TestVault is Test{ +contract TestVault is Test { Factory public factory; Vault public vault; function setUp() public { factory = new Factory(); + console2.log('factory'); + console2.log(address(factory)); vault = Vault(factory.deployVault(address(this))); + console2.log('vault'); + console2.log(address(vault)); } function testDeterministicAddress() public {