I'm doing estimateGas. here's my code.
var sdate = new Date('2021-07-01 00:00:00');
var edate = new Date('2021-07-31 00:00:00');
var arrDate = [];
arrDate.push(sdate/1000);
arrDate.push(edate/1000);
var arrCategory = [1,12,14];
var param1 = web3.eth.abi.encodeParameter('uint32[]',arrDate);
var param2 = web3.eth.abi.encodeParameter('uint8[]',arrCategory);
let Contract = new web3.eth.Contract(myPack.abi, myPack.ca);
Contract.methods.createTicket(param1, param2).estimateGas({from: accounts[0]})
.then(console.log);
and I met error
Uncaught TypeError: t.map is not a function
at h.formatParam (index.js:218)
at index.js:100
at Array.map ()
at h.encodeParameters (index.js:94)
at index.js:439
at Array.map ()
at Object.d._encodeMethodABI (index.js:438)
at Object.d._processExecuteArguments (index.js:701)
at Object.d._executeMethod (index.js:720)
at estimateGas (issuecfm:257)
I tried something before encodeParameter
BigNumber
var BN = web3.utils.BN;
arrDate = arrDate.map(item => {return new BN(item)});
arrCategory = arrCategory.map(item => {return new BN(item)});
and String
arrDate = arrDate.map(item => {return item.toString()});
arrCategory = arrCategory.map(item => {return item.toString()});
After a lot of searching, I tried what I could. but I still get the same error. I would really appreciate it if you could teach me how to modify my code.
Using the web3 contract helper function, you need to pass the "raw" JS params, not the ABI-encoded data.
let contract = new web3.eth.Contract(myPack.abi, myPack.ca);
// mind the `arrDate` and `arrCategory` instead of `param1` and `param2`
contract.methods.createTicket(arrDate, arrCategory)
.estimateGas({from: accounts[0]})
.then(console.log);
Related
I'm trying to make a js file for multiple HTML files to reference from for various, relatively quickly changing info, That way I don't need to open all of the HTML pages to locate what I'm looking for in each one. As far as I'm aware my code looks fine, but the only thing that leaves me with text is the "Template" id, unless it's written in an HTML file, then I can utilize them all. I am a noob when it comes to some of these things...
class Price{
constructor(price){
this.price = price;
}
}
const Template = new Price("0");
document.getElementById("Template").innerHTML = Template.price;
const DD = new Price("1");
document.getElementById("DD").innerHTML = DD.price;
const PrintDD = new Price("2");
document.getElementById("PrintDD").innerHTML = PrintDD.price;
const FFFF = new Price("3");
document.getElementById("FFFF").innerHTML = FFFF.price;
const TTT = new Price("4");
document.getElementById("TTT").innerHTML = TTT.price;
I wouldn't be surprised if I overcomplicated the coding, but I've been scouring for a solution in an attempt to fix the problem.
If I understand the issue correctly, the problem is, if your code is in a page with no element with id="Template" then your code will fail with an exception at
document.getElementById("Template").innerHTML = Template.price;
This is because document.getElementById("Template") returns null and you can't assign a property (.innerHTML in your case) to null - so the rest of the code won't run after the Uncaught TypeError error is thrown
You could do this
class Price{
constructor(price){
this.price = price;
}
assignToId(id) {
const element = document.getElementById(id);
if (element) {
element.innerHTML = this.price;
}
}
}
const Template = new Price("0");
Template.assignToId("Template");
const DD = new Price("1");
DD.assignToId("DD");
const PrintDD = new Price("2");
PrintDD.assignToId("PrintDD");
const FFFF = new Price("3");
FFFF.assignToId("FFFF");
const TTT = new Price("4");
TTT.assignToId("TTT");
Or even
class Price{
constructor(price, id){
this.price = price;
const element = document.getElementById(id);
if (element) {
element.innerHTML = price;
}
}
}
const Template = new Price("0", "Template");
const DD = new Price("1", "DD");
const PrintDD = new Price("2", "PrintDD");
const FFFF = new Price("3", "FFFF");
const TTT = new Price("4", "TTT");
However, unless you're using the class Price for some other purpose that you haven't shown, the simplest code would be
function price(price, id) {
const element = document.getElementById(id);
if (element) {
element.innerHTML = price;
}
}
price("0", "Template");
price("1", "DD");
price("2", "PrintDD");
price("3", "FFFF");
price("4", "TTT");
I try to pass different amount of arguments in different cases.
I have the next code:
function getByteCode(tokenData, incomeData){
incomeData.volume = Number(incomeData.volume) * Math.pow(10,tokenData.decimals);
incomeData.volume = incomeData.volume.noExponents();
let web3 = new Web3();
let instanceContract = new web3.eth.Contract(abi);
instanceContract.options.address = tokenData.address;
let necessaryMethod = instanceContract.methods[incomeData.methodCall];
let methodCall = necessaryMethod(incomeData.destination_address, incomeData.volume);
return methodCall.encodeABI();
} catch (err) {
sails.log(err);
return {
state: 'fail',
message: 'There is error in creation byte code \n' + err
}
}
}
In necessaryMethod I want to pass two arguments in one case and pass three or more argument in other case. How can I do it? Maybe I should use "arguments", but I don`t understand how?
Ok, tank you for the helping. I have used method .apply()
let web3 = new Web3();
let instanceContract = new web3.eth.Contract(abi);
instanceContract.options.address = tokenData.address;
let necessaryMethod = instanceContract.methods[incomeData.methodCall];
let methodCall = necessaryMethod.apply(this, incomeData.argumentsForFunction);
return methodCall.encodeABI();
In first case I called function getByteCode(firstObj, incomeData) and there is next array in incomeData.argumentsForFunction = ['0x323.....', '1500000000'].
In other case I called function getByteCode(firstObj, incomeData) with incomeData.argumentsForFunction = ['0x323.....', '0x3228....', '54454000000']
My code is:
var strzal = [];
var nr = Object.keys(strzal);
strzal[nr.length][0] = new Image();
strzal[nr.length][0].src = 'data/strzal_01.png';
ctx.drawImage(strzal[nr.length][0], gracz.x, gracz.y);
strzal[nr][1] = gracz.x;
strzal[nr][2] = gracz.y;
strzal[nr][3] = setInterval(functionName, 150);
strzal[nr][4] = gracz.zasieg;
I have error in "strzal[nr.length][0] = new Image();". Something in this line is wrong, but I don't know what. When I added [0] an error has occurred.
strzal is empty, and you're trying to acces a variable on an index. You should change it to
strzal[nr.length] = array(new Image());
More info:
This can be done:
variable[new_index] = other_variable;
This can't be done, because it doesn't exist yet:
variable[new_index][second_new_index] = other_variable;
Javascript doesn't create the indexes all the way, so keep that in mind!
When i was writing this promise to query an sql datbase i was not testing it with require i was just running the js file straight from node in the console. Now i need it to return the data when finishing the loop and I can't figure out how. Promises as well as reading data from an SQL are both new to me so i was happy to have gotten it working. But now when i require this code with
var dbData = new getDataSQL();
it returns {} instead of a nice big chunk of data.
It finishes its promises but the data is not returned.
Any ideas on how best to return the data?
module.exports = function getDataSQL(){
//JSON OBJECTS
var dates = require('./JSON/dates.js');
var companies = require('./JSON/companies.js');
//SQL FUNCTION
var sqlConJS = require('./sqlCon.js');
function fn(retVal, i, startDate, endDate){
data[i] = JSON.parse(retVal);
var total = 0;
for(var b = 0; b<Object.keys(data[i].result).length;b++){
total = total + data[i].result[b].Amount
}
data[i].totalAmount = total;
data[i].startDate = startDate;
data[i].endDate = endDate;
console.log("No= "+i,"Reccs= " + Object.keys(data[i].result).length,"StartDate=" + startDate,"EndDate=" + endDate, "Amount = " + Math.floor(total));
dataP();
}
//INIT SQL QUERY
var data = [];
var incrDat = 0;
var dataPromise = function(i){
return new Promise(function(resolve, reject){
data[i]={};
var sqlCon = new sqlConJS(fn, dates[i].startDate, dates[i].endDate, companies[9].company, i);
if(dates.length===i)reject();
else resolve();
});
};
var dataP = function(){
dataPromise(incrDat++).then().catch(function(){
console.log("done!");
console.log(data[0].result[0]["Posting Date"]);
return data;
});
}
dataP();
}
Never mind i got it, callback function offcourse! I have so much to learn still. Add cbRetData when declaring the function at the top.
module.exports = function getDataSQL(cbRetData){
under Console.log("done!"); i put
cbRetData(data);
in the main js script we create the function cbRetData like so
function cbRetData(retData){
dbData = retData;
console.log("retData to dbData coming in!");
console.log(dbData);
}
where we call the getDataSQL function after requiring it u simply pass the function cbRetData along like so.
var getdbData = new getDataSQL(cbRetData);
I'm only just beginning to come to terms with callback functions and such.
I'm fairly new to JavaScript and am hoping someone can help me understand how to modify the function below so it will properly return a result when called. The code currently works and the handleResults function is called once the session string is generated. What I would like to do is modify the generateSessionString function so it will return the session string rather than passing it to handleResults. Can anyone give me suggestions on how I can accomplish this?
function generateSessionString(){
var cb = function (success, results){
if(!success)
alert(results);
if(results.code && results.message){
alert (results.message);
return;
}
handleResults(results);
};
var config = new KalturaConfiguration(gPartnerID);
config.serviceUrl = gServiceURL;
var client = new KalturaClient(config);
var partnerId = gPartnerID;
var userId = gUserName;
var password = gPassWord;
var expiry = gExpiry;
var privileges = gPrivileges;
var result = client.user.login(cb, partnerId, userId, password, expiry, privileges);
return result;
}
function handleResults(ks){
KalturaSessionString = ks;
}
if you like to write it in a sync way(it's still async code) you can try promise(in this example i used jQuery)
function generateSessionString(){
var dfd = new jQuery.Deferred();
var cb = function (success, results){
if(!success)
dfd.fail(results);
if(results.code && results.message){
dfd.fail (results.message);
return;
}
dfd.resolve(results);
};
var config = new KalturaConfiguration(gPartnerID);
config.serviceUrl = gServiceURL;
var client = new KalturaClient(config);
var partnerId = gPartnerID;
var userId = gUserName;
var password = gPassWord;
var expiry = gExpiry;
var privileges = gPrivileges;
client.user.login(cb, partnerId, userId, password, expiry, privileges);
return dfd.promise();
}
$.when(generateSessionString()).then(
function(session)
{
alert(session);
}
)
#Itay Kinnrot's answer is right.Actually,jQuery's on/trigger is another way to solve it,but $.Deferred is better.
if you want to know more about it,you could try to understand Pub/Sub Pattern.
This article is recommended:
http://www.elijahmanor.com/2013/03/angry-birds-of-javascript-blue-bird.html