Skip to content

Instantly share code, notes, and snippets.

@satran004
Last active May 28, 2021 15:22
Show Gist options
  • Save satran004/38b7d95b64c164ac185969eb751c44e2 to your computer and use it in GitHub Desktop.
Save satran004/38b7d95b64c164ac185969eb751c44e2 to your computer and use it in GitHub Desktop.
Mint Token
public void mintToken() throws ApiException, AddressExcepion, CborSerializationException {
BackendService backendService =
BackendFactory.getBlockfrostBackendService(Constants.BLOCKFROST_TESTNET_URL, Constant.BF_PROJECT_KEY);
FeeCalculationService feeCalculationService = backendService.getFeeCalculationService();
TransactionHelperService transactionHelperService = backendService.getTransactionHelperService();
BlockService blockService = backendService.getBlockService();
String senderMnemonic = "kit color frog trick speak employ suit sort bomb goddess jewel primary spoil fade person useless measure manage warfare reduce few scrub beyond era";
Account sender = new Account(Networks.testnet(), senderMnemonic);
Keys keys = KeyGenUtil.generateKey();
VerificationKey vkey = keys.getVkey();
SecretKey skey = keys.getSkey();
ScriptPubkey scriptPubkey = ScriptPubkey.create(vkey);
String policyId = scriptPubkey.getPolicyId();
MultiAsset multiAsset = new MultiAsset();
multiAsset.setPolicyId(policyId);
Asset asset = new Asset("TestCoin", BigInteger.valueOf(50000));
multiAsset.getAssets().add(asset);
CBORMetadataMap tokenInfoMap
= new CBORMetadataMap()
.put("token", "Test Token")
.put("symbol", "TTOK");
CBORMetadataList tagList
= new CBORMetadataList()
.add("tag1")
.add("tag2");
Metadata metadata = new CBORMetadata()
.put(new BigInteger("770001"), tokenInfoMap)
.put(new BigInteger("770002"), tagList);
MintTransaction mintTransaction =
MintTransaction.builder()
.sender(sender)
.mintAssets(Arrays.asList(multiAsset))
.policyScript(scriptPubkey)
.policyKeys(Arrays.asList(skey))
.build();
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
BigInteger fee
= feeCalculationService.calculateFee(mintTransaction, detailsParams, metadata);
mintTransaction.setFee(fee);
Result<String> result
= transactionHelperService.mintToken(mintTransaction, detailsParams, metadata);
if(result.isSuccessful())
System.out.println("Transaction Id: " + result.getValue());
else
System.out.println("Transaction failed: " + result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment