The contract in reference can be found at - https://rinkeby.etherscan.io/address/0x0e04ca9e56af778424b94eb904da9d97a63582f8
I'm trying to call the payUser function where the user sends some eth and based on the amount of eth the user is transferred some tokens.
The code written in javascript is
var current_user_account = ""
var abi_token_contract = ""
var token_contract = ""
const address_token_contract = "0x0E04Ca9E56AF778424B94EB904Da9D97A63582F8"
const web = new Web3("https://rinkeby.infura.io/v3/XXXXXXXXXXXXXXXXX")
$.ajax({
url: "https://api-rinkeby.etherscan.io/api?module=contract&action=getabi&address=0x0E04Ca9E56AF778424B94EB904Da9D97A63582F8&apikey=XXXXXXXXXXXXXXXXX",
dataType: "json",
success: function (data) {
abi_token_contract = JSON.parse(data.result)
token_contract = new web.eth.Contract(abi_token_contract, address_token_contract)
}
});
window.addEventListener('load', async () => {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
current_user_account = accounts[0]
} catch (error) {
if (error.code === 4001) {
// User rejected request
}
setError(error);
}
window.ethereum.on('accountsChanged', (accounts) => {
current_user_account = accounts[0]
});
} else {
window.alert(
"Non-Ethereum browser detected. You should consider trying MetaMask!"
);
}
})
async function send(transaction, value = 0) {
const params = [{
from: current_user_account,
to: transaction._parent._address,
data: transaction.encodeABI(),
gas: '0x186a0',
gasPrice: null,
value: web.utils.toHex(value)
},]
window.ethereum.request({
method: 'eth_sendTransaction',
params,
})
}
function getTokens() {
token_count_rs = parseFloat($("#token_num").val().trim()) * 10
$.ajax({
url: "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=INR&tsyms=ETH",
dataType: "json",
success: function (data) {
amount_tobe_payed = (data.RAW.INR.ETH.OPEN24HOUR * token_count_rs).toFixed(18);
send(token_contract.methods.payUser(address_token_contract, current_user_account), web.utils.toWei(amount_tobe_payed, "ether"))
}
});
}
I'm not understanding why the transaction is failing with javascript and working on etherscan "Write" section