Skip to content

Commit

Permalink
increased logging for notification engine
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfears committed Jan 18, 2024
1 parent 471ba43 commit 961c734
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/paulfears/StellarSnap.git"
},
"source": {
"shasum": "GrNtYQY0zlfcfbkst5bpWRYCw/MJZ01K27cLnCDCWtY=",
"shasum": "gEfexAJfyQ+SlicTpGeYgco9SVyVtXRQlUaFWSYjUW0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
14 changes: 13 additions & 1 deletion src/notificationEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class NotificationEngine{
const mainNetBalances = await this.client.getAssets(this.wallet.address);
this.client.setNetwork("testnet")
const testNetBalances = await this.client.getAssets(this.wallet.address);

let currentState:State = await StateManager.getState();
const currentAccountAddr = currentState.currentAccount;
let currentBalances = currentState.accounts[currentAccountAddr].assets;
Expand All @@ -25,29 +26,36 @@ export class NotificationEngine{
}

async handleAssetNotifications(prevAssets, currentAssets, currentState:State, network:"mainnet"|"testnet"){
console.log("handle assets called");
console.log(network);
let outputItems = {}
function findIncreasedBalanceOrNewObjects(prevAssets, currentAssets) {
// Create a map of issuer to balance from the first array
const balanceMap = new Map();
prevAssets.forEach((item) => {
console.log(item);
if(item.asset_type === "native"){
console.log("is native");
balanceMap.set("native", item.balance);
}
else{
balanceMap.set(item.issuer, item.balance);
}
});
console.log("balancemap is");
console.log(balanceMap);

// Filter the second array to find objects with increased balance or new objects
console.log("current Assets is");
currentAssets = Array.from(currentAssets);

console.log(currentAssets);
const result = currentAssets.filter((item) => {
if(item.asset_type === "native"){
item.issuer = "native";
}
const balanceInArray1 = balanceMap.get(item.issuer);
console.log("balance in Array 1");
console.log(balanceInArray1);
if (balanceInArray1 === undefined) {
// If the issuer is not in the first array, it's a new object
outputItems[item.issuer] = {asset: item, "diff":item.balance}
Expand All @@ -59,16 +67,20 @@ export class NotificationEngine{
return Array.from(result);
}
const diffAssets:any = findIncreasedBalanceOrNewObjects(prevAssets, currentAssets);
console.log("diff assets is");
console.log(diffAssets);
if(diffAssets.length === 0){
return true;
}
currentState.accounts[currentState.currentAccount].assets[network] = currentAssets;

await StateManager.setState(currentState)

if(diffAssets.length < 4){
for(let i = 0; i<diffAssets.length; i++){
let itemInfo = outputItems[diffAssets[i].issuer]
console.log("itemInfo");
console.log(itemInfo);
if(itemInfo.diff < 0){
await Utils.notify(`spent ${itemInfo.diff} ${network} ${itemInfo.asset.asset_code}`)
}
Expand Down

0 comments on commit 961c734

Please sign in to comment.