Search in sources :

Example 1 with TransferAssetContract

use of org.tron.protos.Contract.TransferAssetContract in project java-tron by tronprotocol.

the class TransferAssetActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        TransferAssetContract transferAssetContract = this.contract.unpack(TransferAssetContract.class);
        Preconditions.checkNotNull(transferAssetContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(transferAssetContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(transferAssetContract.getAssetName(), "AssetName is null");
        Preconditions.checkNotNull(transferAssetContract.getAmount(), "Amount is null");
        if (transferAssetContract.getOwnerAddress().equals(transferAssetContract.getToAddress())) {
            throw new ContractValidateException("Cannot transfer asset to yourself.");
        }
        byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
        if (!this.dbManager.getAccountStore().has(ownerKey)) {
            throw new ContractValidateException();
        }
        // if account with to_address is not existed,  create it.
        ByteString toAddress = transferAssetContract.getToAddress();
        if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {
            AccountCapsule account = new AccountCapsule(toAddress, AccountType.Normal);
            dbManager.getAccountStore().put(toAddress.toByteArray(), account);
        }
        byte[] nameKey = transferAssetContract.getAssetName().toByteArray();
        if (!this.dbManager.getAssetIssueStore().has(nameKey)) {
            throw new ContractValidateException();
        }
        long amount = transferAssetContract.getAmount();
        AccountCapsule ownerAccount = this.dbManager.getAccountStore().get(ownerKey);
        Map<String, Long> asset = ownerAccount.getAssetMap();
        if (asset.isEmpty()) {
            throw new ContractValidateException();
        }
        Long assetAmount = asset.get(ByteArray.toStr(nameKey));
        if (amount <= 0 || null == assetAmount || amount > assetAmount || assetAmount <= 0) {
            throw new ContractValidateException();
        }
    } catch (InvalidProtocolBufferException e) {
        throw new ContractValidateException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ByteString(com.google.protobuf.ByteString) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) TransferAssetContract(org.tron.protos.Contract.TransferAssetContract)

Example 2 with TransferAssetContract

use of org.tron.protos.Contract.TransferAssetContract in project java-tron by tronprotocol.

the class TransferAssetActuator method execute.

@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
    long fee = calcFee();
    if (!this.contract.is(TransferAssetContract.class)) {
        throw new ContractExeException();
    }
    if (this.dbManager == null) {
        throw new ContractExeException();
    }
    try {
        TransferAssetContract transferAssetContract = this.contract.unpack(TransferAssetContract.class);
        AccountStore accountStore = this.dbManager.getAccountStore();
        byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
        byte[] toKey = transferAssetContract.getToAddress().toByteArray();
        ByteString assertName = transferAssetContract.getAssetName();
        long amount = transferAssetContract.getAmount();
        AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
        if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
            throw new ContractExeException("reduceAssetAmount failed !");
        }
        accountStore.put(ownerKey, ownerAccountCapsule);
        AccountCapsule toAccountCapsule = accountStore.get(toKey);
        toAccountCapsule.addAssetAmount(assertName, amount);
        accountStore.put(toKey, toAccountCapsule);
        ret.setStatus(fee, code.SUCESS);
    } catch (InvalidProtocolBufferException e) {
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) AccountStore(org.tron.core.db.AccountStore) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TransferAssetContract(org.tron.protos.Contract.TransferAssetContract) ContractExeException(org.tron.core.exception.ContractExeException)

Aggregations

ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 AccountCapsule (org.tron.core.capsule.AccountCapsule)2 TransferAssetContract (org.tron.protos.Contract.TransferAssetContract)2 AccountStore (org.tron.core.db.AccountStore)1 ContractExeException (org.tron.core.exception.ContractExeException)1 ContractValidateException (org.tron.core.exception.ContractValidateException)1