-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b776e9
commit aff621b
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |