From a80bd182851462ca914b84175ce49c758e921364 Mon Sep 17 00:00:00 2001 From: denhampreen Date: Thu, 17 Aug 2023 17:55:50 +0200 Subject: [PATCH] feat: add which standard the token is --- generated/persisted_state.envio.json | 2 +- schema.graphql | 1 + src/EventHandlers.bs.js | 58 +++++++++++++++++++--------- src/EventHandlers.res | 30 +++++++++----- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/generated/persisted_state.envio.json b/generated/persisted_state.envio.json index 6be2144..8987088 100644 --- a/generated/persisted_state.envio.json +++ b/generated/persisted_state.envio.json @@ -1 +1 @@ -{"has_run_db_migrations":true,"config_hash":"b2670a66fe3ea530f9e04003e27dce31c59b4c31926bb50a1a191edcac6bbe8c","schema_hash":"43ff9276960cca0460cf430db51c8da2bb6cfc4be3b06716e73af6f6dcf7ebc3","handler_files_hash":"2bf798587ecddbb0bcdeb806defe1d730c5c92515784332a1cd84b4761c58f38","abi_files_hash":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"} \ No newline at end of file +{"has_run_db_migrations":true,"config_hash":"74b7df50a76dc93cade163b0a25b19736c61e0c7bd6202531a5f6535cae9dfed","schema_hash":"46ae8352a6ad9458d29e50ca072865a6d52601b9859d7d41d2af5d2223410ef2","handler_files_hash":"46253e4b5d63fef38796cd057ab70b06b5eb248d9991252bfe11ab54d7a19fbe","abi_files_hash":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"} \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index 1d192d4..18c8c10 100644 --- a/schema.graphql +++ b/schema.graphql @@ -20,6 +20,7 @@ type Token @entity { owner: User! value: BigInt! # This is mostly relevant to ERC1155 tokens where each token has a value or 'number' metadata: Metadata @derivedFrom(field: "token_id") + standard: String! } type Metadata @entity { diff --git a/src/EventHandlers.bs.js b/src/EventHandlers.bs.js index b0156c0..a8d2e5b 100644 --- a/src/EventHandlers.bs.js +++ b/src/EventHandlers.bs.js @@ -4,7 +4,6 @@ var Curry = require("rescript/lib/js/curry.js"); var Ethers = require("generated/src/bindings/Ethers.bs.js"); var Ethers$1 = require("ethers"); -var Caml_obj = require("rescript/lib/js/caml_obj.js"); var Handlers = require("generated/src/Handlers.bs.js"); var Belt_Array = require("rescript/lib/js/belt_Array.js"); var Belt_Option = require("rescript/lib/js/belt_Option.js"); @@ -27,22 +26,31 @@ Curry._1(Handlers.ERC721Contract.Transfer.handler, (function ($$event, context) tokenId: token_tokenId, collection: token_collection, owner: token_owner, - value: token_value + value: token_value, + standard: "ERC721" }; var nftCollectionUpdated = Curry._1(context.nftcollection.nftCollectionUpdated, undefined); if (nftCollectionUpdated !== undefined) { var optExistingToken = Curry._1(context.token.existingTransferredToken, undefined); if (Belt_Option.isNone(optExistingToken)) { var currentSupply = nftCollectionUpdated.currentSupply + 1 | 0; - var newrecord = Caml_obj.obj_dup(nftCollectionUpdated); - newrecord.currentSupply = currentSupply; - Curry._1(context.nftcollection.set, newrecord); + Curry._1(context.nftcollection.set, { + id: nftCollectionUpdated.id, + contractAddress: nftCollectionUpdated.contractAddress, + name: nftCollectionUpdated.name, + symbol: nftCollectionUpdated.symbol, + maxSupply: nftCollectionUpdated.maxSupply, + currentSupply: currentSupply + }); } } else { Curry._1(context.nftcollection.set, { id: Ethers.ethAddressToString($$event.srcAddress), contractAddress: Ethers.ethAddressToString($$event.srcAddress), + name: undefined, + symbol: undefined, + maxSupply: undefined, currentSupply: 1 }); } @@ -77,22 +85,31 @@ Curry._1(Handlers.ERC1155Contract.TransferSingle.handler, (function ($$event, co tokenId: token_tokenId, collection: token_collection, owner: token_owner, - value: token_value + value: token_value, + standard: "ERC1155" }; var nftCollectionUpdated = Curry._1(context.nftcollection.nftCollectionUpdated, undefined); if (nftCollectionUpdated !== undefined) { var optExistingToken = Curry._1(context.token.existingTransferredToken, undefined); if (Belt_Option.isNone(optExistingToken)) { var currentSupply = nftCollectionUpdated.currentSupply + 1 | 0; - var newrecord = Caml_obj.obj_dup(nftCollectionUpdated); - newrecord.currentSupply = currentSupply; - Curry._1(context.nftcollection.set, newrecord); + Curry._1(context.nftcollection.set, { + id: nftCollectionUpdated.id, + contractAddress: nftCollectionUpdated.contractAddress, + name: nftCollectionUpdated.name, + symbol: nftCollectionUpdated.symbol, + maxSupply: nftCollectionUpdated.maxSupply, + currentSupply: currentSupply + }); } } else { Curry._1(context.nftcollection.set, { id: Ethers.ethAddressToString($$event.srcAddress), contractAddress: Ethers.ethAddressToString($$event.srcAddress), + name: undefined, + symbol: undefined, + maxSupply: undefined, currentSupply: 1 }); } @@ -113,42 +130,47 @@ Curry._1(Handlers.ERC1155Contract.TransferSingle.handler, (function ($$event, co Curry._1(Handlers.ERC1155Contract.TransferBatch.loader, (function ($$event, context) { Curry._1(context.nftcollection.nftCollectionUpdatedLoad, Ethers.ethAddressToString($$event.srcAddress)); - console.log("event.params.ids"); - console.log($$event.params.ids); Belt_Array.forEach($$event.params.ids, (function (id) { Curry._2(context.token.existingTransferredTokenLoad, Ethers.ethAddressToString($$event.srcAddress) + id.toString(), {}); })); })); Curry._1(Handlers.ERC1155Contract.TransferBatch.handler, (function ($$event, context) { - console.log("in handler event.params.ids"); - console.log($$event.params.ids); Belt_Array.mapWithIndex($$event.params.ids, (function (index, id) { var token_id = "" + Ethers.ethAddressToString($$event.srcAddress) + "-" + id.toString() + ""; var token_collection = Ethers.ethAddressToString($$event.srcAddress); var token_owner = Ethers.ethAddressToString($$event.params.to); - var token_value = Belt_Option.getWithDefault(Belt_Array.get($$event.params[4], index), BigInt(1)); + var token_value = Belt_Option.getWithDefault(Belt_Array.get($$event.params.values, index), BigInt(1)); var token = { id: token_id, tokenId: id, collection: token_collection, owner: token_owner, - value: token_value + value: token_value, + standard: "ERC1155" }; var nftCollectionUpdated = Curry._1(context.nftcollection.nftCollectionUpdated, undefined); if (nftCollectionUpdated !== undefined) { var optExistingToken = Curry._1(context.token.existingTransferredToken, undefined); if (Belt_Option.isNone(optExistingToken)) { var currentSupply = nftCollectionUpdated.currentSupply + 1 | 0; - var newrecord = Caml_obj.obj_dup(nftCollectionUpdated); - newrecord.currentSupply = currentSupply; - Curry._1(context.nftcollection.set, newrecord); + Curry._1(context.nftcollection.set, { + id: nftCollectionUpdated.id, + contractAddress: nftCollectionUpdated.contractAddress, + name: nftCollectionUpdated.name, + symbol: nftCollectionUpdated.symbol, + maxSupply: nftCollectionUpdated.maxSupply, + currentSupply: currentSupply + }); } } else { Curry._1(context.nftcollection.set, { id: Ethers.ethAddressToString($$event.srcAddress), contractAddress: Ethers.ethAddressToString($$event.srcAddress), + name: undefined, + symbol: undefined, + maxSupply: undefined, currentSupply: 1 }); } diff --git a/src/EventHandlers.res b/src/EventHandlers.res index d76bfa1..8c948b5 100644 --- a/src/EventHandlers.res +++ b/src/EventHandlers.res @@ -17,6 +17,7 @@ Handlers.ERC721Contract.Transfer.handler((~event, ~context) => { collection: event.srcAddress->Ethers.ethAddressToString, owner: event.params.to->Ethers.ethAddressToString, value: 1->Ethers.BigInt.fromInt, + standard: "ERC721", } switch context.nftcollection.nftCollectionUpdated() { @@ -42,6 +43,9 @@ Handlers.ERC721Contract.Transfer.handler((~event, ~context) => { contractAddress: event.srcAddress->Ethers.ethAddressToString, //First NFT is being created so current suplly is 1 currentSupply: 1, + name: None, + symbol: None, + maxSupply: None, } context.nftcollection.set(newNftCollection) @@ -81,6 +85,7 @@ Handlers.ERC1155Contract.TransferSingle.handler((~event, ~context) => { collection: event.srcAddress->Ethers.ethAddressToString, owner: event.params.to->Ethers.ethAddressToString, value: event.params.value, + standard: "ERC1155", } switch context.nftcollection.nftCollectionUpdated() { @@ -106,6 +111,9 @@ Handlers.ERC1155Contract.TransferSingle.handler((~event, ~context) => { contractAddress: event.srcAddress->Ethers.ethAddressToString, //First NFT is being created so current suplly is 1 currentSupply: 1, + name: None, + symbol: None, + maxSupply: None, } context.nftcollection.set(newNftCollection) @@ -133,24 +141,25 @@ Handlers.ERC1155Contract.TransferSingle.handler((~event, ~context) => { Handlers.ERC1155Contract.TransferBatch.loader((~event, ~context) => { context.nftcollection.nftCollectionUpdatedLoad(event.srcAddress->Ethers.ethAddressToString) - event.params.ids->Belt.Array.forEach(id => { - context.token.existingTransferredTokenLoad( - event.srcAddress->Ethers.ethAddressToString ++ id->Ethers.BigInt.toString, - ~loaders={}, - ) - }) + event.params.ids->Belt.Array.forEach(id => { + context.token.existingTransferredTokenLoad( + event.srcAddress->Ethers.ethAddressToString ++ id->Ethers.BigInt.toString, + ~loaders={}, + ) + }) }) Handlers.ERC1155Contract.TransferBatch.handler((~event, ~context) => { - Js.log("in handler event.params.ids") - Js.log(event.params.ids) let _ = event.params.ids->Belt.Array.mapWithIndex((index, id) => { let token = { id: `${event.srcAddress->Ethers.ethAddressToString}-${id->Ethers.BigInt.toString}`, tokenId: id, collection: event.srcAddress->Ethers.ethAddressToString, owner: event.params.to->Ethers.ethAddressToString, - value: event.params.values->Belt.Array.get(index)->Belt.Option.getWithDefault(1->Ethers.BigInt.fromInt), + value: event.params.values + ->Belt.Array.get(index) + ->Belt.Option.getWithDefault(1->Ethers.BigInt.fromInt), + standard: "ERC1155", } switch context.nftcollection.nftCollectionUpdated() { @@ -176,6 +185,9 @@ Handlers.ERC1155Contract.TransferBatch.handler((~event, ~context) => { contractAddress: event.srcAddress->Ethers.ethAddressToString, //First NFT is being created so current suplly is 1 currentSupply: 1, + name: None, + symbol: None, + maxSupply: None, } context.nftcollection.set(newNftCollection)