|
@@ -13,6 +13,20 @@ const MANAGER_ADDRESS = "0x257853ac4319c76f1cdb00831fd14a48977b45ef";
|
|
|
function normalizePrivateKey(pk) {
|
|
|
return pk.startsWith("0x") ? pk : "0x" + pk;
|
|
|
}
|
|
|
+function scaleGasPrice(originalGasPrice, gasRate) {
|
|
|
+ if (gasRate >= 1) {
|
|
|
+ return BigInt(originalGasPrice) * BigInt(Math.floor(gasRate));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 小于1的小数处理
|
|
|
+ const gasRateStr = gasRate.toString(); // 比如 "0.1"
|
|
|
+ const decimalPlaces = gasRateStr.split(".")[1]?.length || 0;
|
|
|
+ const denominator = 10 ** decimalPlaces;
|
|
|
+ const numerator = Math.floor(gasRate * denominator);
|
|
|
+
|
|
|
+ const scaled = (BigInt(originalGasPrice) * BigInt(numerator)) / BigInt(denominator);
|
|
|
+ return scaled;
|
|
|
+}
|
|
|
|
|
|
// 获取余额
|
|
|
async function getBalance(privateKey, type) {
|
|
@@ -35,7 +49,7 @@ async function getBalance(privateKey, type) {
|
|
|
|
|
|
|
|
|
// 发放奖励
|
|
|
-async function sendSingleReward(privateKey, amount) {
|
|
|
+async function sendSingleReward(privateKey, amount, GasRate) {
|
|
|
if (!privateKey) {
|
|
|
return { status: "error", message: "未获取到私钥,无法发放奖励" };
|
|
|
}
|
|
@@ -54,14 +68,23 @@ async function sendSingleReward(privateKey, amount) {
|
|
|
|
|
|
if (!signature) throw new Error("签名为空");
|
|
|
|
|
|
+ const gasPrice = await web3.eth.getGasPrice();
|
|
|
+ let adjustedGasPrice = scaleGasPrice(gasPrice, GasRate);
|
|
|
+ const maxTotalFee = BigInt(web3.utils.toWei("1", "ether")); // 1 ACC 上限
|
|
|
+ const gasLimit = 500000n;
|
|
|
+
|
|
|
+ // 如果 gasPrice 太高,限制为不会超过 1 ACC 的值
|
|
|
+ if (adjustedGasPrice * gasLimit > maxTotalFee) {
|
|
|
+ adjustedGasPrice = maxTotalFee / gasLimit;
|
|
|
+ }
|
|
|
const tx = {
|
|
|
from: sender,
|
|
|
to: contractAddress,
|
|
|
data: contract.methods
|
|
|
.distributeSingleReward(sender, amountWei, timestamp, nonceForSig, signature)
|
|
|
.encodeABI(),
|
|
|
- gas: 500000,
|
|
|
- gasPrice: await web3.eth.getGasPrice(),
|
|
|
+ gas: gasLimit,
|
|
|
+ gasPrice: adjustedGasPrice.toString(),
|
|
|
nonce: await web3.eth.getTransactionCount(sender, "pending"),
|
|
|
chainId: await web3.eth.getChainId()
|
|
|
};
|
|
@@ -80,7 +103,7 @@ async function sendSingleReward(privateKey, amount) {
|
|
|
}
|
|
|
|
|
|
// 通用报名函数(支持 WGT 和 STT)
|
|
|
-async function tokenSignUp(privateKey, amount, type) {
|
|
|
+async function tokenSignUp(privateKey, amount, GasRate,type) {
|
|
|
try {
|
|
|
const web3 = await getWeb3();
|
|
|
privateKey = normalizePrivateKey(privateKey);
|
|
@@ -95,11 +118,13 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
web3.eth.getTransactionCount(sender)
|
|
|
]);
|
|
|
|
|
|
- let adjustedGasPrice = BigInt(gasPrice) * BigInt(10);
|
|
|
- const maxGasPrice = BigInt(web3.utils.toWei('100', 'gwei'));
|
|
|
+ let adjustedGasPrice = scaleGasPrice(gasPrice, GasRate);
|
|
|
+ const maxTotalFee = BigInt(web3.utils.toWei("1", "ether")); // 1 ACC 上限
|
|
|
+ let gasLimit = 500000n;
|
|
|
|
|
|
- if (adjustedGasPrice > maxGasPrice) {
|
|
|
- adjustedGasPrice = maxGasPrice;
|
|
|
+ // 如果 gasPrice 太高,限制为不会超过 1 ACC 的值
|
|
|
+ if (adjustedGasPrice * gasLimit > maxTotalFee) {
|
|
|
+ adjustedGasPrice = maxTotalFee / gasLimit;
|
|
|
}
|
|
|
|
|
|
const tokenContract = new web3.eth.Contract(pubData, tokenAddress);
|
|
@@ -132,7 +157,7 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
// gas 估算
|
|
|
const receiveMethod = type.toLowerCase() === 'wgt' ? 'receiveWGT' : 'receiveSTT';
|
|
|
const gasEstimate = await exchangeContract.methods[receiveMethod](tokenAmount).estimateGas({ from: sender });
|
|
|
- const gasLimit = Math.ceil(Number(gasEstimate) * 1.1);
|
|
|
+ gasLimit = Math.ceil(Number(gasEstimate) * 1.1);
|
|
|
const minGasNeeded = BigInt(gasLimit) * adjustedGasPrice; // Use BigInt
|
|
|
const ethBalance = await web3.eth.getBalance(sender);
|
|
|
console.log('估算gas:', minGasNeeded)
|
|
@@ -164,7 +189,7 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-const WGTForToken = (pk, num) => tokenSignUp(pk, num, 'wgt');
|
|
|
-const STTForToken = (pk, num) => tokenSignUp(pk, num, 'stt');
|
|
|
+const WGTForToken = (pk, num, GasRate) => tokenSignUp(pk, num, GasRate, 'wgt');
|
|
|
+const STTForToken = (pk, num, GasRate) => tokenSignUp(pk, num, GasRate, 'stt');
|
|
|
|
|
|
export { getBalance, sendSingleReward, WGTForToken, STTForToken };
|