Skip to content

Commit

Permalink
fix: enable strict option
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Jul 28, 2024
1 parent 94dcec9 commit 00066e6
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 180 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@types/node": "^20.5.9",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
"typescript": "^5.5.4"
},
"dependencies": {
"@planetarium/account": "^3.7.0",
Expand Down
12 changes: 6 additions & 6 deletions src/actions/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import { FungibleAssetValue, encodeCurrency } from "@planetarium/tx";
import { FungibleItemId } from "../types/fungible-item-id";

export interface IFungibleAssetValues {
recipient: string;
recipient: Address;
amount: FungibleAssetValue;
}

export interface IFungibleItems {
recipient: string;
recipient: Address;
fungibleItemId: FungibleItemId;
count: number;
count: bigint;
}

function encodeMintSpec(value: IFungibleAssetValues | IFungibleItems): Value {
if ((value as IFungibleAssetValues).amount !== undefined) {
const favs = value as IFungibleAssetValues;
return [
Address.fromHex(favs.recipient, true).toBytes(),
favs.recipient.toBytes(),
[encodeCurrency(favs.amount.currency), favs.amount.rawValue],
null,
];
} else {
const fis = value as IFungibleItems;
return [
Address.fromHex(fis.recipient, true).toBytes(),
fis.recipient.toBytes(),
null,
[Buffer.from(fis.fungibleItemId, "hex"), BigInt(fis.count)],
[Buffer.from(fis.fungibleItemId, "hex"), fis.count],
];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function encodeTransferAssetAction(
recipient: Address,
sender: Address,
amount: FungibleAssetValue,
memo: string,
memo: string | null,
) {
return new RecordView(
{
Expand Down
24 changes: 21 additions & 3 deletions src/events/assets-transferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,43 @@ import { IHeadlessGraphQLClient } from "../headless-graphql-client";
import { AssetTransferredEvent } from "../types/asset-transferred-event";
import { TransactionLocation } from "../types/transaction-location";

export type ValidatedAssetTransferredEvent = Omit<AssetTransferredEvent, "memo"> & {
targetAddress: Address;
};

export async function getAssetTransferredEvents(
headlessGraphQLClient: IHeadlessGraphQLClient,
recipient: Address,
blockIndex: number,
): Promise<(AssetTransferredEvent & TransactionLocation)[]> {
): Promise<(ValidatedAssetTransferredEvent & TransactionLocation)[]> {
const planetID = headlessGraphQLClient.getPlanetID();
const blockHash = await headlessGraphQLClient.getBlockHash(blockIndex);
const events = await headlessGraphQLClient.getAssetTransferredEvents(
blockIndex,
recipient,
);

const successEvents: AssetTransferredEvent[] = [];
const successEvents: ValidatedAssetTransferredEvent[] = [];
for (const event of events) {
const { txStatus } = await headlessGraphQLClient.getTransactionResult(
event.txId,
);
if (event.memo === null) {
continue;
}

let targetAddress;
try {
targetAddress = Address.fromHex(event.memo, true);
} catch {
continue;
}

if (txStatus === "SUCCESS") {
successEvents.push(event);
successEvents.push({
...event,
targetAddress,
});
}
}

Expand Down
28 changes: 19 additions & 9 deletions src/events/garage-unload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { GarageUnloadEvent } from "../types/garage-unload-event";

export type ValidatedGarageUnloadEvent = Omit<GarageUnloadEvent, "memo"> & {
parsedMemo: {
agentAddress: string;
avatarAddress: string;
memo: string;
agentAddress: Address;
avatarAddress: Address;
memo: string | null;
};
};

Expand All @@ -15,7 +15,7 @@ export async function getGarageUnloadEvents(
agentAddress: Address,
avatarAddress: Address,
blockIndex: number,
) {
): Promise<ValidatedGarageUnloadEvent[]> {
const planetID = headlessGraphQLClient.getPlanetID();
const blockHash = await headlessGraphQLClient.getBlockHash(blockIndex);
const events = await headlessGraphQLClient.getGarageUnloadEvents(
Expand All @@ -42,6 +42,12 @@ export async function getGarageUnloadEvents(

let parsedMemo = null;
try {
if (event.memo === null) {
console.error(
`Skip ${event.txId} because event.memo field is required but null.`
);
continue;
}
parsedMemo = parseMemo(event.memo);
} catch (e) {
console.error(
Expand All @@ -64,15 +70,19 @@ export async function getGarageUnloadEvents(
}

function parseMemo(memo: string): {
agentAddress: string;
avatarAddress: string;
memo: string;
agentAddress: Address;
avatarAddress: Address;
memo: string | null;
} {
const parsed = JSON.parse(memo);

if (typeof parsed[2] !== "string" && parsed[2] !== null) {
throw new TypeError();
}

return {
agentAddress: parsed[0],
avatarAddress: parsed[1],
agentAddress: Address.fromHex(parsed[0], true),
avatarAddress: Address.fromHex(parsed[1], true),
memo: parsed[2],
};
}
Loading

0 comments on commit 00066e6

Please sign in to comment.