use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.
the class Parcel method addPayingAmountV2.
/**
* Adds an additional paying amount to the main contract of the parcel.
*
* Main payment contract of a parcel is used for an additional payment so it must contain required amount of units.
* An additional payment is used by various types of {@link com.icodici.universa.contract.services.NSmartContract}
*
* Note: this method uses new way of providing additional payments to {@link com.icodici.universa.contract.services.NSmartContract}
* currently it is only supported by {@link com.icodici.universa.contract.services.UnsContract}
*
* @param payingAmount an amount paid additionally.
* @param uKeys keys to resolve owner of parcel main payment contract
*/
public void addPayingAmountV2(int payingAmount, Collection<PrivateKey> uKeys) {
Contract transactionPayment = payment.getContract();
if (getRemainingU() != transactionPayment) {
throw new IllegalArgumentException("The paying amount has been added already");
}
Contract payment = createPayment(transactionPayment, uKeys, payingAmount, false);
// we add new item to the contract, so we need to recreate transaction pack
Contract mainContract = payload.getContract();
Compound compound = new Compound();
compound.addContract(COMPOUND_MAIN_TAG, mainContract, null);
compound.addContract(COMPOUND_PAYMENT_TAG, payment, null);
TransactionPack tp = compound.getCompoundContract().getTransactionPack();
tp.addTag(TP_PAYING_FOR_TAG_PREFIX + mainContract.getId().toBase64String(), payment.getId());
this.payload = tp;
}
use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.
the class ParcelTest method getRemainingUManualCreated.
@Test
public void getRemainingUManualCreated() throws Exception {
int amount = 100000;
Contract u = InnerContractsService.createFreshU(amount, new HashSet<>(Do.listOf(TestKeys.publicKey(1))));
u.seal();
assertEquals(u.getRevision(), 1);
Contract u2 = createPayment(u, Do.listOf(TestKeys.privateKey(1)), 20, false);
amount -= 20;
Contract payload1 = new Contract(TestKeys.privateKey(2));
Contract u3 = createPayment(u2, Do.listOf(TestKeys.privateKey(1)), 200, false);
amount -= 200;
payload1.addNewItems(u3);
payload1.seal();
Contract payload2 = new Contract(TestKeys.privateKey(2));
Contract u4 = createPayment(u3, Do.listOf(TestKeys.privateKey(1)), 300, false);
amount -= 300;
payload2.addNewItems(u4);
payload2.seal();
Contract payload3 = new Contract(TestKeys.privateKey(2));
payload3.seal();
Contract payload4 = new Contract(TestKeys.privateKey(2));
Contract u5 = createPayment(u4, Do.listOf(TestKeys.privateKey(1)), 302, false);
amount -= 302;
payload4.addNewItems(u5);
payload4.seal();
Compound compound = new Compound();
compound.addContract("tag1", payload1, new Binder());
compound.addContract("tag2", payload2, new Binder());
compound.addContract("tag3", payload3, new Binder());
compound.addContract("tag4", payload4, new Binder());
Parcel p = new Parcel(compound.getCompoundContract().getTransactionPack(), u2.getTransactionPack());
p = Parcel.unpack(p.pack());
Contract remaining = p.getRemainingU();
assertEquals(remaining.getRevision(), 5);
assertEquals(remaining.getStateData().get("transaction_units"), amount);
}
use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.
the class MainTest method secureLoanTestRepaymentMintableSimple.
@Test
public void secureLoanTestRepaymentMintableSimple() 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 200 tokens for borrower
Contract rest = ContractsService.createMintableTokenContract(new HashSet<>(Do.listOf(TestKeys.privateKey(15))), new HashSet<>(Do.listOf(borrowerKey.getPublicKey())), new BigDecimal("200"), new BigDecimal("0.1"), "uEUR", "UniEuro", "Coin description");
// change public key to address in issuer. it is important for reference matching
rest.seal();
assertEquals(ts.client.register(rest.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);
PrivateKey issuerKey = TestKeys.privateKey(13);
// init loan contract
Contract[] res = SecureLoanHelper.initSecureLoan(Do.listOf(issuerKey.getPublicKey().getLongAddress()), null, lenderAddress, borrowerAddress, token, Duration.ofSeconds(300), collateral, "1000", true, null, token.getIssuer().getSimpleAddress(), (String) token.getDefinition().getData().get("currency"));
Contract secureLoan = res[0];
secureLoan.seal();
Compound compound = new Compound();
compound.addContract("loan", secureLoan, null);
// add signatures
compound.getCompoundContract().addSignatureToSeal(borrowerKey);
compound.getCompoundContract().addSignatureToSeal(lenderKey);
compound.getCompoundContract().addSignatureToSeal(issuerKey);
ItemResult ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
token = res[1];
// prepare repayment using second contract of 200
token = token.createRevision(borrowerKey);
token.addRevokingItems(rest);
token.getStateData().set("amount", "1000");
token.seal();
ir = ts.client.register(token.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// set loan contract ot REPAID
res = SecureLoanHelper.repaySecureLoan(secureLoan, token);
secureLoan = res[0];
compound = new Compound();
compound.addContract("loan", secureLoan, null);
// add signatures
compound.getCompoundContract().addSignatureToSeal(borrowerKey);
ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// close loan contract
res = SecureLoanHelper.closeSecureLoan(secureLoan);
secureLoan = res[0];
token = res[1];
collateral = res[2];
compound = new Compound();
compound.addContract("loan", secureLoan, null);
// add signatures
compound.getCompoundContract().addSignatureToSeal(borrowerKey);
compound.getCompoundContract().addSignatureToSeal(lenderKey);
ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// Check that token and collateral are available
token = token.createRevision(lenderKey);
token.setOwnerKeys(TestKeys.publicKey(5));
token.seal();
ir = ts.client.register(token.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
token = collateral.createRevision(borrowerKey);
token.setOwnerKeys(TestKeys.publicKey(5));
token.seal();
ir = ts.client.register(token.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
ts.shutdown();
}
use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.
the class MainTest method testCompound.
@Test
public void testCompound() throws Exception {
Contract referencedItem = new Contract(TestKeys.privateKey(1));
referencedItem.seal();
Contract contract = new Contract(TestKeys.privateKey(2));
contract.seal();
contract.getTransactionPack().addReferencedItem(referencedItem);
Contract contract2 = new Contract(TestKeys.privateKey(2));
contract2.seal();
contract2.getTransactionPack().addReferencedItem(referencedItem);
Compound compound = new Compound();
compound.addContract("main", contract, Binder.of("foo", "bar"));
compound.addContract("second", contract2, Binder.of("key", "value"));
compound = new Compound(compound.getCompoundContract().getPackedTransaction());
System.out.println(compound.getTags());
System.out.println(compound.getData("main"));
Contract main = compound.getContract("main");
assertEquals(main.getId(), contract.getId());
assertEquals(compound.getContract("second").getId(), contract2.getId());
assertEquals(main.getTransactionPack().getReferencedItems().keySet(), contract.getTransactionPack().getReferencedItems().keySet());
}
use of com.icodici.universa.contract.helpers.Compound in project universa by UniversaBlockchain.
the class MainTest method escrowHelperTest.
@Test
public void escrowHelperTest() throws Exception {
KeyAddress issuerAddress = TestKeys.publicKey(9).getShortAddress();
KeyAddress contractorAddress = TestKeys.publicKey(10).getShortAddress();
KeyAddress customerAddress = TestKeys.publicKey(11).getShortAddress();
KeyAddress arbitratorAddress = TestKeys.publicKey(12).getShortAddress();
KeyAddress storageServiceAddress = TestKeys.publicKey(13).getShortAddress();
PrivateKey issuerKey = TestKeys.privateKey(9);
PrivateKey contractorKey = TestKeys.privateKey(10);
PrivateKey customerKey = TestKeys.privateKey(11);
PrivateKey arbitratorKey = TestKeys.privateKey(12);
PrivateKey storageServiceKey = TestKeys.privateKey(13);
TestSpace ts = prepareTestSpace();
ts.nodes.forEach(n -> {
n.config.setIsFreeRegistrationsAllowedFromYaml(true);
n.config.getAddressesWhiteList().add(ts.client.getSession().getPrivateKey().getPublicKey().getLongAddress());
});
Contract payment;
Contract escrow;
ItemResult ir;
Contract[] res;
// INIT->ASSIGN->COMPLETE->CLOSE(customer accepts work)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), Binder.of("data", "asdasd123"), Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofDays(500), payment)[0];
escrow.addSignatureToSeal(issuerKey);
escrow.addSignatureToSeal(customerKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = EscrowHelper.completeEscrow(escrow, Binder.of("description", "Completion details"))[0];
Compound compound = new Compound();
compound.addContract("escrow", escrow, null);
// add signatures
compound.getCompoundContract().addSignatureToSeal(contractorKey);
compound.getCompoundContract().addSignatureToSeal(storageServiceKey);
ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = compound.getContract("escrow");
res = EscrowHelper.closeByCustomer(escrow);
escrow = res[0];
compound = new Compound();
compound.addContract("escrow", escrow, null);
// add signatures
compound.getCompoundContract().addSignatureToSeal(customerKey);
compound.getCompoundContract().addSignatureToSeal(storageServiceKey);
ir = ts.client.register(compound.getCompoundContract().getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = compound.getContract("escrow");
res = EscrowHelper.obtainPaymentOnClosedEscrow(escrow);
payment = res[0];
payment.addSignatureToSeal(contractorKey);
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = payment.createRevision(contractorKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->ASSIGN->COMPLETE->CLOSE(arbitration)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), null, Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofDays(500), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = EscrowHelper.completeEscrow(escrow, Binder.of("description", "Completion details"))[0];
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(storageServiceKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
res = EscrowHelper.closeByArbitration(escrow);
escrow = res[0];
escrow.addSignatureToSeal(arbitratorKey);
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(storageServiceKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
res = EscrowHelper.obtainPaymentOnClosedEscrow(escrow);
payment = res[0];
payment.addSignatureToSeal(contractorKey);
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = payment.createRevision(contractorKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->ASSIGN->COMPLETE->CANCEL(cancel agreed)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), Binder.of("aaa", "111"), Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofDays(500), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = EscrowHelper.completeEscrow(escrow, Binder.of("description", "Completion details"))[0];
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(storageServiceKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
res = EscrowHelper.cancelEscrow(escrow);
escrow = res[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = res[1];
payment = payment.createRevision(customerKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->ASSIGN->COMPLETE->CANCEL(arbitration)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), null, Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofDays(500), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
escrow = EscrowHelper.completeEscrow(escrow, Binder.of("description", "Completion details"))[0];
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(storageServiceKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
res = EscrowHelper.cancelEscrow(escrow);
escrow = res[0];
escrow.addSignatureToSeal(arbitratorKey);
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(storageServiceKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = res[1];
payment = payment.createRevision(customerKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->ASSIGN->CANCEL(contractor,customer)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), null, Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofDays(500), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
res = EscrowHelper.cancelEscrow(escrow);
escrow = res[0];
escrow.addSignatureToSeal(contractorKey);
escrow.addSignatureToSeal(customerKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = res[1];
payment = payment.createRevision(customerKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
payment.getTransactionPack().addReferencedItem(escrow);
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->ASSIGN->CANCEL(customer,timeout)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), null, Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofSeconds(3), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.assignEscrow(escrow, contractorAddress, Binder.of("description", "Assignment is done with additional info..."))[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(contractorKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
Thread.sleep(3000);
res = EscrowHelper.cancelEscrow(escrow);
escrow = res[0];
escrow.addSignatureToSeal(customerKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = res[1];
payment = payment.createRevision(customerKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
// INIT->CANCEL(customer)
payment = new Contract(customerKey);
payment.seal();
assertEquals(ts.client.register(payment.getPackedTransaction(), 8000).state, ItemState.APPROVED);
escrow = EscrowHelper.initEscrow(Do.listOf(issuerAddress), null, Binder.of("description", "This is description the work to be done"), customerAddress, arbitratorAddress, storageServiceAddress, Duration.ofSeconds(30000), payment)[0];
escrow.addSignatureToSeal(customerKey);
escrow.addSignatureToSeal(issuerKey);
assertEquals(ts.client.register(escrow.getPackedTransaction(), 8000).state, ItemState.APPROVED);
res = EscrowHelper.cancelEscrow(escrow);
escrow = res[0];
escrow.addSignatureToSeal(customerKey);
ir = ts.client.register(escrow.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
payment = res[1];
payment = payment.createRevision(customerKey);
payment.setOwnerKeys(TestKeys.publicKey(20));
payment.seal();
ir = ts.client.register(payment.getPackedTransaction(), 8000);
System.out.println(ir);
assertEquals(ir.state, ItemState.APPROVED);
ts.shutdown();
}
Aggregations