From 604beb493745b2639faa0f751b25343edd7b6234 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Thu, 5 Dec 2024 15:40:56 +0100 Subject: [PATCH] review comments --- crates/contracts/artifacts/Solver.json | 2 +- crates/contracts/artifacts/Spardose.json | 2 +- crates/contracts/solidity/Solver.sol | 10 +++++++--- crates/contracts/solidity/Spardose.sol | 16 +++++++--------- crates/shared/src/price_estimation/factory.rs | 7 +++---- crates/shared/src/price_estimation/mod.rs | 9 ++++++++- .../src/price_estimation/trade_verifier.rs | 9 +++++---- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/crates/contracts/artifacts/Solver.json b/crates/contracts/artifacts/Solver.json index 82e749c93b..0b3ffa8e18 100644 --- a/crates/contracts/artifacts/Solver.json +++ b/crates/contracts/artifacts/Solver.json @@ -1 +1 @@ -{"abi":[{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"bool","name":"countGas","type":"bool"}],"name":"storeBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISettlement","name":"settlementContract","type":"address"},{"internalType":"address payable","name":"trader","type":"address"},{"internalType":"address","name":"sellToken","type":"address"},{"internalType":"uint256","name":"sellAmount","type":"uint256"},{"internalType":"address","name":"nativeToken","type":"address"},{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"address payable","name":"receiver","type":"address"},{"internalType":"bytes","name":"settlementCall","type":"bytes"},{"components":[{"internalType":"bool","name":"enabled","type":"bool"},{"internalType":"address","name":"spardose","type":"address"}],"internalType":"struct Solver.Mock","name":"mock","type":"tuple"}],"name":"swap","outputs":[{"internalType":"uint256","name":"gasUsed","type":"uint256"},{"internalType":"uint256[]","name":"queriedBalances","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b50610a68806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806317b86b161461003b5780633bbb2e1d14610065575b600080fd5b61004e6100493660046107b4565b61007a565b60405161005c92919061089f565b60405180910390f35b6100786100733660046108ed565b610332565b005b60006060333014610111576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603860248201527f6f6e6c792073696d756c6174696f6e206c6f67696320697320616c6c6f77656460448201527f20746f2063616c6c202773776170272066756e6374696f6e0000000000000000606482015260840160405180910390fd5b825115610247576040517f57d5a1d300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8e811660048301528c81166024830152604482018c90528a811660648301528d16906357d5a1d390608401600060405180830381600087803b15801561019857600080fd5b505af11580156101ac573d6000803e3d6000fd5b50505060208401516040517fc56cca8300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8f811660048301528e81166024830152604482018e9052909116915063c56cca8390606401600060405180830381600087803b15801561022e57600080fd5b505af1158015610242573d6000803e3d6000fd5b505050505b60008673ffffffffffffffffffffffffffffffffffffffff16600060405160006040518083038185875af1925050503d80600081146102a2576040519150601f19603f3d011682016040523d82523d6000602084013e6102a7565b606091505b50509050506102b788888f610467565b6102c28d868661053f565b91506102cf88888f610467565b600180548060200260200160405190810160405280929190818152602001828054801561031b57602002820191906000526020600020905b815481526020019060010190808311610307575b505050505090509b509b9950505050505050505050565b60005a9050600173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff8616146103ff576040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff85811660048301528616906370a0823190602401602060405180830381865afa1580156103d6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103fa9190610934565b610418565b8373ffffffffffffffffffffffffffffffffffffffff16315b815460018101835560009283526020909220909101558115610461575a61043f908261097c565b61044b9061116c610995565b60008082825461045b9190610995565b90915550505b50505050565b60005b828110156104615730633bbb2e1d85858481811061048a5761048a6109a8565b905060200201602081019061049f91906109d7565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b16815273ffffffffffffffffffffffffffffffffffffffff9182166004820152908516602482015260006044820152606401600060405180830381600087803b15801561051457600080fd5b505af1158015610528573d6000803e3d6000fd5b505050508080610537906109f4565b91505061046a565b6000805a905061059c84848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff8916929150506105be565b506000545a6105ab908361097c565b6105b5919061097c565b95945050505050565b60606105cc836000846105d3565b9392505050565b606060008473ffffffffffffffffffffffffffffffffffffffff1684846040516105fd9190610a2c565b60006040518083038185875af1925050503d806000811461063a576040519150601f19603f3d011682016040523d82523d6000602084013e61063f565b606091505b50925090508061065157815160208301fd5b509392505050565b73ffffffffffffffffffffffffffffffffffffffff8116811461067b57600080fd5b50565b803561068981610659565b919050565b60008083601f8401126106a057600080fd5b50813567ffffffffffffffff8111156106b857600080fd5b6020830191508360208260051b85010111156106d357600080fd5b9250929050565b60008083601f8401126106ec57600080fd5b50813567ffffffffffffffff81111561070457600080fd5b6020830191508360208285010111156106d357600080fd5b8035801515811461068957600080fd5b60006040828403121561073e57600080fd5b6040516040810181811067ffffffffffffffff82111715610788577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040529050806107978361071c565b815260208301356107a781610659565b6020919091015292915050565b60008060008060008060008060008060006101408c8e0312156107d657600080fd5b6107e08c35610659565b8b359a506107f160208d0135610659565b60208c0135995061080460408d0161067e565b985060608c0135975061081960808d0161067e565b965067ffffffffffffffff8060a08e0135111561083557600080fd5b6108458e60a08f01358f0161068e565b909750955061085660c08e0161067e565b94508060e08e0135111561086957600080fd5b5061087a8d60e08e01358e016106da565b909350915061088d8d6101008e0161072c565b90509295989b509295989b9093969950565b6000604082018483526020604081850152818551808452606086019150828701935060005b818110156108e0578451835293830193918301916001016108c4565b5090979650505050505050565b60008060006060848603121561090257600080fd5b833561090d81610659565b9250602084013561091d81610659565b915061092b6040850161071c565b90509250925092565b60006020828403121561094657600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b8181038181111561098f5761098f61094d565b92915050565b8082018082111561098f5761098f61094d565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b6000602082840312156109e957600080fd5b81356105cc81610659565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610a2557610a2561094d565b5060010190565b6000825160005b81811015610a4d5760208186018101518583015201610a33565b50600092019182525091905056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b50600436106100365760003560e01c806317b86b161461003b5780633bbb2e1d14610065575b600080fd5b61004e6100493660046107b4565b61007a565b60405161005c92919061089f565b60405180910390f35b6100786100733660046108ed565b610332565b005b60006060333014610111576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603860248201527f6f6e6c792073696d756c6174696f6e206c6f67696320697320616c6c6f77656460448201527f20746f2063616c6c202773776170272066756e6374696f6e0000000000000000606482015260840160405180910390fd5b825115610247576040517f57d5a1d300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8e811660048301528c81166024830152604482018c90528a811660648301528d16906357d5a1d390608401600060405180830381600087803b15801561019857600080fd5b505af11580156101ac573d6000803e3d6000fd5b50505060208401516040517fc56cca8300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8f811660048301528e81166024830152604482018e9052909116915063c56cca8390606401600060405180830381600087803b15801561022e57600080fd5b505af1158015610242573d6000803e3d6000fd5b505050505b60008673ffffffffffffffffffffffffffffffffffffffff16600060405160006040518083038185875af1925050503d80600081146102a2576040519150601f19603f3d011682016040523d82523d6000602084013e6102a7565b606091505b50509050506102b788888f610467565b6102c28d868661053f565b91506102cf88888f610467565b600180548060200260200160405190810160405280929190818152602001828054801561031b57602002820191906000526020600020905b815481526020019060010190808311610307575b505050505090509b509b9950505050505050505050565b60005a9050600173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff8616146103ff576040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff85811660048301528616906370a0823190602401602060405180830381865afa1580156103d6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103fa9190610934565b610418565b8373ffffffffffffffffffffffffffffffffffffffff16315b815460018101835560009283526020909220909101558115610461575a61043f908261097c565b61044b9061116c610995565b60008082825461045b9190610995565b90915550505b50505050565b60005b828110156104615730633bbb2e1d85858481811061048a5761048a6109a8565b905060200201602081019061049f91906109d7565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b16815273ffffffffffffffffffffffffffffffffffffffff9182166004820152908516602482015260006044820152606401600060405180830381600087803b15801561051457600080fd5b505af1158015610528573d6000803e3d6000fd5b505050508080610537906109f4565b91505061046a565b6000805a905061059c84848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff8916929150506105be565b506000545a6105ab908361097c565b6105b5919061097c565b95945050505050565b60606105cc836000846105d3565b9392505050565b606060008473ffffffffffffffffffffffffffffffffffffffff1684846040516105fd9190610a2c565b60006040518083038185875af1925050503d806000811461063a576040519150601f19603f3d011682016040523d82523d6000602084013e61063f565b606091505b50925090508061065157815160208301fd5b509392505050565b73ffffffffffffffffffffffffffffffffffffffff8116811461067b57600080fd5b50565b803561068981610659565b919050565b60008083601f8401126106a057600080fd5b50813567ffffffffffffffff8111156106b857600080fd5b6020830191508360208260051b85010111156106d357600080fd5b9250929050565b60008083601f8401126106ec57600080fd5b50813567ffffffffffffffff81111561070457600080fd5b6020830191508360208285010111156106d357600080fd5b8035801515811461068957600080fd5b60006040828403121561073e57600080fd5b6040516040810181811067ffffffffffffffff82111715610788577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040529050806107978361071c565b815260208301356107a781610659565b6020919091015292915050565b60008060008060008060008060008060006101408c8e0312156107d657600080fd5b6107e08c35610659565b8b359a506107f160208d0135610659565b60208c0135995061080460408d0161067e565b985060608c0135975061081960808d0161067e565b965067ffffffffffffffff8060a08e0135111561083557600080fd5b6108458e60a08f01358f0161068e565b909750955061085660c08e0161067e565b94508060e08e0135111561086957600080fd5b5061087a8d60e08e01358e016106da565b909350915061088d8d6101008e0161072c565b90509295989b509295989b9093969950565b6000604082018483526020604081850152818551808452606086019150828701935060005b818110156108e0578451835293830193918301916001016108c4565b5090979650505050505050565b60008060006060848603121561090257600080fd5b833561090d81610659565b9250602084013561091d81610659565b915061092b6040850161071c565b90509250925092565b60006020828403121561094657600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b8181038181111561098f5761098f61094d565b92915050565b8082018082111561098f5761098f61094d565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b6000602082840312156109e957600080fd5b81356105cc81610659565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610a2557610a2561094d565b5060010190565b6000825160005b81811015610a4d5760208186018101518583015201610a33565b50600092019182525091905056fea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}} +{"abi":[{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"bool","name":"countGas","type":"bool"}],"name":"storeBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISettlement","name":"settlementContract","type":"address"},{"internalType":"address payable","name":"trader","type":"address"},{"internalType":"address","name":"sellToken","type":"address"},{"internalType":"uint256","name":"sellAmount","type":"uint256"},{"internalType":"address","name":"nativeToken","type":"address"},{"internalType":"address[]","name":"tokens","type":"address[]"},{"internalType":"address payable","name":"receiver","type":"address"},{"internalType":"bytes","name":"settlementCall","type":"bytes"},{"components":[{"internalType":"bool","name":"enabled","type":"bool"},{"internalType":"address","name":"spardose","type":"address"}],"internalType":"struct Solver.Mock","name":"mock","type":"tuple"}],"name":"swap","outputs":[{"internalType":"uint256","name":"gasUsed","type":"uint256"},{"internalType":"uint256[]","name":"queriedBalances","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b50610b26806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806317b86b161461003b5780633bbb2e1d14610065575b600080fd5b61004e610049366004610851565b61007a565b60405161005c92919061093c565b60405180910390f35b61007861007336600461098a565b6103cf565b005b60006060333014610112576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603860248201527f6f6e6c792073696d756c6174696f6e206c6f67696320697320616c6c6f77656460448201527f20746f2063616c6c202773776170272066756e6374696f6e000000000000000060648201526084015b60405180910390fd5b8251156102e4576040517f57d5a1d300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8e811660048301528c81166024830152604482018c90528a811660648301528d16906357d5a1d390608401600060405180830381600087803b15801561019957600080fd5b505af11580156101ad573d6000803e3d6000fd5b50505060208401516040517fc56cca8300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8f811660048301528e81166024830152604482018e9052909116915063c56cca83906064016020604051808303816000875af1158015610234573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061025891906109d5565b6102e4576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f20746f6b656e00000000000000000000000000000000000000000000000000006064820152608401610109565b60008673ffffffffffffffffffffffffffffffffffffffff16600060405160006040518083038185875af1925050503d806000811461033f576040519150601f19603f3d011682016040523d82523d6000602084013e610344565b606091505b505090505061035488888f610504565b61035f8d86866105dc565b915061036c88888f610504565b60018054806020026020016040519081016040528092919081815260200182805480156103b857602002820191906000526020600020905b8154815260200190600101908083116103a4575b505050505090509b509b9950505050505050505050565b60005a9050600173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff86161461049c576040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff85811660048301528616906370a0823190602401602060405180830381865afa158015610473573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061049791906109f2565b6104b5565b8373ffffffffffffffffffffffffffffffffffffffff16315b8154600181018355600092835260209092209091015581156104fe575a6104dc9082610a3a565b6104e89061116c610a53565b6000808282546104f89190610a53565b90915550505b50505050565b60005b828110156104fe5730633bbb2e1d85858481811061052757610527610a66565b905060200201602081019061053c9190610a95565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b16815273ffffffffffffffffffffffffffffffffffffffff9182166004820152908516602482015260006044820152606401600060405180830381600087803b1580156105b157600080fd5b505af11580156105c5573d6000803e3d6000fd5b5050505080806105d490610ab2565b915050610507565b6000805a905061063984848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff89169291505061065b565b506000545a6106489083610a3a565b6106529190610a3a565b95945050505050565b606061066983600084610670565b9392505050565b606060008473ffffffffffffffffffffffffffffffffffffffff16848460405161069a9190610aea565b60006040518083038185875af1925050503d80600081146106d7576040519150601f19603f3d011682016040523d82523d6000602084013e6106dc565b606091505b5092509050806106ee57815160208301fd5b509392505050565b73ffffffffffffffffffffffffffffffffffffffff8116811461071857600080fd5b50565b8035610726816106f6565b919050565b60008083601f84011261073d57600080fd5b50813567ffffffffffffffff81111561075557600080fd5b6020830191508360208260051b850101111561077057600080fd5b9250929050565b60008083601f84011261078957600080fd5b50813567ffffffffffffffff8111156107a157600080fd5b60208301915083602082850101111561077057600080fd5b801515811461071857600080fd5b6000604082840312156107d957600080fd5b6040516040810181811067ffffffffffffffff82111715610823577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040529050808235610834816107b9565b81526020830135610844816106f6565b6020919091015292915050565b60008060008060008060008060008060006101408c8e03121561087357600080fd5b61087d8c356106f6565b8b359a5061088e60208d01356106f6565b60208c013599506108a160408d0161071b565b985060608c013597506108b660808d0161071b565b965067ffffffffffffffff8060a08e013511156108d257600080fd5b6108e28e60a08f01358f0161072b565b90975095506108f360c08e0161071b565b94508060e08e0135111561090657600080fd5b506109178d60e08e01358e01610777565b909350915061092a8d6101008e016107c7565b90509295989b509295989b9093969950565b6000604082018483526020604081850152818551808452606086019150828701935060005b8181101561097d57845183529383019391830191600101610961565b5090979650505050505050565b60008060006060848603121561099f57600080fd5b83356109aa816106f6565b925060208401356109ba816106f6565b915060408401356109ca816107b9565b809150509250925092565b6000602082840312156109e757600080fd5b8151610669816107b9565b600060208284031215610a0457600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b81810381811115610a4d57610a4d610a0b565b92915050565b80820180821115610a4d57610a4d610a0b565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600060208284031215610aa757600080fd5b8135610669816106f6565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610ae357610ae3610a0b565b5060010190565b6000825160005b81811015610b0b5760208186018101518583015201610af1565b50600092019182525091905056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b50600436106100365760003560e01c806317b86b161461003b5780633bbb2e1d14610065575b600080fd5b61004e610049366004610851565b61007a565b60405161005c92919061093c565b60405180910390f35b61007861007336600461098a565b6103cf565b005b60006060333014610112576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152603860248201527f6f6e6c792073696d756c6174696f6e206c6f67696320697320616c6c6f77656460448201527f20746f2063616c6c202773776170272066756e6374696f6e000000000000000060648201526084015b60405180910390fd5b8251156102e4576040517f57d5a1d300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8e811660048301528c81166024830152604482018c90528a811660648301528d16906357d5a1d390608401600060405180830381600087803b15801561019957600080fd5b505af11580156101ad573d6000803e3d6000fd5b50505060208401516040517fc56cca8300000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8f811660048301528e81166024830152604482018e9052909116915063c56cca83906064016020604051808303816000875af1158015610234573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061025891906109d5565b6102e4576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f20746f6b656e00000000000000000000000000000000000000000000000000006064820152608401610109565b60008673ffffffffffffffffffffffffffffffffffffffff16600060405160006040518083038185875af1925050503d806000811461033f576040519150601f19603f3d011682016040523d82523d6000602084013e610344565b606091505b505090505061035488888f610504565b61035f8d86866105dc565b915061036c88888f610504565b60018054806020026020016040519081016040528092919081815260200182805480156103b857602002820191906000526020600020905b8154815260200190600101908083116103a4575b505050505090509b509b9950505050505050505050565b60005a9050600173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff86161461049c576040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff85811660048301528616906370a0823190602401602060405180830381865afa158015610473573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061049791906109f2565b6104b5565b8373ffffffffffffffffffffffffffffffffffffffff16315b8154600181018355600092835260209092209091015581156104fe575a6104dc9082610a3a565b6104e89061116c610a53565b6000808282546104f89190610a53565b90915550505b50505050565b60005b828110156104fe5730633bbb2e1d85858481811061052757610527610a66565b905060200201602081019061053c9190610a95565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b16815273ffffffffffffffffffffffffffffffffffffffff9182166004820152908516602482015260006044820152606401600060405180830381600087803b1580156105b157600080fd5b505af11580156105c5573d6000803e3d6000fd5b5050505080806105d490610ab2565b915050610507565b6000805a905061063984848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff89169291505061065b565b506000545a6106489083610a3a565b6106529190610a3a565b95945050505050565b606061066983600084610670565b9392505050565b606060008473ffffffffffffffffffffffffffffffffffffffff16848460405161069a9190610aea565b60006040518083038185875af1925050503d80600081146106d7576040519150601f19603f3d011682016040523d82523d6000602084013e6106dc565b606091505b5092509050806106ee57815160208301fd5b509392505050565b73ffffffffffffffffffffffffffffffffffffffff8116811461071857600080fd5b50565b8035610726816106f6565b919050565b60008083601f84011261073d57600080fd5b50813567ffffffffffffffff81111561075557600080fd5b6020830191508360208260051b850101111561077057600080fd5b9250929050565b60008083601f84011261078957600080fd5b50813567ffffffffffffffff8111156107a157600080fd5b60208301915083602082850101111561077057600080fd5b801515811461071857600080fd5b6000604082840312156107d957600080fd5b6040516040810181811067ffffffffffffffff82111715610823577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040529050808235610834816107b9565b81526020830135610844816106f6565b6020919091015292915050565b60008060008060008060008060008060006101408c8e03121561087357600080fd5b61087d8c356106f6565b8b359a5061088e60208d01356106f6565b60208c013599506108a160408d0161071b565b985060608c013597506108b660808d0161071b565b965067ffffffffffffffff8060a08e013511156108d257600080fd5b6108e28e60a08f01358f0161072b565b90975095506108f360c08e0161071b565b94508060e08e0135111561090657600080fd5b506109178d60e08e01358e01610777565b909350915061092a8d6101008e016107c7565b90509295989b509295989b9093969950565b6000604082018483526020604081850152818551808452606086019150828701935060005b8181101561097d57845183529383019391830191600101610961565b5090979650505050505050565b60008060006060848603121561099f57600080fd5b83356109aa816106f6565b925060208401356109ba816106f6565b915060408401356109ca816107b9565b809150509250925092565b6000602082840312156109e757600080fd5b8151610669816107b9565b600060208284031215610a0457600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b81810381811115610a4d57610a4d610a0b565b92915050565b80820180821115610a4d57610a4d610a0b565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600060208284031215610aa757600080fd5b8135610669816106f6565b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610ae357610ae3610a0b565b5060010190565b6000825160005b81811015610b0b5760208186018101518583015201610af1565b50600092019182525091905056fea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}} diff --git a/crates/contracts/artifacts/Spardose.json b/crates/contracts/artifacts/Spardose.json index ce19b8db54..08787fb5a7 100644 --- a/crates/contracts/artifacts/Spardose.json +++ b/crates/contracts/artifacts/Spardose.json @@ -1 +1 @@ -{"abi":[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"ensureBalance","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b506103ee806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}} +{"abi":[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"ensureBalance","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b50610364806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610277565b610057565b604051901515815260200160405180910390f35b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff848116600483015260009182918516906370a0823190602401602060405180830381865afa1580156100c8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ec91906102b3565b90508281106100ff576001915050610133565b600061010b82856102cc565b905061012e73ffffffffffffffffffffffffffffffffffffffff8616878361013a565b925050505b9392505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092606091908716906101d3908490610306565b6000604051808303816000865af19150503d8060008114610210576040519150601f19603f3d011682016040523d82523d6000602084013e610215565b606091505b50909350905082801561012e575061012e8160008151600014806102485750818060200190518101906102489190610335565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461027257600080fd5b919050565b60008060006060848603121561028c57600080fd5b6102958461024e565b92506102a36020850161024e565b9150604084013590509250925092565b6000602082840312156102c557600080fd5b5051919050565b81810381811115610248577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b81811015610327576020818601810151858301520161030d565b506000920191825250919050565b60006020828403121561034757600080fd5b8151801515811461013357600080fdfea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610277565b610057565b604051901515815260200160405180910390f35b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff848116600483015260009182918516906370a0823190602401602060405180830381865afa1580156100c8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ec91906102b3565b90508281106100ff576001915050610133565b600061010b82856102cc565b905061012e73ffffffffffffffffffffffffffffffffffffffff8616878361013a565b925050505b9392505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092606091908716906101d3908490610306565b6000604051808303816000865af19150503d8060008114610210576040519150601f19603f3d011682016040523d82523d6000602084013e610215565b606091505b50909350905082801561012e575061012e8160008151600014806102485750818060200190518101906102489190610335565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461027257600080fd5b919050565b60008060006060848603121561028c57600080fd5b6102958461024e565b92506102a36020850161024e565b9150604084013590509250925092565b6000602082840312156102c557600080fd5b5051919050565b81810381811115610248577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b81811015610327576020818601810151858301520161030d565b506000920191825250919050565b60006020828403121561034757600080fd5b8151801515811461013357600080fdfea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}} diff --git a/crates/contracts/solidity/Solver.sol b/crates/contracts/solidity/Solver.sol index a7a27a9fff..eece404458 100644 --- a/crates/contracts/solidity/Solver.sol +++ b/crates/contracts/solidity/Solver.sol @@ -74,9 +74,13 @@ contract Solver { nativeToken ); - // Ensure that the user has sufficient sell token balance for the - // swap using balance overrides. - Spardose(mock.spardose).ensureBalance(trader, sellToken, sellAmount); + // Ensure that the user has sufficient sell token balance. In case + // balance overrides are enabled, the Spardose will fund the trader + // with simulated balances. + require( + Spardose(mock.spardose).ensureBalance(trader, sellToken, sellAmount), + "trader does not have enough sell token" + ); } // Warm the storage for sending ETH to smart contract addresses. diff --git a/crates/contracts/solidity/Spardose.sol b/crates/contracts/solidity/Spardose.sol index 502261ac02..c1fa6167be 100644 --- a/crates/contracts/solidity/Spardose.sol +++ b/crates/contracts/solidity/Spardose.sol @@ -13,22 +13,20 @@ contract Spardose { using SafeERC20 for *; /// @dev Ensures that the trader has at least `amount` tokens. If not, it - /// will transfer the difference to the trader. + /// will try and transfer the difference to the trader. /// /// @param trader - the address of the trader /// @param token - the token to ensure a balance for - /// @param amount - the amount of `token` that the `trader` must hold. - function ensureBalance(address trader, address token, uint256 amount) external { + /// @param amount - the amount of `token` that the `trader` must hold + /// + /// @return success - the `trader`'s `token` balance is more than `amount` + function ensureBalance(address trader, address token, uint256 amount) external returns (bool success) { uint256 traderBalance = IERC20(token).balanceOf(trader); if (traderBalance >= amount) { - // Nothing to do. - return; + return true; } uint256 difference = amount - traderBalance; - require( - IERC20(token).trySafeTransfer(trader, difference), - "trader does not have enough sell_token" - ); + return IERC20(token).trySafeTransfer(trader, difference); } } diff --git a/crates/shared/src/price_estimation/factory.rs b/crates/shared/src/price_estimation/factory.rs index cd5e38e2b2..2d660adf03 100644 --- a/crates/shared/src/price_estimation/factory.rs +++ b/crates/shared/src/price_estimation/factory.rs @@ -124,11 +124,10 @@ impl<'a> PriceEstimatorFactory<'a> { network.native_token, args.quote_inaccuracy_limit.clone(), ) - .await?; + .await? + .with_balance_overrides(balance_overrides); - Ok(Some(Arc::new( - verifier.with_balance_overrides(balance_overrides), - ))) + Ok(Some(Arc::new(verifier))) } fn native_token_price_estimation_amount(&self) -> Result { diff --git a/crates/shared/src/price_estimation/mod.rs b/crates/shared/src/price_estimation/mod.rs index 50d1b32d7f..607925bc83 100644 --- a/crates/shared/src/price_estimation/mod.rs +++ b/crates/shared/src/price_estimation/mod.rs @@ -213,7 +213,14 @@ pub struct Arguments { )] pub quote_timeout: Duration, - /// Token configuration for simulated balances on verified quotes. + /// Token configuration for simulated balances on verified quotes. This + /// allows the quote verification system to produce verified quotes for + /// traders without sufficient balance for the configured token pairs. + /// + /// The expected format is a comma separated list of `${ADDR}@${SLOT}`, + /// where `ADDR` is the token address and `SLOT` is the Solidity storage + /// slot for the balances mapping. For example for WETH: + /// `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2@3`. #[clap(long, env, default_value_t)] pub quote_token_balance_overrides: ConfigurationBalanceOverrides, } diff --git a/crates/shared/src/price_estimation/trade_verifier.rs b/crates/shared/src/price_estimation/trade_verifier.rs index e13e4ca70e..fed26bb928 100644 --- a/crates/shared/src/price_estimation/trade_verifier.rs +++ b/crates/shared/src/price_estimation/trade_verifier.rs @@ -335,12 +335,13 @@ impl TradeVerifier { }, ); - // Provide mocked balances if possible to the solver to allow it to + // Provide mocked balances if possible to the spardose to allow it to // give some balances to the trader in order to verify trades even for - // owners without balances. Note that we explicitly override the - // **solver's** balance and not the trader's directly. This allows the + // owners without balances. Note that we use a separate account for + // funding to not interfere with the settlement process. This allows the // simulation to conditionally transfer the balance only when it is - // safe to mock the trade pre-conditions on behalf of the user. We use + // safe to mock the trade pre-conditions on behalf of the user and to + // not alter solver balances which may be used during settlement. We use // a similar strategy for determining whether or not to set approvals on // behalf of the trader. if let Some(solver_balance_override) =