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

Support ecosystem parameter for "send all" transactions #93

Merged
merged 1 commit into from
Aug 10, 2015
Merged
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
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