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 #6 from GregTheGreek/feat/ganache
Major updates
- Loading branch information
Showing
19 changed files
with
662 additions
and
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import ethers, { Transaction } from "ethers"; | ||
import { GanacheEngine } from "./rules/ganache"; | ||
import { JsonRpcProvider } from "@ethersproject/providers"; | ||
|
||
export interface Rule { | ||
moduleName: string; | ||
useGanache: boolean; | ||
provider: ethers.providers.JsonRpcProvider; | ||
run(tx: Transaction): Promise<void>; | ||
} | ||
|
||
export class RuleEngine { | ||
rules: Rule[] = []; | ||
provider: JsonRpcProvider; | ||
ganache: GanacheEngine; | ||
|
||
constructor(provider: JsonRpcProvider, rules: Rule[] = []) { | ||
this.provider = provider; | ||
// Create ganache instancing for forking features | ||
this.ganache = new GanacheEngine(provider.connection.url) | ||
rules.map(x => this.addRule(x)); | ||
}; | ||
|
||
addRule(rule: Rule): void { | ||
// If rule requires ganache forking, swap provider | ||
// @TODO - ganache may need to be instatiated in the rule itself | ||
// I think this is actually causing the fork to happen too early. | ||
// We should fork on demand | ||
if (rule.useGanache) { | ||
rule.provider = this.ganache.provider; | ||
} | ||
this.rules.push(rule); | ||
} | ||
|
||
async run(tx: Transaction): Promise<void> { | ||
for (let i=0; i < this.rules.length; i++) { | ||
await this.rules[i].run(tx); | ||
console.log("\n") | ||
} | ||
} | ||
} |
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,15 @@ | ||
import Rules from "./rules"; | ||
const inquirer = require("inquirer"); | ||
|
||
export const selectRules = async (): Promise<string[]> => { | ||
const choices = Object.keys(Rules); | ||
const answer = await inquirer.prompt([ | ||
{ | ||
type: "checkbox", | ||
message: "Select the rules you want to use:", | ||
name: "modules", | ||
choices | ||
} | ||
]); | ||
return answer.modules; | ||
} |
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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
import { JsonRpcProvider } from "@ethersproject/providers"; | ||
import { Contract, Transaction } from "ethers"; | ||
import { Rule } from "../../engine"; | ||
import { isContract, logModuleHeader, serializeTx } from "../utils"; | ||
import ethers from "ethers"; | ||
|
||
import { Erc20Interface } from "./interfacces/erc20"; | ||
|
||
/** | ||
* The ERC20 module attempts to inform the user about a given token contract. | ||
*/ | ||
export class Erc20Rules implements Rule { | ||
moduleName = "ERC20 Rule Module"; | ||
useGanache: boolean = true; | ||
provider: JsonRpcProvider; | ||
contract: Contract | null; | ||
|
||
constructor(provider: JsonRpcProvider) { | ||
this.provider = provider; | ||
this.contract = null; | ||
} | ||
|
||
async run(tx: Transaction): Promise<void> { | ||
if (!tx.from) return; | ||
if (!tx.to) return; | ||
if (!(await isContract(this.provider, tx.to))) return; | ||
|
||
this.contract = new ethers.Contract(tx.to, Erc20Interface, this.provider); | ||
|
||
if (!(await this.isErc20(tx.from))) { | ||
this.log("Not an erc20 contract!"); | ||
return; | ||
} | ||
|
||
await this.checkBalance(tx); | ||
|
||
} | ||
|
||
private log(msg: string): void { | ||
logModuleHeader(this.moduleName); | ||
console.log(msg); | ||
} | ||
|
||
private async checkBalance(tx: Transaction): Promise<void> { | ||
if (!this.contract) return; | ||
const balanceBefore: ethers.BigNumber = await this.contract.balanceOf(tx.from); | ||
const txResponse = await this.provider.sendTransaction(serializeTx(tx)); | ||
await txResponse.wait(); | ||
const balanceAfter: ethers.BigNumber = await this.contract.balanceOf(tx.from); | ||
|
||
if (balanceAfter.gt(balanceBefore)) { | ||
this.log("Increase"); | ||
} else if (balanceAfter.lt(balanceBefore)) { | ||
this.log("Decrease"); | ||
} else { | ||
this.log("No change"); | ||
} | ||
} | ||
|
||
private async isErc20(address: string): Promise<boolean> { | ||
if (!this.contract) return false; | ||
try { | ||
await this.contract.name(); | ||
await this.contract.symbol(); | ||
await this.contract.balanceOf(address); | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
} |
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 @@ | ||
export * from "./erc20"; |
Oops, something went wrong.