Skip to content

Commit

Permalink
Merge pull request #101 from OmniLayer/msgilligan-omni-types
Browse files Browse the repository at this point in the history
Begin conversion from BigInteger to OmniValue and Coin
  • Loading branch information
msgilligan committed Sep 14, 2015
2 parents f1c3946 + 02dbea0 commit 1ba919d
Show file tree
Hide file tree
Showing 28 changed files with 431 additions and 188 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
}

ext.groovyVersion = '2.4.4'
ext.bitcoinjAddonsVersion = '0.0.7'

apply from: 'gradle/groovydoc.gradle'
apply from: 'gradle/asciidoctor.gradle'
Expand Down
2 changes: 1 addition & 1 deletion omnij-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ configurations {
dependencies {
compile project(':omnij-rpc')

compile 'com.msgilligan:bitcoinj-cli:0.0.4'
compile "com.msgilligan:bitcoinj-cli:${bitcoinjAddonsVersion}"

robovm "org.codehaus.groovy:groovy:${groovyVersion}:grooid"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ public BigDecimal bigDecimalValue() {
BigDecimal willets = new BigDecimal(value);
return willets.divide(bdWilletsPerCoin, DEFAULT_SCALE, RoundingMode.UNNECESSARY);
}

public OmniDivisibleValue plus(OmniDivisibleValue right) {
return OmniDivisibleValue.ofWillets(this.value + right.value);
}

public OmniDivisibleValue minus(OmniDivisibleValue right) {
return OmniDivisibleValue.ofWillets(this.value - right.value);
}

}
13 changes: 13 additions & 0 deletions omnij-core/src/main/java/foundation/omni/OmniIndivisibleValue.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foundation.omni;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
Expand Down Expand Up @@ -34,9 +35,21 @@ private OmniIndivisibleValue(long value) {
super(value);
}

public BigDecimal bigDecimalValue() {
return new BigDecimal(value);
}

@Override
public PropertyType getPropertyType() {
return PropertyType.INDIVISIBLE;
}

public OmniIndivisibleValue plus(OmniIndivisibleValue right) {
return OmniIndivisibleValue.of(this.value + right.value);
}

public OmniIndivisibleValue minus(OmniIndivisibleValue right) {
return OmniIndivisibleValue.of(this.value - right.value);
}

}
6 changes: 5 additions & 1 deletion omnij-core/src/main/java/foundation/omni/OmniValue.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package foundation.omni;

import org.bitcoinj.core.Coin;

import javax.money.NumberValue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.PropertyPermission;

/**
Expand Down Expand Up @@ -60,7 +63,7 @@ public static OmniValue of(long amount, PropertyType type) {
OmniDivisibleValue.of(amount) : OmniIndivisibleValue.of(amount);
}

public long asWillets() {
public long getWillets() {
return value;
}

Expand Down Expand Up @@ -227,4 +230,5 @@ public String toString() {
return Long.toString(value);
}

public abstract BigDecimal bigDecimalValue();
}
21 changes: 21 additions & 0 deletions omnij-core/src/main/java/foundation/omni/test/MoneyMan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package foundation.omni.test;

import foundation.omni.OmniDivisibleValue;
import org.bitcoinj.core.Coin;

/**
* Definitions for MoneyMan address functionality on TestNet and RegTest
*/
public class MoneyMan {
public static final long willetsPerSatoshi = 100; // Exchange rate for MoneyMan: 100 Omni per BTC

/**
* Calculate Omni returned in a MoneyMan exchange.
*
* @param bitcoin An amount of bitcoin
* @return the amount of Omni the MoneyMan will exchange it for
*/
public static OmniDivisibleValue toOmni(Coin bitcoin) {
return OmniDivisibleValue.ofWillets(bitcoin.value * willetsPerSatoshi);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ class OmniDivisibleValueSpec extends Specification {
ArithmeticException e = thrown()
}
def "Equality works"() {
expect:
OmniDivisibleValue.of(1) == OmniDivisibleValue.of(1)
// OmniDivisibleValue.of(0.1) == OmniDivisibleValue.of(0.1) // Broken!!! is this a groovy bug?
}
}
13 changes: 13 additions & 0 deletions omnij-dsl/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apply plugin: 'java'
apply plugin: 'groovy'

dependencies {
compile "org.codehaus.groovy:groovy:2.4.4"
compile project(':omnij-core')

testCompile("org.spockframework:spock-core:1.0-groovy-2.4") {
exclude module: "groovy-all"
}
}

sourceCompatibility = 1.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package foundation.omni.dsl.categories

import foundation.omni.OmniValue
import groovy.transform.CompileStatic

import foundation.omni.OmniDivisibleValue
import foundation.omni.OmniIndivisibleValue


/**
* Convenience Category for converting Numbers to the Coin class
*
*/
@CompileStatic
@Category(Number)
class NumberCategory {
private static final BigDecimal willetsPerBTCDecimal = new BigDecimal(OmniDivisibleValue.willetsPerCoin)
private static final BigInteger willetsPerBTCBigInt = BigInteger.valueOf(OmniDivisibleValue.willetsPerCoin)

/**
* Treat number as a decimal, divisible amount of an Omni currency
*
* @return a OmniDivisibleValue object
*/
public OmniDivisibleValue getDivisible() {
return OmniDivisibleValue.of(asDivisible(this))
}

/**
* Treat number as an integer, indivisible amount of an Omni currency
*
* @return a OmniIndivisibleValue object
*/
public OmniIndivisibleValue getIndivisible() {
return OmniIndivisibleValue.of(asIndivisible(this))
}

private static BigDecimal asDivisible(Number self) {
switch(self) {
case BigDecimal: return self
case BigInteger: return new BigDecimal((BigInteger)self)
default: return new BigDecimal(self.longValue())
}
}

private static long asIndivisible(Number self) {
switch(self) {
case BigDecimal: return ((BigDecimal) self).longValueExact()
case BigInteger: return ((BigInteger) self).longValue()
default: return self.longValue()
}
}

public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof OmniValue) {
return ((OmniValue)this).getWillets() == ((OmniValue)obj).getWillets();
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
moduleName=omnij groovy extensions
moduleVersion=0.0.1-dev
extensionClasses=foundation.omni.dsl.categories.NumberCategory
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package foundation.omni.dsl.categories

import foundation.omni.OmniDivisibleValue
import foundation.omni.OmniIndivisibleValue
import spock.lang.Specification
import spock.util.mop.Use


/**
* NumberCategory Spec
*/
@Use(NumberCategory)
class NumberCategorySpec extends Specification {

def "basic test of .divisible convenience method"() {
expect:
1.divisible == OmniDivisibleValue.of(1)
// 0.1G.divisible == OmniDivisibleValue.of(0.1) // Broken!!! is this a groovy bug?
}

def "basic test of .indivisible convenience method"() {
expect:
1.indivisible == OmniIndivisibleValue.of(1)
}

}
4 changes: 3 additions & 1 deletion omnij-rpc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ plugins {

dependencies {
compile project(':omnij-core')
compile project(':omnij-dsl')

compile 'com.msgilligan:bitcoinj-rpcclient:0.0.4'
compile "com.msgilligan:bitcoinj-rpcclient:${bitcoinjAddonsVersion}"
compile "com.msgilligan:bitcoinj-groovy:${bitcoinjAddonsVersion}"

compile "org.codehaus.groovy:groovy:${groovyVersion}"
compile "org.codehaus.groovy:groovy-json:${groovyVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ abstract class BaseRegTestSpec extends Specification implements OmniClientDelega
// Set and confirm default fees, so a known reference value can be used in tests
assert client.setTxFee(stdTxFee)
def basicinfo = client.getinfo()
assert basicinfo['paytxfee'] == stdTxFee
assert basicinfo['relayfee'] == stdRelayTxFee
assert basicinfo.paytxfee == stdTxFee.decimalBtc
assert basicinfo.relayfee == stdRelayTxFee.decimalBtc
}

void cleanupSpec() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package foundation.omni.test.rpc.basic

import foundation.omni.OmniDivisibleValue
import foundation.omni.dsl.categories.NumberCategory
import org.bitcoinj.core.Address
import foundation.omni.BaseRegTestSpec
import org.bitcoinj.core.Coin
import spock.lang.Shared

/**
* Rudimentary tests of the raw transaction interface of Omni Core.
*/
class SendRawTransactionSpec extends BaseRegTestSpec {

final static BigDecimal startBTC = 1.0
final static BigDecimal startMSC = 50.0
final static Coin startBTC = 1.btc
final static OmniDivisibleValue startMSC = 50.divisible

@Shared
Address activeAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SimpleSendSpec extends BaseRegTestSpec {
@Unroll
def "Can simple send #amount MSC from one address to another"() {
setup:
def faucetAddress = createFundedAddress(faucetBTC, fundingMSC)
def faucetAddress = createFundedAddress(faucetBTC.btc, fundingMSC.divisible)

when: "we send MSC"
def startBalance = omniGetBalance(faucetAddress, MSC).balance
Expand Down
Loading

0 comments on commit 1ba919d

Please sign in to comment.