Skip to content

Commit

Permalink
Merge pull request #125 from walt-id/feat/algorand_chain
Browse files Browse the repository at this point in the history
feat/ added mainnet to asset creation
  • Loading branch information
SuperBatata authored Jul 23, 2023
2 parents ee67f3e + 0d622c9 commit 1a88585
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/id/walt/nftkit/Values.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ object Values {

const val ALGORAND_MAINNET_EXPLORER = "https://algoexplorer.io/"
const val ALGORAND_TESTNET_EXPLORER = "https://testnet.algoexplorer.io/"
const val ALGORAND_BETANET_EXPLORER = "https://betanet.algoexplorer.io/"

}
10 changes: 8 additions & 2 deletions src/main/kotlin/id/walt/nftkit/rest/AlgorandNftController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ object AlgorandNftController{

fun accountCreationDocs() = document().operation {
it.summary("Create Algorand Account").operationId("CreateAlgorandAccount").addTagsItem("Algorand Blockchain: Non-fungible tokens(NFTs)")

}.json<AlgorandAccount>(200.toString()) {
it.description("Algorand Account")
}


fun assetCreation(ctx : Context){
val result = AlgorandNftService.createAssetArc3(ctx.pathParam("assetName"), ctx.pathParam("assetUnitName"), ctx.pathParam("url"))
val chain =ctx.pathParam("chain")
val result = AlgorandNftService.createAssetArc3(Common.getAlgorandChain(chain.uppercase()),ctx.pathParam("assetName"), ctx.pathParam("assetUnitName"), ctx.pathParam("url"))
ctx.json(result)
}

fun assetCreationDocs() = document().operation {
it.summary("Create Algorand Asset").operationId("CreateAlgorandAsset").addTagsItem("Algorand Blockchain: Non-fungible tokens(NFTs)")
}.json<AlgodResponse>(200.toString()) {

}
.pathParam<String>("chain") {
it.schema<AlgorandChain>{}}
.json<AlgodResponse>(200.toString()) {
it.description("Algorand Asset")
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/id/walt/nftkit/rest/NftKitApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ object NftKitApi {
post("account/create/",
documented(AlgorandNftController.accountCreationDocs(), AlgorandNftController::accountCreation)
)
post("asset/create/{assetName}/{assetUnitName}/{url}/",
post("chain/{chain}/asset/create/{assetName}/{assetUnitName}/{url}/",
documented(AlgorandNftController.assetCreationDocs(), AlgorandNftController::assetCreation)
)

Expand Down
29 changes: 22 additions & 7 deletions src/main/kotlin/id/walt/nftkit/services/AlgorandNftService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import com.algorand.algosdk.transaction.Transaction
import com.algorand.algosdk.util.Encoder
import com.algorand.algosdk.v2.client.common.AlgodClient
import com.algorand.algosdk.v2.client.common.IndexerClient
import id.walt.nftkit.Values.ALGORAND_BETANET_EXPLORER


import id.walt.nftkit.Values.ALGORAND_TESTNET_EXPLORER
import id.walt.nftkit.Values.ALGORAND_MAINNET_EXPLORER

import id.walt.nftkit.metadata.IPFSMetadata
import id.walt.nftkit.services.WaltIdServices.loadAlgorand
import io.ktor.client.*
Expand Down Expand Up @@ -120,26 +124,32 @@ object AlgorandNftService {


val ALGOD_API_ADDR = "https://testnet-algorand.api.purestake.io/ps2"
val IDX_API_ADDR = "https://testnet-algorand.api.purestake.io/idx2"


val ALGOD_PORT = 443
val ALGOD_API_TOKEN_KEY = "X-API-Key"
val ALGOD_API_TOKEN = "B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab"
val client1 = AlgodClient(ALGOD_API_ADDR, ALGOD_PORT, ALGOD_API_TOKEN, ALGOD_API_TOKEN_KEY)
val idxClient = IndexerClient(IDX_API_ADDR, ALGOD_PORT, ALGOD_API_TOKEN, ALGOD_API_TOKEN_KEY)

fun createAccount(): AlgorandAccount {
val account = com.algorand.algosdk.account.Account()
return AlgorandAccount(account.address.toString(), account.toMnemonic())
}


fun createAssetArc3(assetName : String , assetUnitName : String , url : String) : AlgodResponse{
fun createAssetArc3(chain : AlgorandChain ,assetName : String , assetUnitName : String , url : String) : AlgodResponse{

val client_algod = when(chain) {
AlgorandChain.ALGORAND_MAINNET -> AlgodClient("https://mainnet-algorand.api.purestake.io/ps2", ALGOD_PORT, ALGOD_API_TOKEN, ALGOD_API_TOKEN_KEY)
AlgorandChain.ALGORAND_TESTNET -> AlgodClient("https://testnet-algorand.api.purestake.io/ps2", ALGOD_PORT, ALGOD_API_TOKEN, ALGOD_API_TOKEN_KEY)
AlgorandChain.ALGORAND_BETANET -> AlgodClient("https://betanet-algorand.api.purestake.io/ps2", ALGOD_PORT, ALGOD_API_TOKEN, ALGOD_API_TOKEN_KEY)
}

val SRC_ACCOUNT = loadAlgorand().algorandConfig.algorand_seed_Mnemonic
val src = Account(SRC_ACCOUNT)
println(src.getAddress())

val params = client1.TransactionParams().execute().body()
val params = client_algod.TransactionParams().execute().body()

val tx = Transaction.AssetCreateTransactionBuilder()
.suggestedParams(params)
Expand All @@ -163,9 +173,14 @@ object AlgorandNftService {
val txValues = arrayOf("application/x-binary")
val encodedTxBytes = Encoder.encodeToMsgPack(signedTx)

val txResponse = client1.RawTransaction().rawtxn(encodedTxBytes).execute(txHeaders, txValues).body()
println("Successfully sent tx with ID " + txResponse.txId)
return AlgodResponse(txResponse.txId , "$ALGORAND_TESTNET_EXPLORER"+"tx/${txResponse.txId}")
val txResponse = client_algod.RawTransaction().rawtxn(encodedTxBytes).execute(txHeaders, txValues).body()

return when(chain) {
AlgorandChain.ALGORAND_MAINNET -> AlgodResponse(txResponse.txId, "$ALGORAND_MAINNET_EXPLORER"+"tx/${txResponse.txId}")
AlgorandChain.ALGORAND_TESTNET -> AlgodResponse(txResponse.txId, "$ALGORAND_TESTNET_EXPLORER"+"tx/${txResponse.txId}")
AlgorandChain.ALGORAND_BETANET -> AlgodResponse(txResponse.txId, "$ALGORAND_BETANET_EXPLORER"+"tx/${txResponse.txId}")
}

} catch (e: Exception) {
e.printStackTrace()
}
Expand Down

0 comments on commit 1a88585

Please sign in to comment.