Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new Module for the Liquidity Pool project(s) #56

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ repositories {
mavenCentral()
}

allprojects {
repositories {
jcenter()
flatDir {
dirs '../common/libs'
}
}
}

allprojects {
repositories {
jcenter()
flatDir {
dirs '../common/libs'
}
}
}

dependencies {
implementation project(':trustchain-voter')
implementation project(':trustchain-explorer')
Expand All @@ -102,6 +120,7 @@ dependencies {
implementation project(':peerchat')
implementation project(':eurotoken')
implementation project(':ig-ssi')
implementation project(':liquidity-pool')
api(project(':common')){
exclude group: 'net.java.dev.jna'
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
android:name="nl.tudelft.trustchain.voting.VotingActivity"
android:parentActivityName=".ui.dashboard.DashboardActivity" />

<activity
android:name="nl.tudelft.trustchain.liquidity.LiquidityPoolMainActivity"
android:parentActivityName=".ui.dashboard.DashboardActivity" />

<activity
android:name="com.example.musicdao.MusicService"
android:label="Music app"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/nl/tudelft/trustchain/app/AppDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import nl.tudelft.trustchain.debug.DebugActivity
import nl.tudelft.trustchain.distributedAI.DistributedActivity
import nl.tudelft.trustchain.eurotoken.EuroTokenMainActivity
import nl.tudelft.trustchain.ssi.SSIMainActivity
import nl.tudelft.trustchain.explorer.ui.TrustChainExplorerActivity
import nl.tudelft.trustchain.liquidity.LiquidityPoolMainActivity
import nl.tudelft.trustchain.payloadgenerator.ui.TrustChainPayloadGeneratorActivity
import nl.tudelft.trustchain.peerchat.PeerChatActivity
import nl.tudelft.trustchain.trader.ui.TrustChainTraderActivity
Expand Down Expand Up @@ -96,5 +98,11 @@ enum class AppDefinition(
"EuroToken",
R.color.metallic_gold,
EuroTokenMainActivity::class.java
),
LIQUIDITY(
R.drawable.ic_pool,
"Liquidity Pool",
R.color.blue,
LiquidityPoolMainActivity::class.java
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import nl.tudelft.ipv8.util.toHex
import nl.tudelft.trustchain.app.service.TrustChainService
import nl.tudelft.trustchain.common.DemoCommunity
import nl.tudelft.trustchain.common.MarketCommunity
import nl.tudelft.trustchain.common.bitcoin.WalletService
import nl.tudelft.trustchain.common.eurotoken.GatewayStore
import nl.tudelft.trustchain.common.eurotoken.TransactionRepository
import nl.tudelft.trustchain.currencyii.CoinCommunity
Expand Down Expand Up @@ -109,18 +110,17 @@ class TrustChainApplication : Application() {
val euroTokenCommunity = ipv8.getOverlay<EuroTokenCommunity>()!!
euroTokenCommunity.setTransactionRepository(tr)

trustchain.registerTransactionValidator(
BLOCK_TYPE,
object : TransactionValidator {
override fun validate(
block: TrustChainBlock,
database: TrustChainStore
): ValidationResult {
return if (block.transaction["message"] != null || block.isAgreement) {
ValidationResult.Valid
} else {
ValidationResult.Invalid(listOf("Proposal must have a message"))
}
WalletService.createGlobalWallet(this.cacheDir ?: throw Error("CacheDir not found"))

trustchain.registerTransactionValidator(BLOCK_TYPE, object : TransactionValidator {
override fun validate(
block: TrustChainBlock,
database: TrustChainStore
): ValidationResult {
if (block.transaction["message"] != null || block.isAgreement) {
return ValidationResult.Valid
} else {
return ValidationResult.Invalid(listOf("Proposal must have a message"))
}
}
)
Expand Down Expand Up @@ -206,7 +206,8 @@ class TrustChainApplication : Application() {
}

private fun createTrustChainCommunity(): OverlayConfiguration<TrustChainCommunity> {
val settings = TrustChainSettings()
val blockTypesBcDisabled: Set<String> = setOf("eurotoken_join", "eurotoken_trade")
val settings = TrustChainSettings(blockTypesBcDisabled)
val driver = AndroidSqliteDriver(Database.Schema, this, "trustchain.db")
val store = TrustChainSQLiteStore(Database(driver))
val randomWalk = RandomWalk.Factory()
Expand Down
12 changes: 12 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ android {

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'

buildConfigField "String", "DEFAULT_GATEWAY_IP", "\"gateway.euro-token.nl\""
buildConfigField "Integer", "DEFAULT_GATEWAY_PORT", "8090"
buildConfigField "String", "DEFAULT_GATEWAY_PK", "\"4c69624e61434c504b3a035fd325276e03b9d0d106a91353cdd00f7a21aa861be79226224809cfedf80cbcc0e210c2ddc2f91a1fbc3e1e3cd0622e32027a27a8be7f5d28a73b42c0369f\""
buildConfigField "String", "DEFAULT_GATEWAY_NAME", "\"Demo Gateway\""

buildConfigField "String", "BITCOIN_FAUCET", "\"http://134.122.59.107:3000\""
buildConfigField "String", "BITCOIN_DEFAULT_PEER", "\"134.122.59.107\""
}

buildTypes {
Expand Down Expand Up @@ -100,6 +108,10 @@ dependencies {

implementation 'com.github.MattSkala:recyclerview-itemadapter:0.4'

implementation group: 'org.bitcoinj', name: 'bitcoinj-core', version: '0.15.10'

implementation group: 'org.bitcoinj', name: 'bitcoinj-core', version: '0.15.10'

// BitTorrent
api files('libs/jlibtorrent-1.2.10.0.jar')
api files('libs/jlibtorrent-android-arm64-1.2.10.0.jar')
Expand Down
Binary file added common/libs/geth-android-all-1.10.1-c2d2f4ed.aar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package nl.tudelft.trustchain.common.bitcoin

import android.util.Log
import com.google.common.util.concurrent.Service
import nl.tudelft.ipv8.keyvault.PublicKey
import nl.tudelft.trustchain.common.BuildConfig
import nl.tudelft.trustchain.common.eurotoken.TransactionRepository
import org.bitcoinj.core.ECKey
import org.bitcoinj.core.PeerAddress
import org.bitcoinj.kits.WalletAppKit
import org.bitcoinj.params.RegTestParams
import java.io.File
import java.net.InetAddress
import java.net.URL

class WalletService {

companion object {
private lateinit var globalWallet: WalletAppKit
private val walletStore: MutableMap<String, WalletAppKit> = mutableMapOf()
val params: RegTestParams = RegTestParams.get()
private lateinit var lastDir: File

/**
* Creates a global bitcoin wallet
*/
fun createGlobalWallet(dir: File) {
lastDir = dir
globalWallet = createWallet(dir, "global")
}

/**
* Returns the global bitcoin wallet, [createGlobalWallet] needs to be called at least once first for the correct cache location
*/
fun getGlobalWallet(): WalletAppKit {
globalWallet = createWallet(lastDir, "global")
return globalWallet
}

/**
* Creates a personal wallet and saves it continuously in the given file. If an app-kit has already
* started, this function looks up the running app-kit.
*/
fun createPersonalWallet(dir: File): WalletAppKit =
createWallet(dir, "personal")

/**
* Creates a wallet with the given name and saves it continuously in the given file. If an app-kit
* has already started, this function looks up the running app-kit and waits for it to be surely
* running.
*/
fun createWallet(dir: File, name: String): WalletAppKit {
// If a wallet app-kit was already stored and not terminated, retrieve it.
if (walletStore.containsKey(name) &&
!setOf(
Service.State.TERMINATED,
Service.State.STOPPING,
Service.State.FAILED
).contains(walletStore[name]?.state())
) {
walletStore[name]!!.awaitRunning()

return walletStore[name]!!
}

// Create an app-kit with testing bitcoins if empty.
val app = object : WalletAppKit(params, dir, name) {
override fun onSetupCompleted() {
if (wallet().keyChainGroupSize < 1) {
wallet().importKey(ECKey())
}

if (wallet().balance.isZero) {
val address = wallet().issuedReceiveAddresses.first().toString()
URL("${BuildConfig.BITCOIN_FAUCET}?id=$address").readBytes()
}
}
}

app.setPeerNodes(
PeerAddress(
params,
InetAddress.getByName(BuildConfig.BITCOIN_DEFAULT_PEER),
params.port
)
)

app.setAutoSave(true)
app.setBlockingStartup(false)

app.startAsync()
app.awaitRunning()

// Store the app-kit in the running wallet store
walletStore[name] = app

return app
}

/**
* Initializes the bitcoin side of the liquidity pool
*/
fun initializePool(transactionRepository: TransactionRepository, publicKey: PublicKey) {

// TODO: Look into different listeners, this event is called before the transfer is verified, not sure if this will be an issue
globalWallet.wallet().addCoinsReceivedEventListener { wallet, tx, _, _ ->
val transaction = mapOf(
"bitcoin_tx" to tx!!.txId.toString(),
"amount" to tx.getValueSentToMe(wallet).toFriendlyString()
)
Log.d("bitcoin_received", "Bitcoins received making a note on my chain")
transactionRepository.trustChainCommunity.createProposalBlock(
"bitcoin_transfer",
transaction,
publicKey.keyToBin()
)
}
}
}
}
Loading