Skip to content

Commit

Permalink
Merge pull request #16 from perun-network/feat-sender-transactor
Browse files Browse the repository at this point in the history
Add Sender interface
  • Loading branch information
iljabvh authored May 29, 2024
2 parents 08aa9bf + c6f0b94 commit 94a49dd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion channel/test/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func InitTokenContract(kp *keypair.Full, contractIDAddress xdr.ScAddress) error

txMeta, err := cb.InvokeSignedTx("initialize", initArgs, contractIDAddress)
if err != nil {
return errors.New("error while invoking and processing host function: abort_funding")
return errors.New("error while invoking and processing host function: initialize" + err.Error())
}

_, err = event.DecodeEventsPerun(txMeta)
Expand Down
53 changes: 44 additions & 9 deletions client/contractbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"

"github.com/stellar/go/xdr"
"perun.network/go-perun/wallet"
"perun.network/perun-stellar-backend/wallet/types"
Expand All @@ -13,6 +15,10 @@ import (

const stellarDefaultChainId = 1

type Sender interface {
SignSendTx(txnbuild.Transaction) (xdr.TransactionMeta, error)
}

type ContractBackend struct {
Invoker
tr StellarSigner
Expand All @@ -22,7 +28,6 @@ type ContractBackend struct {

func NewContractBackend(trConfig *TransactorConfig) *ContractBackend {
transactor := NewTransactor(*trConfig)

return &ContractBackend{
tr: *transactor,
chainID: stellarDefaultChainId,
Expand All @@ -36,12 +41,14 @@ type StellarSigner struct {
participant *types.Participant
account *wallet.Account
hzClient *horizonclient.Client
sender Sender
}

type TransactorConfig struct {
keyPair *keypair.Full
participant *types.Participant
account *wallet.Account
sender Sender
}

func (tc *TransactorConfig) SetKeyPair(kp *keypair.Full) {
Expand All @@ -56,17 +63,32 @@ func (tc *TransactorConfig) SetAccount(account *wallet.Account) {
tc.account = account
}

func (tc *TransactorConfig) SetSender(sender Sender) {
tc.sender = sender
}

func NewTransactor(cfg TransactorConfig) *StellarSigner {
st := &StellarSigner{}

if cfg.sender != nil {
st.sender = cfg.sender
} else {
st.sender = &TxSender{}
}

if cfg.keyPair != nil {
st.keyPair = cfg.keyPair
if txSender, ok := st.sender.(*TxSender); ok {
txSender.kp = st.keyPair
}
}
if cfg.participant != nil {
st.participant = cfg.participant
}
if cfg.account != nil {
st.account = cfg.account
}

st.hzClient = NewHorizonClient()

return st
Expand Down Expand Up @@ -118,24 +140,37 @@ func (c *ContractBackend) InvokeSignedTx(fname string, callTxArgs xdr.ScVec, con

hzClient := c.tr.GetHorizonClient()

txSender, ok := c.tr.sender.(*TxSender)
if !ok {
return xdr.TransactionMeta{}, errors.New("sender is not of type *TxSender")
}

txSender.SetHzClient(hzClient)

invokeHostFunctionOp := BuildContractCallOp(hzAcc, fnameXdr, callTxArgs, contractAddr)
preFlightOp, minFee := PreflightHostFunctions(hzClient, &hzAcc, *invokeHostFunctionOp)

txParams := GetBaseTransactionParamsWithFee(&hzAcc, minFee, &preFlightOp)
txSigned, err := c.tr.createSignedTxFromParams(txParams)
if err != nil {
return xdr.TransactionMeta{}, err
}
tx, err := hzClient.SubmitTransaction(txSigned)
txUnsigned, err := txnbuild.NewTransaction(txParams)
if err != nil {
return xdr.TransactionMeta{}, err
}
// txSigned, err := c.tr.createSignedTxFromParams(txParams)
txMeta, err := c.tr.sender.SignSendTx(*txUnsigned)

txMeta, err := DecodeTxMeta(tx)
if err != nil {
return xdr.TransactionMeta{}, ErrCouldNotDecodeTxMeta
return xdr.TransactionMeta{}, err
}
_ = txMeta.V3.SorobanMeta.ReturnValue
// tx, err := hzClient.SubmitTransaction(txSigned)
// if err != nil {
// return xdr.TransactionMeta{}, err
// }

// txMeta, err := DecodeTxMeta(txSigned)
// if err != nil {
// return xdr.TransactionMeta{}, ErrCouldNotDecodeTxMeta
// }
// _ = txMeta.V3.SorobanMeta.ReturnValue

return txMeta, nil
}
36 changes: 36 additions & 0 deletions client/transactor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package client

import (
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
)

func CreateSignedTransactionWithParams(signers []*keypair.Full, txParams txnbuild.TransactionParams,
Expand All @@ -20,3 +22,37 @@ func CreateSignedTransactionWithParams(signers []*keypair.Full, txParams txnbuil
}
return tx, nil
}

type TxSender struct {
kp *keypair.Full
hzClient *horizonclient.Client
}

func NewSender(kp *keypair.Full) Sender {
return &TxSender{kp: kp}

}

func (s *TxSender) SignSendTx(txUnsigned txnbuild.Transaction) (xdr.TransactionMeta, error) {
tx, err := txUnsigned.Sign(NETWORK_PASSPHRASE, s.kp)
if err != nil {
return xdr.TransactionMeta{}, err

}

txSent, err := s.hzClient.SubmitTransaction(tx)
if err != nil {
return xdr.TransactionMeta{}, err
}
txMeta, err := DecodeTxMeta(txSent)
if err != nil {
return xdr.TransactionMeta{}, ErrCouldNotDecodeTxMeta
}
_ = txMeta.V3.SorobanMeta.ReturnValue
return txMeta, nil

}

func (s *TxSender) SetHzClient(hzClient *horizonclient.Client) {
s.hzClient = hzClient
}

0 comments on commit 94a49dd

Please sign in to comment.