|
| 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 | + |
0 commit comments