Skip to main content

mnft.ensureMachineNftAllowance(EnsureMachineNftAllowance)

Ensure the ERC20 allowance for the Machine NFT registration account is sufficient to cover the registration fee for N machines of a given value. If the allowance is insufficient, it submits an approval transaction.

EnsureMachineNftAllowance Type Parameters

ParameterTypeRequiredDescription
machineControllerSignerRequiredSigner that will pay the ERC20 fee. Must be connected to a provider.
machineNftstringRequiredMachine NFT contract address.
machineValueHumanstringRequiredMachine value in human-readable units (e.g., "10").
erc20stringRequiredERC20 token address used to pay the fee.
tokenDecimalsnumberRequiredERC20 token decimals.
machineCountnumberRequiredNumber of machines to register.

Returns

FieldTypeDescription
statusapproved or already_sufficientWhether an approval was sent.
machineNftstringMachine NFT contract address.
feeTokenstringERC20 token address used for fees.
feePerMachinebigintFee per machine in token units.
requiredAllowancebigintTotal allowance required for machineCount.
currentAllowancebigintCurrent allowance after the check/approval.
receiptTransactionReceiptOnly present when status is 'approved'.

Usage

TypeScript

import 'dotenv/config';
import { RWA, Chain, type SDKInit } from "@peaq-network/rwa";
import { JsonRpcProvider, Wallet } from "ethers";

async function main() {
  // 0. Create RWA instance and get provider
  const provider = new JsonRpcProvider(process.env.HTTPS_BASE_URL);
  const init: SDKInit = { chainId: Chain.AGUNG, provider: provider };
  const rwa_sdk = new RWA(init);

  // 1. Machine controller (pays ERC20 fee via allowance)
  const alice = new Wallet(process.env.ALICE_PRIVATE_KEY!, provider);

  // 2. Ensure allowance
  const result = await rwa_sdk.mnft.ensureMachineNftAllowance({
    machineController: alice,
    machineNft: "0x5Ca3db1f292f913DDdA8C5385E5391438665463c",
    machineValueHuman: "10",
    erc20: rwa_sdk.getAddresses().erc20.peaq,
    tokenDecimals: 18,
    machineCount: 2
  });
  console.log("Result", result);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

JavaScript

import 'dotenv/config';
import { RWA, Chain } from "@peaq-network/rwa";
import { JsonRpcProvider, Wallet } from "ethers";

async function main() {
  // 0. Create RWA instance and get provider
  const provider = new JsonRpcProvider(process.env.HTTPS_BASE_URL);
  const rwa_sdk = new RWA({ chainId: Chain.AGUNG, provider });

  // 1. Machine controller (pays ERC20 fee via allowance)
  const alice = new Wallet(process.env.ALICE_PRIVATE_KEY, provider);

  // 2. Ensure allowance
  const result = await rwa_sdk.mnft.ensureMachineNftAllowance({
    machineController: alice,
    machineNft: "0x5Ca3db1f292f913DDdA8C5385E5391438665463c",
    machineValueHuman: "10",
    erc20: rwa_sdk.getAddresses().erc20.peaq,
    tokenDecimals: 18,
    machineCount: 2
  });
  console.log("Result", result);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Example outputs

Result {
  status: 'approved',
  machineNft: '0x5Ca3db1f292f913DDdA8C5385E5391438665463c',
  feeToken: '0x...',
  feePerMachine: 10000000000000000000n,
  requiredAllowance: 20000000000000000000n,
  currentAllowance: 20000000000000000000n,
  receipt: ContractTransactionReceipt {
    ...
  }
}