-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MoralisWeb3/moralis-scan-ep08
Moralis scan ep08
- Loading branch information
Showing
9 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react' | ||
import { | ||
BrowserRouter as Router, | ||
Switch, | ||
Route, | ||
NavLink, | ||
useParams, | ||
} from "react-router-dom"; | ||
import Transactions from './Transactions'; | ||
|
||
export default function AddressResults() { | ||
const {address} = useParams(); | ||
return ( | ||
<div className="py-3"> | ||
<Router> | ||
<div className="card"> | ||
<div className="card-header"> | ||
<ul className="nav mb-1"> | ||
<li className="nav-item"> | ||
<NavLink | ||
className="nav-link" | ||
activeClassName="active" | ||
to={`/address/${address}/main`} | ||
> | ||
Transactions | ||
</NavLink> | ||
</li> | ||
<li className="nav-item"> | ||
<NavLink | ||
className="nav-link" | ||
activeClassName="active" | ||
to={`/address/${address}/erc20`} | ||
> | ||
ERC20 Token Txns | ||
</NavLink> | ||
</li> | ||
</ul> | ||
</div> | ||
<div className="card-body"> | ||
<Switch> | ||
<Route | ||
exact | ||
path="/address/:address/:transType" | ||
component={Transactions} | ||
/> | ||
</Switch> | ||
</div> | ||
</div> | ||
</Router> | ||
</div> | ||
); | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect, useState } from "react" | ||
import { useParams } from "react-router"; | ||
import { processTokenTransfer } from "../queries/tokenTransfer"; | ||
import { processTransaction } from "../queries/transactions"; | ||
|
||
const TransTypeOptions = { | ||
main: { | ||
cols: [ | ||
{ colName: "Txn Hash", key: "hash" }, | ||
{ colName: "Block", key: "block_number" }, | ||
{ colName: "Age", key: "ago" }, | ||
{ colName: "From", key: "from_address" }, | ||
{ colName: "To", key: "to_address" }, | ||
{ colName: "Value", key: "value" }, | ||
{ colName: "Txn Fee", key: "gas_price" }, | ||
], | ||
methodName: "getTransactions", | ||
postProcess: processTransaction, | ||
}, | ||
erc20: { | ||
cols: [ | ||
{ colName: "Txn Hash", key: "transaction_hash" }, | ||
{ colName: "Age", key: "ago" }, | ||
{ colName: "From", key: "from_address" }, | ||
{ colName: "To", key: "to_address" }, | ||
{ colName: "Value", key: "value" }, | ||
{ colName: "Token", key: "name" }, | ||
], | ||
methodName: "getTokenTranfers", | ||
postProcess: processTokenTransfer, | ||
}, | ||
}; | ||
|
||
export const useTransType = () => { | ||
const [state, setState] = useState({}); | ||
const { transType } = useParams(); | ||
|
||
useEffect(()=>{ | ||
setState(TransTypeOptions[transType]) | ||
}, [transType]) | ||
|
||
return state; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Moralis from "moralis"; | ||
|
||
Moralis.Cloud.define("searchEthAddress", async function(request) { | ||
const { address } = request.params | ||
if (!address) { | ||
return null | ||
} | ||
|
||
// find out if address is already watched | ||
const query = new Moralis.Query("WatchedEthAddress"); | ||
query.equalTo("address", address) | ||
const watchCount = await query.count(); | ||
|
||
if (watchCount > 0) { | ||
// already watched don't sync again | ||
return null; | ||
} | ||
|
||
return Moralis.Cloud.run("watchEthAddress", {address}); | ||
}); | ||
|
||
Moralis.Cloud.define("getTransactions", function(request) { | ||
const {userAddress, pageSize, pageNum } = request.params; | ||
const offset = (pageNum - 1) * pageSize; | ||
|
||
const query = new Moralis.Query("EthTransactions"); | ||
query.equalTo("from_address", userAddress); | ||
query.descending("block_number"); | ||
query.withCount(); | ||
query.skip(offset) | ||
query.limit(pageSize); | ||
|
||
return query.find(); | ||
}); | ||
|
||
Moralis.Cloud.define("getTokenTranfers", async (request) => { | ||
const {userAddress, pageSize, pageNum } = request.params; | ||
const offset = (pageNum - 1) * pageSize; | ||
const output = { | ||
results: [], | ||
count: 0, | ||
}; | ||
|
||
// count results | ||
const matchPipeline = { | ||
match: { | ||
$expr: { | ||
$or: [ | ||
{ $eq: ["$from_address", userAddress] }, | ||
{ $eq: ["$to_address", userAddress] }, | ||
], | ||
}, | ||
}, | ||
sort: { block_number: -1 }, | ||
count: "count", | ||
}; | ||
const query = new Moralis.Query("EthTokenTransfers"); | ||
const countResult = await query.aggregate(matchPipeline); | ||
output.count = countResult[0].count; | ||
|
||
// get page results | ||
const lookupPipeline = { | ||
...matchPipeline, | ||
skip: offset, | ||
limit: pageSize, | ||
lookup: { | ||
from: "EthTokenBalance", | ||
let: { tokenAddress: "$token_address", userAddress }, | ||
pipeline: [ | ||
{ | ||
$match: { | ||
$expr: { | ||
$and: [ | ||
{ $eq: ["$token_address", "$$tokenAddress"] }, | ||
{ $eq: ["$address", "$$userAddress"] }, | ||
], | ||
}, | ||
}, | ||
}, | ||
], | ||
as: "EthTokenBalance", | ||
}, | ||
unwind: "$EthTokenBalance", | ||
}; | ||
delete lookupPipeline.count; | ||
|
||
output.results = await query.aggregate(lookupPipeline); | ||
return output; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { agoTxt, getEllipsisTxt, tokenValueTxt } from "./utils"; | ||
|
||
export const processTokenTransfer = (r) => ({ | ||
transaction_hash: getEllipsisTxt(r.transaction_hash), | ||
ago: agoTxt(r.block_timestamp.valueOf()), | ||
from_address: getEllipsisTxt(r.from_address), | ||
to_address: getEllipsisTxt(r.to_address), | ||
value: tokenValueTxt( | ||
r.value, | ||
r.EthTokenBalance?.decimals, | ||
r.EthTokenBalance?.symbol | ||
), | ||
name: r.EthTokenBalance?.name || "", | ||
}); |