Skip to content

Commit

Permalink
Merge pull request #75 from xoriole/feature/liquidity-pool
Browse files Browse the repository at this point in the history
Add a new Module for the Liquidity Pool project(s)

Closes #56
  • Loading branch information
xoriole authored Jun 3, 2021
2 parents 70c7774 + 3b73668 commit c66fd08
Show file tree
Hide file tree
Showing 75 changed files with 6,230 additions and 31 deletions.
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
7 changes: 7 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,7 @@ 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.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 +97,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 @@ -108,17 +109,19 @@ class TrustChainApplication : Application() {
val euroTokenCommunity = ipv8.getOverlay<EuroTokenCommunity>()!!
euroTokenCommunity.setTransactionRepository(tr)

WalletService.createGlobalWallet(this.cacheDir ?: throw Error("CacheDir not found"))

trustchain.registerTransactionValidator(
BLOCK_TYPE,
object : TransactionValidator {
override fun validate(
block: TrustChainBlock,
database: TrustChainStore
): ValidationResult {
return if (block.transaction["message"] != null || block.isAgreement) {
ValidationResult.Valid
if (block.transaction["message"] != null || block.isAgreement) {
return ValidationResult.Valid
} else {
ValidationResult.Invalid(listOf("Proposal must have a message"))
return ValidationResult.Invalid(listOf("Proposal must have a message"))
}
}
}
Expand Down Expand Up @@ -205,7 +208,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,16 @@ 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_FAUCET", "\"https://taproot.tribler.org\""
// buildConfigField "String", "BITCOIN_DEFAULT_PEER", "\"134.122.59.107\""
buildConfigField "String", "BITCOIN_DEFAULT_PEER", "\"131.180.27.224\""
}

buildTypes {
Expand Down Expand Up @@ -100,6 +110,8 @@ dependencies {

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

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,121 @@
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

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()
println("Address:$address")
// TODO: Fix the faucet
// 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

0 comments on commit c66fd08

Please sign in to comment.