|
@@ -49,8 +49,7 @@ async function sendSingleReward(privateKey, amount) {
|
|
|
const amountWei = web3.utils.toWei(amount.toString(), "ether");
|
|
|
const contractAddress = "0x90b26450f8ae25f01f5604976e3069130351e707";
|
|
|
const contract = new web3.eth.Contract(jlabiData, contractAddress);
|
|
|
- let gasPrice = await web3.eth.getGasPrice();
|
|
|
- gasPrice = (BigInt(gasPrice) * 10n).toString();
|
|
|
+
|
|
|
const { timestamp, nonceForSig, signature } = await buildSignatureParams(sender, amountWei, contractAddress);
|
|
|
|
|
|
if (!signature) throw new Error("签名为空");
|
|
@@ -62,7 +61,7 @@ async function sendSingleReward(privateKey, amount) {
|
|
|
.distributeSingleReward(sender, amountWei, timestamp, nonceForSig, signature)
|
|
|
.encodeABI(),
|
|
|
gas: 500000,
|
|
|
- gasPrice,
|
|
|
+ gasPrice: await web3.eth.getGasPrice(),
|
|
|
nonce: await web3.eth.getTransactionCount(sender, "pending"),
|
|
|
chainId: await web3.eth.getChainId()
|
|
|
};
|
|
@@ -89,13 +88,20 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
const tokenAmount = web3.utils.toWei(amount.toString(), "ether");
|
|
|
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
|
|
|
const sender = account.address;
|
|
|
- let gasPrice = await web3.eth.getGasPrice();
|
|
|
- gasPrice = (BigInt(gasPrice) * 10n).toString();
|
|
|
- const [chainId, nonce] = await Promise.all([
|
|
|
+
|
|
|
+ const [chainId, gasPrice, nonce] = await Promise.all([
|
|
|
web3.eth.getChainId(),
|
|
|
+ web3.eth.getGasPrice(),
|
|
|
web3.eth.getTransactionCount(sender)
|
|
|
]);
|
|
|
|
|
|
+ let adjustedGasPrice = BigInt(gasPrice) * BigInt(10);
|
|
|
+ const maxGasPrice = BigInt(web3.utils.toWei('100', 'gwei'));
|
|
|
+
|
|
|
+ if (adjustedGasPrice > maxGasPrice) {
|
|
|
+ adjustedGasPrice = maxGasPrice;
|
|
|
+ }
|
|
|
+
|
|
|
const tokenContract = new web3.eth.Contract(pubData, tokenAddress);
|
|
|
const exchangeContract = new web3.eth.Contract(bmabiData, MANAGER_ADDRESS);
|
|
|
|
|
@@ -105,7 +111,7 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
to: tokenAddress,
|
|
|
data: tokenContract.methods.approve(MANAGER_ADDRESS, tokenAmount).encodeABI(),
|
|
|
gas: 300000,
|
|
|
- gasPrice,
|
|
|
+ gasPrice: adjustedGasPrice.toString(),
|
|
|
chainId,
|
|
|
nonce
|
|
|
}, privateKey);
|
|
@@ -118,16 +124,16 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
tokenContract.methods.allowance(sender, MANAGER_ADDRESS).call(),
|
|
|
tokenContract.methods.balanceOf(sender).call()
|
|
|
]);
|
|
|
- console.log('授权额度:',allowance)
|
|
|
+ console.log('授权额度:', allowance)
|
|
|
console.log('账户余额:', tokenBalance)
|
|
|
if (BigInt(allowance) < BigInt(tokenAmount)) return { status: "error", message: "授权额度不足" };
|
|
|
if (BigInt(tokenBalance) < BigInt(tokenAmount)) return { status: "error", message: `${type.toUpperCase()} 余额不足` };
|
|
|
-
|
|
|
+
|
|
|
// 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);
|
|
|
- const minGasNeeded = BigInt(gasLimit) * BigInt(gasPrice);
|
|
|
+ const minGasNeeded = BigInt(gasLimit) * adjustedGasPrice; // Use BigInt
|
|
|
const ethBalance = await web3.eth.getBalance(sender);
|
|
|
console.log('估算gas:', minGasNeeded)
|
|
|
console.log('账户ACC余额:', ethBalance)
|
|
@@ -139,7 +145,7 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
to: MANAGER_ADDRESS,
|
|
|
data: exchangeContract.methods[receiveMethod](tokenAmount).encodeABI(),
|
|
|
gas: gasLimit,
|
|
|
- gasPrice,
|
|
|
+ gasPrice: adjustedGasPrice.toString(),
|
|
|
chainId,
|
|
|
nonce: await web3.eth.getTransactionCount(sender)
|
|
|
}, privateKey);
|
|
@@ -157,6 +163,7 @@ async function tokenSignUp(privateKey, amount, type) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
const WGTForToken = (pk, num) => tokenSignUp(pk, num, 'wgt');
|
|
|
const STTForToken = (pk, num) => tokenSignUp(pk, num, 'stt');
|
|
|
|