|
| 1 | +package alchemy |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 9 | + "github.com/ethereum/go-ethereum/rpc" |
| 10 | + |
| 11 | + ac "github.com/status-im/status-go/services/wallet/activity/common" |
| 12 | + "github.com/status-im/status-go/services/wallet/bigint" |
| 13 | + wCommon "github.com/status-im/status-go/services/wallet/common" |
| 14 | + "github.com/status-im/status-go/services/wallet/thirdparty" |
| 15 | +) |
| 16 | + |
| 17 | +const getAssetTransfersMethod = "alchemy_getAssetTransfers" |
| 18 | +const MaxAssetTransfersCount = 1000 |
| 19 | + |
| 20 | +type TransferCategory string |
| 21 | + |
| 22 | +const ( |
| 23 | + TransferCategoryExternal TransferCategory = "external" |
| 24 | + TransferCategoryInternal TransferCategory = "internal" |
| 25 | + TransferCategoryErc20 TransferCategory = "erc20" |
| 26 | + TransferCategoryErc721 TransferCategory = "erc721" |
| 27 | + TransferCategoryErc1155 TransferCategory = "erc1155" |
| 28 | + TransferCategorySpecialNft TransferCategory = "specialnft" |
| 29 | +) |
| 30 | + |
| 31 | +type TransferOrder string |
| 32 | + |
| 33 | +const ( |
| 34 | + TransferOrderOldToNew TransferOrder = "asc" |
| 35 | + TransferOrderNewToOld TransferOrder = "desc" |
| 36 | +) |
| 37 | + |
| 38 | +type GetAssetTransfersParams struct { |
| 39 | + FromBlock *rpc.BlockNumber `json:"fromBlock"` |
| 40 | + ToBlock *rpc.BlockNumber `json:"toBlock"` |
| 41 | + FromAddress *common.Address `json:"fromAddress,omitempty"` |
| 42 | + ToAddress *common.Address `json:"toAddress,omitempty"` |
| 43 | + ContractAddresses []common.Address `json:"contractAddresses,omitempty"` |
| 44 | + Category []TransferCategory `json:"category"` |
| 45 | + Order TransferOrder `json:"order"` |
| 46 | + WithMetadata bool `json:"withMetadata"` |
| 47 | + ExcludeZeroValue bool `json:"excludeZeroValue"` |
| 48 | + MaxCount *hexutil.Big `json:"maxCount"` |
| 49 | + PageKey string `json:"pageKey,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +type GetAssetTranfersResponse struct { |
| 53 | + Transfers []Transfer `json:"transfers"` |
| 54 | + PageKey string `json:"pageKey"` |
| 55 | +} |
| 56 | + |
| 57 | +type transferData struct { |
| 58 | + Token ac.Token |
| 59 | + Value *hexutil.Big |
| 60 | +} |
| 61 | + |
| 62 | +func transfersToCommon(tt []Transfer, isIncoming bool, chainID uint64) []thirdparty.ActivityEntry { |
| 63 | + entries := make([]thirdparty.ActivityEntry, 0, len(tt)) |
| 64 | + |
| 65 | + cChainID := wCommon.ChainID(chainID) |
| 66 | + for _, t := range tt { |
| 67 | + if t.ToAddress == nil { |
| 68 | + entry := thirdparty.ActivityEntry{ |
| 69 | + ActivityType: ac.ContractDeploymentAT, |
| 70 | + ChainIDOut: &chainID, |
| 71 | + Timestamp: t.Metadata.BlockTimestamp.Unix(), |
| 72 | + Sender: &t.FromAddress, |
| 73 | + Recipient: t.ToAddress, |
| 74 | + TxHash: t.Hash, |
| 75 | + BlockNumber: (*hexutil.Big)(t.BlockNum.Int), |
| 76 | + } |
| 77 | + // Alchemy doesn't provide the contract address for contract deployment |
| 78 | + entries = append(entries, entry) |
| 79 | + } else { |
| 80 | + transfers := make([]transferData, 0, 1) |
| 81 | + switch t.Category { |
| 82 | + case TransferCategoryErc20: |
| 83 | + transfers = append(transfers, transferData{ |
| 84 | + Token: ac.Token{ |
| 85 | + ChainID: cChainID, |
| 86 | + TokenType: ac.Erc20, |
| 87 | + Address: *t.RawContract.Address, |
| 88 | + }, |
| 89 | + Value: (*hexutil.Big)(t.RawContract.Value.Int), |
| 90 | + }) |
| 91 | + case TransferCategoryErc721, TransferCategorySpecialNft: |
| 92 | + transfers = append(transfers, transferData{ |
| 93 | + Token: ac.Token{ |
| 94 | + ChainID: cChainID, |
| 95 | + TokenType: ac.Erc721, |
| 96 | + Address: *t.RawContract.Address, |
| 97 | + TokenID: (*hexutil.Big)(t.TokenID.Int), |
| 98 | + }, |
| 99 | + Value: (*hexutil.Big)(big.NewInt(1)), |
| 100 | + }) |
| 101 | + case TransferCategoryErc1155: |
| 102 | + for _, m := range t.Erc1155Metadata { |
| 103 | + transfers = append(transfers, transferData{ |
| 104 | + Token: ac.Token{ |
| 105 | + ChainID: cChainID, |
| 106 | + TokenType: ac.Erc1155, |
| 107 | + Address: *t.RawContract.Address, |
| 108 | + TokenID: (*hexutil.Big)(t.TokenID.Int), |
| 109 | + }, |
| 110 | + Value: (*hexutil.Big)(m.Value.Int), |
| 111 | + }) |
| 112 | + } |
| 113 | + default: |
| 114 | + transfers = append(transfers, transferData{ |
| 115 | + Token: ac.Token{ |
| 116 | + ChainID: cChainID, |
| 117 | + TokenType: ac.Native, |
| 118 | + }, |
| 119 | + Value: (*hexutil.Big)(t.RawContract.Value.Int), |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + for _, transfer := range transfers { |
| 124 | + entry := thirdparty.ActivityEntry{ |
| 125 | + Timestamp: t.Metadata.BlockTimestamp.Unix(), |
| 126 | + Sender: &t.FromAddress, |
| 127 | + Recipient: t.ToAddress, |
| 128 | + TxHash: t.Hash, |
| 129 | + BlockNumber: (*hexutil.Big)(t.BlockNum.Int), |
| 130 | + } |
| 131 | + if isIncoming { |
| 132 | + entry.ActivityType = ac.ReceiveAT |
| 133 | + entry.ChainIDIn = &chainID |
| 134 | + entry.TokenIn = &transfer.Token |
| 135 | + entry.AmountIn = transfer.Value |
| 136 | + } else { |
| 137 | + entry.ActivityType = ac.SendAT |
| 138 | + entry.ChainIDOut = &chainID |
| 139 | + entry.TokenOut = &transfer.Token |
| 140 | + entry.AmountOut = transfer.Value |
| 141 | + } |
| 142 | + entries = append(entries, entry) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return entries |
| 147 | +} |
| 148 | + |
| 149 | +type Transfer struct { |
| 150 | + Category TransferCategory `json:"category"` |
| 151 | + BlockNum *bigint.VarHexBigInt `json:"blockNum"` |
| 152 | + FromAddress common.Address `json:"from"` |
| 153 | + ToAddress *common.Address `json:"to,omitempty"` |
| 154 | + Erc1155Metadata []Erc1155Metadata `json:"erc1155Metadata,omitempty"` |
| 155 | + TokenID *bigint.VarHexBigInt `json:"tokenId"` |
| 156 | + Asset string `json:"asset"` |
| 157 | + UniqueID string `json:"uniqueId"` |
| 158 | + Hash common.Hash `json:"hash"` |
| 159 | + RawContract RawContract `json:"rawContract"` |
| 160 | + Metadata Metadata `json:"metadata"` |
| 161 | +} |
| 162 | + |
| 163 | +type Erc1155Metadata struct { |
| 164 | + TokenID *bigint.VarHexBigInt `json:"tokenId"` |
| 165 | + Value *bigint.VarHexBigInt `json:"value"` |
| 166 | +} |
| 167 | + |
| 168 | +type RawContract struct { |
| 169 | + Value *bigint.VarHexBigInt `json:"value"` // nil if ERC721 or ERC1155 transfer |
| 170 | + Address *common.Address `json:"address"` // nil if external or internal transfer |
| 171 | + Decimal *bigint.VarHexBigInt `json:"decimal"` // nil if not available in the contract |
| 172 | +} |
| 173 | + |
| 174 | +type Metadata struct { |
| 175 | + BlockTimestamp time.Time `json:"blockTimestamp"` |
| 176 | +} |
0 commit comments