Hi, Iâm trying to create a buy alert, working off of the whalewatch youtube tutorial.
The issue Iâm facing is that I am not able to save new objects. Iâve googled and searched this forum everywhere for days, looking for some past similar questions asked and answered. Iâve exhausted all of them and still not getting anywhere. Iâve tried all different methods, by simply trying to do request.object.set for all subclass columns, but did not work. So I decided to create a new class using extend but Iâm still not getting any new classes being saved nor new subclasses. Iâve tried creating a new class manually, creating columns ahead of time as well, and still didnât work.
Please HELP!!
I canât tell what Iâm doing wrong as I donât see any error logs on the dashboard.
Moralis.Cloud.beforeSave(âLeMuNewInvAlertTestAâ, async function (request) {
const account = request.object.get(âinvAddrâ);
const txHash = request.object.get(âtransaction_hashâ);
const NewBuy = Moralis.Object.extend(account);
const newBuy = new NewBuy();
//Get token price on PancakeSwap v2 BSC
const options = {
address: â0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095câ, //WBNB
chain: âbscâ,
exchange: âPancakeSwapv2â,
};
const priceData = await Moralis.Web3API.token.getTokenPrice(options);
const bnbPrice = priceData.usdprice;
const invAmtNoDec = request.object.get(âinvAmount_decimalâ) / Math.pow(10, 18);
const amtBonds = request.object.get(âamtBondsâ);
const bondValNoDec = request.object.get(âbondValue_decimalâ) / Math.pow(10, 18);
const leXCValNoDec = request.object.get(âleXCValue_decimalâ) / Math.pow(10, 18);
const prtnrshpInt = request.object.get(âprtnrshpInt_decimalâ) / 100;
const TVLNoDec = request.object.get(âTVL_decimalâ) / Math.pow(10, 18);
const invAmtInD = invAmtNoDec * bnbPrice;
const bondValInD = bondValNoDec * bnbPrice;
const leXCValInD = leXCValNoDec * bnbPrice;
const TVLInD = TVLNoDec * bnbPrice;
newBuy.set(âaccountAddressâ, account);
newBuy.set(âtxHashâ, txHash);
newBuy.set(âinvAmountâ, invAmtNoDec);
newBuy.set(âamtBondsâ, amtBonds);
newBuy.set(âbondValueâ, bondValNoDec);
newBuy.set(âleXCValueâ, leXCValNoDec);
newBuy.set(âprtnrshpIntâ, prtnrshpInt);
newBuy.set(âTVLâ, TVLNoDec);
newBuy.set(âinvAmtInDâ, invAmtInD);
newBuy.set(âbondValInDâ, bondValInD);
newBuy.set(âleXCValInDâ, leXCValInD);
newBuy.set(âTVLInDâ, TVLInD);
Moralis.Object.registerSubclass(account, NewBuy);
await newBuy.save(null, { useMasterKey: true }).then(
(newBuy) => {
// Execute any logic that should take place after the object is saved.
//alert("New buy recorded with Account address: " + newBuy.accountAddress);
},
(error) => {
// Execute any logic that should take place if the save fails.
// error is a Moralis.Error with an error code and message.
//alert("Failed to record a new buy, with error code: " + error.message);
},
);
});
Moralis.Cloud.afterSave(âLeMuNewInvAlertTestAâ, async function (request) {
const contrAddr = âCONTRACTADDRESSXXXXXXâ;
const confirmed = request.object.get(âconfirmedâ);
if (confirmed) {
// if contract address of tx == watched contract address
if (request.object.get("address") == contrAddr) {
const account = request.object.get("invAddr");
const NewBuy = Moralis.Object.extend({ className: account, }, { useMasterKey: true, });
const buyData = new Moralis.Query(NewBuy, { useMasterKey: true, });
buyData.get(txHash);
sendTelegramAlert(buyData);
};
}
});