|
59 | 59 | import org.tron.protos.Contract.ProposalApproveContract; |
60 | 60 | import org.tron.protos.Contract.ProposalCreateContract; |
61 | 61 | import org.tron.protos.Contract.ProposalDeleteContract; |
| 62 | +import org.tron.protos.Contract.TransferAssetContract; |
62 | 63 | import org.tron.protos.Contract.VoteWitnessContract; |
63 | 64 | import org.tron.protos.Contract.WithdrawBalanceContract; |
64 | 65 | import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
@@ -88,6 +89,7 @@ public class PrecompiledContracts { |
88 | 89 | private static final ProposalDeleteNative proposalDelete = new ProposalDeleteNative(); |
89 | 90 | private static final ConvertFromTronBytesAddressNative convertFromTronBytesAddress = new ConvertFromTronBytesAddressNative(); |
90 | 91 | private static final ConvertFromTronBase58AddressNative convertFromTronBase58Address = new ConvertFromTronBase58AddressNative(); |
| 92 | + private static final TransferAssetNative transferAsset = new TransferAssetNative(); |
91 | 93 |
|
92 | 94 |
|
93 | 95 | private static final DataWord ecRecoverAddr = new DataWord( |
@@ -124,6 +126,8 @@ public class PrecompiledContracts { |
124 | 126 | "0000000000000000000000000000000000000000000000000000000000010008"); |
125 | 127 | private static final DataWord convertFromTronBase58AddressAddr = new DataWord( |
126 | 128 | "0000000000000000000000000000000000000000000000000000000000010009"); |
| 129 | + private static final DataWord transferAssetAddr = new DataWord( |
| 130 | + "000000000000000000000000000000000000000000000000000000000001000a"); |
127 | 131 |
|
128 | 132 | public static PrecompiledContract getContractForAddress(DataWord address) { |
129 | 133 |
|
@@ -169,6 +173,9 @@ public static PrecompiledContract getContractForAddress(DataWord address) { |
169 | 173 | if (address.equals(convertFromTronBase58AddressAddr)) { |
170 | 174 | return convertFromTronBase58Address; |
171 | 175 | } |
| 176 | + if (address.equals(transferAssetAddr)) { |
| 177 | + return transferAsset; |
| 178 | + } |
172 | 179 |
|
173 | 180 |
|
174 | 181 | /* |
@@ -1111,4 +1118,65 @@ public Pair<Boolean, byte[]> execute(byte[] data) { |
1111 | 1118 | return Pair.of(true, new DataWord(new DataWord(hexString).getLast20Bytes()).getData()); |
1112 | 1119 | } |
1113 | 1120 | } |
| 1121 | + |
| 1122 | + /** |
| 1123 | + * Native function for transferring Asset to another account. <br/> |
| 1124 | + * <br/> |
| 1125 | + * |
| 1126 | + * Input data[]: <br/> toAddress, amount, assetName <br/> |
| 1127 | + * |
| 1128 | + * Output: <br/> transfer asset operation success or not <br/> |
| 1129 | + */ |
| 1130 | + public static class TransferAssetNative extends PrecompiledContract { |
| 1131 | + |
| 1132 | + @Override |
| 1133 | + // TODO: Please re-implement this function after Tron cost is well designed. |
| 1134 | + public long getGasForData(byte[] data) { |
| 1135 | + return 200; |
| 1136 | + } |
| 1137 | + |
| 1138 | + @Override |
| 1139 | + public Pair<Boolean, byte[]> execute(byte[] data) { |
| 1140 | + |
| 1141 | + if (data == null) { |
| 1142 | + data = EMPTY_BYTE_ARRAY; |
| 1143 | + } |
| 1144 | + |
| 1145 | + byte[] toAddress = new byte[32]; |
| 1146 | + System.arraycopy(data, 0, toAddress, 0, 32); |
| 1147 | + byte[] amount = new byte[32]; |
| 1148 | + System.arraycopy(data, 32, amount, 0, 32); |
| 1149 | + byte[] name = new byte[32]; |
| 1150 | + System.arraycopy(data, 64, amount, 0, 32); |
| 1151 | + |
| 1152 | + Contract.TransferAssetContract.Builder builder = Contract.TransferAssetContract |
| 1153 | + .newBuilder(); |
| 1154 | + builder.setOwnerAddress(ByteString.copyFrom(getCallerAddress())); |
| 1155 | + builder.setToAddress(ByteString.copyFrom(toAddress)); |
| 1156 | + builder.setAmount(Longs.fromByteArray(amount)); |
| 1157 | + builder.setAssetName(ByteString.copyFrom(name)); |
| 1158 | + |
| 1159 | + |
| 1160 | + TransferAssetContract contract = builder.build(); |
| 1161 | + |
| 1162 | + TransactionCapsule trx = new TransactionCapsule(contract, |
| 1163 | + ContractType.TransferAssetContract); |
| 1164 | + |
| 1165 | + final List<Actuator> actuatorList = ActuatorFactory |
| 1166 | + .createActuator(trx, getDeposit().getDbManager()); |
| 1167 | + try { |
| 1168 | + actuatorList.get(0).validate(); |
| 1169 | + actuatorList.get(0).execute(getResult().getRet()); |
| 1170 | + } catch (ContractExeException e) { |
| 1171 | + logger.debug("ContractExeException when calling transferAssetContract in vm"); |
| 1172 | + logger.debug("ContractExeException: {}", e.getMessage()); |
| 1173 | + return null; |
| 1174 | + } catch (ContractValidateException e) { |
| 1175 | + logger.debug("ContractValidateException when calling transferAssetContract in vm"); |
| 1176 | + logger.debug("ContractValidateException: {}", e.getMessage()); |
| 1177 | + return null; |
| 1178 | + } |
| 1179 | + return Pair.of(true, new DataWord(1).getData()); |
| 1180 | + } |
| 1181 | + } |
1114 | 1182 | } |
0 commit comments