-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ERC-1155 funnel primitive (#348)
* Add ERC1155Contract artifacts extracted from hardhat build * Add IInverseAppProjected1155 artifacts extracted from hardhat build * Import 1155 contract into paima-utils/contracts.ts * Add configuration and data types for 'erc1155-app' * Fix 1055 typo * Add ERC-1155 stuff to paima-sm * Add ERC-1155 stuff to paima-funnel * Read ERC-1155 TransferSingle and TransferBatch events into Transfer datums * Add value to mint event * npm run prettier * Remove leftover base ERC1155Contract files * Rename InverseAppProjected1155Transfer -> Erc1155Transfer, 'erc1155-app' to 'erc1155' * Add IERC1155Contract.json & .ts * Add Erc1155 base contract types * Make logic inside isPaimaErc721 reusable * Expose just ERC-1155 transfer datums, expand rather than flatten them * JSON-encode ids and values lists in scheduled data * Remove contractAddress from ERC-1155 transfer scheduled data * Add cde_erc1155_data and cde_erc1155_burn tables and queries * Actually update data + burn tables * Revert "Make logic inside isPaimaErc721 reusable" This reverts commit 471a81e. * Remove mint leftover from cdeTransitionFunction * Apply presync change from #339 * Remove remaining InverseAppProjected1155 types in favor of stock Erc1155 * Pre-lowercase addresses in ERC-1155 transfer scheduled event * Improve cde-config error logging * Remove unintended depositAddress config field * npm run prettier * Add query functions for ERC-1155 support * Make 'erc-1155' scheduledPrefix optional, add optional burnScheduledPrefix * Fix stale filename in contract-types import * Added get erc1155 token by id * Fixed return type * Fix /src import * Update loadAbi docs * Remove sketchy getERC1155TotalBalanceAllTokens helper --------- Co-authored-by: Edward Alvarado <[email protected]>
- Loading branch information
1 parent
9e11c04
commit add773c
Showing
19 changed files
with
1,346 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ChainDataExtensionDatumType, DEFAULT_FUNNEL_TIMEOUT, timeout } from '@paima/utils'; | ||
import type { | ||
CdeErc1155TransferDatum, | ||
ChainDataExtensionDatum, | ||
ChainDataExtensionErc1155, | ||
} from '@paima/sm'; | ||
import type { | ||
Erc1155TransferSingle as TransferSingle, | ||
Erc1155TransferBatch as TransferBatch, | ||
} from '@paima/utils'; | ||
|
||
export default async function getCdeErc1155Data( | ||
extension: ChainDataExtensionErc1155, | ||
fromBlock: number, | ||
toBlock: number, | ||
network: string | ||
): Promise<ChainDataExtensionDatum[]> { | ||
// TODO: typechain is missing the proper type generation for getPastEvents | ||
// https://github.com/dethcrypto/TypeChain/issues/767 | ||
const transferSingleEvents = (await timeout( | ||
extension.contract.getPastEvents('TransferSingle', { | ||
fromBlock, | ||
toBlock, | ||
}), | ||
DEFAULT_FUNNEL_TIMEOUT | ||
)) as unknown as TransferSingle[]; | ||
const transferBatchEvents = (await timeout( | ||
extension.contract.getPastEvents('TransferBatch', { | ||
fromBlock, | ||
toBlock, | ||
}), | ||
DEFAULT_FUNNEL_TIMEOUT | ||
)) as unknown as TransferBatch[]; | ||
|
||
return [ | ||
...transferSingleEvents.map(e => transferSingleToDatum(e, extension, network)), | ||
...transferBatchEvents.map(e => transferBatchToDatum(e, extension, network)), | ||
]; | ||
} | ||
|
||
function transferSingleToDatum( | ||
event: TransferSingle, | ||
extension: ChainDataExtensionErc1155, | ||
network: string | ||
): CdeErc1155TransferDatum { | ||
return { | ||
cdeId: extension.cdeId, | ||
cdeDatumType: ChainDataExtensionDatumType.Erc1155Transfer, | ||
blockNumber: event.blockNumber, | ||
payload: { | ||
operator: event.returnValues.operator, | ||
from: event.returnValues.from, | ||
to: event.returnValues.to, | ||
// single->array conversion here | ||
ids: [event.returnValues.id], | ||
values: [event.returnValues.value], | ||
}, | ||
contractAddress: extension.contractAddress, | ||
scheduledPrefix: extension.scheduledPrefix, | ||
burnScheduledPrefix: extension.burnScheduledPrefix, | ||
network, | ||
}; | ||
} | ||
|
||
function transferBatchToDatum( | ||
event: TransferBatch, | ||
extension: ChainDataExtensionErc1155, | ||
network: string | ||
): CdeErc1155TransferDatum { | ||
return { | ||
cdeId: extension.cdeId, | ||
cdeDatumType: ChainDataExtensionDatumType.Erc1155Transfer, | ||
blockNumber: event.blockNumber, | ||
payload: { | ||
operator: event.returnValues.operator, | ||
from: event.returnValues.from, | ||
to: event.returnValues.to, | ||
ids: event.returnValues.ids, | ||
values: event.returnValues.values, | ||
}, | ||
contractAddress: extension.contractAddress, | ||
scheduledPrefix: extension.scheduledPrefix, | ||
burnScheduledPrefix: extension.burnScheduledPrefix, | ||
network, | ||
}; | ||
} |
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
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,106 @@ | ||
import { ENV } from '@paima/utils'; | ||
import type { CdeErc1155TransferDatum } from './types.js'; | ||
import { | ||
cdeErc1155ModifyBalance, | ||
cdeErc1155DeleteIfZero, | ||
cdeErc1155Burn, | ||
createScheduledData, | ||
} from '@paima/db'; | ||
import type { | ||
ICdeErc1155BurnParams, | ||
ICdeErc1155DeleteIfZeroParams, | ||
ICdeErc1155ModifyBalanceParams, | ||
SQLUpdate, | ||
} from '@paima/db'; | ||
|
||
export default async function processErc1155TransferDatum( | ||
cdeDatum: CdeErc1155TransferDatum, | ||
inPresync: boolean | ||
): Promise<SQLUpdate[]> { | ||
const { cdeId, scheduledPrefix, burnScheduledPrefix, payload, blockNumber } = cdeDatum; | ||
const { operator, from, to, ids, values } = payload; | ||
const isMint = from == '0x0000000000000000000000000000000000000000'; | ||
const isBurn = /^0x0+(dead)?$/i.test(to); | ||
|
||
const updateList: SQLUpdate[] = []; | ||
|
||
// Always schedule the plain old transfer event. | ||
const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : blockNumber; | ||
if (scheduledPrefix) { | ||
const scheduledInputData = [ | ||
scheduledPrefix, | ||
operator, | ||
from.toLowerCase(), | ||
to.toLowerCase(), | ||
JSON.stringify(ids), | ||
JSON.stringify(values), | ||
].join('|'); | ||
updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); | ||
} | ||
|
||
if (isBurn && burnScheduledPrefix) { | ||
const burnData = [ | ||
burnScheduledPrefix, | ||
operator, | ||
from.toLowerCase(), | ||
// to is excluded because it's presumed 0 | ||
JSON.stringify(ids), | ||
JSON.stringify(values), | ||
].join('|'); | ||
updateList.push(createScheduledData(burnData, scheduledBlockHeight)); | ||
} | ||
|
||
// Update balance + burn tables. | ||
for (let i = 0; i < ids.length; ++i) { | ||
let token_id = ids[i]; | ||
let value = BigInt(values[i]); | ||
|
||
if (!isMint) { | ||
// if not a mint, reduce sender's balance | ||
updateList.push([ | ||
cdeErc1155ModifyBalance, | ||
{ | ||
cde_id: cdeId, | ||
token_id, | ||
wallet_address: from.toLowerCase(), | ||
value: (-value).toString(), | ||
} satisfies ICdeErc1155ModifyBalanceParams, | ||
]); | ||
// And if it's zero, remove the row to keep table size down | ||
updateList.push([ | ||
cdeErc1155DeleteIfZero, | ||
{ | ||
cde_id: cdeId, | ||
token_id, | ||
wallet_address: from.toLowerCase(), | ||
} satisfies ICdeErc1155DeleteIfZeroParams, | ||
]); | ||
} | ||
|
||
if (!isBurn) { | ||
// if not a burn, increase recipient's balance | ||
updateList.push([ | ||
cdeErc1155ModifyBalance, | ||
{ | ||
cde_id: cdeId, | ||
token_id, | ||
wallet_address: to.toLowerCase(), | ||
value: value.toString(), | ||
} satisfies ICdeErc1155ModifyBalanceParams, | ||
]); | ||
} else { | ||
// if a burn, increase sender's burn record | ||
updateList.push([ | ||
cdeErc1155Burn, | ||
{ | ||
cde_id: cdeId, | ||
token_id, | ||
wallet_address: from.toLowerCase(), | ||
value: value.toString(), | ||
} satisfies ICdeErc1155BurnParams, | ||
]); | ||
} | ||
} | ||
|
||
return updateList; | ||
} |
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
Oops, something went wrong.