Skip to content

Commit c7a793a

Browse files
committed
add case for transfer trc10 with assembly
1 parent 6d2f2f4 commit c7a793a

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package stest.tron.wallet.dailybuild.trctoken;
2+
3+
import com.google.protobuf.ByteString;
4+
import io.grpc.ManagedChannel;
5+
import io.grpc.ManagedChannelBuilder;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.junit.Assert;
8+
import org.testng.annotations.AfterClass;
9+
import org.testng.annotations.BeforeClass;
10+
import org.testng.annotations.BeforeSuite;
11+
import org.testng.annotations.Test;
12+
import org.tron.api.GrpcAPI.AccountResourceMessage;
13+
import org.tron.api.WalletGrpc;
14+
import org.tron.common.crypto.ECKey;
15+
import org.tron.common.utils.ByteArray;
16+
import org.tron.common.utils.Utils;
17+
import org.tron.core.Wallet;
18+
import org.tron.protos.Protocol;
19+
import org.tron.protos.Protocol.TransactionInfo;
20+
import org.tron.protos.contract.SmartContractOuterClass;
21+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
22+
import stest.tron.wallet.common.client.Configuration;
23+
import stest.tron.wallet.common.client.Parameter.CommonConstant;
24+
import stest.tron.wallet.common.client.utils.Base58;
25+
import stest.tron.wallet.common.client.utils.PublicMethed;
26+
27+
import java.util.HashMap;
28+
import java.util.List;
29+
import java.util.Optional;
30+
import java.util.concurrent.TimeUnit;
31+
32+
@Slf4j
33+
public class ContractTrcToken081 {
34+
35+
private static final long now = System.currentTimeMillis();
36+
private static final long TotalSupply = 1000L;
37+
private static String tokenName = "testAssetIssue_" + Long.toString(now);
38+
private static ByteString assetAccountId = null;
39+
private final String testKey002 = Configuration.getByPath("testng.conf")
40+
.getString("foundationAccount.key1");
41+
private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey002);
42+
private ManagedChannel channelFull = null;
43+
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
44+
private String fullnode = Configuration.getByPath("testng.conf").getStringList("fullnode.ip.list")
45+
.get(0);
46+
private long maxFeeLimit = Configuration.getByPath("testng.conf")
47+
.getLong("defaultParameter.maxFeeLimit");
48+
private byte[] TokenReceiver = null;
49+
private byte[] TokenSender = null;
50+
51+
private String description = Configuration.getByPath("testng.conf")
52+
.getString("defaultParameter.assetDescription");
53+
private String url = Configuration.getByPath("testng.conf")
54+
.getString("defaultParameter.assetUrl");
55+
56+
private ECKey ecKey1 = new ECKey(Utils.getRandom());
57+
private byte[] dev001Address = ecKey1.getAddress();
58+
private String dev001Key = ByteArray.toHexString(ecKey1.getPrivKeyBytes());
59+
60+
@BeforeSuite
61+
public void beforeSuite() {
62+
Wallet wallet = new Wallet();
63+
Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET);
64+
}
65+
66+
/**
67+
* constructor.
68+
*/
69+
@BeforeClass(enabled = true)
70+
public void beforeClass() {
71+
72+
channelFull = ManagedChannelBuilder.forTarget(fullnode).usePlaintext(true).build();
73+
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);
74+
75+
PublicMethed.printAddress(dev001Key);
76+
Assert.assertTrue(PublicMethed
77+
.sendcoin(dev001Address, 3100_000_000L, fromAddress, testKey002, blockingStubFull));
78+
79+
PublicMethed.waitProduceNextBlock(blockingStubFull);
80+
81+
Assert.assertTrue(PublicMethed.freezeBalanceForReceiver(fromAddress,
82+
PublicMethed.getFreezeBalanceCount(dev001Address, dev001Key, 130000L, blockingStubFull), 0,
83+
1, ByteString.copyFrom(dev001Address), testKey002, blockingStubFull));
84+
85+
Assert.assertTrue(PublicMethed.freezeBalanceForReceiver(fromAddress, 10_000_000L, 0, 0,
86+
ByteString.copyFrom(dev001Address), testKey002, blockingStubFull));
87+
88+
PublicMethed.waitProduceNextBlock(blockingStubFull);
89+
90+
long start = System.currentTimeMillis() + 2000;
91+
long end = System.currentTimeMillis() + 1000000000;
92+
93+
//Create a new AssetIssue success.
94+
Assert.assertTrue(PublicMethed
95+
.createAssetIssue(dev001Address, tokenName, TotalSupply, 1, 10000, start, end, 1,
96+
description, url, 100000L, 100000L, 1L, 1L, dev001Key, blockingStubFull));
97+
98+
PublicMethed.waitProduceNextBlock(blockingStubFull);
99+
100+
assetAccountId = PublicMethed.queryAccount(dev001Address, blockingStubFull).getAssetIssuedID();
101+
102+
logger.info("The token name: " + tokenName);
103+
logger.info("The token ID: " + assetAccountId.toStringUtf8());
104+
105+
Long devAssetCountBefore = PublicMethed
106+
.getAssetIssueValue(dev001Address, assetAccountId, blockingStubFull);
107+
108+
logger.info("before AssetId: " + assetAccountId.toStringUtf8() + ", devAssetCountBefore: "
109+
+ devAssetCountBefore);
110+
111+
String filePath = "./src/test/resources/soliditycode/contractTrcToken081.sol";
112+
String contractName = "TokenReceiver";
113+
HashMap retMap = PublicMethed.getBycodeAbi(filePath, contractName);
114+
115+
String code = retMap.get("byteCode").toString();
116+
String abi = retMap.get("abI").toString();
117+
118+
String tokenId = assetAccountId.toStringUtf8();
119+
120+
TokenReceiver = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit,
121+
500000000L, 100, null, dev001Key, dev001Address, blockingStubFull);
122+
123+
PublicMethed.waitProduceNextBlock(blockingStubFull);
124+
SmartContract smartContract = PublicMethed
125+
.getContract(TokenReceiver, blockingStubFull);
126+
Assert.assertNotNull(smartContract.getAbi());
127+
128+
129+
contractName = "TokenSender";
130+
retMap = PublicMethed.getBycodeAbi(filePath, contractName);
131+
132+
code = retMap.get("byteCode").toString();
133+
abi = retMap.get("abI").toString();
134+
TokenSender = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit,
135+
500000000L, 100, 10000L,assetAccountId.toStringUtf8(),
136+
10L, null, dev001Key, dev001Address, blockingStubFull);
137+
PublicMethed.waitProduceNextBlock(blockingStubFull);
138+
smartContract = PublicMethed.getContract(TokenSender,
139+
blockingStubFull);
140+
Assert.assertNotNull(smartContract.getAbi());
141+
142+
Long contractAssetCount = PublicMethed
143+
.getAssetIssueValue(TokenSender, assetAccountId, blockingStubFull);
144+
logger.info("TokenSender has AssetId before: " + assetAccountId.toStringUtf8() + ", Count: "
145+
+ contractAssetCount);
146+
147+
Long devAssetCountAfterDeploy = PublicMethed
148+
.getAssetIssueValue(dev001Address, assetAccountId, blockingStubFull);
149+
logger.info("after deploy tokenSender AssetId: " + assetAccountId.toStringUtf8() + ", devAssetCountAfter: "
150+
+ devAssetCountAfterDeploy);
151+
Assert.assertTrue(10 == devAssetCountBefore-devAssetCountAfterDeploy);
152+
Assert.assertTrue(10 == contractAssetCount);
153+
154+
}
155+
156+
157+
@Test(enabled = true, description = "transfer 1 trc10 to contract by assembly")
158+
public void transferTokenToContract() {
159+
Long senderAssetCountBefore = PublicMethed
160+
.getAssetIssueValue(TokenSender, assetAccountId, blockingStubFull);
161+
logger.info("before trigger TokenSender has AssetId before: " + assetAccountId.toStringUtf8() + ", Count: "
162+
+ senderAssetCountBefore);
163+
164+
Long receiverAssetCountBefore = PublicMethed
165+
.getAssetIssueValue(TokenReceiver, assetAccountId, blockingStubFull);
166+
logger.info("before trigger tokenReceiver AssetId: " + assetAccountId.toStringUtf8() + ", Count: "
167+
+ receiverAssetCountBefore);
168+
String args = "\"" + Base58.encode58Check(TokenReceiver) + "\"";
169+
logger.info("args: " + args);
170+
String triggerTxid = PublicMethed
171+
.triggerContract(TokenSender, "sendTRC10(address)", args, false, 0,
172+
1000000000L, assetAccountId.toStringUtf8(), 0, dev001Address, dev001Key, blockingStubFull);
173+
174+
PublicMethed.waitProduceNextBlock(blockingStubFull);
175+
176+
Optional<Protocol.TransactionInfo> infoById = PublicMethed.getTransactionInfoById(triggerTxid, blockingStubFull);
177+
178+
if (infoById.get().getResultValue() != 0) {
179+
Assert.fail("transaction failed with message: " + infoById.get().getResMessage());
180+
}
181+
Long senderAssetCountAfter = PublicMethed
182+
.getAssetIssueValue(TokenSender, assetAccountId, blockingStubFull);
183+
logger.info("TokenSender has AssetId After trigger: " + assetAccountId.toStringUtf8() + ", Count: "
184+
+ senderAssetCountAfter);
185+
186+
Long receiverAssetCountAfterTrigger = PublicMethed
187+
.getAssetIssueValue(TokenReceiver, assetAccountId, blockingStubFull);
188+
logger.info("after trigger AssetId: " + assetAccountId.toStringUtf8() + ", Count: "
189+
+ receiverAssetCountAfterTrigger);
190+
Assert.assertTrue(1 == senderAssetCountBefore-senderAssetCountAfter);
191+
Assert.assertTrue(1 == receiverAssetCountAfterTrigger-receiverAssetCountBefore);
192+
193+
}
194+
195+
@Test(enabled = true, description = "transfer 1 trc10 to normal address by assembly")
196+
public void transferTokenToNormalAddress() {
197+
long senderAssetCountBefore = PublicMethed
198+
.getAssetIssueValue(TokenSender, assetAccountId, blockingStubFull);
199+
logger.info("TokenSender has AssetId After trigger: " + assetAccountId.toStringUtf8() + ", Count: "
200+
+ senderAssetCountBefore);
201+
202+
long devAssetCountBeforeTrigger = PublicMethed
203+
.getAssetIssueValue(dev001Address, assetAccountId, blockingStubFull);
204+
logger.info("after trigger AssetId: " + assetAccountId.toStringUtf8() + ", devAssetCountAfterTrigger: "
205+
+ devAssetCountBeforeTrigger);
206+
207+
String args = "\"" + Base58.encode58Check(dev001Address) + "\"";
208+
logger.info("args: " + args);
209+
String triggerTxid = PublicMethed
210+
.triggerContract(TokenSender, "sendTRC10(address)", args, false, 0,
211+
1000000000L, assetAccountId.toStringUtf8(), 0, dev001Address, dev001Key, blockingStubFull);
212+
213+
PublicMethed.waitProduceNextBlock(blockingStubFull);
214+
215+
Optional<Protocol.TransactionInfo> infoById = PublicMethed.getTransactionInfoById(triggerTxid, blockingStubFull);
216+
217+
if (infoById.get().getResultValue() != 0) {
218+
Assert.fail("transaction failed with message: " + infoById.get().getResMessage());
219+
}
220+
long senderAssetCountAfter = PublicMethed
221+
.getAssetIssueValue(TokenSender, assetAccountId, blockingStubFull);
222+
logger.info("TokenSender has AssetId After trigger: " + assetAccountId.toStringUtf8() + ", Count: "
223+
+ senderAssetCountAfter);
224+
225+
long devAssetCountAfterTrigger = PublicMethed
226+
.getAssetIssueValue(dev001Address, assetAccountId, blockingStubFull);
227+
logger.info("after trigger AssetId: " + assetAccountId.toStringUtf8() + ", devAssetCountAfterTrigger: "
228+
+ devAssetCountAfterTrigger);
229+
Assert.assertTrue(1 == senderAssetCountBefore - senderAssetCountAfter);
230+
Assert.assertTrue(1 == devAssetCountAfterTrigger - devAssetCountBeforeTrigger);
231+
}
232+
233+
/**
234+
* constructor.
235+
*/
236+
@AfterClass
237+
public void shutdown() throws InterruptedException {
238+
PublicMethed.freedResource(dev001Address, dev001Key, fromAddress, blockingStubFull);
239+
PublicMethed.unFreezeBalance(fromAddress, testKey002, 1, dev001Address, blockingStubFull);
240+
PublicMethed.unFreezeBalance(fromAddress, testKey002, 0, dev001Address, blockingStubFull);
241+
if (channelFull != null) {
242+
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
243+
}
244+
}
245+
}
246+
247+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
contract TokenSender {
2+
constructor() payable public{}
3+
function sendTRC10(address target) public payable {
4+
trcToken tokenId = msg.tokenid;
5+
bytes memory callData = abi.encodeWithSignature("receiveTRC10(address,uint256,trcToken)", msg.sender, 1, tokenId);
6+
assembly {
7+
let ret := calltoken(
8+
gas(),
9+
target,
10+
1,
11+
tokenId,
12+
add(callData, 0x20),
13+
mload(callData),
14+
0,
15+
0)
16+
if iszero(ret) {
17+
revert(0, 0)
18+
}
19+
}
20+
}
21+
22+
function sendTRC10NoMethod(address target) public payable {
23+
trcToken tokenId = msg.tokenid;
24+
bytes4 sig = bytes4(keccak256("()")); // function signature
25+
assembly {
26+
let x := mload(0x40) // get empty storage location
27+
mstore(x,sig)
28+
let ret := calltoken(
29+
gas(),
30+
target,
31+
1, //token value
32+
tokenId, //token id
33+
x, // input
34+
0x04, // input size = 4 bytes
35+
x, // output stored at input location, save space
36+
0x04)
37+
if iszero(ret) {
38+
revert(0, 0)
39+
}
40+
}
41+
}
42+
}
43+
44+
contract TokenReceiver {
45+
constructor() payable public{}
46+
event Received(address, address, uint256, trcToken);
47+
48+
function receiveTRC10(address origin, uint256 value, trcToken id) external payable {
49+
emit Received(msg.sender, origin, value, id);
50+
}
51+
}

0 commit comments

Comments
 (0)