Skip to content

Commit 1242e02

Browse files
committed
add TransferAsset Native function
1 parent 3dad719 commit 1242e02

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

‎src/main/java/org/tron/common/runtime/vm/PrecompiledContracts.java‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.tron.protos.Contract.ProposalApproveContract;
6060
import org.tron.protos.Contract.ProposalCreateContract;
6161
import org.tron.protos.Contract.ProposalDeleteContract;
62+
import org.tron.protos.Contract.TransferAssetContract;
6263
import org.tron.protos.Contract.VoteWitnessContract;
6364
import org.tron.protos.Contract.WithdrawBalanceContract;
6465
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
@@ -88,6 +89,7 @@ public class PrecompiledContracts {
8889
private static final ProposalDeleteNative proposalDelete = new ProposalDeleteNative();
8990
private static final ConvertFromTronBytesAddressNative convertFromTronBytesAddress = new ConvertFromTronBytesAddressNative();
9091
private static final ConvertFromTronBase58AddressNative convertFromTronBase58Address = new ConvertFromTronBase58AddressNative();
92+
private static final TransferAssetNative transferAsset = new TransferAssetNative();
9193

9294

9395
private static final DataWord ecRecoverAddr = new DataWord(
@@ -124,6 +126,8 @@ public class PrecompiledContracts {
124126
"0000000000000000000000000000000000000000000000000000000000010008");
125127
private static final DataWord convertFromTronBase58AddressAddr = new DataWord(
126128
"0000000000000000000000000000000000000000000000000000000000010009");
129+
private static final DataWord transferAssetAddr = new DataWord(
130+
"000000000000000000000000000000000000000000000000000000000001000a");
127131

128132
public static PrecompiledContract getContractForAddress(DataWord address) {
129133

@@ -169,6 +173,9 @@ public static PrecompiledContract getContractForAddress(DataWord address) {
169173
if (address.equals(convertFromTronBase58AddressAddr)) {
170174
return convertFromTronBase58Address;
171175
}
176+
if (address.equals(transferAssetAddr)) {
177+
return transferAsset;
178+
}
172179

173180

174181
/*
@@ -1111,4 +1118,65 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
11111118
return Pair.of(true, new DataWord(new DataWord(hexString).getLast20Bytes()).getData());
11121119
}
11131120
}
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+
}
11141182
}

0 commit comments

Comments
 (0)