I know this question may be better suited for the Ethereum stack exchange but I've gotten no responses over there and I'm wondering if it's more a React issue or something.
I'm trying to run 3 async methods
1: Push a file to IPFS:
pushToIPFS = async(e) => {
return new Promise((resolve, reject) => {
ipfs.add(this.state.buffer, (err, ipfsHash) => {
resolve(ipfsHash[0].hash);
})
});
}
2: Write to smart contract and Blockchain:
addToBlockchain = async(_ipfsLink) => {
return new Promise((resolve, reject) => {
const rand = uniqueRandom(1, 10000000)
var key = rand()
let newDate = new Date()
newDate = newDate.getTime()
var _account = this.state.account[0]
var _uid = this.state.uid
storehash.methods.sendDocument(_ipfsLink, newDate, key, _uid).send({from: _account})
resolve(key)
})
}
3: Add an entry to a firebase database:
createStudent = async(_key) => {
//get student details from state variables & current user uid
var _uid = this.state.uid
var _studentName = this.state.StudentName
var _studentNumber = this.state.StudentNumber
var _courseCode = this.state.CourseCode
var _courseName = this.state.CourseName
var _idForBlockchain = _key
// database.ref.students.uid.studentNumber
const db = firebase.database()
db.ref().child("students").child(_uid).child(_studentNumber).set(
{ studentName: _studentName,
courseCode: _courseCode,
courseName: _courseName,
blockchainKey: _idForBlockchain
}
);
alert("Student added")
}
These are called in that order by this function:
AddMyStuff = async (e) => {
e.preventDefault()
const ipfsHash = await this.pushToIPFS()
//await this.createStudent()
const _key = await this.addToBlockchain(ipfsHash)
await this.createStudent(_key)
}
The problem is the student is being added to the database before the metamask (smart contract) transaction has been confirmed. And also After the smart contract executes, all the correct details are written to the Blockchain, but the following error appears in the console:
web3-core-method.umd.js:1191 Uncaught (in promise) Error: Transaction has been reverted by the EVM: { "transactionHash":
"0xe3f411d872bb5f42bd7bd15676647f5056421c5accb5aa6fa22d75eadd09973a",
"transactionIndex": 0, "blockHash":
"0x749ccd76a6888b261b00d8851dca36545fde563fce4c8513407a180d6cac3a00",
"blockNumber": 6, "from":
"0xa5fcbc63d6bcb8e07750cb75073ec3ff7b98c4f5", "to":
"0xadb13cc1a32b64f938be7c1d3447dfcd20c09ae9", "gasUsed": 175983,
"cumulativeGasUsed": 175983, "contractAddress": null, "logs": [],
"status": true, "logsBloom":
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"v": "0x2d46", "r":
"0xa1dfe4e9a4ac8dbbdf49186741cd74bda4fed78b280458ed2cd7f979a3020ccd",
"s":
"0x477fa20a2176975f3db9fb1e375fceeedb6ae0e7be35177d4908ad1744dddd1a" }
at SafeSubscriber._next (web3-core-method.umd.js:1191)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:245)
at SafeSubscriber.next (Subscriber.js:174)
at Subscriber._next (Subscriber.js:99)
at Subscriber.next (Subscriber.js:68)
at TransactionObserver.emitNext (web3-core-method.umd.js:510)
at _callee$ (web3-core-method.umd.js:357)
at tryCatch (runtime.js:63)
at Generator.invoke [as _invoke] (runtime.js:282)
at Generator.prototype.(anonymous function) [as next] (http://localhost:3000/static/js/0.chunk.js:276708:21)
at asyncGeneratorStep (asyncToGenerator.js:3)
at _next (asyncToGenerator.js:25)
Scary stuff indeed, does anyone know what could be causing this? Again sorry if this is better suited for Ethereum stack exchange, but I've gotten no responses and I don't know what else to try. Thanks in advance for any help!
in case sendDocument is a promise, you can try that..
addToBlockchain = async (_ipfsLink) => {
const rand = uniqueRandom(1, 10000000)
var key = rand()
let newDate = new Date()
newDate = newDate.getTime()
var _account = this.state.account[0]
var _uid = this.state.uid
await storehash.methods.sendDocument(_ipfsLink, newDate, key, _uid).send({from: _account})
return key
}
Related
I am attempting to transfer tokens when a user clicks a button. Once a user connects their wallet I have this method that gets called when a user clicks a button
async function claimTokens() {
setShowModal(false);
setIsLoading(true);
var data = await axios.get(`/api/claim?wallet=${wallet.publicKey.toBase58()}`)
if(data.status != 200)
{
notify({ type: 'error', message: `Error with our API, please retry in a few minutes` });
return
} else {
let sig;
try {
var newTransaction = new Transaction();
newTransaction.signatures = data.data.data.signers;
newTransaction.instructions = data.data.data.instructions;
newTransaction.recentBlockhash = data.data.data.recentBlockhash;
newTransaction.feePayer = data.data.data.feePayer;
console.log(newTransaction)
sig = await sendTransaction(newTransaction, connection);
notify({ type: 'error', message: `Claimed tokens!` });
} catch (error) {
console.log(error)
}
}
}
when I console.log the newTransaction it shows a transaction that looks like this transaction screenshot
When I call the API to get the transaction here is what the method looks like.
try {
const connection = new web3.Connection(
"OMITTING URL",
'confirmed',
);
const { wallet } = req.query
const key = <OMITTED-FOR-OBVIOUS-REASONS>
const account = web3.Keypair.fromSeed(key.slice(0, 32));
console.log(account.publicKey.toBase58())
const wantedTokenAddress = new web3.PublicKey(process.env.TOKEN_ADDRESS)
const transaction = new web3.Transaction()
const toPublicKey = new web3.PublicKey(wallet);
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
account,
wantedTokenAddress,
account.publicKey
)
let toATA = await splToken.getOrCreateAssociatedTokenAccount(
connection,
account,
wantedTokenAddress,
toPublicKey,
false,
).catch(() => {/*nothing*/ });
if (!toATA) {
const associatedToken = await splToken.getAssociatedTokenAddress(
wantedTokenAddress,
account.publicKey,
false,
splToken.TOKEN_PROGRAM_ID,
splToken.ASSOCIATED_TOKEN_PROGRAM_ID
);
transaction.add(
splToken.createAssociatedTokenAccountInstruction(
account.publicKey,
associatedToken,
account.publicKey,
wantedTokenAddress,
splToken.TOKEN_PROGRAM_ID,
splToken.ASSOCIATED_TOKEN_PROGRAM_ID
)
);
} else {
transaction.add(
splToken.createTransferInstruction(
tokenAccount.address,
toATA.address,
account.publicKey,
1,
[],
TOKEN_PROGRAM_ID
)
)
}
let blockhash = await (await connection.getLatestBlockhash('finalized')).blockhash;
transaction.recentBlockhash = blockhash;
transaction.feePayer = toPublicKey;
transaction.sign(account)
console.log(transaction)
res.status(200).json({ data: transaction })
} catch (error) {
console.log(error)
res.status(500).json({ data: null })
}
There error I am getting is when I try to send the transaction I am getting
x.pubkey.toBase58 is not a function
That error is thrown inside of the sendTransaction method
A few months ago, a glorious soul here taught me about transactions. I may have gone a little overboard thinking they were they best thing since sliced bread. The problem they solved was obvious, guaranteed concurrent writes on a single doc. However, I've noticed already with as little as three closely timed function triggers that I produced the dreaded: ------------------------"10 ABORTED: Too much contention on these documents."...-------------------------
Optimizing for stability, my question is: Would it be best practice to use a mixed bag of these write calls for different situations? For example: if a cloud function is writing to a location where I do not expect contention, should it just be a set call? Instead of 4 transactions to various locations, should I use a batch?
Reading the Firebase limitations I assumed I was in the clear with max 60w/doc/sec. However, I've learned now that Transactions can timeout AND only try to write 5 times.
Some background on the app and the contention error:
- It's a basic social media app.
- The contention error came from making three posts in close succession from a single user.
- Each post triggers a cloud function that does several transactions to link the post to appropriate places. i.e. followers, feed, groups, sends notifications, and sets activity feed docs for each follower.
Side question: Am I wrongly understanding that firebase can handle an app with this level of activity?
EDIT: I was aware of these firebase limitations early on and did my best work to keep documents and collections spread apart appropriately.
CODE EDIT: index.js: adminPostReview is the specific function to throw the error (did the best I could to simplify).
The specific transaction to throw the error, I believe, is the call to transactionDayIndexAdd().
function transactionDelete(docRef) {
return db.runTransaction(async t => {
var doc = await t.get(docRef);
if (doc.exists)
t.delete(docRef);
})
}
// THIS FUNCTION. Is it bad to read and set two documents?
function transactionDayIndexAdd(docRef, dayPosted, postId, userId) {
return db.runTransaction(async (t) => {
var postMap = {};
const doc = await t.get(docRef.doc(dayPosted));
if (doc.exists) {
postMap = doc.data().pids;
} else {
const indexDoc = await t.get(docRef.doc('index'));
var newIndex = indexDoc.exists ? indexDoc.data().index : {};
newIndex[dayPosted] = true;
t.set(docRef.doc('index'), { 'index': newIndex });
}
postMap[postId] = userId;
t.set(doc.ref, { 'pids': postMap });
})
}
exports.adminPostReview = functions.firestore
.document('/adminPostReview/{postId}')
.onUpdate(async (change, context) => {
const postId = context.params.postId;
const userId = change.before.data().ownerId;
const approvedMaks = change.after.data().approvedMaks;
const approvedRita = change.after.data().approvedRita;
var promises = [];
if (approvedMaks == false || approvedRita == false) {
promises.push(transactionDelete(db.collection('posts').doc(userId).collection('userPosts').doc(postId)));
}
else if (approvedMaks == true || approvedRita == true) {
var newPost = change.after.data();
promises.push(postLive(newPost));
}
if (approvedMaks != null || approvedRita != null) {
promises.push(transactionDelete(db.collection('activityFeed').doc(MAKS_ID).collection('feedItems').doc(`${postId}_review`)));
promises.push(transactionDelete(db.collection('activityFeed').doc(RITA_ID).collection('feedItems').doc(`${postId}_review`)));
}
});
async function postLive(newPost) {
const userId = newPost.ownerId;
const postId = newPost.postId;
const dayPosted = newPost.dayPosted;
var postToFeed = newPost.postToFeed;
var postToGroups = newPost.postToGroups;
newPost.approved = true;
delete newPost.postToFeed;
delete newPost.postToGroups;
var batch = db.batch();
var promises = [];
if (postToFeed == true) {
batch.set(
db.collection('posts').doc(userId).collection('userPosts').doc(postId),
newPost
);
batch.update(
db.collection('userActivity').doc(userId),
'numPosts',
admin.firestore.FieldValue.increment(1),
)
promises.push(batch.commit());
promises.push(transactionDayIndexAdd(db.collection("feedRandom"), dayPosted, postId, userId));
var querySnap = await db.collection('followers')
.doc(userId)
.collection('userFollowers')
.get();
querySnap.docs.forEach(async follower => {
promises.push(transactionDayIndexAdd(
db.collection('feedFollowing').doc(follower.id).collection('feedItems'),
dayPosted, postId, userId));
promises.push(transactionSet(db.collection('activityFeed').doc(follower.id)
.collection('feedItems').doc(postId),
{
media1Url: newPost.media1Url,
media2Url: newPost.media2Url,
postId: newPost.postId,
timestamp: newPost.timestamp,
type: 'newFollowingPost',
userId: userId,
userProfileImg: newPost.ownerProfileImg,
username: newPost.username,
displayName: newPost.displayName,
}
));
if (follower.data().notificationToken != null) {
const payload = {
notification: {
title: 'Someone you follow made a new post!',
body: `${newPost.username} has a new post.`
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
vestiq_type: 'newFollowingPost',
vestiq_uid: follower.id,
vestiq_fid: userId,
vestiq_pid: postId,
vestiq_displayName: newPost.displayName,
vestiq_photoUrl: newPost.ownerProfileImg,
vestiq_username: newPost.username,
}
};
var user = await db.collection('users').doc(follower.id).get();
if (user.data().notificationOp3 == true)
promises.push(pushNotification(follower.data().notificationToken, payload));
}
});
if (postToGroups != null && postToGroups.length > 0) {
promises.push(pushGroupPosts(postToGroups, userId, postId, newPost));
return Promise.all(promises);
} else return Promise.all(promises);
}
else if (postToGroups != null && postToGroups.length > 0) {
promises.push(pushGroupPosts(postToGroups, userId, postId, newPost));
return Promise.all(promises);
}
}
async function pushGroupPosts(postToGroups, userId, postId, newPost) {
var groupBatch = db.batch();
postToGroups.forEach((gid) => {
groupBatch.set(
db.collection('groups').doc(gid).collection('posts').doc(postId),
newPost,
);
groupBatch.set(
db.collection('usersGroupPosts').doc(userId).collection(gid).doc(postId),
{ 'gid': gid, 'postId': postId },
);
});
return push(groupBatch.commit());
}
I was able to fix the contention problem by splitting transactionDayIndexAdd() into two separate transactions. I flip a bool to determine if the second should run instead.
This leads me to believe that the nested t.get/t.set transaction significantly increases chances for contention issues. Since the split, I have not been able to reproduce the error. Here is the new transactionDayIndexAdd() for those who are curious.
HOWEVER, my original question still stands regarding optimising for stability.
async function transactionDayIndexAdd(docRef, dayPosted, postId, userId) {
var dayAdd = 0;
var promises = [];
await db.runTransaction(async (t) => {
var postMap = {};
const doc = await t.get(docRef.doc(dayPosted));
if (doc.exists)
postMap = doc.data().pids;
else {
dayAdd = 1;
}
postMap[postId] = userId;
t.set(doc.ref, { 'pids': postMap });
});
if (dayAdd == 1) {
promises.push(db.runTransaction(async (t) => {
const indexDoc = await t.get(docRef.doc('index'));
var newIndex = indexDoc.exists ? indexDoc.data().index : {};
newIndex[dayPosted] = true;
t.set(indexDoc.ref, { 'index': newIndex });
}));
}
return await Promise.all(promises);
}
I am experiencing an issue with Firebase callable functions and Auth triggers. You can see the callable function below. When it works it usually takes less than 1 second to finish but it started give frequent timeout errors since yesterday. Same thing for the Auth trigger, I was simply returning a Promise that writes user email to the Firestore in that case.
exports.respondToInvite = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
const uid = context.auth.token.uid;
const inviteId = data.inviteId;
const groupId = data.groupId;
const accepted: boolean = data.accepted;
try {
const batch = admin.firestore().batch();
const inviteRef = admin.firestore().collection("invites").doc(inviteId);
batch.update(inviteRef, {
userId: uid,
status: accepted ? "accepted" : "rejected",
})
if (accepted) {
const groupUsersRef = admin.firestore().collection("groups").doc(groupId).collection("users").doc(context.auth.uid);
batch.set(groupUsersRef, {
createdAt: admin.firestore.Timestamp.now()
})
const userRef = admin.firestore().collection("users").doc(uid);
batch.set(userRef, {
"groupId": groupId
});
}
await batch.commit();
return "invitation accepted";
} catch (error) {
console.error(error);
throw new functions.https.HttpsError('failed-precondition', 'invite response failed',error);
}
});
Edit:
Here is the Auth trigger function
exports.newUser = functions.auth.user().onCreate((user) => {
const userRef = admin.firestore().collection("users").doc(user.uid);
return userRef.create({
"email": user.email,
});
});
my firebase function based on realtime database trigger looks like below
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
console.log("start of on_user_created ")
const user = change.val();
console.log("New user:::" + JSON.stringify(user))
const uid = user._uid
const referralCode = user._referralCode
console.log("creating referral node for uid:" + uid + " with code:" + referralCode)
if(referralCode === undefined){
console.error("No referral code created for the user while sign up. Referral node cannot be created.")
return true
}
var db = admin.database();
var ref = db.ref('referrals')
ref.child(referralCode).set({"uid": uid}).then(
(resp) => {
console.log("referral node created")
return true
}
).catch(
(err) => {
console.error("unable to create referral node on user create:" + err)
return true
}
)
})
it on run throws
5:47:02.035 AM on_user_created Function returned undefined, expected Promise or value
I am failing to understand why
Adapted following Doug's comment below: "If you have no async work to be done, it's typical to return null"
This is because you don't return the Promise returned by the set() asynchronous operation.
You should do something like:
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
console.log("start of on_user_created ")
const user = change.val();
console.log("New user:::" + JSON.stringify(user))
const uid = user._uid
const referralCode = user._referralCode
console.log("creating referral node for uid:" + uid + " with code:" + referralCode)
if(referralCode === undefined){
console.error("No referral code created for the user while sign up. Referral node cannot be created.")
return null // <-- See Doug's comment below.
}
var db = admin.database();
var ref = db.ref('referrals')
return ref.child(referralCode).set({"uid": uid}).then( // <-- !! Here we return
(resp) => {
console.log("referral node created")
return null
}
).catch(
(err) => {
console.error("unable to create referral node on user create:" + err)
return null
}
)
})
Note that you could streamline your code as follows if you don't need the console.log()s, e.g. for production.
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
const user = change.val();
const uid = user._uid
const referralCode = user._referralCode
if (referralCode === undefined) {
return null;
} else {
var db = admin.database();
var ref = db.ref('referrals')
return ref.child(referralCode).set({"uid": uid});
}
});
For more detail on the importance of returning the promises in a Cloud Function, I would suggest you watch the official video series and in particular the ones titled "Learn JavaScript Promises": https://firebase.google.com/docs/functions/video-series/
I have been asked to create a firebase (fb) function that triggers onUpdate. Then I have to gather a bunch of information from the fb database and publish a message so that another function triggers at that point.
fb update triggers functionA
functionA publishes a message
functionB is a subscriber to that topic and is triggered after the message publishes.
I have the basics of the onUpdate trigger below:
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`#google-cloud/pubsub`),
admin = require("firebase-admin");
const pubsub = new PubSub();
exports.checkInOrder = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
const after = change.after.val();
// check the status: "pending-pickup" or "fulfilled" TODO
if (after.status === "new") {
console.log("ended because package is new.");
return null;
}
let dsObj = {};
const orderId = context.params.id;
const topicName = 'check-in-order';
const subscriptionName = 'check-in-order';
return // how would I send the message to the pubsub here?
});
So to summarize:
How do I send a message to pubsub
How do I subscribe a firebase function to trigger when a topic receives a message?
If it sounds very confusing I'm sorry - I am completely lost here. Thanks!
So I finally figured it out. Pretty straight forward, just felt overwhelmed with learning all of this stuff at once and in a rushed time frame. Below is my code. I included the whole page so in case this might help someone else out there in the future.
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`#google-cloud/pubsub`),
admin = require("firebase-admin");
const init = () => {
const topicName = "check-in-order";
pubsub
.createTopic(topicName)
.then(results => {
const topic = results[0];
console.log(`Topic ${topicName} created.`);
return;
})
.catch(err => {
console.error("ERROR on init:", err);
return;
});
};
const pubsub = new PubSub();
exports.orderCreated = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
console.log("it's happening!");
const after = change.after.val();
console.log("after::::>", after)
if (after.status === "new") {
console.log('stopped because status is new');
return null;
}
if (after.status === "pending-pickup" || after.status === "fulfilled") {
const orderId = context.params.id;
const topicName = "check-in-order";
let { assignedSlot, userId, parcelLockerId, carrier, trackingNumber, orderInDate, pickUpCode, status, } = after;
const dsObj = {
order: {
orderId,
userId,
parcelLockerId,
carrier,
trackingNumber,
orderInDate,
pickUpCode,
status,
}
};
const dataBuffer = new Buffer.from(dsObj.toString());
// publish to trigger check in function
return pubsub
.topic(topicName)
.publisher()
.publish(dataBuffer)
.then(messageId => {
console.log(`:::::::: Message ${messageId} has now published. :::::::::::`);
return true;
})
.catch(err => {
console.error("ERROR:", err);
throw err;
});
}
return false;
});
exports.checkInOrder = () => {
}
exports.checkIn = functions.pubsub.topic('check-in-order').onPublish((message) => {
console.log("everything is running now", message);
return true;
});
init();