-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
136 lines (114 loc) · 3.89 KB
/
utils.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"use client";
import web3modal from "web3modal";
import { ethers } from "ethers";
import {
addressFactory,
abiFactory,
addressCustomToken,
abiCustomToken,
} from "./config";
async function getFlipFactoryContract(providerOrSigner) {
// const modal = new web3modal();
// const connection = await modal.connect();
// const provider = new ethers.providers.Web3Provider(connection);
const provider = new ethers.BrowserProvider(window.ethereum);
const contract = new ethers.Contract(addressFactory, abiFactory, provider);
if (providerOrSigner == true) {
const signer = await provider.getSigner();
const contract = new ethers.Contract(
addressFactory,
abiFactory,
signer
);
return contract;
}
return contract;
}
async function getTokenContract(providerOrSigner) {
// const modal = new web3modal();
// const connection = await modal.connect();
// const provider = new ethers.providers.Web3Provider(connection);
const provider = new ethers.BrowserProvider(window.ethereum);
const contract = new ethers.Contract(
addressCustomToken,
abiCustomToken,
provider
);
if (providerOrSigner == true) {
const signer = await provider.getSigner();
const contract = new ethers.Contract(
addressCustomToken,
abiCustomToken,
signer
);
return contract;
}
return contract;
}
export async function getUserAddress() {
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
return accounts[0];
}
export async function isExistingUser() {
const contract = await getFlipFactoryContract();
const address = await getUserAddress();
const data = await contract.userAddressToContractAddress(address);
console.log("isExisting: ", data);
return data;
}
export async function createAccount() {
const contract = await getFlipFactoryContract(true);
const tx = await contract.createflips();
await tx.wait();
console.log("Account Created");
}
export async function approveToken(_amount) {
const contract = await getTokenContract(true);
const weiAmount = ethers.parseEther(_amount, 18);
const tx = await contract.approve(addressFactory, weiAmount);
await tx.wait();
console.log("Spender Approved");
}
// this is called on token contract
export async function checkAllowance() {
const contract = await getTokenContract();
const user = await getUserAddress();
const data = await contract.allowance(user, addressFactory);
const formattedData = ethers.formatEther(data);
console.log("Allowance: ", formattedData);
return formattedData;
}
// this is called from factory contract
export async function checkGetAllowance() {
const contract = await getFlipFactoryContract();
const data = await contract.getAllowance();
const formattedData = ethers.formatEther(data);
console.log("Allowance: ", formattedData);
return formattedData;
}
export async function flipCoin(_multiplier, _amount, _coinSide) {
const isExisting = await isExistingUser();
if (isExisting.toString() == "0x0000000000000000000000000000000000000000") {
await createAccount();
}
const allowance = await checkAllowance()
if (allowance < _amount) return
const contract = await getFlipFactoryContract(true);
// console.log("calling approve function");
// await approveToken(_amount);
// await getAllowance();
console.log("calling flip function");
console.log("inputs: ", {
multiplier: _multiplier,
weiAmount: _amount,
coinSide: _coinSide,
});
const weiAmount = ethers.parseEther(_amount, 18);
const tx = await contract.flipACoin(_multiplier, weiAmount, _coinSide, {
gasLimit: 1000000,
});
const data = await tx.wait();
console.log("tx: ", data);
}