generated from The-Solid-Chain/smart-contracts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solveMaya.ts
61 lines (45 loc) · 2.19 KB
/
solveMaya.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
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment, TaskArguments } from "hardhat/types";
import { Maya, MERC20 } from "../typechain";
task("solveMaya", "Solves the Maya challenge")
.addParam("mayaAddress", "Address of the Maya contract", undefined, types.string, false)
.setAction(async (args: TaskArguments, hre: HardhatRuntimeEnvironment) => {
if (!hre.ethers.utils.isAddress(args.mayaAddress)) {
throw new Error(`Invalid contract address format: ${args.mayaAddress}`);
}
const signers = await hre.ethers.getSigners();
// Use a different signer from the local deployer so we can test properly
let signer;
if (hre.network.name === "hardhat" || hre.network.name === "localhost") {
signer = signers[1];
} else {
signer = signers[0];
}
const MayaFactory = await hre.ethers.getContractFactory("Maya", signer);
const mayaChallenge = (await MayaFactory.attach(args.mayaAddress)) as Maya;
const MERC20Factory = await hre.ethers.getContractFactory("MERC20", signer);
console.log("Getting the token contract address...");
const merc20TokenAddress = await mayaChallenge.token();
console.log("Found MERC20 token at address:", merc20TokenAddress);
const merc20Challenge = (await MERC20Factory.attach(merc20TokenAddress)) as MERC20;
// Get some tokens
console.log("Getting some tokens...");
let tx = await merc20Challenge.getTokens();
await tx.wait();
// Buy a maya
const price = await mayaChallenge.price();
console.log("Approve Maya contract to spend tokens...");
tx = await merc20Challenge.approve(mayaChallenge.address, price);
await tx.wait();
console.log("Buying a Maya...");
tx = await mayaChallenge.buy();
await tx.wait();
console.log("Buying a second Maya...");
tx = await mayaChallenge.buy();
await tx.wait();
console.log("Now a third!");
tx = await mayaChallenge.buy();
await tx.wait();
const result = await mayaChallenge.hunter();
console.log("[RESULT]:", result);
});