Skip to content

Commit

Permalink
feat: add in vlog date calculations for volume 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltchuang committed Mar 13, 2023
1 parent dd539e6 commit 80a0ba2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 42 deletions.
3 changes: 2 additions & 1 deletion HaveAMagicalDay/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
.externalNativeBuild
.cxx
local.properties
*.jar
*.jar
app/bin/*
1 change: 1 addition & 0 deletions HaveAMagicalDay/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.TimeZone

plugins {
id("org.jetbrains.kotlin.jvm").version("1.8.10")
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
}

dependencies {
Expand Down
87 changes: 54 additions & 33 deletions HaveAMagicalDay/app/src/main/kotlin/com/michaeltchuang/cron/App.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
package com.michaeltchuang.cron

import com.algorand.algosdk.account.Account
import com.algorand.algosdk.builder.transaction.PaymentTransactionBuilder
import com.algorand.algosdk.crypto.Address
import com.algorand.algosdk.transaction.SignedTransaction
import com.algorand.algosdk.util.Encoder
import com.algorand.algosdk.v2.client.Utils
import com.algorand.algosdk.v2.client.common.AlgodClient
import com.algorand.algosdk.v2.client.model.PendingTransactionResponse
import com.algorand.algosdk.v2.client.model.TransactionParametersResponse
import com.michaeltchuang.cron.data.AlgorandRepository
import com.michaeltchuang.cron.utils.Constants

import java.time.LocalDate
import java.time.Period
import java.time.format.DateTimeFormatter
import java.util.TimeZone

private val TAG: String = "AlgorandRepository"
private var client: AlgodClient = AlgodClient(
Constants.ALGOD_API_ADDR,
Constants.ALGOD_PORT,
Constants.ALGOD_API_TOKEN,
Constants.ALGOD_API_TOKEN_KEY
)

val txHeaders = arrayOf("Content-Type")
val txValues = arrayOf("application/x-binary")

fun main() {
// Security.removeProvider("BC")
// Security.insertProviderAt(BouncyCastleProvider(), 0)
val repository = AlgorandRepository()
val account = repository.recoverAccount(Constants.TEST_PASSPHRASE)
println()

if(account == null) {
throw Exception("Could not find account")
} else {
account.address?.apply { repository.sendPayment(account, Constants.COINFLIP_APP_ID_TESTNET, Constants.DEFAULT_MICRO_ALGO_TRANSFER_AMOUNT) }
private var client: AlgodClient = AlgodClient(
Constants.ALGOD_API_ADDR,
Constants.ALGOD_PORT,
Constants.ALGOD_API_TOKEN,
Constants.ALGOD_API_TOKEN_KEY
)

val txHeaders = arrayOf("Content-Type")
val txValues = arrayOf("application/x-binary")

fun main() {
val repository = AlgorandRepository()
val account = repository.recoverAccount(Constants.TEST_PASSPHRASE)

if (account == null) {
throw Exception("Could not find account")
} else {
account.address?.apply { repository.sendPayment(account, Constants.COINFLIP_APP_ID_TESTNET,
Constants.DEFAULT_MICRO_ALGO_TRANSFER_AMOUNT, getGreeting(false)) }

//send twice if Mondays - 8th vlog
val tz = TimeZone.getTimeZone("America/Los_Angeles")
if(LocalDate.now(tz.toZoneId()).dayOfWeek.value == 1) {
account.address?.apply { repository.sendPayment(account, Constants.COINFLIP_APP_ID_TESTNET,
Constants.DEFAULT_MICRO_ALGO_TRANSFER_AMOUNT, getGreeting(true)) }
}
}



}

fun getGreeting(isEighth: Boolean) : String {
var vlogNum = calculateVlogNum(isEighth)

val note = "Volume 2 Vlog ${vlogNum}, have a magical day everyone! - Michael T Chuang"
println(note)
return note
}

fun calculateVlogNum(isEighth: Boolean) : Int {
val tz = TimeZone.getTimeZone("America/Los_Angeles")
val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(tz.toZoneId())
val from = LocalDate.parse("20230220", dateFormatter)
val to = LocalDate.now(tz.toZoneId())
val period = Period.between(from, to)
val weeks = period.days / 8
println("${period.days} days is ${weeks} weeks since Feb 21, 2023")

if(!isEighth) {
return ((weeks * 8) + (period.days % 7))
} else {
return (weeks * 8) + 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import com.algorand.algosdk.builder.transaction.PaymentTransactionBuilder
import com.algorand.algosdk.crypto.Address
import com.algorand.algosdk.transaction.SignedTransaction
import com.algorand.algosdk.util.Encoder

import com.algorand.algosdk.v2.client.Utils
import com.algorand.algosdk.v2.client.common.AlgodClient
import com.algorand.algosdk.v2.client.model.PendingTransactionResponse
import com.algorand.algosdk.v2.client.model.TransactionParametersResponse
import com.michaeltchuang.cron.txHeaders
import com.michaeltchuang.cron.txValues

import com.michaeltchuang.cron.utils.Constants


class AlgorandRepository() {
private val TAG: String = "AlgorandRepository"
private var client: AlgodClient = AlgodClient(
Expand All @@ -25,7 +22,7 @@ class AlgorandRepository() {
Constants.ALGOD_API_TOKEN_KEY
)

fun recoverAccount(passPhrase : String) : Account? {
fun recoverAccount(passPhrase: String): Account? {
try {
return Account(passPhrase)
} catch (e: Exception) {
Expand All @@ -34,7 +31,7 @@ class AlgorandRepository() {
}
}

fun sendPayment(account: Account, appId: Long, amount: Int) : PendingTransactionResponse? {
fun sendPayment(account: Account, appId: Long, amount: Int, note: String): PendingTransactionResponse? {
try {
val respAcct = client.AccountInformation(account.getAddress()).execute()
if (!respAcct.isSuccessful) {
Expand All @@ -48,7 +45,6 @@ class AlgorandRepository() {
}
val sp: TransactionParametersResponse = resp.body()
?: throw java.lang.Exception("Params retrieval error")
val note = "Have a magical day everyone"

// Create a transaction
val ptxn = PaymentTransactionBuilder.Builder()
Expand All @@ -69,9 +65,9 @@ class AlgorandRepository() {

// Wait for transaction confirmation
val pTrx: PendingTransactionResponse = Utils.waitForConfirmation(client, txnId, 10)
println("${account.address} sent ${amount} microAlgos to ${Address.forApplication(appId)} for transaction ${txnId}")
println("${account.address} sent $amount microAlgos to ${Address.forApplication(appId)} for transaction $txnId")
return pTrx
} catch(e: Exception) {
} catch (e: Exception) {
println(e.toString())
return null
}
Expand Down
6 changes: 6 additions & 0 deletions HaveAMagicalDay/release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
{
preset: "angular",
releaseRules: [
{type: 'breaking', release: 'major'},
{type: 'feat', release: 'minor'},
{type: 'ci', release: 'patch'},
{type: 'fix', release: 'patch'},
Expand All @@ -26,6 +27,11 @@ module.exports = {
preset: "conventionalcommits",
presetConfig: {
types: [
{
"type": "breaking",
"section": "Breaking",
"hidden": false
},
{
"type": "feat",
"section": "Features",
Expand Down

0 comments on commit 80a0ba2

Please sign in to comment.