From 3fcb3a25a3e8fd33216f91369c6ce48e57e81310 Mon Sep 17 00:00:00 2001 From: moreal Date: Wed, 29 Nov 2023 18:01:06 +0900 Subject: [PATCH] Correct unload-garage event handling --- src/headless-graphql-client.spec.ts | 9 +++++++- src/headless-graphql-client.ts | 30 +++++++++++++++---------- src/index.ts | 7 +++++- src/observers/garage-observer.spec.ts | 11 +++++++-- src/observers/garage-observer.ts | 32 ++++++++++++++++++++++----- 5 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/headless-graphql-client.spec.ts b/src/headless-graphql-client.spec.ts index d836cfe..c4bfaea 100644 --- a/src/headless-graphql-client.spec.ts +++ b/src/headless-graphql-client.spec.ts @@ -48,7 +48,14 @@ test(".getGarageUnloadEvents()", async () => { await heimdallClient.getGenesisHash(), ); const minter = new Minter(signer); - const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter); + const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter, { + agentAddress: Address.fromHex( + "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f", + ), + avatarAddress: Address.fromHex( + "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc", + ), + }); await observer.notify({ blockHash: "", events: x.map((ev) => { diff --git a/src/headless-graphql-client.ts b/src/headless-graphql-client.ts index 2ea16a1..f34a797 100644 --- a/src/headless-graphql-client.ts +++ b/src/headless-graphql-client.ts @@ -119,18 +119,24 @@ export class HeadlessGraphQLClient implements IHeadlessGraphQLClient { ]); const memo = values[3]; - return !recipientAvatarAddress.equals(avatarAddress) && - fungibleAssetValues.filter((fav) => - agentAddress.equals(fav[0]), - ).length === 0 - ? null - : { - txId: tx.id, - signer: tx.signer, - fungibleAssetValues, - fungibleItems, - memo, - }; + const filteredFungibleAssetValues = fungibleAssetValues.filter( + (fav) => + agentAddress.equals(fav[0]) || + avatarAddress.equals(fav[0]), + ); + const filteredFungibleItems = recipientAvatarAddress.equals( + avatarAddress, + ) + ? fungibleItems + : []; + + return { + txId: tx.id, + signer: tx.signer, + fungibleAssetValues: filteredFungibleAssetValues, + fungibleItems: filteredFungibleItems, + memo, + }; }) .filter((ev) => ev !== null); } diff --git a/src/index.ts b/src/index.ts index e25baf8..89196ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -170,7 +170,12 @@ async function withMonitors( ), ); - garageMonitor.attach(new GarageObserver(slackBot, minter)); + garageMonitor.attach( + new GarageObserver(slackBot, minter, { + agentAddress, + avatarAddress, + }), + ); const handleSignal = () => { console.log("Handle signal."); diff --git a/src/observers/garage-observer.spec.ts b/src/observers/garage-observer.spec.ts index 01b9fb1..649b764 100644 --- a/src/observers/garage-observer.spec.ts +++ b/src/observers/garage-observer.spec.ts @@ -1,5 +1,5 @@ import test, { mock } from "node:test"; -import { RawPrivateKey } from "@planetarium/account"; +import { Address, RawPrivateKey } from "@planetarium/account"; import { ChatPostMessageResponse } from "@slack/web-api"; import { HeadlessGraphQLClient } from "../headless-graphql-client"; import { Minter } from "../minter"; @@ -32,7 +32,14 @@ test("notify", async () => { await headlessClient.getGenesisHash(), ); const minter = new Minter(signer); - const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter); + const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter, { + agentAddress: Address.fromHex( + "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f", + ), + avatarAddress: Address.fromHex( + "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc", + ), + }); observer.notify({ blockHash: "xxx", events: [ diff --git a/src/observers/garage-observer.ts b/src/observers/garage-observer.ts index 000f2dc..dc1d22f 100644 --- a/src/observers/garage-observer.ts +++ b/src/observers/garage-observer.ts @@ -1,3 +1,4 @@ +import { Address } from "@planetarium/account"; import { IObserver } from "."; import { IFungibleAssetValues, @@ -22,10 +23,22 @@ export class GarageObserver { private readonly _slackbot: ISlackMessageSender; private readonly _minter: IMinter; + private readonly _vaultAddresses: { + agentAddress: Address; + avatarAddress: Address; + }; - constructor(slackbot: ISlackMessageSender, minter: IMinter) { + constructor( + slackbot: ISlackMessageSender, + minter: IMinter, + vaultAddresses: { + agentAddress: Address; + avatarAddress: Address; + }, + ) { this._slackbot = slackbot; this._minter = minter; + this._vaultAddresses = vaultAddresses; } async notify(data: { @@ -46,10 +59,19 @@ export class GarageObserver try { const requests: (IFungibleAssetValues | IFungibleItems)[] = []; for (const fa of fungibleAssetValues) { - requests.push({ - recipient: agentAddress, - amount: fa[1], - }); + if (fa[0].equals(this._vaultAddresses.agentAddress)) { + requests.push({ + recipient: agentAddress, + amount: fa[1], + }); + } else if ( + fa[0].equals(this._vaultAddresses.avatarAddress) + ) { + requests.push({ + recipient: avatarAddress, + amount: fa[1], + }); + } } for (const fi of fungibleItems) {