Skip to content

Commit

Permalink
Add default interpreter for transfers (#105)
Browse files Browse the repository at this point in the history
* Upd interpreter

* Add default interpreter for transfers

* Move transfer interpreter to fallback

* Add changeset
  • Loading branch information
anastasiarods authored Sep 20, 2024
1 parent a1fb907 commit 4204de7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-rats-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@3loop/transaction-interpreter': minor
---

Add transfer interpretations to fallback interpreter
26 changes: 22 additions & 4 deletions packages/transaction-interpreter/interpreters/fallback.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import type { InterpretedTransaction } from '@/types.js'
import type { DecodedTx } from '@3loop/transaction-decoder'
import { assetsReceived, assetsSent } from './std.js'
import { assetsReceived, assetsSent, displayAsset, NULL_ADDRESS } from './std.js'

export function transformEvent(event: DecodedTx): InterpretedTransaction {
const methodName = event.methodCall.name

return {
type: 'unknown',
action: `Called method '${methodName}'`,
const newEvent: Omit<InterpretedTransaction, 'action' | 'type' | 'assetsSent' | 'assetsReceived'> = {
chain: event.chainID,
txHash: event.txHash,
user: { address: event.fromAddress, name: null },
method: methodName,
}

const transfers = event.transfers.filter((t) => t.from !== NULL_ADDRESS && t.to !== NULL_ADDRESS)

if (transfers.length === 1) {
const fromAddress = transfers[0].from
const assetSent = assetsSent(transfers, fromAddress)
return {
type: 'unknown',
action: `Sent ${displayAsset(assetSent[0])}`,
...newEvent,
assetsReceived: [],
assetsSent: assetSent,
}
}

return {
type: 'unknown',
action: `Called method '${methodName}'`,
...newEvent,
assetsSent: assetsSent(event.transfers, event.fromAddress),
assetsReceived: assetsReceived(event.transfers, event.fromAddress),
}
Expand Down
7 changes: 3 additions & 4 deletions packages/transaction-interpreter/interpreters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ function getInterpreter(tx: DecodedTx): string | undefined {
return `${standardLibrary}\n${interpretations[eventInterpreter]}`
}

//if there is a contract type mapping, return the contract type interpreter
const contractTypes = ['ERC20', 'ERC721', 'ERC1155']
if (contractTypes.includes(contractType)) {
// Check for contract type mapping and return the corresponding interpreter
if (contractType && contractTypeToName[contractType.toLowerCase()]) {
const typeId = contractTypeToName[contractType.toLowerCase()]
return `${standardLibrary} \n ${interpretations[typeId]}`
return `${standardLibrary}\n${interpretations[typeId]}`
}

return undefined
Expand Down
6 changes: 3 additions & 3 deletions packages/transaction-interpreter/interpreters/moxie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function transformEvent(event: DecodedTx): InterpretedTransaction {
if (eventType === 'buy' && isSwap) {
return {
type: 'swap',
action: `Bought ${formatNumber(received[0].amount)} shares of ${received[0].asset?.name} for ${displayAsset(
action: `Bought ${formatNumber(received[0].amount)} Fan Tokens of ${received[0].asset?.name} for ${displayAsset(
sent[0],
)}`,
...newEvent,
Expand All @@ -57,7 +57,7 @@ export function transformEvent(event: DecodedTx): InterpretedTransaction {
if (eventType === 'sell' && isSwap) {
return {
type: 'swap',
action: `Sold ${formatNumber(sent[0].amount)} shares of ${sent[0].asset?.name} for ${displayAsset(
action: `Sold ${formatNumber(sent[0].amount)} Fan Tokens of ${sent[0].asset?.name} for ${displayAsset(
received[0],
)}`,
...newEvent,
Expand All @@ -72,7 +72,7 @@ export function transformEvent(event: DecodedTx): InterpretedTransaction {

return {
type: 'burn',
action: `Burned ${displayAsset(sent[0])} for ${buyTokenMetadata?.contractName} fan holders`,
action: `Burned ${displayAsset(sent[0])} for ${buyTokenMetadata?.contractName} Fan Tokens holders`,
...newEvent,
assetsSent: sent,
assetsReceived: [],
Expand Down
43 changes: 43 additions & 0 deletions packages/transaction-interpreter/interpreters/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { assetsReceived, assetsSent, displayAsset, NULL_ADDRESS } from './std.js'
import type { InterpretedTransaction } from '@/types.js'
import type { DecodedTx } from '@3loop/transaction-decoder'

/**
* Fallback interpretator to display transfers.
*/
export function transformEvent(event: DecodedTx): InterpretedTransaction {
const methodName = event.methodCall.name

const newEvent: Omit<InterpretedTransaction, 'action' | 'type' | 'assetsSent' | 'assetsReceived'> = {
chain: event.chainID,
txHash: event.txHash,
user: { address: event.fromAddress, name: null },
method: methodName,
}

const transfers = event.transfers.filter((t) => t.from !== NULL_ADDRESS && t.to !== NULL_ADDRESS)

if (transfers.length === 1) {
const fromAddress = transfers[0].from
const assetSent = assetsSent(transfers, fromAddress)
return {
type: 'transfer-token',
action: `Sent ${displayAsset(assetSent[0])}`,
...newEvent,
assetsReceived: [],
assetsSent: assetSent,
}
}

// TODO: handle multiple transfers

return {
type: 'unknown',
action: `Called method '${methodName}'`,
...newEvent,
assetsReceived: assetsReceived(transfers, event.fromAddress),
assetsSent: assetsSent(transfers, event.fromAddress),
}
}

export const contractType = 'transfer'
6 changes: 4 additions & 2 deletions packages/transaction-interpreter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ interface Address {
address: string
name: string | null
}
type StringWithAutocompleteOptions<TOptions extends string> = (string & Record<string, never>) | TOptions

type TransactionType =
type TransactionType = StringWithAutocompleteOptions<
| 'repay-loan'
| 'deposit-collateral'
| 'borrow'
Expand All @@ -27,8 +28,9 @@ type TransactionType =
| 'account-abstraction'
| 'stake-token'
| 'unstake-token'
| 'burn'
| 'unknown'
| string
>

export interface Asset {
address: string
Expand Down

0 comments on commit 4204de7

Please sign in to comment.