Skip to content

Instantly share code, notes, and snippets.

@nakajo2011
Last active April 8, 2020 16:08
Embed
What would you like to do?
web3.js mock sample
const Web3 = require('web3')
const BLOCK_NUMBER_PER_DAY = 24 * 60 * 4
const contractJson = require('../../build/contracts/MyToken')
class Faucet {
constructor(address, fromAddress) {
this.provider = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
this.myToken = new this.provider.eth.Contract(contractJson.abi, address, {from: fromAddress})
}
// return in a day transfer
async loadLatestDayTransfers() {
const options = await this.getPastEventOption()
this.events = await this.myToken.getPastEvents("Transfer", options)
}
isAlreadyWitdrawed(address) {
const toList = this.events.map(e => e.returnValues.to.toLowerCase())
return toList.includes(address)
}
async getPastEventOption() {
let from = (await this.provider.eth.getBlockNumber()) - BLOCK_NUMBER_PER_DAY
if(from < 0) {
from = 0
}
return {fromBlock: from, toBlock:'latest'}
}
async sendToken(address) {
return await this.myToken.methods.transfer(address, 100).send()
}
}
module.exports = Faucet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment