Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using "fromFields", address will be add an 0x beforehand. #21

Open
EasonC13 opened this issue Nov 17, 2024 · 10 comments
Open

When using "fromFields", address will be add an 0x beforehand. #21

EasonC13 opened this issue Nov 17, 2024 · 10 comments

Comments

@EasonC13
Copy link

Below will return 400 because when I use "fromFields" it will load an extra 0x for any address regardless it had 0x already or not.

import { NextApiRequest, NextApiResponse } from "next";
import { SuiClient } from "@mysten/sui/client";
import {
  CoinAddedEvent,
  CoinAddedEventFields,
  VaultCreatedEvent,
  VaultCreatedEventFields,
} from "@/giftdrop-contracts/giftdrop_vault/giftdrop_vault/vault/structs";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== "POST") {
    return res.status(405).json({ error: "Method not allowed" });
  }

  try {
    const { txDigest } = req.body;

    if (!txDigest) {
      return res.status(400).json({ error: "Transaction digest is required" });
    }

    const suiClient = new SuiClient({
      url:
        process.env.NEXT_PUBLIC_SUI_RPC_URL ||
        "https://fullnode.mainnet.sui.io:443",
    });

    const txData = await suiClient.waitForTransaction({
      digest: txDigest,
      options: {
        showEvents: true,
      },
    });
    console.log({ txData });
    const events = txData.events;
    const vaultCreatedEvent = events?.find((e) =>
      e.type.endsWith("VaultCreatedEvent")
    );
    if (!vaultCreatedEvent || !vaultCreatedEvent.parsedJson) {
      return res.status(400).json({ error: "Vault created event not found" });
    }
    const vaultCreatedEventFields = VaultCreatedEvent.fromFields(
      vaultCreatedEvent.parsedJson as Record<string, any>
    );
    if (
      vaultCreatedEventFields.manager !==
      process.env.NEXT_PUBLIC_MANAGER_ADDRESS
    ) {
// vaultCreatedEventFields.manager will be 0x0x4fb6bb32eb3f5e495430e00233d3f21354088eee6e2b2e0c25c11815f90eea53
// and it should be 0x4fb6bb32eb3f5e495430e00233d3f21354088eee6e2b2e0c25c11815f90eea53
      return res.status(400).json({ error: "Vault manager is incorrect" });
    }
@kklas
Copy link
Contributor

kklas commented Nov 17, 2024

This could indeed be a bug. What is the type of the vaultCreatedEventFields.manager field (in Move)? Also in general you should be decoding from bcs instead of json because it's more stable

@EasonC13
Copy link
Author

Curious how can we decode a event from BCS from suiClient.waitForTransaction?

@kklas
Copy link
Contributor

kklas commented Nov 17, 2024

Does it not return bcs?

@EasonC13
Copy link
Author

vaultCreatedEventFields.manager is an address

    public struct VaultCreatedEvent has copy, drop {
        vault_id: ID,
        creator: address,
        manager: address,
        max_receivers: u64,
    }

@EasonC13
Copy link
Author

EasonC13 commented Nov 17, 2024

Does it not return bcs?

Not for event, it shows bcs-encoded transaction input data

waitForTransaction({ signal, timeout, pollInterval, ...input }: {
        /** An optional abort signal that can be used to cancel */
        signal?: AbortSignal;
        /** The amount of time to wait for a transaction block. Defaults to one minute. */
        timeout?: number;
        /** The amount of time to wait between checks for the transaction block. Defaults to 2 seconds. */
        pollInterval?: number;
    } & Parameters<SuiClient['getTransactionBlock']>[0]): Promise<SuiTransactionBlockResponse>;

getTransactionBlock(input: GetTransactionBlockParams): Promise<SuiTransactionBlockResponse>;

export interface GetTransactionBlockParams {
    /** the digest of the queried transaction */
    digest: string;
    /** options for specifying the content to be returned */
    options?: RpcTypes.SuiTransactionBlockResponseOptions | null | undefined;
}

export interface SuiTransactionBlockResponseOptions {
    /** Whether to show balance_changes. Default to be False */
    showBalanceChanges?: boolean;
    /** Whether to show transaction effects. Default to be False */
    showEffects?: boolean;
    /** Whether to show transaction events. Default to be False */
    showEvents?: boolean;
    /** Whether to show transaction input data. Default to be False */
    showInput?: boolean;
    /** Whether to show object_changes. Default to be False */
    showObjectChanges?: boolean;
    /** Whether to show raw transaction effects. Default to be False */
    showRawEffects?: boolean;
    /** Whether to show bcs-encoded transaction input data */
    showRawInput?: boolean;
}

@EasonC13
Copy link
Author

EasonC13 commented Nov 17, 2024

It will be great to add a function that parses from the events from the transaction blocks.

@kklas
Copy link
Contributor

kklas commented Nov 17, 2024

The returned event type contains a bcs string:

You can decode like this:
VaultCreatedEvent.r.fromBcs(fromBase58(vaultCreatedEvent.bcs)))

I will test the fromFields method to see whether there's a bug on the address field.

@EasonC13
Copy link
Author

You can decode like this:
VaultCreatedEvent.r.fromBcs(fromBase58(vaultCreatedEvent.bcs)))

Oh yes! Got it! It works

@EasonC13
Copy link
Author

EasonC13 commented Nov 17, 2024

Yet when I try event with type, it will showArgument of type 'string' is not assignable to parameter of type 'PhantomReified<string>'.ts(2345).

        const vaultCoinAddedEvent = CoinAddedEvent.fromBcs(
          coinType, // Argument of type 'string' is not assignable to parameter of type 'PhantomReified<string>'.ts(2345)
          fromBase58(vaultCoinAddedEvent_.bcs)
        );
    public struct CoinAddedEvent<phantom T> has copy, drop {
        vault_id: ID,
        coin_value: u64,
    }

@kklas
Copy link
Contributor

kklas commented Nov 17, 2024

For generics you need to use reified https://github.com/kunalabs-io/sui-client-gen?tab=readme-ov-file#reified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants