|
| 1 | +require('../env') |
| 2 | +const { isAddress } = require('web3').utils |
| 3 | +const { privateKeyToAddress, setIntervalAndRun } = require('../src/utils/utils') |
| 4 | +const { EXIT_CODES } = require('../src/utils/constants') |
| 5 | +const { web3Home } = require('../src/services/web3') |
| 6 | +const { sendTx } = require('../src/tx/sendTx') |
| 7 | + |
| 8 | +const privateKey = process.env.INTEREST_FETCHER_PRIVATE_KEY |
| 9 | +const interval = process.env.INTERVAL ? parseInt(process.env.INTERVAL, 10) : 3600 * 1000 |
| 10 | +const contractAddress = process.env.INTEREST_FETCH_CONTRACT_ADDRESS |
| 11 | + |
| 12 | +if (!privateKey) { |
| 13 | + console.error('Environment variable INTEREST_FETCHER_PRIVATE_KEY is not set') |
| 14 | + process.exit(EXIT_CODES.GENERAL_ERROR) |
| 15 | +} |
| 16 | + |
| 17 | +if (interval < 300 * 1000) { |
| 18 | + console.error('Interval is to small, should be at least 5 minutes') |
| 19 | + process.exit(EXIT_CODES.GENERAL_ERROR) |
| 20 | +} |
| 21 | + |
| 22 | +if (!isAddress(contractAddress)) { |
| 23 | + console.error('Invalid contract address provided', contractAddress) |
| 24 | + process.exit(EXIT_CODES.GENERAL_ERROR) |
| 25 | +} |
| 26 | + |
| 27 | +const gasPrice = process.env.COMMON_HOME_GAS_PRICE_FALLBACK || '1000000000' |
| 28 | + |
| 29 | +async function main() { |
| 30 | + // assuming that we are using this contract - https://github.com/omni/interest-fetcher-contract |
| 31 | + const contract = new web3Home.eth.Contract([{ name: 'fetchInterest', type: 'function', inputs: [] }], contractAddress) |
| 32 | + const chainId = await web3Home.eth.getChainId() |
| 33 | + const data = contract.methods.fetchInterest().encodeABI() |
| 34 | + const senderAddress = privateKeyToAddress(privateKey) |
| 35 | + console.log( |
| 36 | + `Initialized, chainId=${chainId}, data=${data}, contract=${contractAddress}, interval=${interval / 1000}s` |
| 37 | + ) |
| 38 | + |
| 39 | + await setIntervalAndRun(async () => { |
| 40 | + let gasLimit |
| 41 | + try { |
| 42 | + gasLimit = await contract.methods.fetchInterest().estimateGas() |
| 43 | + } catch (e) { |
| 44 | + console.log('Gas limit estimation failed, will retry later', new Date()) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + const nonce = await web3Home.eth.getTransactionCount(senderAddress) |
| 49 | + |
| 50 | + const txHash = await sendTx({ |
| 51 | + privateKey, |
| 52 | + to: contractAddress, |
| 53 | + data, |
| 54 | + nonce, |
| 55 | + gasPrice, |
| 56 | + gasLimit: Math.round(gasLimit * 1.5), |
| 57 | + amount: '0', |
| 58 | + chainId, |
| 59 | + web3: web3Home |
| 60 | + }) |
| 61 | + console.log('Sent transaction with fetch interest', txHash, new Date()) |
| 62 | + }, interval) |
| 63 | +} |
| 64 | + |
| 65 | +main() |
0 commit comments