Swap Accessories

Swap involves both unlinking and linking operations within a single transaction.

[
  {
    type: 'asset-link',
    action: 'delete',
    args: [
      // Operator
      'equipWith_asmBrain',
      // Opearand A: Boxer Asset ID
      'did:fv-asset:1:evm:0x6bca6de2dbdc4e0d41f7273011785ea16ba47182:1000',
      // Opearand B: Brain Asset ID - 21
      'did:fv-asset:1:evm:0x1ea66a857de297471bc12dd12d93853ff6617284:21',
    ],
  },
  {
    type: 'asset-link',
    action: 'create',
    args: [
      'equipWith_asmBrain',
      'did:fv-asset:1:evm:0x6bca6de2dbdc4e0d41f7273011785ea16ba47182:1000',
      // Opearand B: Brain Asset ID - 22
      'did:fv-asset:1:evm:0x1ea66a857de297471bc12dd12d93853ff6617284:22',
    ],
  },
]

The user must own both assets to swap unless it is an off-chain asset, where only the creator of the child asset can swap. Swap between assets owned on a FuturePass account and FuturePass account owner EOA is allowed. As long as the ownership resolution is satisfied, accessories can also be swapped across chains.

import {
  useGetARTM,
  useSignAndSubmitTransaction,
} from '@futureverse/asset-register-react/v2'

const unlinkOperation = {
  type: 'asset-link',
  action: 'delete',
  args: [
    'equipWith_asmBrain',
    'did:fv-asset:1:evm:0x6bca6de2dbdc4e0d41f7273011785ea16ba47182:1000',
    'did:fv-asset:1:evm:0x1ea66a857de297471bc12dd12d93853ff6617284:21',
  ],
}

const linkOperation = {
  type: 'asset-link',
  action: 'create',
  args: [
    'equipWith_asmBrain',
    'did:fv-asset:1:evm:0x6bca6de2dbdc4e0d41f7273011785ea16ba47182:1000',
    'did:fv-asset:1:evm:0x1ea66a857de297471bc12dd12d93853ff6617284:22',
  ],
}

const LinkAccessory = ({ address }) => {
  const {
    artm,
    reactQuery: { refetch: getARTM },
  } = useGetARTM(
    {
      address,
      [],
    },
    { enabled: false },
  )

  const { submitAsync, transaction } = useSignAndSubmitTransaction()

  const submitARTM = async () => {
    const { data: artm } = await getARTM()
    if (!artm) {
      return
    }
    artm.addOperation(unlinkOperation)
    artm.addOperation(linkOperation)
    return submitAsync({ artm })
  }

  return (
    <button onClick={submitARTM}></button>
  )
}

Last updated

© 2023 -> ♾️