-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Once-Upon/feat/split-cryptopunks
feat: split cryptopunks from old nfts
- Loading branch information
Showing
6 changed files
with
158 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/transformers/ethereum/assetTransfersCryptopunks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { transform as transactionAssetTransfers } from '../_common/assetTransfers'; | ||
import { transform } from './assetTransfersCryptopunks'; | ||
import { loadBlockFixture } from '../../helpers/utils'; | ||
|
||
describe('transactionAssetTransfersCryptopunks', () => { | ||
it('should return transaction asset transfers CryptoPunks', () => { | ||
// Special NFT transfers | ||
/** CryptoPunks New */ | ||
const cryptoPunksNewBlock = loadBlockFixture('ethereum', 5774644); | ||
const cryptoPunksNewAssetResult = | ||
transactionAssetTransfers(cryptoPunksNewBlock); | ||
const cryptoPunksNewResult = transform(cryptoPunksNewAssetResult); | ||
const cryptoPunksNewTx = cryptoPunksNewResult.transactions.find( | ||
(tx) => | ||
tx.hash === | ||
'0x0da4c50900119b47400d71a9dd3563571145e4e362b952c36a9e38c77f7d25bb', | ||
); | ||
expect(cryptoPunksNewTx).toBeDefined(); | ||
if (cryptoPunksNewTx) { | ||
const cryptoPunksNewTransfers = cryptoPunksNewTx.assetTransfers; | ||
expect(cryptoPunksNewTransfers[0].type).toBe('erc721'); | ||
if ('tokenId' in cryptoPunksNewTransfers[0]) { | ||
expect(cryptoPunksNewTransfers[0].tokenId).toBe('89'); | ||
} | ||
} | ||
/** CryptoPunks Old */ | ||
const cryptoPunksOldBlock = loadBlockFixture('ethereum', 3862484); | ||
const cryptoPunksOldAssetResult = | ||
transactionAssetTransfers(cryptoPunksOldBlock); | ||
const cryptoPunksOldResult = transform(cryptoPunksOldAssetResult); | ||
const cryptoPunksOldTx = cryptoPunksOldResult.transactions.find( | ||
(tx) => | ||
tx.hash === | ||
'0xff75a6739be926fe7328167011b5e2ac6a8883f55e76af70410520ef7b115901', | ||
); | ||
expect(cryptoPunksOldTx).toBeDefined(); | ||
if (cryptoPunksOldTx) { | ||
const cryptoPunksOldTransfers = cryptoPunksOldTx.assetTransfers; | ||
expect(cryptoPunksOldTransfers[0].type).toBe('erc721'); | ||
if ('tokenId' in cryptoPunksOldTransfers[0]) { | ||
expect(cryptoPunksOldTransfers[0].tokenId).toBe('4851'); | ||
} | ||
} | ||
}); | ||
|
||
it('should return asset transfers for CryptoPunks transactions', () => { | ||
const cryptoPunksBlock = loadBlockFixture('ethereum', '19321357_decoded'); | ||
const cryptoPunksAssetResult = transactionAssetTransfers(cryptoPunksBlock); | ||
const cryptoPunksResult = transform(cryptoPunksAssetResult); | ||
const cryptoPunksTx = cryptoPunksResult.transactions.find( | ||
(tx) => | ||
tx.hash === | ||
'0x4b581466cca3f2b50a6b97c053dd207feb911c6f858f21331ff829aa97dc6159', | ||
); | ||
expect(cryptoPunksTx).toBeDefined(); | ||
if (cryptoPunksTx) { | ||
const cryptoPunksTransfers = cryptoPunksTx.assetTransfers; | ||
expect(cryptoPunksTransfers.length).toBe(1); | ||
if ('tokenId' in cryptoPunksTransfers[0]) { | ||
expect(cryptoPunksTransfers[0].tokenId).toBe('7071'); | ||
expect(cryptoPunksTransfers[0].from).toBe( | ||
'0x4e6d2af4931681a024da8feaa4faba2bf8bbdc65', | ||
); | ||
expect(cryptoPunksTransfers[0].to).toBe( | ||
'0x1919db36ca2fa2e15f9000fd9cdc2edcf863e685', | ||
); | ||
} | ||
expect(cryptoPunksTransfers[0].type).toBe('erc721'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { decodeEVMAddress } from '../../helpers/utils'; | ||
import { | ||
AssetType, | ||
type AssetTransfer, | ||
type RawBlock, | ||
type RawTransaction, | ||
} from '../../types'; | ||
import { CRYPTO_PUNKS_ADDRESSES } from '../../helpers/constants'; | ||
|
||
const TRANSFER_SIGNATURES = { | ||
// event PunkTransfer(address indexed from, address indexed to, uint256 punkIndex) | ||
CRYPTO_PUNKS_ERC721: | ||
'0x05af636b70da6819000c49f85b21fa82081c632069bb626f30932034099107d8', | ||
// event PunkBought(uint indexed punkIndex, uint value, address indexed fromAddress, address indexed toAddress) | ||
CRYPTO_PUNKS_ERC721_BUY: | ||
'0x58e5d5a525e3b40bc15abaa38b5882678db1ee68befd2f60bafe3a7fd06db9e3', | ||
}; | ||
|
||
function updateTokenTransfers(tx: RawTransaction) { | ||
const cryptopunksTransfers: AssetTransfer[] = []; | ||
|
||
for (const log of tx.receipt.logs) { | ||
if (!CRYPTO_PUNKS_ADDRESSES.includes(log.address)) { | ||
continue; | ||
} | ||
|
||
const [signature] = log.topics; | ||
|
||
switch (signature) { | ||
case TRANSFER_SIGNATURES.CRYPTO_PUNKS_ERC721: | ||
cryptopunksTransfers.push({ | ||
asset: log.address, | ||
from: decodeEVMAddress(log.topics[1]), | ||
to: decodeEVMAddress(log.topics[2]), | ||
tokenId: BigInt(log.data).toString(), | ||
type: AssetType.ERC721, | ||
}); | ||
break; | ||
case TRANSFER_SIGNATURES.CRYPTO_PUNKS_ERC721_BUY: | ||
cryptopunksTransfers.push({ | ||
asset: log.address, | ||
from: decodeEVMAddress(log.topics[2]), | ||
to: decodeEVMAddress(log.topics[3]), | ||
tokenId: BigInt(log.topics[1]).toString(), | ||
type: AssetType.ERC721, | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
// filter old asset transfers from previous asset transfers | ||
const nonOldAssetTransfers = tx.assetTransfers.filter( | ||
(assetTransfer) => | ||
assetTransfer.type !== AssetType.ETH && | ||
!CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.asset), | ||
); | ||
const assetTransfers = [...nonOldAssetTransfers, ...cryptopunksTransfers]; | ||
|
||
return assetTransfers; | ||
} | ||
|
||
export function transform(block: RawBlock): RawBlock { | ||
block.transactions = block.transactions.map((tx) => { | ||
const hasCryptopunksTransfer = tx.assetTransfers?.some( | ||
(assetTransfer) => | ||
assetTransfer.type !== AssetType.ETH && | ||
CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.asset), | ||
); | ||
if (hasCryptopunksTransfer) { | ||
tx.assetTransfers = updateTokenTransfers(tx); | ||
} | ||
return tx; | ||
}); | ||
|
||
return block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters