Skip to content

Commit

Permalink
feat: add TransferNFT api
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Nov 4, 2024
1 parent 8b776e9 commit aff621b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/content-script/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ export async function ecdh(params: { path?: string; externalPubKey: string }) {
return await createAction('ECDH', 'query', params)
}

export async function transferNFT(params: {
codehash: string
genesis: string
tokenIndex: string
recipient: string
options?: { noBroadcast?: boolean; feeRate?: string | number; useUnconfirmed?: boolean }
}) {
return await createAction('TransferNFT', 'authorize', params)
}

// export async function transferAll(params: {
// receivers: {
// address: string
Expand Down
2 changes: 2 additions & 0 deletions src/content-script/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
signTransactions,
pay,
ecdh,
transferNFT,
} from './actions'

import { btcKeys, createAction, ActionType, on, removeListener } from './actions'
Expand Down Expand Up @@ -126,6 +127,7 @@ const metalet: any = {
exitAccount: disconnect,
getMvcBalance: getBalance,
getSensibleFtBalance: getTokenBalance,
transferNFT,
}

Object.keys(btcKeys).forEach((type) => {
Expand Down
9 changes: 9 additions & 0 deletions src/data/authorize-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as MRC20Deploy from '../lib/actions/btc/mrc20-deploy'
import * as MRC20Mint from '../lib/actions/btc/mrc20-mint'
import * as MRC20Transfer from '../lib/actions/btc/mrc20-transfer'
import * as TransferUtxo from '../lib/actions/btc/transfer-utxo'
import * as TransferNFT from '../lib/actions/transfer-nft'

function doNothing() {}

Expand Down Expand Up @@ -210,4 +211,12 @@ export default {
estimate: doNothing,
closeAfterProcess: true,
},
TransferNFT: {
name: 'TransferNFT',
title: 'Transfer NFT',
description: '',
process: TransferNFT.process,
estimate: doNothing,
closeAfterProcess: true,
},
} as { [key: string]: AuthorizeAction }
1 change: 0 additions & 1 deletion src/lib/actions/btc/transfer-utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export async function process({
changeAddress,
}: TransferParameters): Promise<{ txId: string } | { txHex: string }> {
const wallet = await getCurrentWallet(Chain.BTC)
const address = wallet.getAddress()

let feeRate = options?.feeRate

Expand Down
65 changes: 65 additions & 0 deletions src/lib/actions/transfer-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getNetwork } from '@/lib/network'
import { getCurrentWallet } from '@/lib/wallet'
import { Chain } from '@metalet/utxo-wallet-service'
import { API_NET, API_TARGET, NftManager } from 'meta-contract'

interface TransferParameters {
codehash: string
genesis: string
tokenIndex: string
recipient: string
options?: { noBroadcast?: boolean; feeRate?: string | number; useUnconfirmed?: boolean }
}

export async function process({
options,
recipient,
codehash,
genesis,
tokenIndex,
}: TransferParameters): Promise<{ txId: string } | { txHex: string }> {
const network = await getNetwork()
const currentMVCWallet = await getCurrentWallet(Chain.MVC)
const privateKey = currentMVCWallet.getPrivateKey()

const nftManager = new NftManager({
network: network as API_NET,
apiTarget: API_TARGET.MVC,
purse: privateKey,
})

// Pick the largest utxo from wallet to pay the transaction
const largestUtxo = await nftManager.api
.getUnspents(currentMVCWallet.getAddress())
.then((utxos) => {
return utxos.reduce((prev, curr) => {
if (curr.satoshis > prev.satoshis) return curr
return prev
})
})
.then((utxo) => {
// add wif to utxo
return {
...utxo,
wif: privateKey,
}
})

const transferRes = await nftManager
.transfer({
genesis,
codehash,
tokenIndex,
senderWif: privateKey,
receiverAddress: recipient,
utxos: [largestUtxo],
noBroadcast: options?.noBroadcast,
})
.catch((err) => {
throw err
})
if (options?.noBroadcast) {
return { txHex: transferRes.txHex }
}
return { txId: transferRes.txid }
}

0 comments on commit aff621b

Please sign in to comment.