Created
March 24, 2020 14:46
-
-
Save cloudonshore/fb11f88bcc720b5696c42dfecc88e8e5 to your computer and use it in GitHub Desktop.
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
// IMPORTS | |
const { ethers } = require('ethers') | |
const swapABI = require('./swapABI.json') | |
// UTIL FUNCTIONS | |
const swapContractAddresses = { | |
"1": "0x4572f2554421bd64bef1c22c8a81840e8d496bea", | |
"4": "0x2e7373d70732e0f37f4166d8fd9dbc89dd5bc476", | |
"5": "0x18c90516a38dd7b779a8f6c19fa698f0f4efc7fc", | |
"42": "0x79fb4604f2d7bd558cda0dfadb7d61d98b28ca9f" | |
}; | |
const swapContractDeployBlocks = { | |
"1": 8574958, | |
"4": 5359808, | |
"5": 2322076, | |
"42": 17298087 | |
}; | |
function getEventTopics({ name, params: paramsInputs, abi }) { | |
const params = paramsInputs || {}; // default to empty object if undefined | |
const abiInterface = new ethers.utils.Interface(abi); | |
const { events } = abiInterface; | |
const abiEvent = Object.values(events).find(event => event.name === name); | |
if (!abiEvent) { | |
throw new Error(`${name} not an abi event`); | |
} | |
const paramsArray = abiEvent.inputs.map(({ name: inputName }) => | |
typeof params[inputName] === "undefined" ? null : params[inputName] | |
); | |
return abiEvent.encodeTopics(paramsArray); | |
} | |
function parseSwapLogs(logs) { | |
const abiInterface = new ethers.utils.Interface(swapABI); | |
const returnLogs = []; | |
logs.forEach(log => { | |
log.values = {}; | |
let parsedLog; | |
try { | |
parsedLog = abiInterface.parseLog(log); | |
} catch (e) { | |
console.log(e); | |
return; | |
} | |
for (const prop in parsedLog.values) { | |
if (parsedLog.values.hasOwnProperty(prop) && isNaN(prop)) { | |
log.values[prop] = parsedLog.values[prop].toString().toLowerCase(); | |
} | |
} | |
returnLogs.push(log); | |
}); | |
return returnLogs; | |
} | |
// EVENT FETCHING FUNCTIONS | |
const getSwapLogs = async ({ nonce, signerWallet, senderWallet }, provider) => { | |
const network = provider.network ? provider.network.chainId : provider._web3Provider.networkVersion | |
debugger; | |
const filter = { | |
address: swapContractAddresses[network], | |
fromBlock: swapContractDeployBlocks[network], | |
toBlock: "latest", | |
topics: getEventTopics({ | |
name: "Swap", | |
params: { nonce, signerWallet, senderWallet }, | |
abi: swapABI | |
}) | |
}; | |
return provider.getLogs(filter).then(parseSwapLogs); | |
}; | |
const getSwapCancelLogs = async ({ nonce, signerWallet }, provider) => { | |
const network = provider.network ? provider.network.chainId : provider._web3Provider.networkVersion | |
const filter = { | |
address: swapContractAddresses[network], | |
fromBlock: swapContractDeployBlocks[network], | |
toBlock: "latest", | |
topics: getEventTopics({ | |
name: "Cancel", | |
params: { nonce, signerWallet }, | |
abi: swapABI | |
}) | |
}; | |
return provider.getLogs(filter).then(parseSwapLogs); | |
}; | |
// EXAMPLE USAGE | |
const provider = new ethers.getDefaultProvider() | |
getSwapLogs({ signerWallet: '0x1550d41be3651686e1aeeea073d8d403d0bd2e30', nonce: 1580762246566 }, provider) | |
.then(logs => console.log('Swaps', logs)) | |
.catch(console.log) | |
getSwapCancelLogs({ signerWallet: '0x1550d41be3651686e1aeeea073d8d403d0bd2e30'}, provider) | |
.then(logs => console.log('Cancels', logs)) | |
.catch(console.log) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment