-
Notifications
You must be signed in to change notification settings - Fork 0
/
waterPlants.ts
105 lines (85 loc) · 3.12 KB
/
waterPlants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'dotenv/config';
import { ethers } from "hardhat";
import "@nomiclabs/hardhat-ethers";
import { address, abi } from "./CryptOrchidERC721.json";
import { BigNumber } from '@ethersproject/bignumber';
import Discord, { User } from 'discord.js';
const discordBot = new Discord.Client();
const DISCORD_BOT_TOKEN = process.env.DISCORD_BOT_TOKEN;
const DISCORD_USER_ID = process.env.DISCORD_USER_ID;
function readyForWatering(
{ alive, plantedAt, waterLevel }: { alive: boolean, plantedAt: BigNumber, waterLevel: BigNumber },
GROWTH_CYCLE: BigNumber,
) {
if (!alive) return false;
const now = new Date();
const utcMillisecondsSinceEpoch = now.getTime() + (now.getTimezoneOffset() * 60 * 1000);
const utcSecondsSinceEpoch = Math.round(utcMillisecondsSinceEpoch / 1000);
const elapsed = BigNumber.from(utcSecondsSinceEpoch).sub(plantedAt);
const fullCycles = Math.floor(elapsed.div(GROWTH_CYCLE).toNumber());
return waterLevel.lt(fullCycles);
}
const discordSetup = async (): Promise<User> => {
return new Promise<User>((resolve, reject) => {
['DISCORD_BOT_TOKEN', 'DISCORD_USER_ID'].forEach((envVar) => {
if (!process.env[envVar]) reject(`${envVar} not set`)
})
discordBot.login(DISCORD_BOT_TOKEN);
discordBot.on('ready', async () => {
const user = await discordBot.users.fetch(DISCORD_USER_ID!, false)
resolve(user);
});
})
}
async function main() {
const discordUser = DISCORD_USER_ID ? await discordSetup() : null;
const accounts = await ethers.getSigners();
const CryptOrchidsContract = await ethers.getContractAt(
abi,
address,
accounts[0]
);
const ownedCount = await CryptOrchidsContract.balanceOf(accounts[0].address);
for (let index = 0; index < ownedCount.toNumber(); index++) {
const token = await CryptOrchidsContract.tokenOfOwnerByIndex(accounts[0].address, index);
const alive = await CryptOrchidsContract.alive(token - 1);
if (!alive) {
await discordUser?.send(`CryptOrchid #${token} is dead - please compost it so a new bulb can be planted.`)
continue;
}
const { 1: plantedAt, 2: waterLevel } = await CryptOrchidsContract.getTokenMetadata(token);
const orchid = {
token,
alive,
waterLevel,
plantedAt
}
const GROWTH_CYCLE = await CryptOrchidsContract.GROWTH_CYCLE();
if (readyForWatering(orchid, GROWTH_CYCLE)){
const gas = await CryptOrchidsContract.estimateGas.water(token);
const result = await CryptOrchidsContract.water(token, {
gasLimit: Math.max(
gas.toNumber(),
parseInt(process.env.GAS_LIMIT || '0') // set a GAS_LIMIT env var to limit gas used
),
});
await discordUser?.send(
`CryptOrchid #${token} watered in transaction:
\`\`\`
${JSON.stringify(result, null, 2)}
\`\`\`
View on etherscan: https://rinkeby.etherscan.io/tx/${result.hash}`
);
continue;
}
await discordUser?.send(
`CryptOrchid #${token} not ready for watering.`
);
}
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});