Error when calling web3.sendRawTransaction(signature.serialize()) - web3js

I want to sign a transaction of a user from phantom wallet and then send the transaction through web3.js but after successfully signing the transaction the web3js library function sendRawTransaction() is giving error message in console
const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);

If you look at the implementation of sendTransaction, you'll see that it's adding a blockhash to the transaction before signing, serializing, and sending. Without a blockhash, you'll get that error Blockhash not found. So instead, you need to do something like:
const latestBlockhash = await connection.getLatestBlockhash();
transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
transaction.recentBlockhash = latestBlockhash.blockhash;
const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(signature);
Full implementation of sendTransaction at https://github.com/solana-labs/solana/blob/3fcdc45092b969baeb7273de6596399d98277366/web3.js/src/connection.ts#L4389

Related

ObjectId id not of Function Type

https://i.stack.imgur.com/7Tq6v.png
import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
if(req.method === "POST"){
const data = req.body;
const client = await MongoClient.connect("mongoDB URL")
const db = client.db();
const postItCollection = db.collection("postIts");
const result = await postItCollection.updateOne({_id: ObjectId(data.id)}, {$set:{positionX:data.y, positionY:data.x}});
console.log(result);
client.close();
res.status(201).json({message:"success"})
}
}
export default handler;
I'm attaching a picture just in case.
I've been looking for it for three hours, but I can't find the answer....
I don't think there's anyone on Earth who has the same problem as me....
I tried installed the "bson-objectid" library.
and "mongoose" Types property
import mongoose from "mongoose";
const { ObjectId } = mongoose.Types;
I've tried it, but I've seen same error of "method expression is not of function type"
Help me, all the masters of the earth.
From Newbie.
import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
if(req.method === "POST"){
const data = req.body;
const client = await MongoClient.connect("mongoDB URL")
const db = client.db();
const postItCollection = db.collection("postIts");
const result = await postItCollection.updateOne({_id: data.id}, {$set:{positionX:data.y, positionY:data.x}}); // <= if i not use ObjectId
console.log(result);
client.close();
res.status(201).json({message:"success"})
}
}
export default handler;
if I not use ObjectId I've got success messages but can't find id...
https://i.stack.imgur.com/XJOQH.png
I just want to find id in mongoDB and change data
PS. How can I see the image right away instead of the URL?
I looked at the other answers and did the same thing, but only the URL came out...
Oh i find Answer...!
It's new operation...
How do I search for an object by its ObjectId in the mongo console?
const result = await postItCollection.updateOne({_id: new ObjectId(data.id)}, {$set:{positionX:data.y, positionY:data.x}});

AWS Amplify + React Authentication Issue (Not getting JWT value after Auth.signIn() on Sign)

hope you all are well. I am working on a React project with a serverless backend in AWS Amplify. Facing an issue with the authentication which is blocking me to use admin action queries. The problem is the following:
I am getting "Uncaught (in promise) No current user" or "Uncaught (in promise) The user is not authenticated" after using the Auth.SignIn() method. I need to get the JWT value from Auth.currentAuthenticatedUser() and sign in correctly in order to perform administrative tasks (which require authentication).
The code for this is:
try {
const user = await Auth.signIn(username, password);
console.log(user); //prints the CognitoUser object.
console.log(Auth.currentAuthenticatedUser()); //throws any of the two errors mentioned above.
} catch (error) {
console.log(error);
}
When I implemented this previously, I was able to get the JWT token with the following method after authentication. That function is:
export const getJWT = async () => {
try{
const userCurrentSession = await Auth.currentSession();
const accessToken = userCurrentSession.getAccessToken();
const JWTvalue = accessToken.getJwtToken();
return JWTvalue;
}catch(error){console.log(error)}
};
I would be highly obliged if someone helps in resolving this problem. Thanks in advance.
Replace the lines inside of try block with the following:
const user = await Auth.currentAuthenticatedUser();
const JWTvalue = user.signInUserSession.getAccessToken().getJwtToken();
return JWTvalue;

Is there away to make a popup do a request to display extra information

I am trying to make an information board that when user press for more information, it will do a get /:id to the backend where it will grab the data and display it in the popup
You can do something like:
const onClickHandler= async () => {
const res = await fetch(endpoint);
const data = await res.json();
setData(data)
}
But using try catch and the rest of things to validate a successful req.

Cant receive data from gitHub API

I try to receive data from gitHub API. As the name is entered in input element I send a request to
https://api.github.com/search/users?q=${username} in:login type:user.
And I`m expecting that I received all users with this username (input element may has whole username or just a part of username). But got only one user.
You can use async and await method to get your proper result.
const gitUsers = async (username) => {
const response = await fetch(`https://api.github.com/search/users?q=${username}`);
const result = await response.json();
console.log(result);
};
gitUsers("carl")

Why do I have an error: "Invalid API key: You must be granted a valid key" when trying to make a request in react App?

So I'm using Movies API from TMDB API, and my code looks like this:
let apikey = '{my_apikey}';
let url: string = 'https://api.themoviedb.org/3/movie/apikey=';
url = url + apikey;
console.log(url);
(async function () {
const response = await fetch(url);
const data = await response.json();
console.log(data);
})()
If I insert the url along with my API it displays the data just fine, but when I'm doing it from my app, I have the next error:
I can't figure out what could be the problem, cause my api key is fine and I copied it from the website.
According to the API documentation. The URL should look something like this:
https://api.themoviedb.org/3/movie/76341?api_key=<<api_key>>

Resources