Anastasia-Labs / lucid-evolution

https://anastasia-labs.github.io/lucid-evolution/
35 stars 10 forks source link

Convert Lucid Instance to Effect type and set provider as optional #203

Open solidsnakedev opened 1 month ago

solidsnakedev commented 1 month ago

Currently Lucid is not capable to init without passing a provider, this is problematic when you just want to select a wallet and signing a transaction from an api.

The ideal workflow should be as follows.

        const lucid = await Lucid(undefined, undefined);
        const api = await window.cardano[enabledWallet].enable();
        lucid.selectWallet.fromAPI(api);
        const response = await fetch("/api/buildtransaction", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({ address: usedAddresses[0] }),
        });
        const { tx } = await response.json();
        const signedTx = await lucid.fromTx(tx).sign.withWallet().complete();

At the same time the side effects from having an undefined expression is problematic as well, therefore Lucid must be converted to Effect type so we can have a better error control

https://github.com/Anastasia-Labs/lucid-evolution/blob/85bfd542c3de3decdc0842ab7cac6833a0f83ebd/packages/lucid/src/lucid-evolution/LucidEvolution.ts#L71-L74

SynthLuvr commented 1 month ago

Somewhat related, I created a selectWallet.fromRandom method that creates a random wallet so I get the provider functions and all the other functions don't fail when I don't need address and UTXOs. It's quite simple in that it generates a random seed phrase, derives the address, then passes in the address and empty UTXO array into selectWallet.fromAddress