Created
December 13, 2021 11:17
-
-
Save elmariachi111/4f339381320abc8944c0cb84806cd54b to your computer and use it in GitHub Desktop.
subgraph mapping for Splice mints
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
import { ERC721 as ERC721Contract } from '../generated/Splice/ERC721'; | |
import { Bytes, ByteArray, ethereum } from '@graphprotocol/graph-ts'; | |
import { Splice, Style } from '../generated/schema'; | |
import { Minted, Splice as SpliceContract } from '../generated/Splice/Splice'; | |
export function handleMinted(event: Minted): void { | |
const tokenId = event.params.token_id.toString(); | |
const splice = new Splice(tokenId); | |
const spliceContract = SpliceContract.bind(event.address); | |
splice.metadata_url = spliceContract.tokenURI(event.params.token_id); | |
splice.owner = event.transaction.from; | |
splice.origin_hash = event.params.origin_hash; | |
splice.minting_fee = event.transaction.value; | |
//1st 4 byte = method keccak hash fragment | |
const functionInput = event.transaction.input.subarray(4); | |
//prepend a "tuple" prefix (function params are arrays, not tuples) | |
const tuplePrefix = ByteArray.fromHexString( | |
'0x0000000000000000000000000000000000000000000000000000000000000020' | |
); | |
const functionInputAsTuple = new Uint8Array( | |
tuplePrefix.length + functionInput.length | |
); | |
functionInputAsTuple.set(tuplePrefix, 0); | |
functionInputAsTuple.set(functionInput, tuplePrefix.length); | |
const tupleInputBytes = Bytes.fromUint8Array(functionInputAsTuple); | |
const decoded = ethereum.decode( | |
'(address[],uint[],uint,bytes32[],bytes)', | |
tupleInputBytes | |
); | |
const t = decoded.toTuple(); | |
const originAddresses = t[0].toAddressArray(); | |
const originTokenIds = t[1].toBigIntArray(); | |
splice.origin_collection = originAddresses[0]; | |
splice.origin_token_id = originTokenIds[0]; | |
const originContract = ERC721Contract.bind(originAddresses[0]); | |
splice.origin_metadata_url = originContract.tokenURI(originTokenIds[0]); | |
const style_id = event.params.style_token_id.toString(); | |
splice.style = style_id; | |
const style = Style.load(style_id); | |
if (style) { | |
style.minted = style.minted + 1; | |
style.save(); | |
} | |
splice.save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment