diff --git a/pkg/parser/uniswapx/parser.go b/pkg/parser/uniswapx/parser.go index 0efb653..2db9a79 100644 --- a/pkg/parser/uniswapx/parser.go +++ b/pkg/parser/uniswapx/parser.go @@ -211,7 +211,7 @@ func (p *Parser) recursiveDetectRFQTrades(order storage.TradeLog, call types.Cal if err != nil { continue } - finalOrder, err := p.updateOrder(order, parsedOrder) + finalOrder, err := p.updateOrder(order, parsedOrder, common.HexToAddress(call.From)) if err != nil { continue } @@ -228,7 +228,7 @@ func (p *Parser) recursiveDetectRFQTrades(order storage.TradeLog, call types.Cal return order, fmt.Errorf("%w %s", parser.ErrNotFoundTrade, string(traceData)) } -func (p *Parser) updateOrder(internal storage.TradeLog, parsed []interface{}) (storage.TradeLog, error) { +func (p *Parser) updateOrder(internal storage.TradeLog, parsed []interface{}, sender common.Address) (storage.TradeLog, error) { data, err := json.Marshal(parsed) if err != nil { return storage.TradeLog{}, err @@ -253,19 +253,21 @@ func (p *Parser) updateOrder(internal storage.TradeLog, parsed []interface{}) (s internal.TakerToken = order.InputToken.InputToken.String() internal.MakerToken = order.Outputs[0].Token.String() - + blockTime := big.NewInt(int64(internal.Timestamp / 1000)) internal.TakerTokenAmount = decay(order.InputToken.InputStartAmount, order.InputToken.InputEndAmount, order.CosignerData.DecayStartTime, order.CosignerData.DecayEndTime, - big.NewInt(int64(internal.Timestamp/1000))).String() + blockTime).String() makerAmount := big.NewInt(0) for _, o := range order.Outputs { output := decay(o.StartAmount, o.EndAmount, order.CosignerData.DecayStartTime, order.CosignerData.DecayEndTime, - big.NewInt(int64(internal.Timestamp/1000))) - output = mulDivUp(output, new(big.Int).Add(BPS, order.CosignerData.ExclusivityOverrideBps), BPS) + blockTime) + if !hasFillingRights(order.CosignerData.ExclusiveFiller, sender, order.CosignerData.DecayStartTime, blockTime) { + output = mulDivUp(output, new(big.Int).Add(BPS, order.CosignerData.ExclusivityOverrideBps), BPS) + } makerAmount = makerAmount.Add(makerAmount, output) } internal.MakerTokenAmount = makerAmount.String() @@ -299,10 +301,6 @@ func decay(startAmount, endAmount, decayStartTime, decayEndTime, blockTime *big. return } -// func handleOverride(amount, exclusivityOverrideBps, BPS *big.Int) *big.Int { -// return mulDiv(amount, new(big.Int).Add(exclusivityOverrideBps, BPS), BPS) -// } - func mulDiv(a, b, c *big.Int) *big.Int { return new(big.Int).Div(new(big.Int).Mul(a, b), c) } @@ -317,9 +315,9 @@ func mulDivUp(x, y, denominator *big.Int) *big.Int { return result } -// func checkExclusivity(exclusive, sender common.Address, exclusivityEndTime, ts uint64) bool { -// return exclusive == common.Address{} || ts > exclusivityEndTime || exclusive == sender -// } +func hasFillingRights(exclusive, sender common.Address, exclusivityEndTime, blockTime *big.Int) bool { + return exclusive == common.Address{} || blockTime.Cmp(exclusivityEndTime) > 0 || exclusive == sender +} func (p *Parser) Exchange() string { return parser.ExUniswapX diff --git a/pkg/parser/uniswapx/uniswap_test.go b/pkg/parser/uniswapx/uniswap_test.go index 1392c7b..36850ec 100644 --- a/pkg/parser/uniswapx/uniswap_test.go +++ b/pkg/parser/uniswapx/uniswap_test.go @@ -30,8 +30,8 @@ func TestFetchEvent(t *testing.T) { require.Equal(t, p.abi.Events[FilledEvent].ID, common.HexToHash("0x78ad7ec0e9f89e74012afa58738b6b661c024cb0fd185ee2f616c0a28924bd66")) logs, err := ethClient.FilterLogs(context.Background(), ethereum.FilterQuery{ BlockHash: nil, - FromBlock: big.NewInt(20655762), - ToBlock: big.NewInt(20655762), + FromBlock: big.NewInt(20878510), + ToBlock: big.NewInt(20878510), Addresses: nil, Topics: [][]common.Hash{ { @@ -47,7 +47,7 @@ func TestFetchEvent(t *testing.T) { func TestParseEvent(t *testing.T) { t.Skip("Need to add the rpc url that enables the trace call JSON-RPC") - eventRaw := `{"address":"0x00000011f84b9aa48e5f8aa8b9897600006289be","topics":["0x78ad7ec0e9f89e74012afa58738b6b661c024cb0fd185ee2f616c0a28924bd66","0x9e57c601cc099a21f2adebee775f5c87197f64449d8dc14f4daa363a0acccd82","0x000000000000000000000000807cf9a772d5a3f9cefbc1192e939d62f0d9bd38","0x00000000000000000000000097e1e6c70fe9f2209792640336534bfc9f93aa7a"],"data":"0x046832e0bb8b67660fa0dab20dd6c717f3c188907d1b33cf6bb432d0434b4631","blockNumber":"0x13b2e92","transactionHash":"0x82f36abfb0f492ce6f6363cd9f233b2a86626234bfac5b1efe54e1c25a6fc410","transactionIndex":"0x1e","blockHash":"0xe6877169821ccccaf15256d0f802b4de56f11d6ca86783eb29a2530cc19fcdbe","logIndex":"0xc3","removed":false}` + eventRaw := `{"address":"0x00000011f84b9aa48e5f8aa8b9897600006289be","topics":["0x78ad7ec0e9f89e74012afa58738b6b661c024cb0fd185ee2f616c0a28924bd66","0xa09fd699de09d6492bd94a552d1411802fcce905984e90fe5720039931546cfc","0x000000000000000000000000807cf9a772d5a3f9cefbc1192e939d62f0d9bd38","0x000000000000000000000000cf2828bb821e935292f1d3ff7f996e19efe4910c"],"data":"0x046832975f1fff57796138f8a594ca97d887a6d4fef01f55f262cc210b31ba04","blockNumber":"0x13e94ae","transactionHash":"0xa0f1fb44e76f666e79bc4b290f226dac851e60c8eb57226012167b2cd313cc98","transactionIndex":"0x34","blockHash":"0xea65325c25577b35c89d5b7fee914f16ae9e3a4683fe7abf3ec5eeba0f17e128","logIndex":"0x11f","removed":false}` event := types.Log{} err := json.Unmarshal([]byte(eventRaw), &event) require.NoError(t, err) @@ -55,16 +55,18 @@ func TestParseEvent(t *testing.T) { if err != nil { panic(err) } + block, err := ethClient.BlockByHash(context.Background(), event.BlockHash) + require.NoError(t, err) traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient)) p := MustNewParser(traceCalls) - log, err := p.Parse(event, 1713889895) + log, err := p.Parse(event, block.Time()) require.NoError(t, err) fmt.Printf("%+v\n", log) require.Equal(t, log.EventHash, p.eventHash) require.Equal(t, log.Maker, "0x807cF9A772d5a3f9CeFBc1192e939D62f0D9bD38") - require.Equal(t, log.Taker, "0x97e1e6C70fe9F2209792640336534bFc9F93AA7A") - require.Equal(t, log.MakerTokenAmount, "197069169177783279401") - require.Equal(t, log.TakerTokenAmount, "10000000000000000000") - require.Equal(t, log.Expiry, uint64(0x66d46102)) + require.Equal(t, log.Taker, "0xcF2828bb821E935292f1D3Ff7F996e19eFe4910c") + require.Equal(t, log.MakerTokenAmount, "10195811258") + require.Equal(t, log.TakerTokenAmount, "10200030000") + require.Equal(t, log.Expiry, uint64(0x66fd5e45)) t.Log(log) }