Related
Trying to swap exact tokens for tokens using ethers.js - for some reason, I can't get it to work past the approve stage.
Here's a little code snippet:
const provider = new ethers.providers.WebSocketProvider(provider_address)
const wallet = ethers.Wallet.fromMnemonic(mnemonic)
const account = wallet.connect(provider)
const erc20_ABI =
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
const STG_address = '0x2F6F07CDcf3588944Bf4C42aC74ff24bF56e7590'
const STG_contract = new ethers.Contract(
STG_address,
erc20_ABI,
account
)
const init = async () => {
console.log('before contract approve')
//
let receipt = await STG_contract.approve(account, ethers.utils.parseUnits('1000.0', 18), { gasLimit: 100000, gasPrice: 5e9 });
console.log('await contract approve happened')
}
init()
Running the above results in a 'invalid address or ENS name' error.
If I change the let receipt... part to:
let receipt = await STG_contract.approve('my actual wallet address', ethers.utils.parseUnits('1000.0', 18), { gasLimit: 100000, gasPrice: 5e9 });
Then the code executes, however the contract approve never resolves
What am I doing wrong?
Thanks
I hope someone can enlighten me. I have a website with a button which should create a new Contract object of my token contract via the ethers.js library. I successfully connected my metamask with the website but when I click the button there is an error message like this:
ethers-5.2.umd.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'map')
at new Interface (ethers-5.2.umd.min.js:1:199125)
at BaseContract.getInterface (ethers-5.2.umd.min.js:1:321623)
at Contract.BaseContract (ethers-5.2.umd.min.js:1:317578)
at new Contract (ethers-5.2.umd.min.js:1:329138)
at buyTicket ((index):707:25)
at HTMLButtonElement.onclick ((index):274:91)
This is my button code
<button class="btn btn-primary" type="button" id="buyTicket" onclick="buyTicket()">
Buy Ticket
</button>
And this is my JavaScript section at the bottom of my html.file
<script src='https://cdn.jsdelivr.net/npm/bignumber.js#9.0.2/bignumber.min.js'></script>
<!--<script src='https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.3/web3.min.js'></script>-->
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript">
</script>
<script>
const provider = new ethers.providers.Web3Provider(window.ethereum)
provider.send("eth_requestAccounts", []);
const signer = provider.getSigner()
//let tokenContractAddress = "0xf209CE1960Fb7E750ff30Ba7794ea11C6Acdc1f3";
let tokenContractAddress = "0xb37f4a11045FB48A89fb4930C72481fce9942E07";
let lotteryContractAddress = "0x3E03D4595b164F5C2098a5D154d7f2Bf516ba102";
// Use BigNumber
let decimals = BigInt(18);
let amount = BigInt(5);
const tokenABI = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
];
function buyTicket(lotteryContractAddress, tokenContractAddress, amount, tokenABI, provider) {
const tokenInstance = new ethers.Contract(tokenContractAddress, tokenABI, provider);
}
</script>
After that I want to interact with that Contract instance, but I am not able to create it and don't know what I am missing here.
I finally did it. I simply had to put the whole thing into the function section so that the variables were created after pushing the button. So this is the working code, i hope that helps someone fixing the same error faster than me :-)
<script src='https://cdn.jsdelivr.net/npm/bignumber.js#9.0.2/bignumber.min.js'></script>
<!--<script src='https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.3/web3.min.js'></script>-->
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript">
</script>
<script>
function buyTicket() {
const provider = new ethers.providers.Web3Provider(window.ethereum)
provider.send("eth_requestAccounts", []);
const signer = provider.getSigner()
//let tokenContractAddress = "0xf209CE1960Fb7E750ff30Ba7794ea11C6Acdc1f3";
let tokenContractAddress = "0xb37f4a11045FB48A89fb4930C72481fce9942E07";
let lotteryContractAddress = "0x3E03D4595b164F5C2098a5D154d7f2Bf516ba102";
// Use BigNumber
let decimals = BigInt(18);
let amount = BigInt(5);
const tokenABI = [{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "balanceOf",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [{
"internalType": "uint8",
"name": "",
"type": "uint8"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [{
"internalType": "string",
"name": "",
"type": "string"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [{
"internalType": "string",
"name": "",
"type": "string"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}
];
const tokenInstance = new ethers.Contract(tokenContractAddress, tokenABI, provider);
//Do something with this instance here
}
</script>
I was able to call the contract, its properties and get the addresses in my html file. My dapp uses metamask and the metamask portion is working fine. However, when I attempted to call a method from the smart contract (it is similar to OpenZepplin's ERC20), the error in the title appears. Below is the relevant parts of my code (i used pure JS):
web3 = new Web3(new Web3.providers.HttpProvider(the_provider_link_which_is_correct));
const privateKey = my_private_key
const loginButton = document.getElementById('loginButton')
const userWallet = document.getElementById('userWallet')
async function loginWithMetaMask() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
.catch((e) => {
console.error(e.message)
return
})
if (!accounts) { return }
else{
web3.eth.defaultAccount = accounts[0]
console.log(web3.eth.defaultAccount)
}
}
loginWithMetaMask()
var key = prompt("What is the key?")
console.log(key)
var coinABI = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "balances",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
},
{
"name": "",
"type": "address"
}
],
"name": "allowed",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "remaining",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"name": "_initialAmount",
"type": "uint256"
},
{
"name": "_tokenName",
"type": "string"
},
{
"name": "_decimalUnits",
"type": "uint8"
},
{
"name": "_tokenSymbol",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_owner",
"type": "address"
},
{
"indexed": true,
"name": "_spender",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]
var tokenContractAddress= token_contract_address
var tokenContract = web3.eth.contract(coinABI).at(tokenContractAddress);
// view number of tokens (this part is alright)
function viewBalance(){
const Addr = $('#viewAddr').val();
console.log(Addr)
const balance = tokenContract.balanceOf(Addr);
console.log(balance)
$("#balance").html(balance.c/1000);
}
// view your balance (this part is fine)
function viewCurrBalance(){
const balance = tokenContract.balanceOf(window.userWalletAddress );
console.log(balance.c)
$("#balance").html(balance.c/1000)
}
// you send to another address (THIS IS THE PROBLEM)
function sendTokens(){
console.log()
console.log($("#targetAddr").val())
console.log($("#amt").val())
// sendData(window.userWalletAddress,$("#targetAddr").val(),$("#amt").val())
tokenContract.transfer($("#targetAddr").val().toString(),$("#amt").val())
}
I had deployed my smart contracts and my transaction was also completed in metamask but when I try to display my result by calling a function of my smart contract,I had checked the data types and all but I'm getting error like this
Error: Returned values aren't valid, did it run Out of Gas?
at i.decodeParameters (web3.min.js:10290)
at o._decodeMethodReturn (web3.min.js:12156)
at _.outputFormatter (web3.min.js:12196)
at _.formatOutput (web3.min.js:7396)
at u (web3.min.js:7437)
at web3.min.js:8026
at s._handle (inpage.js:17) undefined
I don't know why it is not decoding info in web3.min.js ,and my js code is
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'));
}
// Set Contract Abi
var contractAbi = [{
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "DistributorAdded",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "DistributorRemoved",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": false,
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "Made",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "ManufacturerAdded",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "ManufacturerRemoved",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": false,
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "Packed",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": false,
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "Sold",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "UserAdded",
"type": "event"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "UserRemoved",
"type": "event"
}, {
"constant": false,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "addDistributor",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "addManufacturer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "addUser",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": true,
"inputs": [{
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "fetchMedicineBufferOne",
"outputs": [{
"internalType": "string",
"name": "batchno",
"type": "string"
}, {
"internalType": "string",
"name": "medicineName",
"type": "string"
}, {
"internalType": "address",
"name": "ownerID",
"type": "address"
}, {
"internalType": "address",
"name": "originManufacturerID",
"type": "address"
}, {
"internalType": "string",
"name": "dosage",
"type": "string"
}, {
"internalType": "enum main.State",
"name": "medicineState",
"type": "uint8"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": true,
"inputs": [{
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "fetchMedicineBufferTwo",
"outputs": [{
"internalType": "string",
"name": "batchno",
"type": "string"
}, {
"internalType": "string",
"name": "FactoryName",
"type": "string"
}, {
"internalType": "string",
"name": "mfgdate",
"type": "string"
}, {
"internalType": "string",
"name": "expdate",
"type": "string"
}, {
"internalType": "address",
"name": "distributorID",
"type": "address"
}, {
"internalType": "address",
"name": "userID",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": true,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "isDistributor",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": true,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "isManufacturer",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": true,
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "isUser",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"constant": false,
"inputs": [{
"internalType": "string",
"name": "_batchno",
"type": "string"
}, {
"internalType": "string",
"name": "_medicineName",
"type": "string"
}, {
"internalType": "string",
"name": "_dosage",
"type": "string"
}, {
"internalType": "address",
"name": "_originManufacturerID",
"type": "address"
}, {
"internalType": "address",
"name": "_ownerID",
"type": "address"
}, {
"internalType": "string",
"name": "_FactoryName",
"type": "string"
}, {
"internalType": "string",
"name": "_mfgdate",
"type": "string"
}, {
"internalType": "string",
"name": "_expdate",
"type": "string"
}, {
"internalType": "enum main.State",
"name": "Made",
"type": "uint8"
}, {
"internalType": "address",
"name": "_distributorID",
"type": "address"
}, {
"internalType": "address",
"name": "_userID",
"type": "address"
}],
"name": "makeMedicine",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [{
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "packMedicine",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [{
"internalType": "string",
"name": "_batchno",
"type": "string"
}],
"name": "receiveMedicine",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "renounceDistributor",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "renounceManufacturer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}, {
"constant": false,
"inputs": [],
"name": "renounceUser",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]; // Add Your Contract ABI here!!!
// Set Contract Address
var contractAddress = '0xd9145CCE52D386f254917e481eB44e9943F39138'; // Add Your Contract address here!!!
// Set the Contract
var contract = new web3.eth.Contract(contractAbi, contractAddress);
$('form').on('submit', function(event) {
event.preventDefault();
batchno = $("#batchno").val();
console.log(batchno)
//Display the Medicine
contract.methods.fetchMedicineBufferOne(batchno).call(function(err, result) {
console.log(err, result)
$('#ftc-medicine').html(result);
});
});
my smart contract function is
function fetchMedicineBufferOne(string memory _batchno) public view returns
(
string memory batchno, //the primary key batchno
string memory medicineName, //Medicine name
address ownerID,
address originManufacturerID,
string memory dosage,
State medicineState
)
}
return
(
medicines[_batchno].batchno,
medicines[_batchno].medicineName,
medicines[_batchno].ownerID,
medicines[_batchno].originManufacturerID,
medicines[_batchno].dosage,
medicines[_batchno].medicineState
);
}
the medicines array is created and values are stored in this function
function makeMedicine(string memory _batchno, string memory _medicineName,string memory _dosage, address _originManufacturerID,address _ownerID, string memory _FactoryName, string memory _mfgdate,string memory _expdate,State,address _distributorID,address _userID) public
onlyManufacturer
{
// Add the new medicine as part of medicines
Medicine memory makeMedicine = Medicine({
batchno:_batchno,
medicineName:_medicineName,
dosage:_dosage,
originManufacturerID:_originManufacturerID,// Metamask-Ethereum address of the Manufacturer
ownerID:_ownerID, // Metamask-Ethereum address of the current owner as the medicine moves through 3 stages
FactoryName:_FactoryName, // Manufacturer Name
mfgdate:_mfgdate,//mfgdate
expdate:_expdate,//expdate
medicineState: State.Made,
distributorID: _distributorID,
userID: _userID
});
medicines[_batchno] = makeMedicine;
medicines[_batchno].medicineState = State.Made;
I had ran compiled and deployed my contracts in ganache and I had made deployments in remix also,, in which functions ran properly
where am I getting error ?
var events = require('events');
const express = require('express');
const router = express.Router();
const tokenbalance = require('./tokenbalance')
//const accCollection = require('../models/account.js');
const keythereum = require("keythereum");
const request = require("request");
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const config = require('../../config');
const log = require('log4js').getLogger("tokenadmin");
var erc20Abi = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "version",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_extraData",
"type": "bytes"
}
],
"name": "approveAndCall",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "remaining",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"payable": false,
"stateMutability": "nonpayable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_owner",
"type": "address"
},
{
"indexed": true,
"name": "_spender",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
]
const Abiarray = erc20Abi;
console.log(Abiarray);
const web3 = new Web3(new Web3.providers.HttpProvider(config.rpcURL));
//event Transfer(myAddress,toAddress,amount);
let Tokenadmin ={
tokenadmin :function(toAddress,myAddress,amount,contractAddress,callback) {
//set from address,to address and token amount
let tokena = web3.eth.contract(Abiarray).at(contractAddress);
// console.log(tokena);
console.log("bbbbbbbbbbbb");
console.log(toAddress)
console.log(myAddress)
console.log(amount)
console.log(contractAddress)
amount1 = web3.toHex(amount)//To convert any given value to HEX
console.log("Amount to transfer" +amount)
tokena.balanceOf.call(myAddress, function (err, balance) {
console.log("BALNCE B4 TRANSFER" +balance);
})
tokena.balanceOf.call(toAddress,function(err,result) {
console.log("BALANCE B4 TRANSFER OF TOADDRESS"+result);
})
var privatekey = new Buffer('cc479124b293d2abd98ea4a57f2670ef7671c90d1eaa382258618d23440b12c2', 'hex')
// //To get nonce
// get transaction count, later will used as nonce
web3.eth.getTransactionCount(myAddress,function(err,v) {
console.log(v);
var count = v;
console.log(toAddress,"aaaaaaaaaa", amount);
var rawTransaction = {"from":myAddress,
"gasPrice":web3.toHex(2 * 1e9),
"gasLimit":web3.toHex(210000),
"to": contractAddress,
"value":"0x0",
//"data" :tokena,
"data" : tokena.transfer.getData(toAddress,amount1),
"nonce":web3.toHex(count),
"chainid" : "0x04"
}
//console.log(rawTransaction);
var transaction = new Tx(rawTransaction);
transaction.sign(privatekey);
let serializedTx = transaction.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
console.log("Response",err,hash);
if (!err){
console.log(hash);
//filter.emit('Transfer','Event emitted')
}
else
console.log("HASHERROR"+err);
return callback ({hash:hash})
//})
});
let filter =web3.eth.filter('Transfer',{fromBlock:2383351,toBlock:'latest'})
console.log("Filter" +filter);
// watch for changes
filter.watch(function(error, result){
if (!error)
console.log(result);
});
tokena.balanceOf.call(myAddress, function (err, balance) {
console.log("BALANCE AFTER "+balance);
})
})
}
}
module.exports =Tokenadmin;
I want to use event Transfer (erc20 contract) in my js file to get the transaction details for getting the transaction details of token transfer. I have passed the abi of erc20 token contract in the js code. Also transfer function is working.
For the Transfer event, I tried using:
.getpastevents,
.watch,
.allevents,
.web3.eth.filter
in my node js code.
Anyways I could see the event log in myetherscan.
Also Transfer event is there in the erc20 contract abi.
But could not see the transfer event log in my console and postman.
Can anybody please help to solve this issue?
I sorted the issue.Its because infura wont support events and filters .So I used another method using web3 to solve this issue.And finally got it solved.Thanks!
Anu