From e3099859b580536758a0921b160df8dae516f0c5 Mon Sep 17 00:00:00 2001
From: rrozek <jakubrojek@gmail.com>
Date: Fri, 16 Aug 2024 09:55:57 +0200
Subject: [PATCH] fix types mapping for evm

---
 .github/workflows/build-and-push-ecr-dev.yml  |  2 +-
 .github/workflows/build-and-push-ecr-prod.yml |  2 +-
 .../dapp-analytics.controller.ts              | 11 +++---
 .../dapp-analytics/helpers/utils.ts           | 11 ++----
 ...rate-types.mapping.ts => types.mapping.ts} | 35 +++++++++++++++----
 5 files changed, 39 insertions(+), 22 deletions(-)
 rename src/components/dapp-analytics/{substrate-types.mapping.ts => types.mapping.ts} (61%)

diff --git a/.github/workflows/build-and-push-ecr-dev.yml b/.github/workflows/build-and-push-ecr-dev.yml
index eef0cbf..7a497f9 100644
--- a/.github/workflows/build-and-push-ecr-dev.yml
+++ b/.github/workflows/build-and-push-ecr-dev.yml
@@ -19,7 +19,7 @@ jobs:
   #     - name: Check out code
   #       uses: actions/checkout@v3
   #     - name: Run tests
-  #       run: docker compose -f docker-compose.test.yml run server npm test
+  #       run: docker compose -f docker-compose-test.yml run server npm test
 
   build-push:
     name: Build image and push to ECR(tokenguard-dev)
diff --git a/.github/workflows/build-and-push-ecr-prod.yml b/.github/workflows/build-and-push-ecr-prod.yml
index b7fbf63..0a319f2 100644
--- a/.github/workflows/build-and-push-ecr-prod.yml
+++ b/.github/workflows/build-and-push-ecr-prod.yml
@@ -19,7 +19,7 @@ jobs:
   #     - name: Check out code
   #       uses: actions/checkout@v3
   #     - name: Run tests
-  #       run: docker compose -f docker-compose.test.yml run server npm test
+  #       run: docker compose -f docker-compose-test.yml run server npm test
 
   build-push:
     name: Build and push to ECR(tokenguard-prod)
diff --git a/src/components/dapp-analytics/dapp-analytics.controller.ts b/src/components/dapp-analytics/dapp-analytics.controller.ts
index 95f00c6..e038194 100644
--- a/src/components/dapp-analytics/dapp-analytics.controller.ts
+++ b/src/components/dapp-analytics/dapp-analytics.controller.ts
@@ -18,7 +18,7 @@ import {
   IEvmEventAbiItem,
   IEvmFunctionAbiItem,
 } from './abi.interface';
-import { resolveType } from './substrate-types.mapping';
+import { resolveInkType, resolveEvmType } from './types.mapping';
 import { docker, k8sApi } from 'server';
 import { getCurrentBlock } from '@components/node-api/chainstate';
 import { convertAndFormatNumbers } from './helpers/formatting';
@@ -660,7 +660,7 @@ const extractInkAbiEvents = function (
       name: event.label,
       args: event.args.map((arg: IInkAbiArg) => ({
         name: arg.label,
-        type: resolveType(arg.type.type, contractAbi.types),
+        type: resolveInkType(arg.type.type, contractAbi.types),
       })),
     }),
   );
@@ -676,7 +676,7 @@ const extractEvmAbiEvents = function (
       name: event.name!,
       args: event.inputs!.map((input) => ({
         name: input.name,
-        type: input.type,
+        type: resolveEvmType(input.type),
       })),
     }));
 
@@ -694,7 +694,7 @@ const extractInkAbiFunctions = function (
       selector: message.selector,
       args: message.args.map((arg: IInkAbiArg) => ({
         name: arg.label,
-        type: resolveType(arg.type.type, contractAbi.types),
+        type: resolveInkType(arg.type.type, contractAbi.types),
       })),
     }));
   return calls;
@@ -715,7 +715,7 @@ const extractEvmAbiFunctions = function (
       selector: '0x00000000', // jrojek TODO: calculate selector
       args: func.inputs!.map((input) => ({
         name: input.name,
-        type: input.type,
+        type: resolveEvmType(input.type),
       })),
     }));
 
@@ -736,7 +736,6 @@ export const getDappAbiEvents = async (
       let dappEventsOutput: IAbiEventsOutput = { contracts: [] };
 
       for (const contract of dapp.abis) {
-        logger.debug(`Contract ABI: ${JSON.stringify(contract.abi)}`);
         if (isSubstrateAbi(contract.abi)) {
           const dAppContract: IAbiEventsOutputContract = {
             name: contract.name,
diff --git a/src/components/dapp-analytics/helpers/utils.ts b/src/components/dapp-analytics/helpers/utils.ts
index 85351c8..88dd466 100644
--- a/src/components/dapp-analytics/helpers/utils.ts
+++ b/src/components/dapp-analytics/helpers/utils.ts
@@ -11,20 +11,15 @@ const isSubstrateAbi = (abi: any): boolean => {
 };
 
 const isEvmAbi = (abi: any): boolean => {
-  logger.debug(`isEvmAbi abi: ${JSON.stringify(abi)}`);
-  abi.forEach((item) => {
-    logger.debug(`isEvmAbi abi item.type: ${JSON.stringify(item.type)}`);
-  });
-  const isEvmAbi =
+  return (
     abi &&
     Array.isArray(abi) &&
     abi.every((item) =>
       ['function', 'event', 'constructor', 'fallback', 'receive'].includes(
         item.type,
       ),
-    );
-  logger.debug(`isEvmAbi isEvmAbi: ${isEvmAbi}`);
-  return isEvmAbi;
+    )
+  );
 };
 
 export { isSubstrateAbi, isEvmAbi };
diff --git a/src/components/dapp-analytics/substrate-types.mapping.ts b/src/components/dapp-analytics/types.mapping.ts
similarity index 61%
rename from src/components/dapp-analytics/substrate-types.mapping.ts
rename to src/components/dapp-analytics/types.mapping.ts
index dc3bcf2..c55c1a0 100644
--- a/src/components/dapp-analytics/substrate-types.mapping.ts
+++ b/src/components/dapp-analytics/types.mapping.ts
@@ -9,13 +9,13 @@ const substrateToPostgresTypeMap = {
   i32: 'integer',
   i64: 'integer',
   i128: 'integer',
-  f32: 'float',
-  f64: 'float',
+  f32: 'number',
+  f64: 'number',
   str: 'string',
   bool: 'boolean',
 };
 
-function resolveType(typeId, types) {
+function resolveInkType(typeId, types) {
   const typeDef = types.find((t) => t.id === typeId);
   if (!typeDef) {
     throw new Error(`Type ID ${typeId} not found in ABI.`);
@@ -29,7 +29,7 @@ function resolveType(typeId, types) {
   // Handle other types (e.g., composite, array, sequence, tuple, variant) if needed
   const typeDefArray = typeDef.type.def.array;
   if (typeDefArray) {
-    return resolveType(typeDefArray.type, types); // Simplified: assume the array elements have the same type
+    return resolveInkType(typeDefArray.type, types); // Simplified: assume the array elements have the same type
   }
 
   const typeDefComposite = typeDef.type.def.composite;
@@ -39,7 +39,7 @@ function resolveType(typeId, types) {
 
   const typeDefSequence = typeDef.type.def.sequence;
   if (typeDefSequence) {
-    return resolveType(typeDefSequence.type, types); // Simplified: assume sequence elements have the same type
+    return resolveInkType(typeDefSequence.type, types); // Simplified: assume sequence elements have the same type
   }
 
   const typeDefTuple = typeDef.type.def.tuple;
@@ -55,4 +55,27 @@ function resolveType(typeId, types) {
   return 'string'; // Default type
 }
 
-export { resolveType };
+const evmToPostgresTypeMap = {
+  uint8: 'integer',
+  uint16: 'integer',
+  uint32: 'integer',
+  uint64: 'integer',
+  uint128: 'integer',
+  uint256: 'integer',
+  int8: 'integer',
+  int16: 'integer',
+  int32: 'integer',
+  int64: 'integer',
+  int128: 'integer',
+  int256: 'integer',
+  bool: 'boolean',
+  address: 'string',
+  bytes: 'string',
+  string: 'string',
+};
+
+function resolveEvmType(type) {
+  return evmToPostgresTypeMap[type] || 'string';
+}
+
+export { resolveInkType, resolveEvmType };