Skip to content

Commit 7f4d86d

Browse files
committed
update
1 parent 60e772f commit 7f4d86d

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

receipts/token_transfers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,15 @@ func (s TokenTransfers) ComputeBalanceOutputs() TokenBalances {
207207

208208
return out
209209
}
210+
211+
func (b TokenBalances) FilterByAccount(account common.Address, optToken ...common.Address) TokenBalances {
212+
var out TokenBalances
213+
for _, bal := range b {
214+
if bal.Account == account {
215+
if len(optToken) == 0 || bal.Token == optToken[0] {
216+
out = append(out, bal)
217+
}
218+
}
219+
}
220+
return out
221+
}

receipts/token_transfers_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,100 @@ func TestFetchReceiptTokenTransfers(t *testing.T) {
161161
// over CCTP. This includes many calls with USDC and MAGIC.
162162
// https://arbiscan.io/tx/0xa5c17e51443c8a8ce60cdcbe84b89fd2570f073bbb3b9ec8cdc9361aa1ca984f
163163
t.Run("Case 4: Trails swap intent call", func(t *testing.T) {
164+
provider, err := ethrpc.NewProvider("https://nodes.sequence.app/arbitrum")
165+
require.NoError(t, err)
166+
167+
txnHash := common.HexToHash("0xa5c17e51443c8a8ce60cdcbe84b89fd2570f073bbb3b9ec8cdc9361aa1ca984f")
168+
receipt, transfers, err := receipts.FetchReceiptTokenTransfers(context.Background(), provider, txnHash)
169+
require.NoError(t, err)
170+
require.NotNil(t, receipt)
171+
require.Greater(t, len(transfers), 0)
172+
require.Equal(t, 31, len(receipt.Logs))
173+
174+
// Trails intent
175+
require.Equal(t, 10, len(transfers))
176+
// spew.Dump(transfers)
177+
178+
// Get the balance outputs from the transfer logs
179+
balances := transfers.ComputeBalanceOutputs()
180+
require.NotNil(t, balances)
181+
require.Equal(t, len(balances), 9)
182+
// spew.Dump(balances)
183+
184+
usdc := common.HexToAddress("0xaf88d065e77c8cC2239327C5EDb3A432268e5831")
185+
magic := common.HexToAddress("0x539bdE0d7Dbd336b79148AA742883198BBF60342")
186+
owner := common.HexToAddress("0x8f2951E6b9Bd9F8cf3522A7Fa98A0F9bC767b155")
187+
trailsRouter := common.HexToAddress("0xF8A739B9F24E297a98b7aba7A9cdFDBD457F6fF8")
188+
collector := common.HexToAddress("0x76008498f26789dd8b691Bebe24C889A3dd1A2fc")
189+
190+
// intermediary token used via uniswap
191+
weth := common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1")
192+
uniswap := common.HexToAddress("0xB7E50106A5bd3Cf21AF210A755F9C8740890A8c9")
193+
194+
// some rando token used in the swap
195+
liqBook := common.HexToAddress("0xb7236B927e03542AC3bE0A054F2bEa8868AF9508")
196+
197+
// NOTE: these balances are not in order of operations
198+
// it is the outputs, and sorted by smallest to highest
199+
// in terms of numeric value (not USD value obviously)
200+
// as decimals are not factored in here per token.
201+
202+
// owner sending magic
203+
require.Equal(t, magic, balances[0].Token)
204+
require.Equal(t, owner, balances[0].Account)
205+
require.Equal(t, makeBigInt(t, "-10686807000000000000"), balances[0].Balance)
206+
207+
// uniswap sending out weth
208+
require.Equal(t, weth, balances[1].Token)
209+
require.Equal(t, uniswap, balances[1].Account)
210+
require.Equal(t, makeBigInt(t, "-383769729558864"), balances[1].Balance)
211+
212+
// liqbook sending usdc
213+
require.Equal(t, usdc, balances[2].Token)
214+
require.Equal(t, liqBook, balances[2].Account)
215+
require.Equal(t, makeBigInt(t, "-1191946"), balances[2].Balance)
216+
217+
// balance of some 0x related wallet, probably a fee collector for 0x
218+
require.Equal(t, usdc, balances[3].Token)
219+
require.Equal(t, common.HexToAddress("0xaD01C20d5886137e056775af56915de824c8fCe5"), balances[3].Account)
220+
require.Equal(t, makeBigInt(t, "1787"), balances[3].Balance)
221+
222+
// trailsRouter receiving usdc
223+
// TODO: this must be a bug in trails router or calls, as there shouldn't be any
224+
// dust left in the router.
225+
require.Equal(t, usdc, balances[4].Token)
226+
require.Equal(t, trailsRouter, balances[4].Account)
227+
require.Equal(t, makeBigInt(t, "36299"), balances[4].Balance)
228+
229+
// usdc burn for cctp
230+
require.Equal(t, usdc, balances[5].Token)
231+
require.Equal(t, common.HexToAddress("0x0000000000000000000000000000000000000000"), balances[5].Account)
232+
require.Equal(t, makeBigInt(t, "1153860"), balances[5].Balance)
233+
234+
// liqBook got the weth from the swap flow
235+
require.Equal(t, weth, balances[6].Token)
236+
require.Equal(t, liqBook, balances[6].Account)
237+
require.Equal(t, makeBigInt(t, "383769729558864"), balances[6].Balance)
238+
239+
// fee collector paid in magic
240+
require.Equal(t, magic, balances[7].Token)
241+
require.Equal(t, collector, balances[7].Account)
242+
require.Equal(t, makeBigInt(t, "109223258035414205"), balances[7].Balance)
243+
244+
// uniswap ending up with the magic from swap in
245+
require.Equal(t, magic, balances[8].Token)
246+
require.Equal(t, uniswap, balances[8].Account)
247+
require.Equal(t, makeBigInt(t, "10577583741964585795"), balances[8].Balance)
248+
249+
// Get balance of just the cctp burn address
250+
cctpBurnAddress := balances.FilterByAccount(common.HexToAddress("0x0000000000000000000000000000000000000000"), usdc)
251+
require.Equal(t, 1, len(cctpBurnAddress))
252+
require.Equal(t, usdc, cctpBurnAddress[0].Token)
253+
require.Equal(t, makeBigInt(t, "1153860"), cctpBurnAddress[0].Balance)
254+
255+
// Get balance of uniswap only
256+
uniswapBalances := balances.FilterByAccount(uniswap)
257+
require.Equal(t, 2, len(uniswapBalances))
164258
})
165259

166260
// Case 5: vault bridge USDC .. lets check the token transfer event, prob just erc20 too
@@ -181,3 +275,9 @@ func TestFetchReceiptTokenTransfers(t *testing.T) {
181275

182276
// TODO: lets test the TokenTransfers directly with mock
183277
// data we write by hand to ensure aggregation works properly, etc.
278+
279+
func makeBigInt(t *testing.T, s string) *big.Int {
280+
bi, ok := new(big.Int).SetString(s, 10)
281+
require.True(t, ok)
282+
return bi
283+
}

0 commit comments

Comments
 (0)