• useUpdateFollowPolicy allows you to update the follow policy of the authenticated Profile.

    You MUST be authenticated via useLogin to use this hook.

    Returns UseDeferredTask<void, BroadcastingError | PendingSigningRequestError | UserRejectedError | WalletConnectionError | TransactionError | InsufficientGasError, UpdateFollowPolicyArgs>

    Example

    const { execute, loading, error } = useUpdateFollowPolicy();
    

    Follow policy types

    Anyone can follow.

    const { execute, loading, error } = useUpdateFollowPolicy();

    await execute({
    followPolicy: {
    type: FollowPolicyType.ANYONE,
    },
    });

    No one can follow.

    const { execute, loading, error } = useUpdateFollowPolicy();

    await execute({
    followPolicy: {
    type: FollowPolicyType.NO_ONE,
    },
    });

    Anyone can follow, but they must pay a fee. To setup a FollowPolicyType.CHARGE you need to define an amount of a currency as a fee.

    As with anything involving amounts in the Lens SDK you can use the Amount helper with currencies from the useCurrencies hook to create the desired amounts.

    const { execute, loading, error } = useUpdateFollowPolicy();

    const wmatic = ... // from useCurrencies hook

    await execute({
    followPolicy: {
    type: FollowPolicyType.CHARGE,
    amount: Amount.erc20(wmatic, 100), // 100 WMATIC
    recipient: '0x1234123412341234123412341234123412341234',
    },
    });

    Self-funded approach

    It just takes a single parameter to disable the sponsorship of the transaction gas costs.

    await execute({
    followPolicy: {
    type: FollowPolicyType.CHARGE,
    amount: Amount.erc20(wmatic, 100), // 100 WMATIC
    recipient: '0x1234123412341234123412341234123412341234',
    },
    sponsored: false, // <--- this is the only difference
    });

    In this example you can also see a new error type: InsufficientGasError. This error happens only with self-funded transactions and it means that the wallet does not have enough funds to pay for the transaction gas costs.

    Self-funded fallback

    If for some reason the Lens API cannot sponsor the transaction, the hook will fail with a BroadcastingError with one of the following reasons:

    In those cases you can retry the transaction as self-funded like in the following example:

    const update = async () => {
    // the first part is the same as in the initial example

    // sponsored attempt
    const sponsoredResult = await execute({
    followPolicy: {
    type: FollowPolicyType.CHARGE,
    amount: Amount.erc20(wmatic, 100), // 100 WMATIC
    recipient: '0x1234123412341234123412341234123412341234',
    },
    sponsored: false,
    });

    if (sponsoredResult.isFailure()) {
    switch (sponsoredResult.error.name) {
    case 'BroadcastingError':
    if ([BroadcastingErrorReason.NOT_SPONSORED, BroadcastingErrorReason.RATE_LIMITED].includes(sponsoredResult.error.reason)) {

    const selfFundedResult = await execute({
    metadataURI: uri,
    sponsored: false
    });

    // continue with selfFundedResult as in the previous example
    }
    break;

    // ...
    }
    }

    In this example we omitted BroadcastingErrorReason.APP_NOT_ALLOWED as it's not normally a problem per-se. It just requires the app to apply for whitelisting. See https://docs.lens.xyz/docs/gasless-and-signless#whitelisting-your-app.

    You can still include it in your fallback logic if you want to. For example to unblock testing your app from a domain that is not the whitelisted one (e.g. localhost).