Smart contract returns empty - solidity

I have a smart contract as you can see below:
pragma solidity ^0.5.2;
contract Coursetro {
string fName="foo";
uint age=123;
function setInstructor(string memory _fName, uint _age) public {
fName = _fName;
age = _age;
}
function getInstructor() public view returns (string memory,uint){
return (fName,age);
}
}
I want to call my function from js with web3. I use testrpc. Here is my js code:
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
var CoursetroContract = new web3.eth.Contract([ { "constant": false, "inputs": [ { "internalType": "string", "name": "_fName", "type": "string" }, { "internalType": "uint256", "name": "_age", "type": "uint256" } ], "name": "setInstructor", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getInstructor", "outputs": [ { "internalType": "string", "name": "", "type": "string" }, { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" } ], '0x346dE53Bc9B3a26aeE96f919e471962F5A31d58F');
web3.eth.getAccounts().then(e => web3.eth.defaultAccount = e[0]);
CoursetroContract.methods.getInstructor().call((err, result) => console.log(result))
Everything looks well on remix but when i try to run this js code on my local it writes console empty things like p {0: "", 1: "0"}. But i expect something like p {0: "foo", 1: "123"}. Because i initialized data.

Related

why the onlyowner function type of solidity is showing result by all the wallet adress?

I am using this solidity onlywoner code
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract vote{
address public participant1 = 0xF6C486B8A4e67b8eff4d5045C804E9be4ed39FF9;
address public participant2 = 0xBBDb2A08711D7b2b9c15318E77B6e026eD8fA278;
mapping(address=>uint) user;
mapping(address=>bool) chek;
address public owner;
constructor(){
owner = msg.sender;
}
modifier onlyonwner(){
require(owner == msg.sender,"You are not allowed to declare the result");
_;
}
function participant1_vote() public {
require(msg.sender !=participant1,"You can not vote to yourself");
require(chek[msg.sender]!=true,"You have already voted");
user[participant1]++;
chek[msg.sender] = true;
}
function participant2_vote() public {
require(msg.sender !=participant2,"You can not vote to yourself");
require(chek[msg.sender]!=true,"You have already voted");
user[participant2]+=1;
chek[msg.sender] = true;
}
function pati1_cnt_VOTE() view public onlyonwner returns(uint){
return user[participant1];
}
function pati2_cnt_VOTE() view public onlyonwner returns(uint){
return user[participant2];
}
function declare_winner() view public onlyonwner returns(string memory){
if(user[participant1]>user[participant2]){
return ("Partcipant1 is wiiner !!");
}
else if(user[participant1]==user[participant2]){
return("Both the participant are equal");
}
else{
return ("Participant 2 is winenr");
}
}
}
In this the function which are the onlywoner is only acessed by the owner but this is being acessed by all the wallet adrress .
You can see my frontend code ->
if(typeof window.ethereum =="undefined"){
console.log("PLease install the metamask");
}
var accounts;
let web3 = new Web3(window.ethereum);
let contract = new web3.eth.Contract(
[
{
"inputs": [],
"name": "participant1_vote",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "participant2_vote",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "declare_winner",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "participant1",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "participant2",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "pati1_cnt_VOTE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "pati2_cnt_VOTE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
],
"0x78AC9c94Bfd5F64b14515F575791d1822B194E48"
);
async function account(){
accounts = await web3.eth.requestAccounts();
contract.methods.owner().call().then(function (resp){
const owner_address = resp;
$("#btn").click(async function(){
accounts1 = await web3.eth.requestAccounts();
contract.methods.participant1_vote().send({from:String(accounts1)},function(err,res){
if(err){
console.log("error"+err);
}
else{
console.log("Voted to the participant1 sucessfully");
}
});
});
$("#btn1").click(async function(){
accounts2 = await web3.eth.requestAccounts();
alert(accounts2);
contract.methods.participant2_vote().send({from: String(accounts2)},function(err,res){
if(err){
console.log("error"+err);
}
else{
console.log("Voted to the participant2 sucessfully");
}
});
});
$("#res").click(async function() {
contract.methods.declare_winner().call(function(err,res){
if(err){
alert("U r not the owner");
}
else{
contract.methods.declare_winner().call().then(function (resp){
alert(resp);
})
}
});
// const res = await contract.methods.declare_winner().call({from:owner_address})
// console.log(res);
});
$("#show-vote1").click(async function(){
// calling the return method of the solidity fucntion
contract.methods.pati1_cnt_VOTE().call().then(function (resp){
alert("The vote for the candidate 1 is ->"+resp);
})
});
$("#show-vote2").click(async function(){
// calling the return method of the solidity fucntion
contract.methods.pati2_cnt_VOTE().call().then(function (resp){
alert("The vote for the candidate 2 is ->"+resp);
});
});
});
}
account();
plsease give a solution that only owner of the contract is being able to get the onlyowner restricted address .
// instead of public, private is better, noone can get from outside
address payable private owner;
constructor(){
// type of msg.sender is payable
owner = payable(msg.sender);
}

× Unhandled Rejection (Error): invalid fragment object (argument="value", value=

I am new to Smart Contracts.
I am unable to read a smart contract from Binance test network. I keep getting an Unhandled Rejection every time I attempt to read my smart contract using ethers.js.
Error
Unhandled Rejection (Error): invalid fragment object (argument="value", value=[{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_sr_number","type":"string"},{"internalType":"string","name":"_branch","type":"string"},{"internalType":"string","name":"_date","type":"string"},{"internalType":"string","name":"_grade","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"branch","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"date","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getName","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"grade","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sr_number","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}], code=INVALID_ARGUMENT, version=abi/5.0.13)
My code
const first_div = async e => {
e.preventDefault();
let abi = [
[
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_sr_number",
"type": "string"
},
{
"internalType": "string",
"name": "_branch",
"type": "string"
},
{
"internalType": "string",
"name": "_date",
"type": "string"
},
{
"internalType": "string",
"name": "_grade",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "branch",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "date",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "grade",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "sr_number",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
]
];
let provider = ethers.getDefaultProvider();
let contractAddress = "0x60aD9FCD26092de54CDF3Ff83900a5B5a52887Ab";
let contract = new ethers.Contract(contractAddress, abi, provider);
let currentValue = await contract.getName();
alert(currentValue);
};
My solidity file
pragma solidity ^0.8.0;
contract FactoryContract {
address public owner = msg.sender;
address [] public createdContracts;
event ContractCreated(address contractAddress);
function createContract(string memory _name, string memory _sr_number, string memory _branch, string memory _date, string memory _grade) public {
address newContract = address(new Contract(msg.sender, _name, _sr_number, _branch, _date, _grade));
emit ContractCreated(newContract);
createdContracts.push(newContract);
}
function getDeployedContracts() public view returns (address[] memory)
{
return createdContracts;
}
function getOwner() public view returns (address)
{
return msg.sender;
}
}
contract Contract {
address public owner;
string public name;
string public sr_number;
string public branch;
string public date;
string public grade;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor (address _owner, string memory _name, string memory _sr_number, string memory _branch, string memory _date, string memory _grade) {
owner = _owner;
name = _name;
sr_number=_sr_number;
branch = _branch;
date = _date;
grade = _grade;
}
function getName() public view returns (string memory)
{
return name;
}
}
Where am I going wrong? I have deployed the contracts using a factory contract. I've used the ABI of the contract and not the ABI of the factory contract.
Remove the extra set of square brackets when you're declaring your abi variable. There should only be one set of square brackets surrounding the ABI.

solidity, how to send tuple array data to contract

my code :
{
"constant": false,
"inputs": [
{
"name": "_id",
"type": "uint256"
},
{
"components": [
{
"name": "trait_type",
"type": "string"
},
{
"name": "display_type",
"type": "string"
},
{
"name": "value",
"type": "uint8"
}
],
"name": "_attr",
"type": "tuple[]"
}
],
"name": "pushAttribute",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
This abi receives a tuple array as input data.
so, I tried encoding according to the data condition.
how can i make send to data tuple array???
If you're sending the value from a JS library, you can just pass an array of JS objects. Example using web3:
const id = 1;
const attr = [
{trait_type: 'foo', display_type: 'bar', value: 1}
];
await contract.methods.pushAttribute(id, attr).send();

Pass an already existing Model to next in Loopback

There is Project model
{
"name": "Project",
"plural": "Projects",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"title": {
"type": "string",
"required": true
},
"description": {
"type": "string"
},
"code": {
"type": "string"
},
"startDate": {
"type": "date",
"required": true
},
"endDate": {
"type": "date"
},
"value": {
"type": "number"
},
"infoEN": {
"type": "string"
},
"infoRU": {
"type": "string"
},
"infoAM": {
"type": "string"
},
"externalLinks": {
"type": [
"string"
]
}
},
"validations": [],
"relations": {
"industry": {
"type": "belongsTo",
"model": "Industry",
"foreignKey": "",
"options": {
"nestRemoting": true
}
},
"service": {
"type": "belongsTo",
"model": "Service",
"foreignKey": "",
"options": {
"nestRemoting": true
}
},
"tags": {
"type": "hasAndBelongsToMany",
"model": "Tag",
"foreignKey": "",
"options": {
"nestRemoting": true
}
}
},
"acls": [],
"methods": {}
}
And it hasAndBelongsToMany tags
here is Tag model
{
"name": "Tag",
"plural": "Tags",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Now when the relation is created loopback api gives this api endpoint.
POST /Projects/{id}/tags
This creates a new tag into the tags collection and adds it to the project.
But what about adding an already existing tag to the project?
So I figured maybe I add before save hook to the Tag
Here I'll check if the tag exists and then pass the existing one for the relation.
Something like this.
tag.js
'use strict';
module.exports = function(Tag) {
Tag.observe('before save', function(ctx, next) {
console.log(ctx.instance);
Tag.find({name: ctx.instance.name})
next();
});
// Tag.validatesUniquenessOf('name', {message: 'name is not unique'});
};
#HaykSafaryan it just demo to show you how to use tag inside project
var app = require('../../server/server');
module.exports = function(project) {
var tag=app.models.tags
//afterremote it just demo. you can use any method
project.afterRemote('create', function(ctx, next) {
tag.find({name: ctx.instance.name},function(err,result)){
if(err) throw err;
next()
}
});
};
this is just example code to show you how to use update,create ,find ,upsertwithwhere etc. tag for validation you have to setup condition over here it will not take validation which you defined in tags models

Failed to execute 'send' on 'XMLHttpRequest' for only one url in Loopback

Strongloop/Loopback node.js server used with 'ng-admin' editor and sqlite db. I need to get count of entities:
var Httpreq = new XMLHttpRequest();
Httpreq.open('GET', yourUrl, false);
Httpreq.send(null);
return Httpreq.responseText;
where yourUrl is like http://localhost:3000/api/v1/entity/count.
All urls works except one of entity named 'Advertisement', I have angular.js:12783 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load http://localhost:3000/api/v1/advertisement/count. This url works in API explorer.
Advertisement.json:
{
"name": "Advertisement",
"base": "EntityBase",
"plural": "Advertisement",
"options": {
"validateUpsert": true,
"sqlite3": {
"table": "advertisement"
}
},
"properties": {
"name": {
"type": "string",
"required": true
},
"category_id": {
"type": "number"
},
"due_date": {
"type": "string"
},
"from_date": {
"type": "string"
},
"phone": {
"type": "string"
},
"site": {
"type": "string"
}
},
"validations": [],
"relations": {
"category": {
"type": "belongsTo",
"model": "Category",
"foreignKey": "category_id"
},
"photos": {
"type": "hasMany",
"model": "Photo",
"foreignKey": "advertisement_id"
}
},
"acls": [],
"methods": {},
"mixins": {
"Timestamp": {},
"SoftDelete": {},
"GenderAge": {},
"Descripted": {}
}
}
Renamed entity to Commercial to fix this.