Skip to content

Commit

Permalink
Merge pull request #93 from dexX7/tx-4-ecosystem
Browse files Browse the repository at this point in the history
Support ecosystem parameter for "send all" transactions
  • Loading branch information
msgilligan committed Aug 10, 2015
2 parents 3b1ffc8 + fac8611 commit 8477618
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import foundation.omni.CurrencyID
import foundation.omni.Ecosystem
import foundation.omni.PropertyType
import org.junit.internal.AssumptionViolatedException
import spock.lang.Unroll

class SendAllSpec extends BaseRegTestSpec {

final static BigDecimal startBTC = 0.1
final static BigDecimal startMSC = 0.1
final static BigDecimal zeroAmount = 0.0

def "All available tokens can be transferred with transaction type 4"() {
@Unroll
def "In #ecosystem all available tokens can be transferred with transaction type 4"() {
when:
def actorAddress = createFundedAddress(startBTC, startMSC)
def otherAddress = newAddress
Expand All @@ -24,7 +26,7 @@ class SendAllSpec extends BaseRegTestSpec {
getbalance_MP(otherAddress, CurrencyID.TMSC).balance == zeroAmount

when:
def sendTxid = sendAll(actorAddress, otherAddress)
def sendTxid = sendAll(actorAddress, otherAddress, ecosystem)
generateBlock()
def sendTx = getTransactionMP(sendTxid)

Expand All @@ -36,24 +38,35 @@ class SendAllSpec extends BaseRegTestSpec {
sendTx.sendingaddress == actorAddress.toString()
sendTx.referenceaddress == otherAddress.toString()
sendTx.type_int == 4
sendTx.ecosystem == ecosystemToString(ecosystem)
sendTx.containsKey('subsends')

and:
List<Map<String, Object>> subSends = sendTx['subsends']
subSends.size() == 2
for (def send : subSends) {
assert send.divisible
assert send.amount as BigDecimal == startMSC
}
subSends.size() == 1
subSends[0].propertyid == ecosystem.longValue()
subSends[0].divisible
subSends[0].amount as BigDecimal == startMSC

and:
getbalance_MP(actorAddress, CurrencyID.MSC).balance == zeroAmount
getbalance_MP(actorAddress, CurrencyID.TMSC).balance == zeroAmount
getbalance_MP(otherAddress, CurrencyID.MSC).balance == startMSC
getbalance_MP(otherAddress, CurrencyID.TMSC).balance == startMSC
if (ecosystem == Ecosystem.MSC) {
assert getbalance_MP(actorAddress, CurrencyID.MSC).balance == zeroAmount
assert getbalance_MP(actorAddress, CurrencyID.TMSC).balance == startMSC
assert getbalance_MP(otherAddress, CurrencyID.MSC).balance == startMSC
assert getbalance_MP(otherAddress, CurrencyID.TMSC).balance == zeroAmount
} else {
assert getbalance_MP(actorAddress, CurrencyID.MSC).balance == startMSC
assert getbalance_MP(actorAddress, CurrencyID.TMSC).balance == zeroAmount
assert getbalance_MP(otherAddress, CurrencyID.MSC).balance == zeroAmount
assert getbalance_MP(otherAddress, CurrencyID.TMSC).balance == startMSC
}

where:
ecosystem << [Ecosystem.MSC, Ecosystem.TMSC]
}

def "\"Send All\" transactions are only valid, if at least one token was transferred"() {
@Unroll
def "In #ecosystem sending all tokens is only valid, if at least one unit was transferred"() {
when:
def actorAddress = createFundedAddress(startBTC, startMSC)
def otherAddress = newAddress
Expand All @@ -68,7 +81,7 @@ class SendAllSpec extends BaseRegTestSpec {
getbalance_MP(otherAddress, CurrencyID.TMSC).balance == startMSC

when:
def sendTxid = sendAll(actorAddress, otherAddress)
def sendTxid = sendAll(actorAddress, otherAddress, ecosystem)
generateBlock()

then:
Expand All @@ -79,20 +92,25 @@ class SendAllSpec extends BaseRegTestSpec {
getbalance_MP(actorAddress, CurrencyID.TMSC).balance == zeroAmount
getbalance_MP(otherAddress, CurrencyID.MSC).balance == startMSC
getbalance_MP(otherAddress, CurrencyID.TMSC).balance == startMSC

where:
ecosystem << [Ecosystem.MSC, Ecosystem.TMSC]
}

def "Only available, unreserved balances are transferred with \"Send All\""() {
@Unroll
def "In #ecosystem only available, unreserved balances are transferred, when sending all tokens"() {
when:
def actorAddress = createFundedAddress(startBTC, zeroAmount)
def otherAddress = createFundedAddress(startBTC, zeroAmount)
def nonManagedID = fundNewProperty(actorAddress, 10.0, PropertyType.DIVISIBLE, Ecosystem.MSC)
def nonManagedID = fundNewProperty(actorAddress, 10.0, PropertyType.DIVISIBLE, ecosystem)
def tradeCurrency = new CurrencyID(ecosystem.longValue())

then:
getbalance_MP(actorAddress, nonManagedID).balance == 10.0
getbalance_MP(otherAddress, nonManagedID).balance == zeroAmount

when:
def tradeTxid = trade_MP(actorAddress, nonManagedID, 4.0, CurrencyID.MSC, 4.0, 1 as Byte)
def tradeTxid = trade_MP(actorAddress, nonManagedID, 4.0, tradeCurrency, 4.0, 1 as Byte)
generateBlock()
def tradeTx = getTransactionMP(tradeTxid)

Expand All @@ -102,7 +120,7 @@ class SendAllSpec extends BaseRegTestSpec {
getbalance_MP(actorAddress, nonManagedID).reserved == 4.0

when:
def sendTxid = sendAll(actorAddress, otherAddress)
def sendTxid = sendAll(actorAddress, otherAddress, ecosystem)
generateBlock()
def sendTx = getTransactionMP(sendTxid)

Expand All @@ -111,6 +129,17 @@ class SendAllSpec extends BaseRegTestSpec {
getbalance_MP(actorAddress, nonManagedID).balance == zeroAmount
getbalance_MP(actorAddress, nonManagedID).reserved == 4.0
getbalance_MP(otherAddress, nonManagedID).balance == 6.0

where:
ecosystem << [Ecosystem.MSC, Ecosystem.TMSC]
}

def ecosystemToString(Ecosystem ecosystem) {
if (ecosystem == Ecosystem.MSC) {
return "main"
} else {
return "test"
}
}

def setupSpec() {
Expand Down
12 changes: 7 additions & 5 deletions omnij-rpc/src/main/java/foundation/omni/rpc/OmniClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package foundation.omni.rpc;

import com.msgilligan.bitcoin.rpc.BitcoinClient;
import com.msgilligan.bitcoin.rpc.JsonRPCException;
import com.msgilligan.bitcoin.rpc.RPCConfig;
import foundation.omni.CurrencyID;
import foundation.omni.Ecosystem;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Sha256Hash;
import com.msgilligan.bitcoin.rpc.BitcoinClient;
import com.msgilligan.bitcoin.rpc.JsonRPCException;
import foundation.omni.CurrencyID;

import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -250,11 +251,12 @@ public Sha256Hash sendToOwnersMP(Address fromAddress, CurrencyID currency, BigDe
*
* @param fromAddress The address to spent from
* @param toAddress The address to send to
* @param ecosystem The ecosystem of the tokens to send
* @return The hash of the transaction
*/
public Sha256Hash sendAll(Address fromAddress, Address toAddress)
public Sha256Hash sendAll(Address fromAddress, Address toAddress, Ecosystem ecosystem)
throws JsonRPCException, IOException {
List<Object> params = createParamList(fromAddress.toString(), toAddress.toString());
List<Object> params = createParamList(fromAddress.toString(), toAddress.toString(), ecosystem.byteValue());
String txid = send("omni_sendall", params);
Sha256Hash hash = Sha256Hash.wrap(txid);
return hash;
Expand Down

0 comments on commit 8477618

Please sign in to comment.