surf icon indicating copy to clipboard operation
surf copied to clipboard

Support module at different addresses

Open banool opened this issue 1 year ago • 1 comments

You can imagine the same module being deployed at different addresses, for example when working with different networks. As it is now though, createEntryPayload uses the address from the ABI to build the function string (e.g. 0x123::my_module::my_function). It would be nice if createEntryPayload had support for providing a different address, e.g.:

    const data = createEntryPayload(CHESS_ABI, {
      moduleAddress: "0x321",
      function: "make_move",
      typeArguments: [],
      functionArguments: [gameAddress as any, source.x, source.y, target.x, target.y, chessJsPieceTypeToNumber(promotion)],
    });

banool avatar Dec 22 '23 18:12 banool

@banool good point, will try to support that!

SamuelQZQ avatar Dec 23 '23 08:12 SamuelQZQ

gm @SamuelQZQ I'd like to tackle this one. Idea is to build an ABI constructor.

async function buildABI(ABI=[], address="") : ABI;

If empty ABI, builds from address by fetching module data using RESTful API by using the client Node URL. Substitutes ABI address by provided address.

Built ABI can be used as an argument in .useABI().

Let me know if you have another idea.

0xPrimata avatar May 10 '24 17:05 0xPrimata

@0xPrimata I think add a address params for createEntryPayload, and directly override the address in createEntryPayload could also work.

This approach is used in Wagmi, see here: https://wagmi.sh/react/api/hooks/useReadContract#type-inference It has a address param.

As for the buildABI solution you mentioned, I'm unsure if I understand it correctly. If buildABI fetches the ABI at runtime, the type system won't recognize the functions in the ABI through code parsing.

SamuelQZQ avatar May 11 '24 03:05 SamuelQZQ

@SamuelQZQ can this be closed by #113 or not?

0xbe1 avatar May 21 '24 14:05 0xbe1

Could you clarify here / in the docs how you can provide the module address?

banool avatar May 23 '24 14:05 banool

@banool will add in the doc. You can use createViewPayload or createEntryPayload like this:

const viewPayload = createViewPayload(COIN_ABI, {
      function: 'balance',
      functionArguments: ['0x1'],
      typeArguments: ['0x1::aptos_coin::AptosCoin'],
      address: "override address"
    });

SamuelQZQ avatar May 23 '24 15:05 SamuelQZQ

Magic!! Love to see it.

banool avatar May 23 '24 15:05 banool