Skip to content

Commit

Permalink
Use conditional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
matjazv committed Nov 7, 2024
1 parent f06b9a7 commit 48df895
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions web3-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type DataFeed = {
storedTimestamp: number;
};

const isDebugMode = false;

Web3Function.onRun(async (context: Web3FunctionContext) => {
const { userArgs, multiChainProvider } = context;

Expand All @@ -48,7 +50,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
storedTimestamp: 0,
});
}
//console.log("Data feed ids: ", dataFeedIds);
conditionalLog(isDebugMode, "Data feed ids: ", dataFeedIds);

// Wrap contract with redstone data service
var wrappedOraclePrimaryProd;
Expand Down Expand Up @@ -90,44 +92,60 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
const txCalldataBytes = arrayify(String(data));
const parsingResult = redstone.RedstonePayload.parse(txCalldataBytes);

/*console.log(
conditionalLog(
isDebugMode,
"Unsigned metadata: ",
toUtf8String(parsingResult.unsignedMetadata)
);
console.log("Data packages count: ", parsingResult.signedDataPackages.length);
console.log(
conditionalLog(
isDebugMode,
"Data packages count: ",
parsingResult.signedDataPackages.length
);
conditionalLog(
isDebugMode,
"------------------------------------------------------------------------"
);*/
);

let dataPackageIndex = 0;
for (const signedDataPackage of parsingResult.signedDataPackages) {
/*console.log(
conditionalLog(
isDebugMode,
"------------------------------------------------------------------------"
);
console.log(`Data package: ${dataPackageIndex}`);
console.log(
conditionalLog(isDebugMode, `Data package: ${dataPackageIndex}`);
conditionalLog(
isDebugMode,
`Timestamp: ${signedDataPackage.dataPackage.timestampMilliseconds}`
);
console.log(
conditionalLog(
isDebugMode,
`Date and time: ${new Date(
signedDataPackage.dataPackage.timestampMilliseconds
).toUTCString()}`
);
console.log("Signer address: ", signedDataPackage.recoverSignerAddress());
console.log(
conditionalLog(
isDebugMode,
"Signer address: ",
signedDataPackage.recoverSignerAddress()
);
conditionalLog(
isDebugMode,
"Data points count: ",
signedDataPackage.dataPackage.dataPoints.length
);
console.log(
conditionalLog(
isDebugMode,
"Data points symbols: ",
signedDataPackage.dataPackage.dataPoints.map((dp) => dp.dataFeedId)
);
console.log(
conditionalLog(
isDebugMode,
"Data points values: ",
signedDataPackage.dataPackage.dataPoints.map((dp) =>
BigNumber.from(dp.value).toNumber()
)
);*/
);

let dataFeed = dataFeedIds.get(
signedDataPackage.dataPackage.dataPoints[0].dataFeedId
Expand All @@ -145,7 +163,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
signedDataPackage.dataPackage.timestampMilliseconds;
}
}
//console.log("Data feed: ", dataFeed);
conditionalLog(isDebugMode, "Data feed: ", dataFeed);
dataPackageIndex++;
}

Expand Down Expand Up @@ -277,3 +295,9 @@ function computePriceDeviation(
.div(oldPrice);
}
}

function conditionalLog(condition: boolean, ...args: any[]): void {
if (condition) {
console.log(...args);
}
}

0 comments on commit 48df895

Please sign in to comment.