Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination for 'getExpiredSwaps' #871

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.apollocurrency.aplwallet.apl.dex.core.model.SwapDataInfo;
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthDepositInfo;
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthDepositsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.model.ExpiredSwapsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.service.EthereumWalletService;
import com.apollocurrency.aplwallet.apl.exchange.util.DexCurrencyValidator;
import com.apollocurrency.aplwallet.apl.util.Convert2;
Expand Down Expand Up @@ -843,11 +844,17 @@ public void refundExpiredAtomicSwaps(long accountId) {
List<String> addresses = kmsService.getEthWalletAddresses(accountId, passphrase);
for (String address : addresses) {
try {
List<ExpiredSwap> expiredSwaps = dexSmartContractService.getExpiredSwaps(address);
for (ExpiredSwap expiredSwap : expiredSwaps) {
log.info("Refunding atomic swap {}, id {}", Numeric.toHexString(expiredSwap.getSecretHash()), expiredSwap.getOrderId());
dexSmartContractService.refundAndWithdraw(expiredSwap.getSecretHash(), passphrase, address, accountId, false);
}
long offset = 0;
ExpiredSwapsWithOffset swapsWithOffset;
do {
swapsWithOffset = dexSmartContractService.getExpiredSwaps(address, offset, CONTRACT_FETCH_SIZE);
offset = swapsWithOffset.getOffset();
for (ExpiredSwap expiredSwap : swapsWithOffset.getSwaps()) {
log.info("Refunding atomic swap {}, id {}", Numeric.toHexString(expiredSwap.getSecretHash()), expiredSwap.getOrderId());
dexSmartContractService.refundAndWithdraw(expiredSwap.getSecretHash(), passphrase, address, accountId, false);
}
} while (swapsWithOffset.getSwaps().size() == CONTRACT_FETCH_SIZE);

} catch (AplException.ExecutiveProcessException e) {
log.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthDepositInfo;
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthDepositsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthWalletBalanceInfo;
import com.apollocurrency.aplwallet.apl.dex.eth.model.ExpiredSwapsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.service.EthereumWalletService;
import com.apollocurrency.aplwallet.apl.dex.eth.utils.EthUtil;
import com.apollocurrency.aplwallet.apl.exchange.dao.DexContractDao;
Expand Down Expand Up @@ -1052,11 +1053,13 @@ public List<AddressEthDepositsInfo> getAllFilledOrders() throws AplException.Exe
public List<AddressEthExpiredSwaps> getAllExpiredSwaps() throws AplException.ExecutiveProcessException {
List<AddressEthExpiredSwaps> addressEthExpiredSwaps = new ArrayList<>();
List<String> addresses = getAllUsers();
int offset = 0;
int limit = 100;

for (String address : addresses) {
try {
List<ExpiredSwap> expiredSwaps = dexSmartContractService.getExpiredSwaps(address);
addressEthExpiredSwaps.add(new AddressEthExpiredSwaps(address, expiredSwaps));
ExpiredSwapsWithOffset expiredSwaps = dexSmartContractService.getExpiredSwaps(address, offset, limit);
addressEthExpiredSwaps.add(new AddressEthExpiredSwaps(address, expiredSwaps.getSwaps()));
} catch (Exception ex) {
log.warn(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import com.apollocurrency.aplwallet.apl.dex.core.model.DepositedOrderDetails;
import com.apollocurrency.aplwallet.apl.dex.core.model.DexCurrency;
import com.apollocurrency.aplwallet.apl.dex.core.model.DexTransaction;
import com.apollocurrency.aplwallet.apl.dex.core.model.ExpiredSwap;
import com.apollocurrency.aplwallet.apl.dex.core.model.OrderType;
import com.apollocurrency.aplwallet.apl.dex.core.model.SwapDataInfo;
import com.apollocurrency.aplwallet.apl.dex.core.model.UserAddressesWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.contracts.DexContract;
import com.apollocurrency.aplwallet.apl.dex.eth.contracts.DexContractImpl;
import com.apollocurrency.aplwallet.apl.dex.eth.model.EthDepositsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.model.ExpiredSwapsWithOffset;
import com.apollocurrency.aplwallet.apl.dex.eth.service.DexBeanProducer;
import com.apollocurrency.aplwallet.apl.dex.eth.service.DexEthService;
import com.apollocurrency.aplwallet.apl.dex.eth.service.EthereumWalletService;
Expand Down Expand Up @@ -278,10 +278,10 @@ public EthDepositsWithOffset getUserFilledOrders(String user, long offset, long
}
}

public List<ExpiredSwap> getExpiredSwaps(String user) throws AplException.ExecutiveProcessException {
public ExpiredSwapsWithOffset getExpiredSwaps(String user, long offset, long limit) throws AplException.ExecutiveProcessException {
DexContract dexContract = new DexContractImpl(smartContractAddress, dexBeanProducer.web3j(), Credentials.create(ACCOUNT_TO_READ_DATA), null);
try {
return ExpiredSwapMapper.map(dexContract.getExpiredSwaps(user).sendAsync().get());
return ExpiredSwapMapper.map(dexContract.getExpiredSwaps(user, offset, limit).sendAsync().get());
} catch (Exception e) {
throw new AplException.ExecutiveProcessException(e.getMessage());
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package com.apollocurrency.aplwallet.apl.dex.core.mapper;

import com.apollocurrency.aplwallet.apl.dex.core.model.ExpiredSwap;
import com.apollocurrency.aplwallet.apl.dex.eth.model.ExpiredSwapsWithOffset;
import com.apollocurrency.aplwallet.apl.util.AplCollectionUtils;
import org.web3j.tuples.generated.Tuple2;
import org.web3j.tuples.generated.Tuple3;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class ExpiredSwapMapper {
public static List<ExpiredSwap> map(Tuple2<List<BigInteger>, List<byte[]>> data) {
List<ExpiredSwap> swaps = new ArrayList<>();
public static ExpiredSwapsWithOffset map(Tuple3<List<BigInteger>, List<byte[]>, BigInteger> data) {
ExpiredSwapsWithOffset swapsWithOffset = new ExpiredSwapsWithOffset();

if (data == null || AplCollectionUtils.isEmpty(data.component1())) {
return swaps;
return swapsWithOffset;
}

for (int i = 0; i < data.component1().size(); i++) {
swaps.add(new ExpiredSwap(Long.parseUnsignedLong(
swapsWithOffset.getSwaps().add(new ExpiredSwap(Long.parseUnsignedLong(
data.component1().get(i).toString()),
(data.component2().get(i))
));
}
return swaps;
swapsWithOffset.setOffset(data.component3().longValue());
return swapsWithOffset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -710,19 +710,24 @@ public AssetWithdrawalEventResponse apply(Log log) {
});
}

public RemoteCall<Tuple2<List<BigInteger>, List<byte[]>>> getExpiredSwaps(String user) {
public RemoteCall<Tuple3<List<BigInteger>, List<byte[]>, BigInteger>> getExpiredSwaps(String user, long offset, long limit) {
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC_GETEXPIREDSWAPS,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(user)),
Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Uint256>>() {
}, new TypeReference<DynamicArray<Bytes32>>() {
}));
return new RemoteCall<>(
() -> {
List<Type> results = executeCallMultipleValueReturn(function);
return new Tuple2<>(
convertToNative((List<Uint256>) results.get(0).getValue()),
convertToNative((List<Bytes32>) results.get(1).getValue()));
});
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(user), new Uint256(offset), new Uint256(limit)),
Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Uint256>>() {
}, new TypeReference<DynamicArray<Bytes32>>() {
}, new TypeReference<Uint256>() {}));
return new RemoteCall<Tuple3<List<BigInteger>, List<byte[]>, BigInteger>>(
new Callable<Tuple3<List<BigInteger>, List<byte[]>, BigInteger>>() {
@Override
public Tuple3<List<BigInteger>, List<byte[]>, BigInteger> call() throws Exception {
List<Type> results = executeCallMultipleValueReturn(function);
return new Tuple3<List<BigInteger>, List<byte[]>, BigInteger>(
convertToNative((List<Uint256>) results.get(0).getValue()),
convertToNative((List<Bytes32>) results.get(1).getValue()),
(BigInteger) results.get(2).getValue()
);
}
});
}

public RemoteCall<String> getUserById(BigInteger userId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.apollocurrency.aplwallet.apl.dex.eth.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EthDepositsWithOffset {
public class EthDepositsWithOffset extends OffsetModel {
private List<EthDepositInfo> deposits = new ArrayList<>();
private long offset;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.apollocurrency.aplwallet.apl.dex.eth.model;

import com.apollocurrency.aplwallet.apl.dex.core.model.ExpiredSwap;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class ExpiredSwapsWithOffset extends OffsetModel {
List<ExpiredSwap> swaps = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.apollocurrency.aplwallet.apl.dex.eth.model;

import lombok.Data;

@Data
public abstract class OffsetModel {
private long offset;
}