Skip to content

Commit

Permalink
Merge pull request #62 from moreal/bugfix/support-unload-to-avatar-ad…
Browse files Browse the repository at this point in the history
…dress

🔨 PDX-185: Correct unload-garage event handling
  • Loading branch information
moreal authored Nov 29, 2023
2 parents 5a7abfb + 3fcb3a2 commit 6ca05f8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/headless-graphql-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
30 changes: 18 additions & 12 deletions src/headless-graphql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
11 changes: 9 additions & 2 deletions src/observers/garage-observer.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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: [
Expand Down
32 changes: 27 additions & 5 deletions src/observers/garage-observer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address } from "@planetarium/account";
import { IObserver } from ".";
import {
IFungibleAssetValues,
Expand All @@ -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: {
Expand All @@ -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) {
Expand Down

0 comments on commit 6ca05f8

Please sign in to comment.