Check in a demo of how to get an interest rate fix from an oracle.

This commit is contained in:
Mike Hearn 2016-03-08 17:27:34 +01:00
parent d63a3a8923
commit 0f208d8b4d
3 changed files with 126 additions and 0 deletions

View File

@ -118,4 +118,18 @@ tasks.withType(Test) {
tasks.withType(JavaExec) { tasks.withType(JavaExec) {
jvmArgs "-javaagent:${configurations.quasar.singleFile}" jvmArgs "-javaagent:${configurations.quasar.singleFile}"
jvmArgs "-Dco.paralleluniverse.fibers.verifyInstrumentation" jvmArgs "-Dco.paralleluniverse.fibers.verifyInstrumentation"
}
// Package up the other demo programs.
task getRateFixDemo(type: CreateStartScripts) {
mainClassName = "demos.RateFixDemoKt"
applicationName = "get-rate-fix"
defaultJvmOpts = ["-javaagent:${configurations.quasar.singleFile}"]
outputDir = new File(project.buildDir, 'scripts')
classpath = jar.outputs.files + project.configurations.runtime
}
applicationDistribution.into("bin") {
from(getRateFixDemo)
fileMode = 0755
} }

22
scripts/get-rate-fix.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# This needs the buyer node to be running first.
if [ ! -e ./gradlew ]; then
echo "Run from the root directory please"
exit 1
fi
if [ ! -d build/install/r3prototyping ]; then
./gradlew installDist
fi
if [ ! -e buyer/identity-public ]; then
echo "You must run scripts/trade-demo.sh buyer before running this script (and keep it running)"
exit 1
fi
# Upload the rates to the buyer node
curl -F rates=@scripts/example.rates.txt http://localhost:31338/upload/interest-rates
build/install/r3prototyping/bin/get-rate-fix --network-address=localhost:31300 --oracle=localhost --oracle-identity-file=buyer/identity-public

View File

@ -0,0 +1,90 @@
/*
* Copyright 2015 Distributed Ledger Group LLC. Distributed as Licensed Company IP to DLG Group Members
* pursuant to the August 7, 2015 Advisory Services Agreement and subject to the Company IP License terms
* set forth therein.
*
* All other rights reserved.
*/
package demos
import contracts.Cash
import core.*
import core.messaging.LegallyIdentifiableNode
import core.node.Node
import core.node.NodeConfiguration
import core.node.services.ArtemisMessagingService
import core.node.services.NodeInterestRates
import core.serialization.deserialize
import core.utilities.ANSIProgressRenderer
import core.utilities.BriefLogFormatter
import core.utilities.Emoji
import joptsimple.OptionParser
import protocols.RatesFixProtocol
import java.math.BigDecimal
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess
/**
* Creates a dummy transaction that requires a rate fix within a certain range, and gets it signed by an oracle
* service.
*/
fun main(args: Array<String>) {
val parser = OptionParser()
val networkAddressArg = parser.accepts("network-address").withRequiredArg().required()
val dirArg = parser.accepts("directory").withRequiredArg().defaultsTo("rate-fix-demo-data")
val oracleAddrArg = parser.accepts("oracle").withRequiredArg().required()
val oracleIdentityArg = parser.accepts("oracle-identity-file").withRequiredArg().required()
val fixOfArg = parser.accepts("fix-of").withRequiredArg().defaultsTo("LIBOR 2016-03-16 30")
val expectedRateArg = parser.accepts("expected-rate").withRequiredArg().defaultsTo("0.67")
val rateToleranceArg = parser.accepts("rate-tolerance").withRequiredArg().defaultsTo("0.1")
val options = try {
parser.parse(*args)
} catch (e: Exception) {
println(e.message)
exitProcess(1)
}
// Suppress the Artemis MQ noise, and activate the demo logging.
BriefLogFormatter.initVerbose("+demo.ratefix", "-org.apache.activemq")
// TODO: Move this into the AbstractNode class.
val dir = Paths.get(options.valueOf(dirArg))
if (!Files.exists(dir)) {
Files.createDirectory(dir)
}
// Load oracle stuff (in lieu of having a network map service)
val oracleAddr = ArtemisMessagingService.makeRecipient(options.valueOf(oracleAddrArg))
val oracleIdentity = Files.readAllBytes(Paths.get(options.valueOf(oracleIdentityArg))).deserialize<Party>(includeClassName = true)
val oracleNode = LegallyIdentifiableNode(oracleAddr, oracleIdentity)
val fixOf: FixOf = NodeInterestRates.parseFixOf(options.valueOf(fixOfArg))
val expectedRate = BigDecimal(options.valueOf(expectedRateArg))
val rateTolerance = BigDecimal(options.valueOf(rateToleranceArg))
// Bring up node.
val myNetAddr = ArtemisMessagingService.toHostAndPort(options.valueOf(networkAddressArg))
val config = object : NodeConfiguration {
override val myLegalName: String = "Rate fix demo node"
}
val node = logElapsedTime("Node startup") { Node(dir, myNetAddr, config, null).start() }
// Make a garbage transaction that includes a rate fix.
val tx = TransactionBuilder()
tx.addOutputState(Cash.State(node.storage.myLegalIdentity.ref(1), 1500.DOLLARS, node.keyManagement.freshKey().public))
val protocol = RatesFixProtocol(tx, oracleNode, fixOf, expectedRate, rateTolerance)
ANSIProgressRenderer.progressTracker = protocol.progressTracker
node.smm.add("demo.ratefix", protocol).get()
// Show the user the output.
println("Got rate fix")
println()
print(Emoji.renderIfSupported(tx.toWireTransaction()))
println(tx.toSignedTransaction().sigs)
node.stop()
}