Search in sources :

Example 1 with DefaultBlockParameter

use of org.web3j.protocol.core.DefaultBlockParameter in project Ethernity-Wallet-Android by kris-krytech.

the class ERC721Token method updateBalance.

/**
 * Uses both events and balance call. Each call to updateBalance uses 3 Node calls:
 * 1. Get new Transfer events since last call
 * 2.
 * <p>
 * Once we have the current balance for potential tokens the database is updated to reflect the current status
 * <p>
 * Note that this function is used even for contracts covered by OpenSea: This is because we could be looking at
 * a contract 'join' between successive opensea reads. With accounts with huge quantity of NFT, this happens a lot
 *
 * @param realm
 * @return
 */
@Override
public BigDecimal updateBalance(Realm realm) {
    // first get current block
    SyncDef sync = eventSync.getSyncDef(realm);
    if (sync == null)
        return balance;
    DefaultBlockParameter startBlock = DefaultBlockParameter.valueOf(sync.eventReadStartBlock);
    DefaultBlockParameter endBlock = DefaultBlockParameter.valueOf(sync.eventReadEndBlock);
    if (sync.eventReadEndBlock.compareTo(BigInteger.valueOf(-1L)) == 0)
        endBlock = DefaultBlockParameterName.LATEST;
    // take a note of the current block#
    BigInteger currentBlock = TransactionsService.getCurrentBlock(tokenInfo.chainId);
    try {
        final Web3j web3j = TokenRepository.getWeb3jService(tokenInfo.chainId);
        Pair<Integer, HashSet<BigInteger>> evRead = eventSync.processTransferEvents(web3j, getTransferEvents(), startBlock, endBlock, realm);
        // means our event read was fine
        eventSync.updateEventReads(realm, sync, currentBlock, evRead.first);
        HashSet<BigInteger> tokenIdsHeld = checkBalances(web3j, evRead.second);
        // should we check existing assets too?
        // add to realm
        updateRealmBalance(realm, tokenIdsHeld);
        return new BigDecimal(tokenIdsHeld.size());
    } catch (LogOverflowException e) {
        // handle log read overflow; reduce search size
        if (eventSync.handleEthLogError(e.error, startBlock, endBlock, sync, realm)) {
            // recurse until we find a good value
            updateBalance(realm);
        }
    } catch (Exception e) {
        Timber.w(e);
    }
    return balance;
}
Also used : BigInteger(java.math.BigInteger) SyncDef(com.alphawallet.app.entity.SyncDef) DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) Web3j(org.web3j.protocol.Web3j) LogOverflowException(com.alphawallet.app.entity.LogOverflowException) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) LogOverflowException(com.alphawallet.app.entity.LogOverflowException) HashSet(java.util.HashSet)

Example 2 with DefaultBlockParameter

use of org.web3j.protocol.core.DefaultBlockParameter in project Ethernity-Wallet-Android by kris-krytech.

the class SignCallbackJSInterface method ethCall.

@JavascriptInterface
public void ethCall(int callbackId, String recipient) {
    try {
        JSONObject json = new JSONObject(recipient);
        DefaultBlockParameter defaultBlockParameter;
        String to = json.has("to") ? json.getString("to") : ZERO_ADDRESS;
        String payload = json.has("data") ? json.getString("data") : "0x";
        String value = json.has("value") ? json.getString("value") : null;
        String gasLimit = json.has("gas") ? json.getString("gas") : null;
        // TODO: Take block param from query if present
        defaultBlockParameter = DefaultBlockParameterName.LATEST;
        Web3Call call = new Web3Call(new Address(to), defaultBlockParameter, payload, value, gasLimit, callbackId);
        webView.post(() -> onEthCallListener.onEthCall(call));
    } catch (Exception e) {
    // 
    }
}
Also used : DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) JSONObject(org.json.JSONObject) Address(com.alphawallet.app.web3.entity.Address) Web3Call(com.alphawallet.app.web3.entity.Web3Call) JsonSyntaxException(com.google.gson.JsonSyntaxException) JavascriptInterface(android.webkit.JavascriptInterface)

Example 3 with DefaultBlockParameter

use of org.web3j.protocol.core.DefaultBlockParameter in project besu by hyperledger.

the class ContractOperations method getCode.

public static String getCode(final Quorum web3j, final String contractAddress) throws IOException {
    final DefaultBlockParameter blockParam = DefaultBlockParameter.valueOf(DefaultBlockParameterName.LATEST.toString());
    final EthGetCode codeResult = web3j.ethGetCode(contractAddress, blockParam).send();
    assertThat(codeResult.getCode()).withFailMessage("Code for contractAddress not found.").isNotEmpty();
    return codeResult.getCode();
}
Also used : DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) EthGetCode(org.web3j.protocol.core.methods.response.EthGetCode)

Example 4 with DefaultBlockParameter

use of org.web3j.protocol.core.DefaultBlockParameter in project web3j by web3j.

the class RequestTest method testParityListAccountsWithAccountOffsetWithBlockTag.

@Test
public void testParityListAccountsWithAccountOffsetWithBlockTag() throws Exception {
    BigInteger maxQuantityReturned = BigInteger.valueOf(100);
    DefaultBlockParameter blockParameter = DefaultBlockParameterName.LATEST;
    web3j.parityListAccounts(maxQuantityReturned, "0x407d73d8a49eeb85d32cf465507dd71d507100c1", blockParameter).send();
    verifyResult("{\"jsonrpc\":\"2.0\",\"method\":\"parity_listAccounts\"," + "\"params\":[100,\"0x407d73d8a49eeb85d32cf465507dd71d507100c1\",\"latest\"],\"id\":1}");
}
Also used : DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) BigInteger(java.math.BigInteger) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultBlockParameter

use of org.web3j.protocol.core.DefaultBlockParameter in project alpha-wallet-android by AlphaWallet.

the class ERC1155Token method updateBalance.

/**
 * Uses both events and balance call. Each call to updateBalance uses 3 Node calls:
 * 1. Get new TransferSingle events since last call
 * 2. Get new TransferBatch events since last call
 * 3. Call ERC1155 contract function balanceOfBatch on all tokenIds
 *
 * Once we have the current balance for potential tokens the database is updated to reflect the current status
 *
 * Note that this function is used even for contracts covered by OpenSea: This is because we could be looking at
 * a contract 'join' between successive opensea reads. With accounts with huge quantity of NFT, this happens a lot
 *
 * @param realm
 * @return
 */
@Override
public BigDecimal updateBalance(Realm realm) {
    SyncDef sync = eventSync.getSyncDef(realm);
    if (sync == null)
        return balance;
    DefaultBlockParameter startBlock = DefaultBlockParameter.valueOf(sync.eventReadStartBlock);
    DefaultBlockParameter endBlock = DefaultBlockParameter.valueOf(sync.eventReadEndBlock);
    if (sync.eventReadEndBlock.compareTo(BigInteger.valueOf(-1L)) == 0)
        endBlock = DefaultBlockParameterName.LATEST;
    // take a note of the current block#
    BigInteger currentBlock = TransactionsService.getCurrentBlock(tokenInfo.chainId);
    try {
        final Web3j web3j = TokenRepository.getWeb3jService(tokenInfo.chainId);
        Pair<Integer, Pair<HashSet<BigInteger>, HashSet<BigInteger>>> evRead = eventSync.processTransferEvents(web3j, getBalanceUpdateEvents(), startBlock, endBlock, realm);
        Pair<Integer, Pair<HashSet<BigInteger>, HashSet<BigInteger>>> batchRead = eventSync.processTransferEvents(web3j, getBatchBalanceUpdateEvents(), startBlock, endBlock, realm);
        // All tokenIds which have passed through the owner address
        evRead.second.first.addAll(evRead.second.second);
        evRead.second.first.addAll(batchRead.second.first);
        evRead.second.first.addAll(batchRead.second.second);
        // combine the tokenIds with existing assets
        evRead.second.first.addAll(assets.keySet());
        // update balances of all
        List<Uint256> balances = fetchBalances(evRead.second.first);
        // update realm
        updateRealmBalance(realm, evRead.second.first, balances);
        // update read points
        // means our event read was fine
        eventSync.updateEventReads(realm, sync, currentBlock, evRead.first);
    } catch (LogOverflowException e) {
        // handle log read overflow; reduce search size
        if (eventSync.handleEthLogError(e.error, startBlock, endBlock, sync, realm)) {
            // recurse until we find a good value
            updateBalance(realm);
        }
    } catch (Exception e) {
        Timber.e(e);
    }
    return new BigDecimal(assets.keySet().size());
}
Also used : BigInteger(java.math.BigInteger) SyncDef(com.alphawallet.app.entity.SyncDef) DefaultBlockParameter(org.web3j.protocol.core.DefaultBlockParameter) Web3j(org.web3j.protocol.Web3j) LogOverflowException(com.alphawallet.app.entity.LogOverflowException) BigInteger(java.math.BigInteger) Uint256(org.web3j.abi.datatypes.generated.Uint256) LogOverflowException(com.alphawallet.app.entity.LogOverflowException) BigDecimal(java.math.BigDecimal) Pair(android.util.Pair)

Aggregations

DefaultBlockParameter (org.web3j.protocol.core.DefaultBlockParameter)18 BigInteger (java.math.BigInteger)7 Web3j (org.web3j.protocol.Web3j)5 LogOverflowException (com.alphawallet.app.entity.LogOverflowException)4 SyncDef (com.alphawallet.app.entity.SyncDef)4 Test (org.junit.jupiter.api.Test)4 EthFilter (org.web3j.protocol.core.methods.request.EthFilter)4 BigDecimal (java.math.BigDecimal)3 HashSet (java.util.HashSet)3 Pair (android.util.Pair)2 JavascriptInterface (android.webkit.JavascriptInterface)2 Address (com.alphawallet.app.web3.entity.Address)2 Web3Call (com.alphawallet.app.web3.entity.Web3Call)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 JSONObject (org.json.JSONObject)2 Event (org.web3j.abi.datatypes.Event)2 Uint (org.web3j.abi.datatypes.Uint)2 Utf8String (org.web3j.abi.datatypes.Utf8String)2