Skip to content

Commit

Permalink
feat: support polka estimate fee.
Browse files Browse the repository at this point in the history
  • Loading branch information
iosguang committed Apr 13, 2022
1 parent 722ddc6 commit fa2788d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
9 changes: 9 additions & 0 deletions core/btc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,12 @@ func TestBatchTransactionStatus(t *testing.T) {
}
t.Log(statuses)
}

func TestSbtcDetail(t *testing.T) {
hashString := "efb7849f8f5a76da41faaa100977d189b025f1d01dee0fade87ffca4515af23a"
detail, err := FetchTransactionDetail(hashString, chainSignet)
if err != nil {
t.Fatal(err)
}
t.Log(detail)
}
43 changes: 43 additions & 0 deletions core/wallet/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,49 @@ func (c *PolkaChain) QueryBalanceXBTC(address string) (b *PolkaBalance, err erro
}, nil
}

// 查询交易的预估手续费
// @param transaction 交易的构造对象
func (c *PolkaChain) EstimateFeeForTransaction(transaction *Transaction, ss58 int) (s string, err error) {
s = "0"
wallet := mockWallet()
mockAddress, err := wallet.GetAddress(ss58)
if err != nil {
return
}

signData, err := transaction.GetSignDataFromChain(c, mockAddress)
if err != nil {
return
}
signature, err := wallet.Sign(signData, "")
if err != nil {
return
}
pubkey, err := wallet.GetPublicKey()
if err != nil {
return
}
sendTx, err := transaction.GetTx(pubkey, signature)
if err != nil {
return
}

cl, err := getPolkaClient(c.RpcUrl)

data := make(map[string]interface{})
err = client.CallWithBlockHash(cl.api.Client, &data, "payment_queryInfo", nil, sendTx)
if err != nil {
return
}

estimateFee, ok := data["partialFee"].(string)
if !ok {
return s, errors.New("get estimated fee result nil")
}

return estimateFee, nil
}

// 发起交易
func (c *PolkaChain) SendRawTransaction(txHex string) (s string, err error) {
client, err := getPolkaClient(c.RpcUrl)
Expand Down
22 changes: 21 additions & 1 deletion core/wallet/chain_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wallet

import "testing"
import (
"testing"
)

func TestQueryBalance(t *testing.T) {
rpcUrl := "wss://testnet3.chainx.org"
Expand Down Expand Up @@ -120,3 +122,21 @@ func TestMINIScriptHash(t *testing.T) {

t.Log(scriptHash)
}

func TestEstimatedFee(t *testing.T) {
// rpcUrl := "https://rpc-minichain.coming.chat"
rpcUrl := "https://mainnet.chainx.org/rpc"
address := "5RNt3DACYRhwHyy9esTZXVvffkFL3pQHv4qoEMFVfDqeDEnH"
amount := "10000"

chain := NewPolkaChain(rpcUrl, "")
metadata, _ := chain.GetMetadataString()
tx, _ := NewTx(metadata)
transaction, _ := tx.NewBalanceTransferTx(address, amount)

fee, err := chain.EstimateFeeForTransaction(transaction, 44)
if err != nil {
t.Fatal(err)
}
t.Log(fee)
}
9 changes: 9 additions & 0 deletions core/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wallet
import (
"encoding/json"
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/vedhavyas/go-subkey"
Expand Down Expand Up @@ -128,3 +129,11 @@ func (w *Wallet) GetPrivateKeyHex() (string, error) {
}
return types.HexEncodeToString(kyr.Seed()), nil
}

// 内置账号,主要用来给用户未签名的交易签一下名
// 然后给用户去链上查询手续费,保护用户资产安全
func mockWallet() *Wallet {
mnemonic := "infant carbon above canyon corn collect finger drip area feature mule autumn"
w, _ := NewWallet(mnemonic)
return w
}

0 comments on commit fa2788d

Please sign in to comment.