Search in sources :

Example 6 with Compound

use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.

the class CompoundTest method basicTest.

@Test
public void basicTest() throws Exception {
    Contract contractWithSubItems = new Contract(TestKeys.privateKey(1));
    Contract newItem = new Contract(TestKeys.privateKey(2));
    newItem.seal();
    contractWithSubItems.addNewItems(newItem);
    Contract revokingItem = new Contract(TestKeys.privateKey(3));
    revokingItem.seal();
    contractWithSubItems.addRevokingItems(revokingItem);
    contractWithSubItems.seal();
    Contract contractWithReferencedItems = new Contract(TestKeys.privateKey(1));
    contractWithReferencedItems.seal();
    Contract refItem = new Contract(TestKeys.privateKey(4));
    refItem.seal();
    contractWithReferencedItems.getTransactionPack().addReferencedItem(refItem);
    Contract contractWithTags = new Contract(TestKeys.privateKey(1));
    contractWithTags.seal();
    contractWithTags.getTransactionPack().addTag("main", contractWithTags.getId());
    Compound compound = new Compound();
    compound.addContract("sub", contractWithSubItems, null);
    compound.addContract("ref", contractWithReferencedItems, Binder.of("foo", "bar"));
    compound.addContract("tags", contractWithTags, null);
    Contract compoundContract = compound.getCompoundContract();
    compoundContract = Contract.fromPackedTransaction(compoundContract.getPackedTransaction());
    compound = new Compound(compoundContract);
    assertNull(compound.getContract("unknown"));
    assertEquals(compound.getContract("sub").getTransactionPack().getSubItems().size(), 2);
    assertEquals(compound.getContract("ref").getTransactionPack().getReferencedItems().size(), 1);
    assertEquals(compound.getContract("tags").getTransactionPack().getTags().size(), 1);
}
Also used : Compound(com.icodici.universa.contract.helpers.Compound) Test(org.junit.Test)

Example 7 with Compound

use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.

the class MainTest method secureLoanTestDefaultMintableSimple.

@Test
public void secureLoanTestDefaultMintableSimple() throws Exception {
    TestSpace ts = prepareTestSpace();
    ts.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true));
    PrivateKey lenderKey = TestKeys.privateKey(1);
    PrivateKey borrowerKey = TestKeys.privateKey(2);
    KeyAddress lenderAddress = lenderKey.getPublicKey().getLongAddress();
    KeyAddress borrowerAddress = borrowerKey.getPublicKey().getLongAddress();
    // create 800 tokens for lender
    Contract token = ContractsService.createMintableTokenContract(new HashSet<>(Do.listOf(TestKeys.privateKey(15))), new HashSet<>(Do.listOf(lenderKey.getPublicKey())), new BigDecimal("800"), new BigDecimal("0.1"), "uEUR", "UniEuro", "Coin description");
    // change public key to address in issuer. it is important for reference matching
    token.seal();
    assertEquals(ts.client.register(token.getPackedTransaction(), 8000).state, ItemState.APPROVED);
    // create collateral on behalf of borrower
    Contract collateral = new Contract(borrowerKey);
    collateral.getStateData().put("description", "This is very valuable contract");
    collateral.seal();
    assertEquals(ts.client.register(collateral.getPackedTransaction(), 8000).state, ItemState.APPROVED);
    // INIT LOAN
    Contract[] res = SecureLoanHelper.initSecureLoan(new Binder(), lenderAddress, borrowerAddress, token, Duration.ofSeconds(3), collateral, "1000", true, null, token.getIssuer().getSimpleAddress(), (String) token.getDefinition().getData().get("currency"));
    Contract secureLoan = res[0];
    Compound compound = new Compound();
    compound.addContract("loan", secureLoan, null);
    compound.getCompoundContract().addSignatureToSeal(borrowerKey);
    compound.getCompoundContract().addSignatureToSeal(lenderKey);
    ItemResult ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
    System.out.println(ir);
    assertEquals(ir.state, ItemState.APPROVED);
    Thread.sleep(6000);
    // DEFAULT LOAN
    res = SecureLoanHelper.defaultSecureLoan(secureLoan);
    secureLoan = res[0];
    compound = new Compound();
    compound.addContract("loan", secureLoan, null);
    compound.getCompoundContract().addSignatureToSeal(lenderKey);
    collateral = res[1];
    ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
    System.out.println(ir);
    assertEquals(ir.state, ItemState.APPROVED);
    // colateral is now owner by lender
    collateral = collateral.createRevision(lenderKey);
    collateral.setOwnerKeys(TestKeys.publicKey(5));
    collateral.seal();
    ir = ts.client.register(collateral.getPackedTransaction(), 8000);
    System.out.println(ir);
    assertEquals(ir.state, ItemState.APPROVED);
    ts.shutdown();
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) KeyAddress(com.icodici.crypto.KeyAddress) Compound(com.icodici.universa.contract.helpers.Compound) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Compound (com.icodici.universa.contract.helpers.Compound)7 Test (org.junit.Test)6 KeyAddress (com.icodici.crypto.KeyAddress)3 PrivateKey (com.icodici.crypto.PrivateKey)3 BigDecimal (java.math.BigDecimal)2 Binder (net.sergeych.tools.Binder)1