Skip to content

Commit

Permalink
Merge pull request #13 from dymbe/AI_profit
Browse files Browse the repository at this point in the history
Ai profit
  • Loading branch information
MattSkala authored Apr 19, 2020
2 parents 0193e1e + 86d0ce8 commit f184f21
Show file tree
Hide file tree
Showing 29 changed files with 99,824 additions and 200,120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class AppDefinition(
),
TRUSTCHAIN_TRADER(
R.drawable.ic_device_hub_black_24dp,
"Trader",
"AI trading bot",
R.color.blue,
TrustChainTraderActivity::class.java
),
Expand Down
1 change: 0 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,4 @@ dependencies {
testImplementation 'org.json:json:20190722'
testImplementation "com.squareup.sqldelight:sqlite-driver:$sqldelight_version"
testImplementation "com.goterl.lazycode:lazysodium-java:4.2.4"

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class MarketCommunity : Community() {
override val serviceId = "98c1f6342f30528ada9647197f0503d48db9c2fc"

val discoveredAddressesContacted: MutableMap<IPv4Address, Date> = mutableMapOf()
val listenersMap: MutableMap<TradePayload.Type?, MutableList<(TradePayload) -> Unit>> = mutableMapOf()
private val lastTrackerResponses = mutableMapOf<IPv4Address, Date>()
private val listenersMap: MutableMap<TradePayload.Type?, MutableList<(TradePayload) -> Unit>> = mutableMapOf()

init {
messageHandlers[MessageId.TRADE.index] = ::onTradePacket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ class TradePayload(
serializeVarLen(type.toString().toByteArray(Charsets.UTF_8))
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as TradePayload

if (!publicKey.contentEquals(other.publicKey)) return false
if (primaryCurrency != other.primaryCurrency) return false
if (secondaryCurrency != other.secondaryCurrency) return false
if (amount != other.amount) return false
if (price != other.price) return false
if (type != other.type) return false

return true
}

companion object Deserializer : Deserializable<TradePayload> {
override fun deserialize(buffer: ByteArray, offset: Int): Pair<TradePayload, Int> {
var localOffset = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package nl.tudelft.trustchain.common

import io.mockk.*
import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.messaging.EndpointAggregator
import nl.tudelft.ipv8.messaging.Serializable
import nl.tudelft.trustchain.common.messaging.TradePayload
import org.junit.Assert.assertEquals
import org.junit.Test

class MarketCommunityTest {
private val peersSize = 5
private var marketCommunity = spyk(MarketCommunity(), recordPrivateCalls = true)
private val myPeer = mockk<Peer>()
private val endpoint = mockk<EndpointAggregator>()

@Test
fun broadcast_callsSendOneTimePerPeer() {
val payload = mockk<TradePayload>()
every {
marketCommunity["serializePacket"](
any<Int>(),
any<Serializable>(),
any<Boolean>(),
any<Peer>(),
any<ByteArray>()
)
} returns byteArrayOf(0x00)
every { marketCommunity.getPeers() } returns getFakePeers()
every { marketCommunity.myPeer } returns myPeer
every { marketCommunity.endpoint } returns endpoint
every { endpoint.send(any<Peer>(), any()) } just runs

marketCommunity.broadcast(payload)

verify(exactly = peersSize) { endpoint.send(any<Peer>(), any()) }
}

@Test
fun addListener_addsListener() {
val beforeSize = marketCommunity.listenersMap[null]?.size ?: 0
marketCommunity.addListener(null, mockk())
val afterSize = marketCommunity.listenersMap[null]?.size ?: 0
assertEquals(beforeSize, afterSize - 1)
}

private fun getFakePeers(): List<Peer> {
val peers = mutableListOf<Peer>()
for (i in 1..peersSize) {
peers.add(mockk())
}
return peers
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nl.tudelft.trustchain.common.messaging

import nl.tudelft.trustchain.common.constants.Currency
import org.junit.Test
import org.junit.Assert.assertEquals

class TradePayloadTest {
@Test
fun serializeOutputDeserializesToOriginal() {
val payload = TradePayload(
byteArrayOf(0x00),
Currency.DYMBE_DOLLAR,
Currency.BTC,
100.0,
100.0,
TradePayload.Type.ASK
)
val serializedPayload = payload.serialize()
val deserializedPayload = TradePayload.deserialize(serializedPayload).first
assertEquals(payload, deserializedPayload)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.tudelft.trustchain.trader
package nl.tudelft.trustchain.payloadgenerator

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ class PayloadCreateFragment : BaseFragment() {

askBidSwitch.setOnClickListener {
if (isAsk) {
txtExplanationAsk.text = "I have BTC, I want Dymbe $"
txtExplanationAsk.text = "ASK: I have Dymbe $, I want BTC"
editTextAmount.hint = "Enter Amount of Dymbe $ Available"
editTextPrice.hint = "Enter Price in Bitcoin"
} else {
txtExplanationAsk.text = "I have Dymbe $, I want BTC"
txtExplanationAsk.text = "BID: I have BTC, I want Dymbe $"
editTextAmount.hint = "Enter Amount of Bitcoin Available"
editTextPrice.hint = "Enter Price in Dymbe $"
}
isAsk = !isAsk
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mattskala.itemadapter.ItemAdapter
import kotlinx.android.synthetic.main.fragment_payload.*
import kotlinx.coroutines.*
Expand All @@ -30,9 +31,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {

private val adapter = ItemAdapter()
private var isAutoSending = false
private lateinit var publicKey: ByteArray

protected val binding: FragmentPayloadBinding by viewBinding(FragmentPayloadBinding::bind)
private val binding: FragmentPayloadBinding by viewBinding(FragmentPayloadBinding::bind)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -58,10 +58,13 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
)

switchAutoMessage.setOnClickListener {
Log.d("SwitchAutoMessage", "button switched")
if (!isAutoSending) {
isAutoSending = true
sendAutoMessages()
} else {
isAutoSending = false
}
isAutoSending != isAutoSending
}

val marketCommunity = getMarketCommunity()
Expand All @@ -81,7 +84,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
val payloadSerializable =
createPayloadSerializable(amount, price, type)
val payload = createPayload(amount, price, type)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(payload)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
marketCommunity.broadcast(payloadSerializable)
}

Expand All @@ -93,7 +97,6 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
private fun sendAutoMessages() {
thread(start = true) {
while (isAutoSending) {
Thread.sleep(3000)
val marketCommunity = getMarketCommunity()
val r = java.util.Random()
val typeInt = Random.nextInt(0, 2)
Expand All @@ -109,8 +112,12 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
price = String.format("%.2f", price).toDouble()
val payloadSerializable =
createPayloadSerializable(amount, price, type)
val payload = createPayload(amount, price, type)
marketCommunity.broadcast(payloadSerializable)
Log.d("TrustChainPayloadGeneratorActivity::sendAutoMessages", "message send!")
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
Thread.sleep(3000)
}
}
}
Expand All @@ -120,12 +127,12 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
price: Double,
type: String?
): TradePayload {
var primaryCurrency = Currency.BTC
var secondaryCurrency = Currency.DYMBE_DOLLAR
var primaryCurrency = Currency.DYMBE_DOLLAR
var secondaryCurrency = Currency.BTC
var type2 = TradePayload.Type.BID
if (type.equals("Ask")) {
primaryCurrency = Currency.DYMBE_DOLLAR
secondaryCurrency = Currency.BTC
primaryCurrency = Currency.BTC
secondaryCurrency = Currency.DYMBE_DOLLAR
type2 = TradePayload.Type.ASK
}
return TradePayload(
Expand Down Expand Up @@ -175,7 +182,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
"New ask came in! They are selling ${payload.amount} ${payload.primaryCurrency}. The price is ${payload.price} ${payload.secondaryCurrency} per ${payload.primaryCurrency}"
)
if (!(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.contains(payload)) {
TrustChainPayloadGeneratorActivity.payloads.add(payload)
TrustChainPayloadGeneratorActivity.payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
}
}
private fun bidListener(payload: TradePayload) {
Expand All @@ -184,7 +192,8 @@ class PayloadFragment : BaseFragment(R.layout.fragment_payload) {
"New bid came in! They are asking ${payload.amount} ${payload.primaryCurrency}. The price is ${payload.price} ${payload.secondaryCurrency} per ${payload.primaryCurrency}"
)
if (!(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.contains(payload)) {
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(payload)
(TrustChainPayloadGeneratorActivity.PayloadsList).payloads.add(0, payload)
recyclerViewPayload.layoutManager!!.smoothScrollToPosition(recyclerViewPayload, RecyclerView.State(), 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@ data class PayloadItem(
val amount: Double?,
val price: Double?,
val type: TradePayload.Type
) : Item()
) : Item() {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as PayloadItem

if (!publicKey.contentEquals(other.publicKey)) return false
if (primaryCurrency != other.primaryCurrency) return false
if (secondaryCurrency != other.secondaryCurrency) return false
if (amount != other.amount) return false
if (price != other.price) return false
if (type != other.type) return false

return true
}

override fun hashCode(): Int {
var result = publicKey.contentHashCode()
result = 31 * result + primaryCurrency.hashCode()
result = 31 * result + secondaryCurrency.hashCode()
result = 31 * result + (amount?.hashCode() ?: 0)
result = 31 * result + (price?.hashCode() ?: 0)
result = 31 * result + type.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="parent"
tools:layout_editor_absoluteY="1dp" />


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="5dp"
Expand Down Expand Up @@ -36,7 +35,7 @@
android:id="@+id/txtExplanationAsk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I have BTC, I want Dymbe $"
android:text="ASK: I have Dymbe $, I want BTC"
android:paddingHorizontal="3dp" />

</LinearLayout>
Expand All @@ -63,7 +62,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Enter Amount Available" />
android:hint="Enter Amount of Dymbe $ Available" />

<TextView
android:id="@+id/txtPrice"
Expand All @@ -79,7 +78,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Price" />
android:hint="Enter Price in Bitcoin" />

<Button
android:id="@+id/btnCreatePayload"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand All @@ -9,28 +8,36 @@
android:id="@+id/buttonPayload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"
android:text="New Payload"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<Switch
android:id="@+id/switchAutoMessage"
android:layout_width="161dp"
android:layout_height="59dp"
android:layout_marginEnd="200dp"
android:layout_marginTop="16dp"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:checked="false"
android:text="Generate Payloads"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<View
android:id="@+id/divider3"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/Wallet" />

<LinearLayout
android:id="@+id/Wallet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginTop="95dp"
android:gravity="top"
android:orientation="horizontal"
android:weightSum="4">
Expand Down
Loading

0 comments on commit f184f21

Please sign in to comment.