Search in sources :

Example 26 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class WalletTest method createListOfCoinsWithAmount.

private List<Contract> createListOfCoinsWithAmount(List<Integer> values) throws Exception {
    List<Contract> contracts = new ArrayList<>();
    for (Integer value : values) {
        Contract contract = createCoinWithAmount(String.valueOf(value), FIELD_NAME);
        contracts.add(contract);
    }
    return contracts;
}
Also used : ArrayList(java.util.ArrayList) Contract(com.icodici.universa.contract.Contract)

Example 27 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class WalletTest method shouldSplitAndMergeOne.

@Test
public void shouldSplitAndMergeOne() throws Exception {
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(100, 200));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    // want to send 250
    Wallet wallet = wallets.get(0);
    Contract output = wallet.buildContractWithValue("amount", new Decimal(250));
    output.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(output, true);
    Contract contract = wallet.getContracts().get(0);
    sealCheckTrace(contract, true);
    assertEquals("50", contract.getState().getData().get("amount"));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) Test(org.junit.Test)

Example 28 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class WalletTest method shouldTransferAmountFromCoupleContracts.

@Test
public void shouldTransferAmountFromCoupleContracts() throws Exception {
    Decimal valueToSend = new Decimal(70);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(50, 45));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(2, wallets.get(0).getContracts().size());
    // gonna send 70 but I have 2 contracts (50, 45)
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    assertEquals(1, wallet.getContracts().size());
    sealCheckTrace(contract, true);
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 29 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class WalletTest method shouldTransferSumOf7.

@Test
public void shouldTransferSumOf7() throws Exception {
    Decimal valueToSend = new Decimal(280);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(12, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    assertEquals(6, wallet.getContracts().size());
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 30 with Contract

use of com.icodici.universa.contract.Contract in project universa by UniversaBlockchain.

the class WalletTest method shouldTransferTheSameValue.

@Test
public void shouldTransferTheSameValue() throws Exception {
    Decimal valueToSend = new Decimal(5);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(5));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(1, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contract, true);
    assertEquals(0, wallet.getContracts().size());
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Aggregations

Contract (com.icodici.universa.contract.Contract)131 Test (org.junit.Test)67 Decimal (com.icodici.universa.Decimal)31 PrivateKey (com.icodici.crypto.PrivateKey)24 File (java.io.File)16 AsyncEvent (net.sergeych.tools.AsyncEvent)16 TimeoutException (java.util.concurrent.TimeoutException)14 Binder (net.sergeych.tools.Binder)14 KeyRecord (com.icodici.universa.contract.KeyRecord)13 HashSet (java.util.HashSet)9 Parcel (com.icodici.universa.contract.Parcel)8 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)7 Quantiser (com.icodici.universa.node2.Quantiser)7 PublicKey (com.icodici.crypto.PublicKey)6 TransactionPack (com.icodici.universa.contract.TransactionPack)6 IOException (java.io.IOException)6 BackingStoreException (java.util.prefs.BackingStoreException)6 OptionException (joptsimple.OptionException)6 HashId (com.icodici.universa.HashId)5 Arrays.asList (java.util.Arrays.asList)5