|
| 1 | +package irita.sdk; |
| 2 | + |
| 3 | +import com.google.protobuf.GeneratedMessageV3; |
| 4 | +import irita.sdk.client.BaseClient; |
| 5 | +import irita.sdk.client.IritaClient; |
| 6 | +import irita.sdk.config.ClientConfig; |
| 7 | +import irita.sdk.config.OpbConfig; |
| 8 | +import irita.sdk.constant.enums.BroadcastMode; |
| 9 | +import irita.sdk.key.KeyManager; |
| 10 | +import irita.sdk.key.KeyManagerFactory; |
| 11 | +import irita.sdk.model.*; |
| 12 | +import irita.sdk.module.bank.BankClient; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Disabled; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import proto.nft.Tx; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.math.BigInteger; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | + |
| 27 | +public class FeeGrantTest { |
| 28 | + private BaseClient baseClient; |
| 29 | + private IritaClient iritaClient; |
| 30 | + private final BaseTx baseTx = new BaseTx(200000, new Fee("300000", "ugas"), BroadcastMode.Commit); |
| 31 | + private String accountAmount; |
| 32 | + private String grantAmount; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void init() { |
| 36 | + //本地环境账户助记词 |
| 37 | + String mnemonic = "plate cable circle skill pigeon lecture lizard sick sail amazing salute other sibling neutral device artefact point pond certain ice discover gift primary worry"; |
| 38 | + KeyManager km = KeyManagerFactory.createDefault(); |
| 39 | + km.recover(mnemonic); |
| 40 | + |
| 41 | + // 本地环境链信息 |
| 42 | + String nodeUri = "http://127.0.0.1:26657"; |
| 43 | + String grpcAddr = "127.0.0.1:9090"; |
| 44 | + String chainId = "irita-tesnet"; |
| 45 | + ClientConfig clientConfig = new ClientConfig(nodeUri, grpcAddr, chainId); |
| 46 | + OpbConfig opbConfig = null; |
| 47 | + |
| 48 | + iritaClient = new IritaClient(clientConfig, opbConfig, km); |
| 49 | + baseClient = iritaClient.getBaseClient(); |
| 50 | + //设置FeeGrant 该FeeGrant账户须是已对本次交易发送方账户进行过feegrant操作 |
| 51 | + baseTx.setFeeGranter("iaa17y3qs2zuanr93nk844x0t7e6ktchwygnc8fr0g"); |
| 52 | + //如果需要设置FeePayer 则该FeePayer必须对本交易签名 |
| 53 | + //baseTx.setFeePayer("iaa1j782zma8xj78wsmyfqvt8muvza8aazj05vpx9p"); |
| 54 | + accountAmount = testQueryAccount("iaa1cfqjw7h5h5xdaz6d05vs5xtpsn5w3vthartxvk",iritaClient); |
| 55 | + grantAmount = testQueryAccount("iaa17y3qs2zuanr93nk844x0t7e6ktchwygnc8fr0g",iritaClient); |
| 56 | + System.out.println("FeeGrant Account Balances:"+grantAmount); |
| 57 | + System.out.println("From Account Balances:"+ accountAmount); |
| 58 | + assertEquals("iaa1cfqjw7h5h5xdaz6d05vs5xtpsn5w3vthartxvk", km.getCurrentKeyInfo().getAddress()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @Disabled |
| 63 | + public void testFeeGrant() throws IOException { |
| 64 | + |
| 65 | + String denomID = "denomid" + new Random().nextInt(1000); |
| 66 | + String denomName = "denomname" + new Random().nextInt(1000); |
| 67 | + String schema = "no shcema"; |
| 68 | + |
| 69 | + Account account = baseClient.queryAccount(baseTx); |
| 70 | + Tx.MsgIssueDenom msg1 = Tx.MsgIssueDenom |
| 71 | + .newBuilder() |
| 72 | + .setId(denomID + "1") |
| 73 | + .setName(denomName + "1") |
| 74 | + .setSchema(schema + "1") |
| 75 | + .setSender(account.getAddress()) |
| 76 | + .build(); |
| 77 | + Tx.MsgIssueDenom msg2 = Tx.MsgIssueDenom |
| 78 | + .newBuilder() |
| 79 | + .setId(denomID + "2") |
| 80 | + .setName(denomName + "2") |
| 81 | + .setSchema(schema + "2") |
| 82 | + .setSender(account.getAddress()) |
| 83 | + .build(); |
| 84 | + |
| 85 | + List<GeneratedMessageV3> msgs = new ArrayList<>(); |
| 86 | + msgs.add(msg1); |
| 87 | + msgs.add(msg2); |
| 88 | + ResultTx resultTx = baseClient.buildAndSend(msgs, baseTx, account); |
| 89 | + System.out.println(resultTx.getResult().getHash()); |
| 90 | + String feeGrantResult = testQueryAccount("iaa17y3qs2zuanr93nk844x0t7e6ktchwygnc8fr0g",iritaClient); |
| 91 | + String amount = testQueryAccount("iaa1cfqjw7h5h5xdaz6d05vs5xtpsn5w3vthartxvk",iritaClient); |
| 92 | + System.out.println("FeeGrant Account Balances:"+feeGrantResult); |
| 93 | + System.out.println("From Account Balances:"+amount); |
| 94 | + BigInteger grantAmountBigInteger = new BigInteger(grantAmount); |
| 95 | + BigInteger grantResult = new BigInteger(feeGrantResult); |
| 96 | + assertEquals(amount, accountAmount); |
| 97 | + assertEquals(grantAmountBigInteger.subtract(new BigInteger("300000")),grantResult); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private String testQueryAccount(String address,IritaClient iritaClien) { |
| 102 | + BankClient bankClient = iritaClien.getBankClient(); |
| 103 | + BaseAccount account = bankClient.queryAccount(address); |
| 104 | + List<Coin> coins = account.getCoins(); |
| 105 | + Optional<Coin> iritaCoin = coins.stream().filter(x -> x.getDenom().equals("ugas")).findFirst(); |
| 106 | + if (!iritaCoin.isPresent()) { |
| 107 | + return "0"; |
| 108 | + } |
| 109 | + return iritaCoin.get().getAmount(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/*tx: |
| 114 | + '@type': /cosmos.tx.v1beta1.Tx |
| 115 | + auth_info: |
| 116 | + fee: |
| 117 | + amount: |
| 118 | + - amount: "300000" |
| 119 | + denom: ugas |
| 120 | + gas_limit: "200000" |
| 121 | + granter: iaa17y3qs2zuanr93nk844x0t7e6ktchwygnc8fr0g |
| 122 | + payer: "" |
| 123 | + signer_infos: |
| 124 | + - mode_info: |
| 125 | + single: |
| 126 | + mode: SIGN_MODE_DIRECT |
| 127 | + public_key: |
| 128 | + '@type': /cosmos.crypto.sm2.PubKey |
| 129 | + key: AxOeWQYtALvVweF7hvXDsFKbfZK2BARgD1YdgBNLZ3Yw |
| 130 | + sequence: "7" |
| 131 | +*/ |
| 132 | + |
0 commit comments