Skip to content

Commit

Permalink
feat: rename variables of bad name (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjchen7 authored Aug 26, 2022
1 parent e2a51a4 commit 465555a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Maven
Gradle

```
implementation 'org.nervos.ckb-api:ckb:{version}'
implementation 'org.nervos.ckb:ckb-api:{version}'
```

## Build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@ private enum TransactionType {
CLAIM,
}

public DaoTransactionBuilder(Iterator<TransactionInput> availableInputs, Network network, OutPoint daoOutpoint, Api api) throws IOException {
public DaoTransactionBuilder(Iterator<TransactionInput> availableInputs, Network network, OutPoint daoOutPoint, Api api) throws IOException {
super(availableInputs, network);
builder = new CkbTransactionBuilder(availableInputs, network);
this.api = api;
CellInput cellInput = new CellInput(daoOutpoint, 0);
CellWithStatus cellWithStatus = api.getLiveCell(daoOutpoint, true);
CellInput cellInput = new CellInput(daoOutPoint, 0);
CellWithStatus cellWithStatus = api.getLiveCell(daoOutPoint, true);
TransactionInput input = new TransactionInput(
cellInput,
cellWithStatus.cell.output,
cellWithStatus.cell.data.content);
transactionType = getTransactionType(cellWithStatus.cell.data.content);
switch (transactionType) {
case WITHDRAW:
TransactionWithStatus txWithStatus = api.getTransaction(daoOutpoint.txHash);
TransactionWithStatus txWithStatus = api.getTransaction(daoOutPoint.txHash);
depositBlockNumber = api.getHeader(txWithStatus.txStatus.blockHash).number;
depositCellCapacity = txWithStatus.transaction.outputs.get(daoOutpoint.index).capacity;
depositCellCapacity = txWithStatus.transaction.outputs.get(daoOutPoint.index).capacity;
break;
case CLAIM:
builder.reward += getDaoReward(daoOutpoint);
builder.reward += getDaoReward(daoOutPoint);
break;
}
builder.transactionInputs.add(input);
}

private static TransactionType getTransactionType(byte[] outputData) {
if (outputData.length != 8) {
throw new IllegalArgumentException("Dao cell's length should be 8 bytes");
throw new IllegalArgumentException("Dao cell's output data length should be 8 bytes");
}
if (Arrays.equals(outputData, DEPOSIT_CELL_DATA)) {
return TransactionType.WITHDRAW;
Expand Down Expand Up @@ -98,10 +98,7 @@ private long getDaoReward(OutPoint withdrawOutpoint) throws IOException {
return daoReward;
}

private static long calculateDaoMaximumWithdraw(Header depositBlockHeader,
Header withdrawBlockHeader,
CellOutput output,
long occupiedCapacity) {
private static long calculateDaoMaximumWithdraw(Header depositBlockHeader, Header withdrawBlockHeader, CellOutput output, long occupiedCapacity) {
BigInteger depositAr = BigInteger.valueOf(extractAr(depositBlockHeader.dao));
BigInteger withdrawAr = BigInteger.valueOf(extractAr(withdrawBlockHeader.dao));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean buildTransaction(AbstractTransactionBuilder txBuilder, ScriptGrou
byte[] inputType = MoleculeConverter.packUint64(depositHeaderDepIndex).toByteArray();
txBuilder.setWitness(index, WitnessArgs.Type.INPUT_TYPE, inputType);
// update input since
txBuilder.setInputSince(index, info.calculateDaoMinimalSince());
txBuilder.setInputSince(index, info.calculateDaoMinimumSince());
} else if (context instanceof WithdrawInfo) {
WithdrawInfo info = (WithdrawInfo) context;
txBuilder.setHeaderDep(info.depositBlockHash);
Expand Down Expand Up @@ -112,12 +112,11 @@ public ClaimInfo(Api api, OutPoint withdrawOutpoint) {
}
}

public long calculateDaoMinimalSince() {
return calculateDaoMinimalSince(depositBlockHeader, withdrawBlockHeader);
public long calculateDaoMinimumSince() {
return calculateDaoMinimumSince(depositBlockHeader, withdrawBlockHeader);
}

private static long calculateDaoMinimalSince(Header depositBlockHeader,
Header withdrawBlockHeader) {
private static long calculateDaoMinimumSince(Header depositBlockHeader, Header withdrawBlockHeader) {
EpochUtils.EpochInfo depositEpoch = EpochUtils.parse(depositBlockHeader.epoch);
EpochUtils.EpochInfo withdrawEpoch = EpochUtils.parse(withdrawBlockHeader.epoch);

Expand All @@ -131,25 +130,25 @@ private static long calculateDaoMinimalSince(Header depositBlockHeader,
long lockEpochs = (depositedEpochs + (DAO_LOCK_PERIOD_EPOCHS - 1))
/ DAO_LOCK_PERIOD_EPOCHS
* DAO_LOCK_PERIOD_EPOCHS;
long minimalSinceEpochNumber = depositEpoch.number + lockEpochs;
long minimalSinceEpochIndex = depositEpoch.index;
long minimalSinceEpochLength = depositEpoch.length;
long minimalSince =
long minimumSinceEpochNumber = depositEpoch.number + lockEpochs;
long minimumSinceEpochIndex = depositEpoch.index;
long minimumSinceEpochLength = depositEpoch.length;
long minimumSince =
EpochUtils.generateSince(
minimalSinceEpochLength, minimalSinceEpochIndex, minimalSinceEpochNumber);
return minimalSince;
minimumSinceEpochLength, minimumSinceEpochIndex, minimumSinceEpochNumber);
return minimumSince;
}
}

public static class WithdrawInfo {
OutPoint depositOutpoint;
OutPoint depositOutPoint;
long depositBlockNumber;
byte[] depositBlockHash;

public WithdrawInfo(Api api, OutPoint depositOutpoint) {
this.depositOutpoint = depositOutpoint;
public WithdrawInfo(Api api, OutPoint depositOutPoint) {
this.depositOutPoint = depositOutPoint;
try {
TransactionWithStatus txWithStatus = api.getTransaction(depositOutpoint.txHash);
TransactionWithStatus txWithStatus = api.getTransaction(depositOutPoint.txHash);
depositBlockHash = txWithStatus.txStatus.blockHash;
depositBlockNumber = api.getHeader(depositBlockHash).number;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.nervos.ckb.transaction.DaoTransactionBuilder;
import org.nervos.ckb.transaction.scriptHandler.DaoScriptHandler;
import org.nervos.ckb.type.OutPoint;
import org.nervos.ckb.type.Script;
import org.nervos.ckb.type.TransactionInput;
import org.nervos.ckb.utils.Numeric;
import org.nervos.indexer.InputIterator;
Expand All @@ -16,24 +15,20 @@
import java.util.Iterator;

public class DaoWithdrawExample {
private static Script daoScript = new Script(Script.DAO_CODE_HASH,
new byte[0],
Script.HashType.TYPE);

public static void main(String[] args) throws IOException {
Network network = Network.TESTNET;
Api api = new Api("https://testnet.ckb.dev", false);
// the block number where the deposit dao transaction is
OutPoint depositOutpoint = new OutPoint(
OutPoint depositOutPoint = new OutPoint(
Numeric.hexStringToByteArray("0xc4662aa4a0c9087aa299121fef06dcc2dbf30271441a85fdf9d62fb312b259e6"), 0);
String sender = "ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r";

Iterator<TransactionInput> iterator = new InputIterator(sender);
TransactionWithScriptGroups txWithGroups = new DaoTransactionBuilder(iterator, network, depositOutpoint, api)
TransactionWithScriptGroups txWithGroups = new DaoTransactionBuilder(iterator, network, depositOutPoint, api)
.addWithdrawOutput(sender)
.setFeeRate(1000)
.setChangeOutput(sender)
.build(new DaoScriptHandler.WithdrawInfo(api, depositOutpoint));
.build(new DaoScriptHandler.WithdrawInfo(api, depositOutPoint));

// Sign transaction
TransactionSigner.getInstance(network)
Expand Down

0 comments on commit 465555a

Please sign in to comment.