generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mapping): map account in objectarium handlers
- Loading branch information
ErikssonJoakim
committed
Sep 29, 2023
1 parent
a9eb8be
commit 1ee2caa
Showing
3 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters