-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished payment-channels, to do better testing+estimateFee;
fix run-method result parsing
- Loading branch information
Showing
16 changed files
with
759 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
smartcontract/src/main/java/org/ton/java/smartcontract/payments/FromWallet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.ton.java.smartcontract.payments; | ||
|
||
import org.ton.java.address.Address; | ||
import org.ton.java.cell.Cell; | ||
import org.ton.java.smartcontract.types.ChannelState; | ||
import org.ton.java.smartcontract.types.ExternalMessage; | ||
import org.ton.java.smartcontract.wallet.Options; | ||
import org.ton.java.smartcontract.wallet.WalletContract; | ||
import org.ton.java.tonlib.Tonlib; | ||
import org.ton.java.utils.Utils; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
|
||
public class FromWallet extends PaymentChannel { | ||
|
||
Options options; | ||
WalletContract wallet; | ||
byte[] secretKey; | ||
|
||
Tonlib tonlib; | ||
|
||
ExternalMessage extMsg; | ||
|
||
public FromWallet(Tonlib tonlib, WalletContract wallet, byte[] secretKey, Options options) { | ||
super(options); | ||
this.options = options; | ||
this.tonlib = tonlib; | ||
this.wallet = wallet; | ||
this.secretKey = secretKey; | ||
} | ||
|
||
public FromWallet deploy(BigInteger amount) { | ||
Utils.sleep(1, "deploying"); | ||
transfer(null, true, amount); | ||
return this; | ||
} | ||
|
||
public FromWallet topUp(BigInteger balanceA, BigInteger balanceB, BigInteger amount) { | ||
Utils.sleep(1, "topup"); | ||
transfer(this.createTopUpBalance(balanceA, balanceB), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet init(BigInteger balanceA, BigInteger balanceB, BigInteger amount) { | ||
Utils.sleep(1, "init"); | ||
transfer(this.createInitChannel(balanceA, balanceB).getCell(), amount); | ||
return this; | ||
} | ||
|
||
|
||
public FromWallet send() { // todo estimateFee | ||
Utils.sleep(1, "sending"); | ||
if (nonNull(extMsg)) { | ||
tonlib.sendRawMessage(extMsg.message.toBocBase64(false)); | ||
} else { | ||
throw new Error("cannot send empty external message"); | ||
} | ||
return this; | ||
} | ||
|
||
public FromWallet close(ChannelState channelState, byte[] hisSignature, BigInteger amount) { | ||
transfer(this.createCooperativeCloseChannel(hisSignature, channelState).getCell(), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet commit(byte[] hisSignature, BigInteger seqnoA, BigInteger seqnoB, BigInteger amount) { | ||
transfer(this.createCooperativeCommit(hisSignature, seqnoA, seqnoB).getCell(), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet startUncooperativeClose(Cell signedSemiChannelStateA, Cell signedSemiChannelStateB, BigInteger amount) { | ||
transfer(this.createStartUncooperativeClose(signedSemiChannelStateA, signedSemiChannelStateB).getCell(), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet challengeQuarantinedState(Cell signedSemiChannelStateA, Cell signedSemiChannelStateB, BigInteger amount) { | ||
transfer(this.createChallengeQuarantinedState(signedSemiChannelStateA, signedSemiChannelStateB).getCell(), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet settleConditionals(Cell conditionalsToSettle, BigInteger amount) { | ||
transfer(this.createSettleConditionals(conditionalsToSettle).getCell(), amount); | ||
return this; | ||
} | ||
|
||
public FromWallet finishUncooperativeClose(BigInteger amount) { | ||
transfer(this.createFinishUncooperativeClose(), amount); | ||
return this; | ||
} | ||
|
||
private void transfer(Cell payload, boolean needStateInit, BigInteger amount) { | ||
extMsg = createExtMsg(payload, needStateInit, amount); | ||
} | ||
|
||
private void transfer(Cell payload, BigInteger amount) { | ||
extMsg = createExtMsg(payload, false, amount); | ||
} | ||
|
||
public ExternalMessage createExtMsg(Cell payload, boolean needStateInit, BigInteger amount) { | ||
|
||
Cell stateInit = needStateInit ? (this.createStateInit()).stateInit : null; | ||
Address myAddress = this.getAddress(); | ||
long seqno = wallet.getSeqno(tonlib); | ||
|
||
return wallet.createTransferMessage( | ||
secretKey, | ||
myAddress.toString(true, true, true), //to payment channel | ||
amount, | ||
seqno, | ||
payload, // body | ||
(byte) 3, | ||
false, | ||
stateInit); | ||
} | ||
} |
Oops, something went wrong.