Web3.js Discussion

Good job David, thanks for sharing :slight_smile:

1 Like

Thank you Filip for all you teach usā€¦Good luck. :hugs:

1 Like

Hi Filip,

I donā€™t understand how do you get the DEPLOY screen with the message because when I click to account I have a DEPLOY button but no area for text message.
By the way when I clicked on the ā€œNeed helpā€ button Telegram said that the group @GetSuperblocks doesnā€™t exist anymore.

Okay when at 6ā€™ you said

Make sure you have an account selected

you should say instead,

click on interact button

click on interact

My apologies you said it

when we interact

Did you solve it? Iā€™m a bit confused, sorry :slight_smile:

Yes I solved it. I missed the step when you click on interact. But now I get it.

1 Like

by mistake- I already found the answer.

1 Like

Hi everyone,
Please excuse me if this question is too noobish, but how can I input an address in a html input field and pass it as type address to the contract using javascript? I already tried converting string to bytes20 and then having solidity taking the bytes20 and making it into an address, but I could not make it work. If anybody has some advice I would highly appreciate it.

contract.methods  .name().call((err, result) => {console.log(result)})

Hello, I am trying to learn web3.js - did not realize there seems to be a course dedicated to this but I am on a terminal and only get back

Promise {<pending>}

how can I actually get the result back from the query?

Which class or video was this for?

Which lesson is this?

Your are dealing with a promise, meaning you must use a async function to wait for the promise result.

something like await contract.methods.name().call(....)

Carlos Z

1 Like
> let res = await contract.methods.balanceOf(accounts[0])
let res = await contract.methods.balanceOf(accounts[0])
          ^^^^^

Uncaught SyntaxError: await is only valid in async function

Thank you Carlos, I have tried that but I get back the above error

These are the steps I am following on the terminal

// CALL A CONTRACT FROM UNISWAP FROM EHTERSCAN
var Web3 = require('web3')

//Infura account
var web3 = new Web3('HTTP://127.0.0.1:7545') 

var abi = // paste abi from Uniswap 


var contractAddress = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'
undefined
contractAddress
'0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'

// CHECK LATEST BLOCK
>web3.eth.getBlock('latest', (err, result) => {console.log(result)})

// call contract
var contract = new web3.eth.Contract(abi, contractAddress)   
           
> contract.methods.name().call((err, result) => {console.log(result)})
Promise { <pending> }
> undefined

here is where I have spent two days, going on three, trying to get the name of the uni contract

contract.methods.name().call().then(console.log)
Promise { <pending> }

Iā€™ve tried this way too but I get back paragraphs full of errors, here is just one

 (node:36820) UnhandledPromiseRejectionWarning: Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
    at ABICoder.decodeParametersWith (C:\Users\benX\Documents\practiceWeb3\node_modules\web3-eth-abi\lib\index.js:297:15)
1 Like

@thecil
hey Carlos, I figured it out. - took me almost 3 days but it showed how much practice I was missing with Web3. here is final code to interact with the contract. The videos I was watching were over 2 years old. I think the Academy should have itā€™s own course on Web3.js - well, here it is:

async function run() {
  var result = await contract.methods.name().call()
  console.log( `Token Name: ${result}`)
}

run()
Token Name: ChainLink Token
1 Like

Hey @bjamRez, hope you are well.

Sorry for the delay man, but im very happy to know that you have figured it out.

Keep in mind that basically that is the same procedure to invoke functions from your contract to your frontend. :nerd_face:

Carlos Z

1 Like

@thecil, @filip
Hello Devs, I am getting close to finishing the crypto kitties project but somethings Iā€™ve been struggling withā€¦
In the terminal, using Truffle - I am trying to execute the functions for the Market Place but I canā€™t seem to get the "setOffer: function. to work. Iā€™ve tried numerous was and days.

await catInstance.setOffer({value: web3.utils.toWei("2", "ether")}, 1)

I get back this

reason: 'invalid BigNumber value',
  code: 'INVALID_ARGUMENT',
  argument: 'value',
  value: { value: '2000000000000000000' },

I canā€™t seem to remember how to do this ?

@kenn.eth
Hello Kenn, I was wondering if you can take a look at this inquiry I made a few days ago, if you have timeā€¦

It looks like the value type is incorrect, you are sending it a object, which is an invalid big number.

if your setOffer() functions goes has: setOffer(amount, id) then you dont need to send the amount has a value in that format.
So you could just try:
await catInstance.setOffer(web3.utils.toWei("2", "ether"), 1)

Carlos Z

1 Like

Thanks Carlos, I will try that