Browse Source

Improved examples of contracts;

tags/1.1.0
huanghaiquan 5 years ago
parent
commit
8bfadfeab5
3 changed files with 60 additions and 67 deletions
  1. +8
    -12
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/contract/samples/AssetContract.java
  2. +41
    -45
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/contract/samples/AssetContractImpl.java
  3. +11
    -10
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Contract.java

+ 8
- 12
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/contract/samples/AssetContract.java View File

@@ -4,7 +4,7 @@ import com.jd.blockchain.contract.Contract;
import com.jd.blockchain.contract.ContractEvent; import com.jd.blockchain.contract.ContractEvent;


/** /**
* 示例:一个“资产管理”智能合约;
* 示例:一个“资产管理”智能合约;
* *
* @author huanghaiquan * @author huanghaiquan
* *
@@ -15,10 +15,8 @@ public interface AssetContract {
/** /**
* 发行资产; * 发行资产;
* *
* @param amount
* 新发行的资产数量;
* @param assetHolderAddress
* 新发行的资产的持有账户;
* @param amount 新发行的资产数量;
* @param assetHolderAddress 新发行的资产的持有账户;
*/ */
@ContractEvent(name = "issue-asset") @ContractEvent(name = "issue-asset")
void issue(long amount, String assetHolderAddress); void issue(long amount, String assetHolderAddress);
@@ -26,14 +24,12 @@ public interface AssetContract {
/** /**
* 转移资产 * 转移资产
* *
* @param fromAddress
* 转出账户;
* @param toAddress
* 转入账户;
* @param amount
* 转移的资产数额;
* @param fromAddress 转出账户;
* @param toAddress 转入账户;
* @param amount 转移的资产数额;
* @return 返回转出账户的余额;
*/ */
@ContractEvent(name = "transfer-asset") @ContractEvent(name = "transfer-asset")
void transfer(String fromAddress, String toAddress, long amount);
long transfer(String fromAddress, String toAddress, long amount);


} }

+ 41
- 45
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/contract/samples/AssetContractImpl.java View File

@@ -11,6 +11,7 @@ import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.ledger.BlockchainIdentity; import com.jd.blockchain.ledger.BlockchainIdentity;
import com.jd.blockchain.ledger.KVDataEntry; import com.jd.blockchain.ledger.KVDataEntry;
import com.jd.blockchain.ledger.KVDataObject; import com.jd.blockchain.ledger.KVDataObject;
import com.jd.blockchain.utils.Bytes;


/** /**
* 示例:一个“资产管理”智能合约的实现; * 示例:一个“资产管理”智能合约的实现;
@@ -48,59 +49,54 @@ public class AssetContractImpl implements EventProcessingAware, AssetContract {
// 查询当前值; // 查询当前值;
KVDataEntry[] kvEntries = eventContext.getLedger().getDataEntries(currentLedgerHash(), ASSET_ADDRESS, KEY_TOTAL, KVDataEntry[] kvEntries = eventContext.getLedger().getDataEntries(currentLedgerHash(), ASSET_ADDRESS, KEY_TOTAL,
assetHolderAddress); assetHolderAddress);
// 计算资产的发行总数; // 计算资产的发行总数;
KVDataObject currTotal = (KVDataObject) kvEntries[0]; KVDataObject currTotal = (KVDataObject) kvEntries[0];
long newTotal = currTotal.longValue() + amount; long newTotal = currTotal.longValue() + amount;
eventContext.getLedger().dataAccount(ASSET_ADDRESS).setInt64(KEY_TOTAL, newTotal,
currTotal.getVersion());
eventContext.getLedger().dataAccount(ASSET_ADDRESS).setInt64(KEY_TOTAL, newTotal, currTotal.getVersion());


// 分配到持有者账户; // 分配到持有者账户;
KVDataObject holderAmount = (KVDataObject) kvEntries[1]; KVDataObject holderAmount = (KVDataObject) kvEntries[1];
long newHodlerAmount = holderAmount.longValue() + amount; long newHodlerAmount = holderAmount.longValue() + amount;
eventContext.getLedger().dataAccount(ASSET_ADDRESS).setInt64(assetHolderAddress, newHodlerAmount,
holderAmount.getVersion()).setText("K2", "info2", -1).setText("k3", "info3", 3);
eventContext.getLedger().dataAccount(ASSET_ADDRESS)
.setInt64(assetHolderAddress, newHodlerAmount, holderAmount.getVersion()).setText("K2", "info2", -1)
.setText("k3", "info3", 3);

} }


@Override @Override
public void transfer(String fromAddress, String toAddress, long amount) {
// if (amount < 0) {
// throw new ContractError("The amount is negative!");
// }
// if (amount == 0) {
// return;
// }
//
// //校验“转出账户”是否已签名;
// checkSignerPermission(fromAddress);
//
// // 查询现有的余额;
// Set<String> keys = new HashSet<>();
// keys.add(fromAddress);
// keys.add(toAddress);
// StateMap origBalances =
// eventContext.getLedger().getStates(currentLedgerHash(), ASSET_ADDRESS, keys);
// KVDataObject fromBalance = origBalances.get(fromAddress);
// KVDataObject toBalance = origBalances.get(toAddress);
//
// //检查是否余额不足;
// if ((fromBalance.longValue() - amount) < 0) {
// throw new ContractError("Insufficient balance!");
// }
//
// // 把数据的更改写入到账本;
// SimpleStateMap newBalances = new SimpleStateMap(origBalances.getAccount(),
// origBalances.getAccountVersion(),
// origBalances.getStateVersion());
// KVDataObject newFromBalance = fromBalance.newLong(fromBalance.longValue() -
// amount);
// KVDataObject newToBalance = toBalance.newLong(toBalance.longValue() +
// amount);
// newBalances.setValue(newFromBalance);
// newBalances.setValue(newToBalance);
//
// eventContext.getLedger().updateState(ASSET_ADDRESS).setStates(newBalances);
public long transfer(String fromAddress, String toAddress, long amount) {
if (amount < 0) {
throw new ContractException("The amount is negative!");
}
if (amount > 20000) {
throw new ContractException("The amount exceeds the limit of 20000!");
}

// 校验“转出账户”是否已签名;
checkSignerPermission(fromAddress);

// 查询现有的余额;
KVDataEntry[] origBalances = eventContext.getLedger().getDataEntries(currentLedgerHash(), ASSET_ADDRESS,
fromAddress, toAddress);
KVDataEntry fromBalanceKV = origBalances[0];
KVDataEntry toBalanceKV = origBalances[1];
long fromBalance = fromBalanceKV.getVersion() == -1 ? 0 : (long) fromBalanceKV.getValue();
long toBalance = toBalanceKV.getVersion() == -1 ? 0 : (long) toBalanceKV.getValue();

// 检查是否余额不足;

if ((fromBalance - amount) < 0) {
throw new ContractException("The balance is insufficient and the transfer failed!");
}
fromBalance = fromBalance + amount;
toBalance = toBalance + amount;
// 把数据的更改写入到账本;
eventContext.getLedger().dataAccount(fromAddress).setInt64(ASSET_ADDRESS, fromBalance, fromBalanceKV.getVersion());
eventContext.getLedger().dataAccount(toAddress).setInt64(ASSET_ADDRESS, toBalance, toBalanceKV.getVersion());

return -1;
} }


// ------------------------------------------------------------- // -------------------------------------------------------------
@@ -117,9 +113,9 @@ public class AssetContractImpl implements EventProcessingAware, AssetContract {
throw new ContractException("Permission Error! -- The requestors is not exactlly being owners!"); throw new ContractException("Permission Error! -- The requestors is not exactlly being owners!");
} }


Map<String, BlockchainIdentity> ownerMap = new HashMap<>();
Map<Bytes, BlockchainIdentity> ownerMap = new HashMap<>();
for (BlockchainIdentity o : owners) { for (BlockchainIdentity o : owners) {
ownerMap.put(o.getAddress().toBase58(), o);
ownerMap.put(o.getAddress(), o);
} }
for (BlockchainIdentity r : requestors) { for (BlockchainIdentity r : requestors) {
if (!ownerMap.containsKey(r.getAddress())) { if (!ownerMap.containsKey(r.getAddress())) {


+ 11
- 10
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Contract.java View File

@@ -11,6 +11,8 @@ import com.jd.blockchain.ledger.PreparedTransaction;
import com.jd.blockchain.ledger.TransactionTemplate; import com.jd.blockchain.ledger.TransactionTemplate;
import com.jd.blockchain.sdk.BlockchainService; import com.jd.blockchain.sdk.BlockchainService;
import com.jd.blockchain.sdk.client.GatewayServiceFactory; import com.jd.blockchain.sdk.client.GatewayServiceFactory;
import com.jd.blockchain.transaction.ContractReturnValue;
import com.jd.blockchain.transaction.LongValueHolder;
import com.jd.blockchain.utils.io.ByteArray; import com.jd.blockchain.utils.io.ByteArray;
import com.jd.blockchain.utils.net.NetworkAddress; import com.jd.blockchain.utils.net.NetworkAddress;
import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils;
@@ -49,8 +51,6 @@ public class SDKDemo_Contract {
BlockchainService service = serviceFactory.getBlockchainService(); BlockchainService service = serviceFactory.getBlockchainService();


HashDigest ledgerHash = getLedgerHash(); HashDigest ledgerHash = getLedgerHash();
// 发起交易;
TransactionTemplate txTemp = service.newTransaction(ledgerHash);


// -------------------------------------- // --------------------------------------
// 一个贸易账户,贸易结算后的利润将通过一个合约账户来执行利润分配; // 一个贸易账户,贸易结算后的利润将通过一个合约账户来执行利润分配;
@@ -71,25 +71,26 @@ public class SDKDemo_Contract {
// 备注信息; // 备注信息;
Remark remark = new Remark(); Remark remark = new Remark();
String remarkJSON = JSONSerializeUtils.serializeToJSON(remark); String remarkJSON = JSONSerializeUtils.serializeToJSON(remark);

// 发起交易;
TransactionTemplate txTemp = service.newTransaction(ledgerHash);

AssetContract assetContract = txTemp.contract(profitDistributionContract, AssetContract.class); AssetContract assetContract = txTemp.contract(profitDistributionContract, AssetContract.class);
assetContract.issue(1000, receiptorAccount1); assetContract.issue(1000, receiptorAccount1);
assetContract.transfer(receiptorAccount1, receiptorAccount2, 600);
// assetContract.
// --------------------------------------
LongValueHolder balance = ContractReturnValue.decode(assetContract.transfer(receiptorAccount1, receiptorAccount2, 600));


// TX 准备就绪; // TX 准备就绪;
PreparedTransaction prepTx = txTemp.prepare(); PreparedTransaction prepTx = txTemp.prepare();
String txHash = ByteArray.toBase64(prepTx.getHash().toBytes());


// 使用私钥进行签名; // 使用私钥进行签名;
AsymmetricKeypair keyPair = getSponsorKey();
AsymmetricKeypair keyPair = getSponsorKey();//示例方法,取发起人的私钥;
prepTx.sign(keyPair); prepTx.sign(keyPair);


// 提交交易; // 提交交易;
prepTx.commit(); prepTx.commit();
//获取返回值;
System.out.println("balance = " + balance.get());
} }


private static HashDigest getLedgerHash() { private static HashDigest getLedgerHash() {


Loading…
Cancel
Save