forked from andreykobal/playfab-web3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb3Functions.js
More file actions
137 lines (107 loc) · 4.75 KB
/
web3Functions.js
File metadata and controls
137 lines (107 loc) · 4.75 KB
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
137
//web3Functions.js
const { Web3 } = require('web3');
const fs = require('fs').promises;
require('dotenv').config();
// Provider URL and private key
const providerUrl = "https://node.botanixlabs.dev";
const privateKey = process.env.PRIVATE_KEY;
// Initialize a new instance of Web3
const web3 = new Web3(providerUrl);
// Contract address
const tokenAddress = "0xaC7e4Ad5d7557B78ebc84Dff668A06709f5Dc62B";
// Read the contract ABI asynchronously and create a contract instance
let tokenContract; // This will hold the contract instance
async function setupContract() {
try {
const abiData = await fs.readFile('./abi.json');
const abi = JSON.parse(abiData);
tokenContract = new web3.eth.Contract(abi, tokenAddress);
} catch (error) {
console.error("Error setting up the contract:", error);
}
}
// Call setupContract at the start of your application
setupContract();
async function distributeDailyRewards(recipients, performanceScores) {
if (!tokenContract) {
console.error("Contract not set up");
return;
}
const dailyRewardPool = 100;
const totalPerformanceScore = performanceScores.reduce((acc, score) => acc + score, 0);
const rewards = performanceScores.map(score => (score / totalPerformanceScore) * dailyRewardPool);
const amountsInWei = rewards.map(amount => web3.utils.toWei(amount.toString(), "ether"));
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const data = tokenContract.methods.batchTransfer(recipients, amountsInWei).encodeABI();
const gasLimit = 3000000;
const gasPrice = await web3.eth.getGasPrice();
const txObject = {
from: account.address,
to: tokenAddress,
data: data,
gas: gasLimit,
gasPrice: gasPrice
};
const signedTx = await web3.eth.accounts.signTransaction(txObject, privateKey);
await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log("\x1b[33m%s\x1b[0m", "💸 Daily rewards distribution successful! 💸");
recipients.forEach((recipient, i) => console.log(`Recipient: ${recipient}, Amount: ${rewards[i]}`));
}
async function getTokenBalance(address) {
if (!tokenContract) {
console.error("Contract not set up");
return;
}
return await tokenContract.methods.balanceOf(address).call();
}
async function makeSimpleTransfer(recipientAddress, amount, userPrivateKey) {
if (!tokenContract) {
console.error("Contract not set up");
return;
}
const amountInWei = web3.utils.toWei(amount.toString(), "ether");
const account = web3.eth.accounts.privateKeyToAccount(userPrivateKey);
// Get the current nonce for the account
const nonce = await web3.eth.getTransactionCount(account.address, "latest");
const data = tokenContract.methods.transfer(recipientAddress, amountInWei).encodeABI();
const gasPrice = await web3.eth.getGasPrice();
// It's better to estimate gas if possible, but for simplicity, we're using a fixed value
const gasLimit = await tokenContract.methods.transfer(recipientAddress, amountInWei).estimateGas({ from: account.address });
const txObject = {
from: account.address,
to: tokenAddress,
data: data,
gas: gasLimit,
gasPrice: gasPrice,
nonce: nonce // Set the nonce explicitly
};
// Sign and send the transaction
const signedTx = await web3.eth.accounts.signTransaction(txObject, userPrivateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`✅ Transfer successful! TxHash: ${receipt.transactionHash}`);
}
async function sendEther(recipientAddress, amountInEther) {
if (!web3) {
console.error("Web3 instance not initialized");
return;
}
const amountInWei = web3.utils.toWei(amountInEther.toString(), "ether");
const account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY);
// Get the current nonce for the account
const nonce = await web3.eth.getTransactionCount(account.address, "latest");
// Prepare the transaction object
const txObject = {
from: account.address,
to: recipientAddress,
value: amountInWei,
gas: '21000', // This is the standard gas limit for a simple ETH transfer
gasPrice: await web3.eth.getGasPrice(),
nonce: nonce
};
// Sign the transaction with the private key
const signedTx = await account.signTransaction(txObject);
// Send the signed transaction to the Ethereum network
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`✅ Ether transfer successful! TxHash: ${receipt.transactionHash}`);
}
module.exports = { distributeDailyRewards, getTokenBalance, makeSimpleTransfer, sendEther};