Skip to content

Commit

Permalink
feat: add objectarium to message
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikssonJoakim committed Sep 6, 2023
1 parent fd4a3a3 commit 91a23c7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
},
plugins: ["@typescript-eslint"],
rules: {
indent: ["error", 4],
indent: ["error", 4, { SwitchCase: 1 }],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"],
Expand Down
8 changes: 8 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ type Message @entity {
"""
block: Block!
"""
Objectarium that message relates to (for a message which concerns an objectarium smart contract).
"""
objectarium: Objectarium
"""
Object that message relates to (for a message which concerns an object).
"""
objectariumObject: ObjectariumObject
Expand Down Expand Up @@ -231,4 +235,8 @@ type Objectarium @entity {
List of objects contained within the Objectarium instance.
"""
objectariumObjects: [ObjectariumObject] @derivedFrom(field: "objectarium")
"""
List of messages that concern the entity.
"""
messages: [Message]! @derivedFrom(field: "objectarium")
}
37 changes: 37 additions & 0 deletions src/mappings/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
import { CosmosEvent, CosmosMessage } from "@subql/types-cosmos";
import { Message } from "../types";

export const messageId = (msg: CosmosMessage | CosmosEvent): string =>
`${msg.tx.hash}-${msg.idx}`;

export const referenceEntityInMessage = async (
msg: CosmosMessage,
entity: {
id: string;
messageField: keyof Pick<
Message,
| "objectariumObjectId"
| "objectariumId"
| "blockId"
| "transactionId"
>;
},
): Promise<void> => {
const message = await Message.get(messageId(msg));
if (!message) {
return;
}

switch (entity.messageField) {
case "objectariumObjectId":
message.objectariumObjectId = entity.id;
break;
case "objectariumId":
message.objectariumId = entity.id;
break;
case "blockId":
message.blockId = entity.id;
break;
case "transactionId":
message.transactionId = entity.id;
break;
}

await message.save();
};
39 changes: 21 additions & 18 deletions src/mappings/objectariumHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import type {
MsgInstantiateContract,
} from "cosmjs-types/cosmwasm/wasm/v1/tx";
import {
Message,
ObjectariumObject,
Objectarium,
BucketConfig,
BucketLimits,
} from "../types";
import { messageId } from "./helper";
import { referenceEntityInMessage } from "./helper";
import type { Event } from "@cosmjs/tendermint-rpc/build/tendermint37";

type StoreObject = {
Expand Down Expand Up @@ -64,7 +63,10 @@ export const handleStoreObject = async (
pins: isPinned ? [sender] : [],
}).save();

await referenceObjectInMessage(msg, objectId);
await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: objectId,
});
};

export const handleForgetObject = async (
Expand All @@ -76,7 +78,10 @@ export const handleForgetObject = async (
}

await ObjectariumObject.remove(objectId);
await referenceObjectInMessage(msg, objectId);
await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: objectId,
});
};

export const handlePinObject = async (
Expand All @@ -94,7 +99,10 @@ export const handlePinObject = async (
await object.save();
}

await referenceObjectInMessage(msg, object.id);
await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: object.id,
});
};

export const handleUnpinObject = async (
Expand All @@ -113,7 +121,10 @@ export const handleUnpinObject = async (
await object.save();
}

await referenceObjectInMessage(msg, object.id);
await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: object.id,
});
};

export const handleInitObjectarium = async (
Expand Down Expand Up @@ -160,19 +171,11 @@ export const handleInitObjectarium = async (
config,
limits,
}).save();
};

export const referenceObjectInMessage = async (
msg: CosmosMessage,
objectId: string,
): Promise<void> => {
const message = await Message.get(messageId(msg));
if (!message) {
return;
}

message.objectariumObjectId = objectId;
await message.save();
await referenceEntityInMessage(msg, {
messageField: "objectariumId",
id: contractAddress,
});
};

export const retrieveObjectariumObject = async (
Expand Down

0 comments on commit 91a23c7

Please sign in to comment.