Last active
August 26, 2021 19:03
-
-
Save klosowsk/c22656119299d646224e2ec3aee62cc2 to your computer and use it in GitHub Desktop.
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
import { Avalanche, BinTools, Buffer, BN } from "avalanche"; | |
import { | |
AVMAPI, | |
KeyChain as AVMKeyChain, | |
UTXOSet, | |
UnsignedTx, | |
Tx, | |
MinterSet, | |
AVMConstants, | |
UTXO, | |
} from "avalanche/dist/apis/avm"; | |
import { OutputOwners } from "avalanche/dist/common"; | |
import { UnixNow } from "avalanche/dist/utils"; | |
const getUTXOIDs = (utxoSet: UTXOSet, txid: string, outputType: AVMConstants, assetID: string): string[] => { | |
const utxoids: string[] = utxoSet.getUTXOIDs(); | |
console.log("===="); | |
console.log(utxoids); | |
let result: string[] = []; | |
for (let index: number = 0; index < utxoids.length; ++index) { | |
// console.log("----"); | |
// console.log(bintools.cb58Encode(utxoSet.getUTXO(utxoids[index]).getTxID())); | |
// console.log(bintools.cb58Encode(utxoSet.getUTXO(utxoids[index]).getAssetID())); | |
// console.log("----"); | |
if ( | |
utxoids[index].indexOf(txid.slice(0, 10)) != -1 && | |
utxoSet.getUTXO(utxoids[index]).getOutput().getOutputID() == outputType && | |
assetID == bintools.cb58Encode(utxoSet.getUTXO(utxoids[index]).getAssetID()) | |
) { | |
result.push(utxoids[index]); | |
} | |
} | |
console.log("===="); | |
return result; | |
}; | |
const bintools = BinTools.getInstance(); | |
const myNetworkID = 5; | |
const myBlockchainID = "X"; | |
const ava = new Avalanche("api.avax-test.network", 443, "https", myNetworkID, myBlockchainID); | |
const xchain = ava.XChain(); | |
/* | |
* 1. Import a key | |
*/ | |
// X-fuji10s8t4jtyy5v787hg0wzrn7rlzlx4uzndj40wct | |
// PrivateKey-2SE3q1EFWoQVUHyCKZ5Z7f7SLLKUK8i1RAG3Voeno81ygW86Uo | |
// PrivateKey-2JN2KCCYJdmkPkNwmmWwbUktfAB6NNJNCXFzYQfUfFLA8McTGk | |
let keypair = xchain.keyChain(); | |
const mypk = "PrivateKey-2JN2KCCYJdmkPkNwmmWwbUktfAB6NNJNCXFzYQfUfFLA8McTGk"; | |
const keys = keypair.importKey(mypk); | |
console.log(keys.getPublicKeyString()); | |
console.log(keys.getPrivateKeyString()); | |
const myAddresses = xchain.keyChain().getAddresses(); | |
const myAddressesStrings = xchain.keyChain().getAddressStrings(); | |
console.log(myAddressesStrings); | |
/* | |
* 2. NFT Definitions | |
*/ | |
const name: string = "TYB stage token 8"; | |
const symbol: string = "TYBS"; | |
const threshold: number = 1; | |
const locktime: BN = new BN(0); | |
const memo: Buffer = Buffer.from("TYB stage token 8"); | |
const minterSets: MinterSet[] = [new MinterSet(threshold, myAddresses)]; | |
const asOf: BN = UnixNow(); | |
/* | |
* 3. NFT minting Definitions | |
*/ | |
// const payload: Buffer = Buffer.from( | |
// `{"avalanche":{"version":1,"type":"generic","title 2":"Welcome!!","img":"https://i.ibb.co/vVW7JQr/Logo-500x500-px.gif","desc":""}}` | |
// ); | |
const payload: Buffer = Buffer.from( | |
`{"avalanche":{"version":1,"type":"generic","title 2":"Welcome!!","img":"ipfs://QmZrd7dQaJHgpfQHpPxnzaGLc7f9JdGo3bJJDkDS863h61","desc":""}}` | |
); | |
const createFamily = async () => { | |
const utxos = (await xchain.getUTXOs(myAddressesStrings)).utxos; | |
const unsignedTx = await xchain.buildCreateNFTAssetTx( | |
utxos, | |
myAddressesStrings, | |
myAddressesStrings, | |
minterSets, | |
name, | |
symbol, | |
memo | |
// asOf, | |
// locktime | |
); | |
const signedTx = unsignedTx.sign(keypair); | |
const txid = await xchain.issueTx(signedTx); | |
// The returned transaction id corresponds to the id of the asset | |
console.log("Transaction id or asset id"); | |
console.log(txid); | |
}; | |
const mint = async (assetID: string) => { | |
const utxos = (await xchain.getUTXOs(myAddressesStrings)).utxos; | |
const outputOwners: OutputOwners = new OutputOwners(myAddresses); | |
// Asset ID and Tx ID are the same after creating the NFT | |
const nftMintOutputUTXOIDs: string[] = getUTXOIDs(utxos, assetID, AVMConstants.NFTMINTOUTPUTID, assetID); | |
const nftMintOutputUTXOID: string = nftMintOutputUTXOIDs[0]; | |
const groupID: number = 0; | |
const unsignedTx: UnsignedTx = await xchain.buildCreateNFTMintTx( | |
utxos, | |
[outputOwners, outputOwners, outputOwners], | |
myAddressesStrings, | |
myAddressesStrings, | |
nftMintOutputUTXOID, | |
groupID, | |
payload | |
// memo, | |
// asOf | |
); | |
const tx: Tx = unsignedTx.sign(keypair); | |
const txid: string = await xchain.issueTx(tx); | |
console.log(`Success! TXID: ${txid}`); | |
}; | |
// Send the ID of the mint transaction and the id of the asset (the tx of the create) | |
const transfer = async (txId: string, assetID: string) => { | |
const utxos = (await xchain.getUTXOs(myAddressesStrings)).utxos; | |
const nftTransferOutputUTXOIDs: string[] = getUTXOIDs(utxos, txId, AVMConstants.NFTXFEROUTPUTID, assetID); | |
const nftTransferOutputUTXOID: string = nftTransferOutputUTXOIDs[0]; | |
//const destination = ["X-fuji19xu2g7lv4e5lv9ctla7u3gst225y4jcc65gdl8"]; | |
const destination = myAddressesStrings; | |
const unsignedTx: UnsignedTx = await xchain.buildNFTTransferTx( | |
utxos, | |
destination, | |
myAddressesStrings, | |
myAddressesStrings, | |
nftTransferOutputUTXOID | |
// memo, | |
// asOf, | |
// locktime, | |
// threshold | |
); | |
const tx: Tx = unsignedTx.sign(keypair); | |
const id: string = await xchain.issueTx(tx); | |
console.log(`Success! TXID: ${id}`); | |
}; | |
// How to run | |
//1 | |
//createFamily(); | |
//2 | |
//mint("2QfCYBomK7QHPq6MmQzmZqCduR41qJMzzJ6dbcjCCuLKBe4DD1"); | |
//3 | |
//transfer("SXfdQW5BU3zmKqSFDHwUGAa23ssRUYG1AUQLrqnC6Too1VXJA", "2jXLbK1NmW1h76vnA4ADmp8FDXiKeDGAqjQGeYPdDqVD8e92yz"); | |
const createAddress = async () => { | |
let keypair = xchain.keyChain(); | |
let newAddress = keypair.makeKey(); | |
console.log(newAddress.getPrivateKeyString()); | |
console.log(newAddress.getPublicKeyString()); | |
}; | |
const getUtxosMinted = async (assetID: string) => { | |
const utxos = (await xchain.getUTXOs(myAddressesStrings)).utxos; | |
const allUtxos = utxos.getAllUTXOs(); | |
//console.log(allUtxos); | |
const mintedAssetIdUtxos = allUtxos.filter( | |
(utxo) => | |
bintools.cb58Encode(utxo.getAssetID()) === assetID && | |
utxo.getOutput().getOutputID() == AVMConstants.NFTXFEROUTPUTID | |
); | |
//console.log(mintedAssetIdUtxos); | |
mintedAssetIdUtxos.map((minted) => { | |
console.log("-------"); | |
console.log("UTXO ID => ", minted.getUTXOID()); | |
console.log("Asset ID => ", bintools.cb58Encode(minted.getAssetID())); | |
console.log("-------"); | |
}); | |
}; | |
const getNFTsWallet = async () => { | |
const utxos = (await xchain.getUTXOs(myAddressesStrings)).utxos; | |
const allUtxos = utxos.getAllUTXOs(); | |
console.log(allUtxos); | |
const allNFTs = allUtxos.filter((utxo) => utxo.getOutput().getOutputID() == AVMConstants.NFTXFEROUTPUTID); | |
//console.log(allNFTs); | |
}; | |
// Helpers | |
//createAddress(); | |
//getUtxosMinted("2QfCYBomK7QHPq6MmQzmZqCduR41qJMzzJ6dbcjCCuLKBe4DD1"); | |
//getNFTsWallet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment