Skip to content

Commit

Permalink
feat(mapping): map account in objectarium handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikssonJoakim committed Sep 29, 2023
1 parent a9eb8be commit 1ee2caa
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 45 deletions.
79 changes: 56 additions & 23 deletions src/mappings/contractHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,78 @@
import type { CosmosMessage } from "@subql/types-cosmos";
import type { MsgStoreCode } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { Contract } from "../types";
import { findEventAttribute } from "./helper";

enum AccessType {
ACCESS_TYPE_UNSPECIFIED = 0,
ACCESS_TYPE_NOBODY = 1,
ACCESS_TYPE_ONLY_ADDRESS = 2,
ACCESS_TYPE_EVERYBODY = 3,
ACCESS_TYPE_ANY_OF_ADDRESSES = 4,
UNRECOGNIZED = -1,
}
import { AccessType } from "cosmjs-types/cosmwasm/wasm/v1/types";
import { Account, Contract, ContractPermissionAccount } from "../types";
import { contractPermissionAccountId, findEventAttribute } from "./helper";

export const handleStoreContract = async (
msg: CosmosMessage<MsgStoreCode>,
): Promise<void> => {
const id = findEventAttribute(msg.tx.tx.events, "store_code", "code_id")
?.value;
const contractId = findEventAttribute(
msg.tx.tx.events,
"store_code",
"code_id",
)?.value;

const dataHash = findEventAttribute(
msg.tx.tx.events,
"store_code",
"code_checksum",
)?.value;

if (!id || !dataHash) {
if (!contractId || !dataHash) {
return;
}

const { instantiatePermission: instantiatePermissionWithEnums, sender } =
msg.msg.decodedMsg;
const { instantiatePermission, sender } = msg.msg.decodedMsg;
const senderAccount = await Account.get(sender);

const instantiatePermission = instantiatePermissionWithEnums && {
...instantiatePermissionWithEnums,
permission: AccessType[instantiatePermissionWithEnums.permission],
};
if (!senderAccount) {
const publicKey = msg.tx.decodedTx.authInfo.signerInfos.find(
(signerInfo) => signerInfo.publicKey,
)?.publicKey;

await Account.create({
id: sender,
pubKey: publicKey && {
typeUrl: publicKey.typeUrl,
key: Buffer.from(publicKey.value).toString("base64"),
},
balances: [
{
denom: "uknow",
amount: "0",
},
],
}).save();
}

const permission =
instantiatePermission && AccessType[instantiatePermission.permission];

await Contract.create({
id,
id: contractId,
dataHash,
creator: sender,
instantiatePermission,
creatorId: sender,
permission,
}).save();

for (const permissionAddress in instantiatePermission?.addresses) {
if (!(await Account.get(permissionAddress))) {
await Account.create({
id: permissionAddress,
balances: [
{
denom: "uknow",
amount: "0",
},
],
}).save();
}

await ContractPermissionAccount.create({
id: contractPermissionAccountId(contractId, sender),
contractId: permissionAddress,
accountId: sender,
}).save();
}
};
10 changes: 10 additions & 0 deletions src/mappings/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import type {
export const messageId = (msg: CosmosMessage | CosmosEvent): string =>
`${msg.tx.hash}-${msg.idx}`;

export const objectariumObjectPinId = (
objectId: string,
sender: string,
): string => `${objectId}-${sender}`;

export const contractPermissionAccountId = (
contractId: string,
account: string,
): string => `${contractId}-${account}`;

export const findEvent = (
events: Readonly<Event[]>,
event: string,
Expand Down
80 changes: 58 additions & 22 deletions src/mappings/objectariumHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import {
Objectarium,
BucketConfig,
BucketLimits,
ObjectariumObjectPin,
Account,
} from "../types";
import { findEventAttribute, referenceEntityInMessage } from "./helper";
import {
findEventAttribute,
objectariumObjectPinId,
referenceEntityInMessage,
} from "./helper";
import type { Event } from "@cosmjs/tendermint-rpc/build/tendermint37";

type StoreObject = {
Expand Down Expand Up @@ -54,15 +60,43 @@ export const handleStoreObject = async (
}

const { sender, contract } = msg.msg.decodedMsg;
const { pin: isPinned } = msg.msg.decodedMsg.msg.store_object;
const senderAccount = await Account.get(sender);

if (!senderAccount) {
const publicKey = msg.tx.decodedTx.authInfo.signerInfos.find(
(signerInfo) => signerInfo.publicKey,
)?.publicKey;

await Account.create({
id: msg.msg.decodedMsg.sender,
pubKey: publicKey && {
typeUrl: publicKey.typeUrl,
key: Buffer.from(publicKey.value).toString("base64"),
},
balances: [
{
denom: "uknow",
amount: "0",
},
],
}).save();
}

await ObjectariumObject.create({
id: objectId,
sender,
senderId: sender,
objectariumId: contract,
pins: isPinned ? [sender] : [],
}).save();

const { pin: isPinned } = msg.msg.decodedMsg.msg.store_object;

isPinned &&
(await ObjectariumObjectPin.create({
id: objectariumObjectPinId(objectId, sender),
objectariumObjectId: objectId,
accountId: sender,
}).save());

await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: objectId,
Expand All @@ -87,43 +121,43 @@ export const handleForgetObject = async (
export const handlePinObject = async (
msg: CosmosMessage<MsgExecuteContract>,
): Promise<void> => {
const object = await retrieveObjectariumObject(msg);
if (!object) {
const objectId = objectariumObjectId(msg.tx.tx.events);
if (!objectId) {
return;
}

const { sender } = msg.msg.decodedMsg;

if (!object.pins.includes(sender)) {
object.pins.push(sender);
await object.save();
const objectPin = ObjectariumObjectPin.get(
objectariumObjectPinId(objectId, sender),
);

if (!objectPin) {
await ObjectariumObjectPin.create({
id: objectariumObjectPinId(objectId, sender),
objectariumObjectId: objectId,
accountId: sender,
}).save();
}

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

export const handleUnpinObject = async (
msg: CosmosMessage<MsgExecuteContract>,
): Promise<void> => {
const object = await retrieveObjectariumObject(msg);
if (!object) {
const objectId = objectariumObjectId(msg.tx.tx.events);
if (!objectId) {
return;
}

const { sender } = msg.msg.decodedMsg;
const filteredPins = object.pins.filter((address) => address !== sender);

if (filteredPins.length !== object.pins.length) {
object.pins = filteredPins;
await object.save();
}

await ObjectariumObjectPin.remove(objectariumObjectPinId(objectId, sender));
await referenceEntityInMessage(msg, {
messageField: "objectariumObjectId",
id: object.id,
id: objectId,
});
};

Expand Down Expand Up @@ -153,6 +187,7 @@ export const handleInitObjectarium = async (

const {
sender,
admin,
label,
msg: { bucket, limits: bucketLimits, config: bucketConfig },
} = msg.msg.decodedMsg;
Expand All @@ -170,7 +205,8 @@ export const handleInitObjectarium = async (

await Objectarium.create({
id: contractAddress,
owner: sender,
creatorId: sender,
ownerId: admin,
label,
name: bucket,
config,
Expand Down

0 comments on commit 1ee2caa

Please sign in to comment.