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

Generating leaner helper functions #659

Open
Zetazzz opened this issue Sep 27, 2024 · 0 comments
Open

Generating leaner helper functions #659

Zetazzz opened this issue Sep 27, 2024 · 0 comments

Comments

@Zetazzz
Copy link
Collaborator

Zetazzz commented Sep 27, 2024

leaner helper functions need to generate for smaller bundle size.

cca poc:

https://github.com/cosmology-tech/create-cosmos-app/blob/feat/helpers-poc/examples/interchainjs/pages/helpers.tsx

util:

/**
 * common helper options for generated helper functions.
 * For getting signingClient;
 * Or using chainName for getting signingClient.
 */
class HelperOptions {
  signingClient?: SigningClient;
  chainName?: string;
}

hook(generated):

// generated react hook helper function for sending tokens
const useSend = (options: HelperOptions) => {
  // getting signingClient from chainName
  const { signingClient } = useChain(options.chainName ?? '');
  let client = options.signingClient ?? signingClient;

  if (!client) {
    return null;
  }

  // register all related encoders and converters
  // at this case, we only need MsgSend
  signingClient?.addEncoders(toEncoders(MsgSend));
  signingClient?.addConverters(toConverters(MsgSend));

  // return the actual send function
  return async (
    signerAddress: string,
    message: MsgSend,
    fee: StdFee | 'auto' = 'auto',
    memo: string = ''
  ): Promise<DeliverTxResponse> => {
    const data = [
      {
        typeUrl: MsgSend.typeUrl,
        value: message,
      },
    ];
    return signingClient.signAndBroadcast!(signerAddress, data, fee, memo);
  };
};

usage:

  const send = useSend({ chainName });

      const response = await send(
        address,
        {
          fromAddress: address,
          toAddress: address,
          amount: [{ denom: coin.base, amount: '1' }],
        },
        fee,
        'using interchainjs'
      );
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

1 participant