npm package: @futureverse/transact
Description: An API to help facilitate transactions on The Root Network
Latest version: 0.8.0

# Futureverse Transact Library

An API to help facilitate transactions on The Root Network

## Installation

NPM:

```bash
npm install @futureverse/transact --save
```

Yarn:

```bash
yarn add @futureverse/transact
```

## Dependancies

```json
{
  "@futureverse/signer": "0.6.6",
  "@polkadot/api": "^10.13.1",
  "@polkadot/util": "^12.6.2",
  "@polkadot/util-crypto": "^12.6.2",
  "@polkadot/types": "^10.13.1",
  "@therootnetwork/api-types": "^1.0.3",
  "viem": "^2.18.1"
}
```

## Usage

This example shows how to mint an NFT on a Root Network Collection

```typescript
import { useAuth, useFutureverseSigner } from '@futureverse/auth-react';
import { TransactionBuilder } from '@futureverse/transact';
import { useTrnApi } from '@futureverse/transact-react';

const { userSession } = useAuth();
const [builder, setBuilder] = useState();
const signer = useFutureverseSigner();
const { trnApi } = useTrnApi();

const triggerInit = useCallback(async () => {
  if (!trnApi || !signer || !userSession) {
    return null;
  }

  const nftBuilder = await TransactionBuilder.nft(trnApi, signer, userSession.eoa, 709732).mint({ quantity: 1, walletAddress: userSession?.futurepass });

  setBuilder(nftBuilder);
}, [trnApi, signer, userSession]);

const signExtrinsic = useCallback(async () => {
  if (builder) {
    const result = await builder?.signAndSend({ onSign, onSend });
    setResult(result as ExtrinsicResult);
  }
}, [builder, onSend, onSign, toSign]);

return (
  <>
    <button
      onClick={() => {
        triggerInit();
      }}
    >
      Mint 1 Nft
    </button>

    <button
      onClick={() => {
        signExtrinsic();
      }}
    >
      Sign &amp; Send
    </button>
  </>
);
```

This example shows how to transfer a token to an wallet address on a Root Network Collection, but minting using the Fee Proxy to pay for gas with a chosen token.

```typescript
import { useAuth, useFutureverseSigner } from '@futureverse/auth-react';
import { TransactionBuilder } from '@futureverse/transact';
import { useTrnApi } from '@futureverse/transact-react';

const { userSession } = useAuth();
const [builder, setBuilder] = useState();
const signer = useFutureverseSigner();
const { trnApi } = useTrnApi();

const triggerInit = useCallback(async () => {
  if (!trnApi || !signer || !userSession) {
    return null;
  }

  const nftBuilder = await TransactionBuilder.transfer({ destinationAddress: userSession?.futurepass, amount: 1 }).addFeeProxy({ assetId: ROOT_TOKEN_ID, slippage: 5 });

  setBuilder(nftBuilder);
}, [trnApi, signer, userSession]);

const signExtrinsic = useCallback(async () => {
  if (builder) {
    const result = await builder?.signAndSend({ onSign, onSend });
    setResult(result as ExtrinsicResult);
  }
}, [builder, onSend, onSign, toSign]);

return (
  <>
    <button
      onClick={() => {
        triggerInit();
      }}
    >
      Mint 1 Nft
    </button>

    <button
      onClick={() => {
        signExtrinsic();
      }}
    >
      Sign &amp; Send
    </button>
  </>
);
```

This example shows how to mint an NFT on a Root Network Collection, but minting using the FuturePass to pay the gas.

```typescript
import { useAuth, useFutureverseSigner } from '@futureverse/auth-react';
import { TransactionBuilder } from '@futureverse/transact';
import { useTrnApi } from '@futureverse/transact-react';

const { userSession } = useAuth();
const [builder, setBuilder] = useState();
const signer = useFutureverseSigner();
const { trnApi } = useTrnApi();

const triggerInit = useCallback(async () => {
  if (!trnApi || !signer || !userSession) {
    return null;
  }

  const nftBuilder = await TransactionBuilder.nft(trnApi, signer, userSession.eoa, 709732).mint({ quantity: 1, walletAddress: userSession?.futurepass }).addFuturePass(userSession.futurepass);

  setBuilder(nftBuilder);
}, [trnApi, signer, userSession]);

const signExtrinsic = useCallback(async () => {
  if (builder) {
    const result = await builder?.signAndSend({ onSign, onSend });
    setResult(result as ExtrinsicResult);
  }
}, [builder, onSend, onSign, toSign]);

return (
  <>
    <button
      onClick={() => {
        triggerInit();
      }}
    >
      Mint 1 Nft
    </button>

    <button
      onClick={() => {
        signExtrinsic();
      }}
    >
      Sign &amp; Send
    </button>
  </>
);
```

This example shows how to batch multiple transactions on The Root Network Collection together and send them using the FuturePass to pay the gas combined with the fee proxy to pay with a chosen token.

```typescript
import { useAuth, useFutureverseSigner } from '@futureverse/auth-react';
import { TransactionBuilder } from '@futureverse/transact';
import { useTrnApi } from 'providers/TRNProvider';

const { userSession } = useAuth();
const [builder, setBuilder] = useState();
const signer = useFutureverseSigner();
const { trnApi } = useTrnApi();

const triggerInit = useCallback(async () => {
  if (!trnApi || !signer || !userSession) {
    return null;
  }

  const tx1 = trnApi?.tx?.nft.mint(709732, 1, eoa);
  const tx2 = trnApi?.tx?.nft.mint(709732, 5, fp);
  const tx3 = trnApi?.tx?.assetsExt.transfer(1, fp, 1, true);
  const tx4 = trnApi?.tx?.assetsExt.transfer(2, fp, 10, true);
  const tx5 = trnApi?.tx?.nft.mint(709732, 3, eoa);

  const batchBuilder = await TransactionBuilder.batch(trnApi, signer, userSession.eoa).addExtrinsic(tx1).addExtrinsic(tx2).addExtrinsic(tx3).addExtrinsic(tx4).addExtrinsic(tx5).batchAll().addFuturePassAndFeeProxy({
    futurePass: userSession.futurepass,
    assetId: assetId,
    slippage: 5,
  });

  setBuilder(nftBuilder);
}, [trnApi, signer, userSession]);

const signExtrinsic = useCallback(async () => {
  if (builder) {
    const result = await builder?.signAndSend({ onSign, onSend });
    setResult(result as ExtrinsicResult);
  }
}, [builder, onSend, onSign, toSign]);

return (
  <>
    <button
      onClick={() => {
        triggerInit();
      }}
    >
      Mint 1 Nft
    </button>

    <button
      onClick={() => {
        signExtrinsic();
      }}
    >
      Sign &amp; Send
    </button>
  </>
);
```