Following your questions:
How can one retrieve all transaction history programmatically?
You can get the transactions of a wallet using Moralis Transaction API.
Here is an example of getting the transactions using moralis
const Moralis = require("moralis").default;
const runApp = async () => {
const { EvmChain } = Moralis.EvmUtils;
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const address = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.transaction.getWalletTransactions({
address,
chain,
});
console.log(response.raw);
};
runApp();
And this is what the results will look like.
{
"total": "2000",
"page": "2",
"page_size": "100",
"result": [
{
"hash": "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
"nonce": 326595425,
"transaction_index": 25,
"from_address": "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
"to_address": "0xa71db868318f0a0bae9411347cd4a6fa23d8d4ef",
"value": 650000000000000000,
"gas": 6721975,
"gas_price": 20000000000,
"input": "string",
"receipt_cumulative_gas_used": 1340925,
"receipt_gas_used": 1340925,
"receipt_contract_address": "0x1d6a4cf64b52f6c73f201839aded7379ce58059c",
"receipt_root": "string",
"receipt_status": 1,
"block_timestamp": "2021-04-02T10:07:54.000Z",
"block_number": 12526958,
"block_hash": "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86"
}
]
}
You can find a guide here: How to get transactions by wallet
For the second question:
How can one listen to real-time transfers happening to/from that account?
You can use moralis streams API to listen to real-time blockchains events, including native eth transactions
Set up a stream
const options = {
chains: [EvmChain.ETHEREUM], // list of blockchains to monitor
description: "Listen to transfers", // your description
tag: "transfers", // give it a tag
includeContractLogs: false,
includeNativeTxs: true,
webhookUrl: "https://YOUR_WEBHOOK_URL", // webhook url to receive events,
};
const stream = await Moralis.Streams.add(options);
const { id } = stream.toJSON(); // { id: 'YOUR_STREAM_ID', ...stream }
// Attach the address to the stream
await Moralis.Streams.addAddress({
id,
address: "0x0.........2e1e", // Wallet address to monitor
});
Feel free to check the docs for more details, and to check some examples
https://docs.moralis.io/streams-api
You can check a more detailed guide if you want to try out streams:
https://youtu.be/pnmVhxdUBao
Disclosure: I work at Moralis