Skip to content

Commit

Permalink
feat(client): Invoke get_channel function in the contract without sen…
Browse files Browse the repository at this point in the history
…ding a signed transaction - decode the result of the simulated transaction result
  • Loading branch information
iljabvh committed Jul 3, 2024
1 parent 8ccdaec commit 61837f2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
11 changes: 2 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,12 @@ func (cb *ContractBackend) GetChannelInfo(ctx context.Context, perunAddr xdr.ScA
if err != nil {
return wire.Channel{}, errors.New("error while building get_channel tx")
}
txMeta, err := cb.InvokeSignedTx("get_channel", getchTxArgs, perunAddr)
chanInfo, err := cb.InvokeUnsignedTx("get_channel", getchTxArgs, perunAddr)
if err != nil {
return wire.Channel{}, errors.New("error while processing and submitting get_channel tx")
}

retVal := txMeta.V3.SorobanMeta.ReturnValue
var getChan wire.Channel

err = getChan.FromScVal(retVal)
if err != nil {
return wire.Channel{}, errors.New("error while decoding return value")
}
return getChan, nil
return chanInfo, nil
}

func (cb *ContractBackend) GetBalanceUser(cID xdr.ScAddress) (string, error) {
Expand Down
25 changes: 25 additions & 0 deletions client/contractbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stellar/go/xdr"
"perun.network/go-perun/wallet"
"perun.network/perun-stellar-backend/wallet/types"
"perun.network/perun-stellar-backend/wire"
"sync"
)

Expand Down Expand Up @@ -129,6 +130,30 @@ func (st *StellarSigner) GetHorizonClient() *horizonclient.Client {
return st.hzClient
}

func (c *ContractBackend) InvokeUnsignedTx(fname string, callTxArgs xdr.ScVec, contractAddr xdr.ScAddress) (wire.Channel, error) { //xdr.TransactionMeta, error
c.cbMutex.Lock()
defer c.cbMutex.Unlock()
fnameXdr := xdr.ScSymbol(fname)
hzAcc, err := c.tr.GetHorizonAccount()
if err != nil {
return wire.Channel{}, err
}

hzClient := c.tr.GetHorizonClient()

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

txSender.SetHzClient(hzClient)

invokeHostFunctionOp := BuildContractCallOp(hzAcc, fnameXdr, callTxArgs, contractAddr)
chanInfo, _, _ := PreflightHostFunctionsResult(hzClient, &hzAcc, *invokeHostFunctionOp)

return chanInfo, nil
}

func (c *ContractBackend) InvokeSignedTx(fname string, callTxArgs xdr.ScVec, contractAddr xdr.ScAddress) (xdr.TransactionMeta, error) {
c.cbMutex.Lock()
defer c.cbMutex.Unlock()
Expand Down
38 changes: 38 additions & 0 deletions client/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"perun.network/perun-stellar-backend/wire"
"strconv"
"time"
)
Expand Down Expand Up @@ -96,6 +97,43 @@ func PreflightHostFunctions(hzClient *horizonclient.Client,
return function, result.MinResourceFee
}

func PreflightHostFunctionsResult(hzClient *horizonclient.Client,
sourceAccount txnbuild.Account, function txnbuild.InvokeHostFunction,
) (wire.Channel, txnbuild.InvokeHostFunction, int64) {
result, transactionData := simulateTransaction(hzClient, sourceAccount, &function)

function.Ext = xdr.TransactionExt{
V: 1,
SorobanData: &transactionData,
}
var getChan wire.Channel

if len(result.Results) != 1 {
panic("expected one result")
}

var decodedXdr xdr.ScVal
err := xdr.SafeUnmarshalBase64(result.Results[0].XDR, &decodedXdr)
if err != nil {
panic(err)
}

decChanInfo := decodedXdr

if decChanInfo.Type != xdr.ScValTypeScvMap {
return getChan, function, result.MinResourceFee

}

err = getChan.FromScVal(decChanInfo)
if err != nil {

panic(err)
}

return getChan, function, result.MinResourceFee
}

func simulateTransaction(hzClient *horizonclient.Client,
sourceAccount txnbuild.Account, op txnbuild.Operation,
) (RPCSimulateTxResponse, xdr.SorobanTransactionData) {
Expand Down

0 comments on commit 61837f2

Please sign in to comment.