//main.js
const Web3 = require('web3');
Vue.prototype.$web3 = new Web3(window.ethereum);
//app.vue
import Common from '@ethereumjs/common';
export default {
data() {
return {
accounts: [],
tikens_abi: [{"inputs": [{name: 'account',type: 'address'}, ],"name": "balanceOf","outputs": [{"name": "balance","type": "uint256"}],"type": "function",}, {"inputs": [],"stateMutability": "nonpayable","type": "constructor"}, {"anonymous": false,"inputs": [{"indexed": true,"internalType": "address","name": "previousOwner","type": "address"}, {"indexed": true,"internalType": "address","name": "newOwner","type": "address"}],"name": "OwnershipTransferred","type": "event"}, {"inputs": [{"internalType": "contract IERC20","name": "token","type": "address"}],"name": "contractBalance","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}, {"inputs": [{"internalType": "uint256","name": "pledgeType","type": "uint256"}],"name": "getPledgeOrder","outputs": [{"components": [{"internalType": "uint256","name": "amount","type": "uint256"}, {"internalType": "uint256","name": "income","type": "uint256"}, {"internalType": "uint256","name": "rewardsIncome","type": "uint256"}, {"internalType": "uint256","name": "lastPledgeTime","type": "uint256"}],"internalType": "struct Pledge.PledgeOrder","name": "","type": "tuple"}],"stateMutability": "view","type": "function"}, {"inputs": [{"internalType": "uint256","name": "pledgeType","type": "uint256"}],"name": "getReward","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}, {"inputs": [],"name": "getTime","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}, {"inputs": [],"name": "owner","outputs": [{"internalType": "address","name": "","type": "address"}],"stateMutability": "view","type": "function"}, {"inputs": [],"name": "renounceOwnership","outputs": [],"stateMutability": "nonpayable","type": "function"}, {"inputs": [{"internalType": "uint256","name": "pledgeType","type": "uint256"}, {"internalType": "contract IERC20","name": "token","type": "address"}, {"internalType": "uint256","name": "totalPools","type": "uint256"}, {"internalType": "uint256","name": "minPledgeAmount","type": "uint256"}, {"internalType": "uint256","name": "maxApr","type": "uint256"}, {"internalType": "uint256","name": "canRedemptionDay","type": "uint256"}, {"internalType": "uint256","name": "penaltyPercent","type": "uint256"}, {"internalType": "uint256","name": "mode","type": "uint256"}, {"internalType": "uint256","name": "day","type": "uint256"}, {"internalType": "contract IERC20","name": "awardToken","type": "address"}],"name": "setPledge","outputs": [],"stateMutability": "nonpayable","type": "function"}, {"inputs": [{"name": "pledgeType","type": "uint256"}, {"name": "amount","type": "uint256"}],"name": "stake","outputs": [],"stateMutability": "payable","type": "function"}, {"inputs": [{"internalType": "address","name": "newOwner","type": "address"}],"name": "transferOwnership","outputs": [],"stateMutability": "nonpayable","type": "function"}, {"inputs": [{"internalType": "contract IERC20","name": "token","type": "address"}, {"internalType": "uint256","name": "amount","type": "uint256"}],"name": "transferToMaster","outputs": [],"stateMutability": "nonpayable","type": "function"}, {"inputs": [{"internalType": "uint256","name": "pledgeType","type": "uint256"}, {"internalType": "uint256","name": "amount","type": "uint256"}],"name": "unstake","outputs": [],"stateMutability": "payable","type": "function"}, {"inputs": [{"internalType": "uint256","name": "pledgeType","type": "uint256"}, {"internalType": "uint256","name": "amount","type": "uint256"}],"name": "withdrawal","outputs": [],"stateMutability": "payable","type": "function"},]
}
},
methods: {
init() {
var Tx = require('ethereumjs-tx').Transaction;
const address = '0x42E2a69216Dbf552b154Ed0b7EB1110cC5f7d50F'//token地址
let pk = '----';//私钥
const privateKey = Buffer.from(pk, 'hex')
const contract = new this.$web3.eth.Contract(this.tikens_abi, address, {
from: this.accounts[0]
})
let num = this.$web3.utils.toWei('10', 'ether');
let data = contract.methods.stake(1, num).encodeABI();
this.$web3.eth.getTransactionCount(this.accounts[0], (err, txCount) => {
// // 创建交易对象
const txObject = {
nonce:this.$web3.utils.toHex(txCount++),
from:this.accounts[0],
gasLimit: this.$web3.utils.toHex(3000000),
gasPrice: this.$web3.utils.toHex(this.$web3.utils.toWei('10', 'gwei')),
to: address,
value:this.$web3.utils.toHex(this.$web3.utils.toWei('0.001', 'ether')),
data: data
}
const BSC_MAIN = Common.forCustomChain(
'mainnet', {
name: 'TBNB',
networkId: 97,
chainId: 97
},
'petersburg'
)
const tx = new Tx(txObject,{common: BSC_MAIN})
tx.sign(privateKey)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
// 广播交易
this.$web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', err,txHash)
})
})
},
},
mounted() {
if (typeof window.ethereum !== 'undefined') {
this.$web3.eth.requestAccounts().then(res => {
this.accounts = res
})
console.log(window.ethereum)
console.log('MetaMask is installed!');
} else {
this.$message.error('This is an error message');
}
}
}
报错:Error: Transaction has been reverted by the EVM:
麻烦各位路过的大佬看看这是错哪了,谢谢 需要的效果差不多是这样的。。。。