|
| 1 | +package org.fisco.bcos.sdk.v3.test.contract.solidity; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import org.fisco.bcos.sdk.v3.client.Client; |
| 9 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Event; |
| 10 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Function; |
| 11 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Type; |
| 12 | +import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference; |
| 13 | +import org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String; |
| 14 | +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.Uint256; |
| 15 | +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple1; |
| 16 | +import org.fisco.bcos.sdk.v3.contract.Contract; |
| 17 | +import org.fisco.bcos.sdk.v3.contract.FunctionWrapper; |
| 18 | +import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; |
| 19 | +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; |
| 20 | +import org.fisco.bcos.sdk.v3.eventsub.EventSubCallback; |
| 21 | +import org.fisco.bcos.sdk.v3.model.CryptoType; |
| 22 | +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; |
| 23 | +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; |
| 24 | +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.ProxySignTransactionManager; |
| 25 | +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.TransactionManager; |
| 26 | +import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; |
| 27 | + |
| 28 | +@SuppressWarnings("unchecked") |
| 29 | +public class Incremental extends Contract { |
| 30 | + public static final String[] BINARY_ARRAY = { |
| 31 | + "60806040526000805534801561001457600080fd5b50610235806100246000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80633fa4f2451461003b578063bdcbbf5414610056575b600080fd5b61004460005481565b60405190815260200160405180910390f35b6100446100643660046100d3565b600060016000808282546100789190610184565b90915550506040517f7ed53cbadbbc7d8800605823ac88c67b7d9c9fa9d54d45a68d221760e0a86552906100ad9084906101aa565b60405180910390a1505060005490565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156100e557600080fd5b813567ffffffffffffffff808211156100fd57600080fd5b818401915084601f83011261011157600080fd5b813581811115610123576101236100bd565b604051601f8201601f19908116603f0116810190838211818310171561014b5761014b6100bd565b8160405282815287602084870101111561016457600080fd5b826020860160208301376000928101602001929092525095945050505050565b600082198211156101a557634e487b7160e01b600052601160045260246000fd5b500190565b600060208083528351808285015260005b818110156101d7578581018301518582016040015282016101bb565b818111156101e9576000604083870101525b50601f01601f191692909201604001939250505056fea26469706673582212204f4929b54be1544a8e77f16102dc301071ee72f711fad9923c31e0e272bc48d164736f6c634300080b0033" |
| 32 | + }; |
| 33 | + |
| 34 | + public static final String BINARY = |
| 35 | + org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", BINARY_ARRAY); |
| 36 | + |
| 37 | + public static final String[] SM_BINARY_ARRAY = { |
| 38 | + "60806040526000805534801561001457600080fd5b50610235806100246000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632d92c59d1461003b57806386ff705814610056575b600080fd5b61004460005481565b60405190815260200160405180910390f35b6100446100643660046100d3565b600060016000808282546100789190610184565b90915550506040517f7d280234d3c48f94f1914a272412c3e0030dd7a5841f7933ab1e45658bfaabbe906100ad9084906101aa565b60405180910390a1505060005490565b63b95aa35560e01b600052604160045260246000fd5b6000602082840312156100e557600080fd5b813567ffffffffffffffff808211156100fd57600080fd5b818401915084601f83011261011157600080fd5b813581811115610123576101236100bd565b604051601f8201601f19908116603f0116810190838211818310171561014b5761014b6100bd565b8160405282815287602084870101111561016457600080fd5b826020860160208301376000928101602001929092525095945050505050565b600082198211156101a55763b95aa35560e01b600052601160045260246000fd5b500190565b600060208083528351808285015260005b818110156101d7578581018301518582016040015282016101bb565b818111156101e9576000604083870101525b50601f01601f191692909201604001939250505056fea26469706673582212203b3bc5791505a1493d2d8c6acd87f26438a740b84bb247353830397fa78e254264736f6c634300080b0033" |
| 39 | + }; |
| 40 | + |
| 41 | + public static final String SM_BINARY = |
| 42 | + org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", SM_BINARY_ARRAY); |
| 43 | + |
| 44 | + public static final String[] ABI_ARRAY = { |
| 45 | + "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"msg\",\"type\":\"string\"}],\"name\":\"incEvent\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"msg\",\"type\":\"string\"}],\"name\":\"inc\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]" |
| 46 | + }; |
| 47 | + |
| 48 | + public static final String ABI = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", ABI_ARRAY); |
| 49 | + |
| 50 | + public static final String FUNC_INC = "inc"; |
| 51 | + |
| 52 | + public static final String FUNC_VALUE = "value"; |
| 53 | + |
| 54 | + public static final Event INCEVENT_EVENT = |
| 55 | + new Event( |
| 56 | + "incEvent", |
| 57 | + Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));; |
| 58 | + |
| 59 | + protected Incremental(String contractAddress, Client client, CryptoKeyPair credential) { |
| 60 | + super(getBinary(client.getCryptoSuite()), contractAddress, client, credential); |
| 61 | + this.transactionManager = new ProxySignTransactionManager(client); |
| 62 | + } |
| 63 | + |
| 64 | + protected Incremental( |
| 65 | + String contractAddress, Client client, TransactionManager transactionManager) { |
| 66 | + super(getBinary(client.getCryptoSuite()), contractAddress, client, transactionManager); |
| 67 | + } |
| 68 | + |
| 69 | + public static String getBinary(CryptoSuite cryptoSuite) { |
| 70 | + return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY); |
| 71 | + } |
| 72 | + |
| 73 | + public static String getABI() { |
| 74 | + return ABI; |
| 75 | + } |
| 76 | + |
| 77 | + public List<IncEventEventResponse> getIncEventEvents(TransactionReceipt transactionReceipt) { |
| 78 | + List<EventValuesWithLog> valueList = |
| 79 | + extractEventParametersWithLog(INCEVENT_EVENT, transactionReceipt); |
| 80 | + ArrayList<IncEventEventResponse> responses = |
| 81 | + new ArrayList<IncEventEventResponse>(valueList.size()); |
| 82 | + for (EventValuesWithLog eventValues : valueList) { |
| 83 | + IncEventEventResponse typedResponse = new IncEventEventResponse(); |
| 84 | + typedResponse.log = eventValues.getLog(); |
| 85 | + typedResponse.msg = (String) eventValues.getNonIndexedValues().get(0).getValue(); |
| 86 | + responses.add(typedResponse); |
| 87 | + } |
| 88 | + return responses; |
| 89 | + } |
| 90 | + |
| 91 | + public void subscribeIncEventEvent( |
| 92 | + BigInteger fromBlock, |
| 93 | + BigInteger toBlock, |
| 94 | + List<String> otherTopics, |
| 95 | + EventSubCallback callback) { |
| 96 | + String topic0 = eventEncoder.encode(INCEVENT_EVENT); |
| 97 | + subscribeEvent(topic0, otherTopics, fromBlock, toBlock, callback); |
| 98 | + } |
| 99 | + |
| 100 | + public void subscribeIncEventEvent(EventSubCallback callback) { |
| 101 | + String topic0 = eventEncoder.encode(INCEVENT_EVENT); |
| 102 | + subscribeEvent(topic0, callback); |
| 103 | + } |
| 104 | + |
| 105 | + public TransactionReceipt inc(String msg) { |
| 106 | + final Function function = |
| 107 | + new Function( |
| 108 | + FUNC_INC, |
| 109 | + Arrays.<Type>asList(new Utf8String(msg)), |
| 110 | + Collections.<TypeReference<?>>emptyList(), |
| 111 | + 0); |
| 112 | + return executeTransaction(function); |
| 113 | + } |
| 114 | + |
| 115 | + public Function getMethodIncRawFunction(String msg) throws ContractException { |
| 116 | + final Function function = |
| 117 | + new Function( |
| 118 | + FUNC_INC, |
| 119 | + Arrays.<Type>asList(new Utf8String(msg)), |
| 120 | + Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
| 121 | + return function; |
| 122 | + } |
| 123 | + |
| 124 | + public FunctionWrapper buildMethodInc(String msg) throws ContractException { |
| 125 | + final Function function = |
| 126 | + new Function( |
| 127 | + FUNC_INC, |
| 128 | + Arrays.<Type>asList(new Utf8String(msg)), |
| 129 | + Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
| 130 | + return new FunctionWrapper(this, function); |
| 131 | + } |
| 132 | + |
| 133 | + public String getSignedTransactionForInc(String msg) { |
| 134 | + final Function function = |
| 135 | + new Function( |
| 136 | + FUNC_INC, |
| 137 | + Arrays.<Type>asList(new Utf8String(msg)), |
| 138 | + Collections.<TypeReference<?>>emptyList(), |
| 139 | + 0); |
| 140 | + return createSignedTransaction(function); |
| 141 | + } |
| 142 | + |
| 143 | + public String inc(String msg, TransactionCallback callback) { |
| 144 | + final Function function = |
| 145 | + new Function( |
| 146 | + FUNC_INC, |
| 147 | + Arrays.<Type>asList(new Utf8String(msg)), |
| 148 | + Collections.<TypeReference<?>>emptyList(), |
| 149 | + 0); |
| 150 | + return asyncExecuteTransaction(function, callback); |
| 151 | + } |
| 152 | + |
| 153 | + public Tuple1<String> getIncInput(TransactionReceipt transactionReceipt) { |
| 154 | + String data = transactionReceipt.getInput().substring(10); |
| 155 | + final Function function = |
| 156 | + new Function( |
| 157 | + FUNC_INC, |
| 158 | + Arrays.<Type>asList(), |
| 159 | + Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {})); |
| 160 | + List<Type> results = |
| 161 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 162 | + return new Tuple1<String>((String) results.get(0).getValue()); |
| 163 | + } |
| 164 | + |
| 165 | + public Tuple1<BigInteger> getIncOutput(TransactionReceipt transactionReceipt) { |
| 166 | + String data = transactionReceipt.getOutput(); |
| 167 | + final Function function = |
| 168 | + new Function( |
| 169 | + FUNC_INC, |
| 170 | + Arrays.<Type>asList(), |
| 171 | + Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
| 172 | + List<Type> results = |
| 173 | + this.functionReturnDecoder.decode(data, function.getOutputParameters()); |
| 174 | + return new Tuple1<BigInteger>((BigInteger) results.get(0).getValue()); |
| 175 | + } |
| 176 | + |
| 177 | + public BigInteger value() throws ContractException { |
| 178 | + final Function function = |
| 179 | + new Function( |
| 180 | + FUNC_VALUE, |
| 181 | + Arrays.<Type>asList(), |
| 182 | + Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
| 183 | + return executeCallWithSingleValueReturn(function, BigInteger.class); |
| 184 | + } |
| 185 | + |
| 186 | + public Function getMethodValueRawFunction() throws ContractException { |
| 187 | + final Function function = |
| 188 | + new Function( |
| 189 | + FUNC_VALUE, |
| 190 | + Arrays.<Type>asList(), |
| 191 | + Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); |
| 192 | + return function; |
| 193 | + } |
| 194 | + |
| 195 | + public static Incremental load( |
| 196 | + String contractAddress, Client client, TransactionManager transactionManager) { |
| 197 | + return new Incremental(contractAddress, client, transactionManager); |
| 198 | + } |
| 199 | + |
| 200 | + public static Incremental load(String contractAddress, Client client) { |
| 201 | + return new Incremental(contractAddress, client, new ProxySignTransactionManager(client)); |
| 202 | + } |
| 203 | + |
| 204 | + public static Incremental deploy(Client client, CryptoKeyPair credential) |
| 205 | + throws ContractException { |
| 206 | + Incremental contract = |
| 207 | + deploy( |
| 208 | + Incremental.class, |
| 209 | + client, |
| 210 | + credential, |
| 211 | + getBinary(client.getCryptoSuite()), |
| 212 | + getABI(), |
| 213 | + null, |
| 214 | + null); |
| 215 | + contract.setTransactionManager(new ProxySignTransactionManager(client)); |
| 216 | + return contract; |
| 217 | + } |
| 218 | + |
| 219 | + public static class IncEventEventResponse { |
| 220 | + public TransactionReceipt.Logs log; |
| 221 | + |
| 222 | + public String msg; |
| 223 | + } |
| 224 | +} |
0 commit comments