Skip to content

Commit 0a93dc1

Browse files
committed
Mint ANT and NFT examples.
1 parent 0d719af commit 0a93dc1

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed

examples/avm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Tests for the Avalanche [AVM RPC](https://docs.avax.network/build/avalanchego-ap
1212
* [exportTx-avax-cchain.ts](./exportTx-avax-cchain.ts)
1313
* [importTx-ant-cchain.ts](./importTx-ant-cchain.ts)
1414
* [importTx-avax-cchain.ts](./importTx-avax-cchain.ts)
15+
* [operationTx-mint-ant.ts](./operationTx-mint-ant.ts)
16+
* [operationTx-mint-avax.ts](./operationTx-mint-avax.ts)

examples/avm/operationTx-mint-ant.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
SECPTransferOutput,
11+
SECPTransferInput,
12+
TransferableOutput,
13+
TransferableInput,
14+
UTXOSet,
15+
UTXO,
16+
AmountOutput,
17+
UnsignedTx,
18+
AVMConstants,
19+
OperationTx,
20+
SECPMintOperation,
21+
SECPMintOutput,
22+
TransferableOperation,
23+
Tx
24+
} from "../../src/apis/avm"
25+
import { Defaults } from "../../src/utils"
26+
27+
const getUTXOIDs = (utxoSet: UTXOSet, txid: string, outputType: number = AVMConstants.SECPXFEROUTPUTID_CODECONE, assetID = "2fombhL7aGPwj3KH4bfrmJwW6PVnMobf9Y2fn9GwxiAAJyFDbe"): string[] => {
28+
const utxoids: string[] = utxoSet.getUTXOIDs()
29+
let result: string[] = []
30+
for (let index: number = 0; index < utxoids.length; ++index) {
31+
if (utxoids[index].indexOf(txid.slice(0,10)) != -1 && utxoSet.getUTXO(utxoids[index]).getOutput().getOutputID() == outputType && assetID == bintools.cb58Encode(utxoSet.getUTXO(utxoids[index]).getAssetID())) {
32+
result.push(utxoids[index])
33+
}
34+
}
35+
return result
36+
}
37+
38+
const ip: string = "localhost"
39+
const port: number = 9650
40+
const protocol: string = "http"
41+
const networkID: number = 12345
42+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
43+
const xchain: AVMAPI = avalanche.XChain()
44+
const bintools: BinTools = BinTools.getInstance()
45+
const xKeychain: AVMKeyChain = xchain.keyChain()
46+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
47+
xKeychain.importKey(privKey)
48+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
49+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
50+
const blockchainid: string = Defaults.network['12345'].X.blockchainID
51+
const outputs: TransferableOutput[] = []
52+
const inputs: TransferableInput[] = []
53+
const operations: TransferableOperation[] = []
54+
const fee: BN = xchain.getDefaultTxFee()
55+
const threshold: number = 1
56+
const locktime: BN = new BN(0)
57+
const memo: Buffer = bintools.stringToBuffer("AVM manual OperationTx to mint an ANT")
58+
// Uncomment for codecID 00 01
59+
// const codecID: number = 1
60+
61+
const main = async (): Promise<any> => {
62+
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
63+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
64+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
65+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
66+
utxos.forEach((utxo: UTXO) => {
67+
const txid: Buffer = utxo.getTxID()
68+
const outputidx: Buffer = utxo.getOutputIdx()
69+
const assetID: Buffer = utxo.getAssetID();
70+
if(utxo.getOutput().getTypeID() != 6) {
71+
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
72+
const amt: BN = amountOutput.getAmount().clone()
73+
74+
if(assetID.toString('hex') === avaxAssetID.toString('hex')) {
75+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt.sub(fee), xAddresses, locktime, threshold)
76+
// Uncomment for codecID 00 01
77+
// secpTransferOutput.setCodecID(codecID)
78+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
79+
outputs.push(transferableOutput)
80+
81+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
82+
// Uncomment for codecID 00 01
83+
// secpTransferInput.setCodecID(codecID)
84+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
85+
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
86+
inputs.push(input)
87+
} else {
88+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt, xAddresses, locktime, threshold)
89+
// Uncomment for codecID 00 01
90+
// secpTransferOutput.setCodecID(codecID)
91+
const transferableOutput: TransferableOutput = new TransferableOutput(assetID, secpTransferOutput)
92+
outputs.push(transferableOutput)
93+
94+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
95+
// Uncomment for codecID 00 01
96+
// secpTransferInput.setCodecID(codecID)
97+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
98+
const input: TransferableInput = new TransferableInput(txid, outputidx, assetID, secpTransferInput)
99+
inputs.push(input)
100+
}
101+
} else {
102+
const vcapAmount: BN = new BN(507)
103+
const vcapSecpTransferOutput: SECPTransferOutput = new SECPTransferOutput(vcapAmount, xAddresses, locktime, threshold)
104+
// Uncomment for codecID 00 01
105+
// vcapSecpTransferOutput.setCodecID(codecID)
106+
const secpMintOutputUTXOIDs: string[] = getUTXOIDs(utxoSet, bintools.cb58Encode(txid), AVMConstants.SECPMINTOUTPUTID, bintools.cb58Encode(assetID))
107+
const mintOwner: SECPMintOutput = utxo.getOutput() as SECPMintOutput
108+
// Uncomment for codecID 00 01
109+
// mintOwner.setCodecID(codecID)
110+
const secpMintOperation: SECPMintOperation = new SECPMintOperation(mintOwner, vcapSecpTransferOutput)
111+
// Uncomment for codecID 00 01
112+
// secpMintOperation.setCodecID(codecID)
113+
const spenders: Buffer[] = mintOwner.getSpenders(xAddresses)
114+
115+
spenders.forEach((spender: Buffer) => {
116+
const idx: number = mintOwner.getAddressIdx(spender)
117+
secpMintOperation.addSignatureIdx(idx, spender)
118+
})
119+
120+
const transferableOperation: TransferableOperation = new TransferableOperation(utxo.getAssetID(), secpMintOutputUTXOIDs, secpMintOperation)
121+
operations.push(transferableOperation)
122+
}
123+
})
124+
const operationTx: OperationTx = new OperationTx (
125+
networkID,
126+
bintools.cb58Decode(blockchainid),
127+
outputs,
128+
inputs,
129+
memo,
130+
operations
131+
)
132+
// Uncomment for codecID 00 01
133+
// operationTx.setCodecID(codecID)
134+
const unsignedTx: UnsignedTx = new UnsignedTx(operationTx)
135+
const tx: Tx = unsignedTx.sign(xKeychain)
136+
const id: string = await xchain.issueTx(tx)
137+
console.log(id)
138+
}
139+
140+
main()
141+

examples/avm/operationTx-mint-nft.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import {
2+
Avalanche,
3+
BinTools,
4+
BN,
5+
Buffer
6+
} from "../../src";
7+
import {
8+
AVMAPI,
9+
KeyChain as AVMKeyChain,
10+
SECPTransferOutput,
11+
SECPTransferInput,
12+
TransferableOutput,
13+
TransferableInput,
14+
UTXOSet,
15+
UTXO,
16+
AmountOutput,
17+
UnsignedTx,
18+
AVMConstants,
19+
OperationTx,
20+
TransferableOperation,
21+
Tx,
22+
KeyChain,
23+
NFTMintOperation,
24+
NFTMintOutput
25+
} from "../../src/apis/avm"
26+
import { OutputOwners } from "../../src/common";
27+
import { Defaults } from "../../src/utils"
28+
29+
const getUTXOIDs = (utxoSet: UTXOSet, txid: string, outputType: number = AVMConstants.SECPXFEROUTPUTID_CODECONE, assetID = "2fombhL7aGPwj3KH4bfrmJwW6PVnMobf9Y2fn9GwxiAAJyFDbe"): string[] => {
30+
const utxoids: string[] = utxoSet.getUTXOIDs()
31+
let result: string[] = []
32+
for (let index: number = 0; index < utxoids.length; ++index) {
33+
if (utxoids[index].indexOf(txid.slice(0,10)) != -1 && utxoSet.getUTXO(utxoids[index]).getOutput().getOutputID() == outputType && assetID == bintools.cb58Encode(utxoSet.getUTXO(utxoids[index]).getAssetID())) {
34+
result.push(utxoids[index])
35+
}
36+
}
37+
return result
38+
}
39+
40+
const ip: string = "localhost"
41+
const port: number = 9650
42+
const protocol: string = "http"
43+
const networkID: number = 12345
44+
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
45+
const xchain: AVMAPI = avalanche.XChain()
46+
const bintools: BinTools = BinTools.getInstance()
47+
const xKeychain: KeyChain = xchain.keyChain()
48+
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
49+
xKeychain.importKey(privKey)
50+
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
51+
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
52+
const blockchainid: string = Defaults.network['12345'].X.blockchainID
53+
const outputs: TransferableOutput[] = []
54+
const inputs: TransferableInput[] = []
55+
const operations: TransferableOperation[] = []
56+
const fee: BN = xchain.getDefaultTxFee()
57+
const threshold: number = 1
58+
const locktime: BN = new BN(0)
59+
const memo: Buffer = bintools.stringToBuffer("AVM manual OperationTx to mint an NFT")
60+
const payload: Buffer = bintools.stringToBuffer("NFT Payload")
61+
const groupID: number = 0
62+
// Uncomment for codecID 00 01
63+
// const codecID: number = 1
64+
65+
const main = async (): Promise<any> => {
66+
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
67+
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
68+
const utxoSet: UTXOSet = avmUTXOResponse.utxos
69+
const utxos: UTXO[] = utxoSet.getAllUTXOs()
70+
utxos.forEach((utxo: UTXO, index: number) => {
71+
const txid: Buffer = utxo.getTxID()
72+
const outputidx: Buffer = utxo.getOutputIdx()
73+
const assetID: Buffer = utxo.getAssetID();
74+
if(utxo.getOutput().getTypeID() != 10 && utxo.getOutput().getTypeID() != 11) {
75+
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
76+
const amt: BN = amountOutput.getAmount().clone()
77+
78+
if(assetID.toString('hex') === avaxAssetID.toString('hex')) {
79+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt.sub(fee), xAddresses, locktime, threshold)
80+
// Uncomment for codecID 00 01
81+
// secpTransferOutput.setCodecID(codecID)
82+
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
83+
outputs.push(transferableOutput)
84+
85+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
86+
// Uncomment for codecID 00 01
87+
// secpTransferInput.setCodecID(codecID)
88+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
89+
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
90+
inputs.push(input)
91+
} else {
92+
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt, xAddresses, locktime, threshold)
93+
// Uncomment for codecID 00 01
94+
// secpTransferOutput.setCodecID(codecID)
95+
const transferableOutput: TransferableOutput = new TransferableOutput(assetID, secpTransferOutput)
96+
outputs.push(transferableOutput)
97+
98+
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
99+
// Uncomment for codecID 00 01
100+
// secpTransferInput.setCodecID(codecID)
101+
secpTransferInput.addSignatureIdx(0, xAddresses[0])
102+
const input: TransferableInput = new TransferableInput(txid, outputidx, assetID, secpTransferInput)
103+
inputs.push(input)
104+
}
105+
} else if(utxo.getOutput().getTypeID() != 7 && utxo.getOutput().getTypeID() != 11) {
106+
const outputOwners: OutputOwners = new OutputOwners(xAddresses, locktime, threshold)
107+
const nftMintOutputUTXOIDs: string[] = getUTXOIDs(utxoSet, bintools.cb58Encode(txid), AVMConstants.NFTMINTOUTPUTID, bintools.cb58Encode(assetID))
108+
const mintOwner: NFTMintOutput = utxo.getOutput() as NFTMintOutput
109+
// Uncomment for codecID 00 01
110+
// mintOwner.setCodecID(codecID)
111+
const nftMintOperation: NFTMintOperation = new NFTMintOperation(groupID, payload, [outputOwners])
112+
// Uncomment for codecID 00 01
113+
// nftMintOperation.setCodecID(codecID)
114+
const spenders: Buffer[] = mintOwner.getSpenders(xAddresses)
115+
const nftMintOutputUTXOID: string = utxo.getUTXOID()
116+
if(nftMintOutputUTXOID === nftMintOutputUTXOIDs[0]) {
117+
spenders.forEach((spender: Buffer) => {
118+
const idx: number = mintOwner.getAddressIdx(spender)
119+
nftMintOperation.addSignatureIdx(idx, spender)
120+
})
121+
122+
const transferableOperation: TransferableOperation = new TransferableOperation(utxo.getAssetID(), [nftMintOutputUTXOID], nftMintOperation)
123+
operations.push(transferableOperation)
124+
}
125+
}
126+
})
127+
const operationTx: OperationTx = new OperationTx (
128+
networkID,
129+
bintools.cb58Decode(blockchainid),
130+
outputs,
131+
inputs,
132+
memo,
133+
operations
134+
)
135+
// Uncomment for codecID 00 01
136+
// operationTx.setCodecID(codecID)
137+
138+
const unsignedTx: UnsignedTx = new UnsignedTx(operationTx)
139+
const tx: Tx = unsignedTx.sign(xKeychain)
140+
const id: string = await xchain.issueTx(tx)
141+
console.log(id)
142+
}
143+
144+
main()
145+

0 commit comments

Comments
 (0)