Skip to content

Commit

Permalink
Dune: Query contract name
Browse files Browse the repository at this point in the history
  • Loading branch information
ylitvinov committed Nov 6, 2022
1 parent b18c566 commit e676b9a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
25 changes: 15 additions & 10 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {abi as ERC1155_abi} from "@openzeppelin/contracts/build/contracts/ERC115

import path from "path";
import {simulateWithTenderly} from "./tenderly-utils";
import {lookupContractNameInDune} from "./dune-utils";
import {getNFTinfo} from "./quicknode-utils";

require('console-stamp')(console);
Expand Down Expand Up @@ -54,7 +55,7 @@ app.post('/api', async (req, res) => {
// }
app.get('/api', async (req, res) => {
try {
console.log('req', req.url)
console.log('REQ', req.url)
const transactionData = JSON.parse(req.query.t as string)
let result = await dryRunTransaction(transactionData)

Expand Down Expand Up @@ -83,7 +84,7 @@ async function dryRunTransaction(transaction) {
let result = {}
let callTraces = tenderlyData.transaction.call_trace;

function lookupContractName(contractAddress) {
async function lookupContractName(contractAddress) {
let betterName = contractAddress
if (contractsMap[contractAddress]) {
let toContract = contractsMap[contractAddress.toLowerCase()]
Expand All @@ -94,6 +95,10 @@ async function dryRunTransaction(transaction) {
if (transaction.from.toLowerCase() == contractAddress.toLowerCase()) {
betterName = "ME"
}
let duneContractName = await lookupContractNameInDune(contractAddress)
if (duneContractName) {
betterName = duneContractName
}
return betterName;
}

Expand Down Expand Up @@ -150,24 +155,24 @@ async function dryRunTransaction(transaction) {
}
if (contract.standards.includes("erc20")) {
if (functionName == "transfer") {
let recipient = lookupContractName(decodedArgs[0])
let recipient = await lookupContractName(decodedArgs[0])
let amount = bigNumberToHumanReadable(decodedArgs[1] as BigNumber)

let title = ++resultIndex + ". Transfer"
let description = "Transfer " + amount + " " + tokenSymbol + " to " + recipient;
result[title] = description
}
if (functionName == "transferFrom") {
let sender = lookupContractName(decodedArgs[0])
let recipient = lookupContractName(decodedArgs[1])
let sender = await lookupContractName(decodedArgs[0])
let recipient = await lookupContractName(decodedArgs[1])
let amount = bigNumberToHumanReadable(decodedArgs[2] as BigNumber);

let title = ++resultIndex + ". Transfer"
let description = "Transfer " + amount + " " + tokenSymbol + " to " + recipient;
result[title] = description
}
if (functionName == "approve") {
let spender = lookupContractName(decodedArgs[0])
let spender = await lookupContractName(decodedArgs[0])
let amount = bigNumberToHumanReadable(decodedArgs[2] as BigNumber);

let title = ++resultIndex + ". Approve " + amount + " " + tokenSymbol + " to " + spender;
Expand All @@ -176,8 +181,8 @@ async function dryRunTransaction(transaction) {
} else if (contract.standards.includes("erc721")) {

if (functionName == "safeTransferFrom" || functionName == "transferFrom") {
let from = lookupContractName(decodedArgs[0])
let recipient = lookupContractName(decodedArgs[1])
let from = await lookupContractName(decodedArgs[0])
let recipient = await lookupContractName(decodedArgs[1])
let tokenId = (decodedArgs[2] as BigNumber).toNumber();

const quicknodeData = await getNFTinfo(from, recipient, contract["address"], tokenId)
Expand All @@ -187,13 +192,13 @@ async function dryRunTransaction(transaction) {
result['NFT Transfer'] = quicknodeData
}
if (functionName == "setApprovalForAll") {
let operator = lookupContractName(decodedArgs[0])
let operator = await lookupContractName(decodedArgs[0])

let title = ++resultIndex + ". NFT approving to all";
result[title] = "TODO"
}
if (functionName == "approve") {
let addressTo = lookupContractName(decodedArgs[0])
let addressTo = await lookupContractName(decodedArgs[0])
let tokenId = (decodedArgs[1] as BigNumber).toNumber();

let title = ++resultIndex + ". NFT approve";
Expand Down
54 changes: 54 additions & 0 deletions api/src/dune-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fetch, {Headers} from 'node-fetch';
import * as dotenv from "dotenv";

dotenv.config();

let cache = {}

export async function lookupContractNameInDune(contractAddress: string) {
if (cache[contractAddress]) {
console.log("Done, returning cached value ", contractAddress)
return cache[contractAddress]
}
console.log("Dune request", contractAddress)
const meta = {
"x-dune-api-key": process.env.DUNE_ACCESS_KEY
};
const header = new Headers(meta);

const queryId = 1528731
var body = JSON.stringify({"query_parameters": {"contract_address": contractAddress.substring(2)}});
const execResponse = await fetch(`https://api.dune.com/api/v1/query/${queryId}/execute`, {
method: 'POST',
headers: header,
body: body
});
const response_object = await execResponse.text();

console.log(response_object);
const executionId = JSON.parse(response_object).execution_id

while (true) {
const resultResponse = await fetch(`https://api.dune.com/api/v1/execution/${executionId}/results`, {
method: 'GET',
headers: header
});
let resultData = JSON.parse(await resultResponse.text());
console.log(resultData)
if (resultData.state == "QUERY_STATE_COMPLETED") {
if (resultData.result.rows.length > 0) {
const rowData = resultData.result.rows[0]
let niceContractName = rowData["namespace"] + "-" + rowData["name"];
cache[contractAddress] = niceContractName
console.log("Dune response", niceContractName)
return niceContractName
} else {
return null
}
}
}

}

lookupContractNameInDune("0xd921A81445Ff6A9114deb7Db011F5ef8353F0bBc")

0 comments on commit e676b9a

Please sign in to comment.