Skip to content

Commit

Permalink
update brc20 mint & transfer request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Jul 2, 2024
1 parent cbf1236 commit c320810
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 33 deletions.
57 changes: 42 additions & 15 deletions core/btc/brc20_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/coming-chat/wallet-SDK/core/base"
"github.com/coming-chat/wallet-SDK/pkg/httpUtil"
)

type Brc20MintTransaction struct {
CommitId string `json:"commit_id"`
Commit string `json:"commit"`
Reveal *base.StringArray `json:"reveal"`
Inscription *base.StringArray `json:"inscription"`
Expand Down Expand Up @@ -132,22 +134,20 @@ func (c *Chain) BuildBrc20MintWithPostage(sender, receiver string, op, ticker, a
if err != nil {
return
}
url := fmt.Sprintf("%v/mintWithPostage", host)
url := fmt.Sprintf("%v/MintInscription", host)
header := map[string]string{"Content-Type": "application/json"}
requestBody := map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "mintWithPostage",
"params": map[string]any{
"source": sender,
"fee_rate": feeRate,
"content": fmt.Sprintf(`{"p":"brc-20","op":"%v","tick":"%v","amt":"%v"}`, op, ticker, amount),
"destination": receiver,
"extension": ".txt",
"repeat": 1,

"target_postage": postage,
"commit_fee_rate": strconv.FormatInt(feeRate, 10),
"reveal_fee_rate": strconv.FormatInt(feeRate, 10),
"source": sender,
"destination": receiver,
"inscriptions": []string{
fmt.Sprintf(`{"p":"brc-20","op":"%v","tick":"%v","amt":"%v"}`, op, ticker, amount),
},
"repeats": []int{
1,
},
"postage": postage,
}
requestBytes, _ := json.Marshal(requestBody)
resp, err := httpUtil.Request(http.MethodPost, url, header, requestBytes)
Expand All @@ -158,9 +158,36 @@ func (c *Chain) BuildBrc20MintWithPostage(sender, receiver string, op, ticker, a
return nil, fmt.Errorf("code: %d, body: %s", resp.Code, string(resp.Body))
}

err = json.Unmarshal(resp.Body, &txn)
var r struct {
CommitId string `json:"commit_id"`
CommitPsbt string `json:"commit_psbt"`
RevealTxs *base.StringArray `json:"reveal_txs"`
RevealIds *base.StringArray `json:"reveal_ids"`

CommitCustom *Brc20CommitCustom `json:"commit_custom"`

NetworkFee int64 `json:"network_fee"`
SatpointFee int64 `json:"satpoint_fee"`
ServiceFee int64 `json:"service_fee"`
CommitFee int64 `json:"commit_fee"`
CommitVsize int64 `json:"commit_vsize"`
}
err = json.Unmarshal(resp.Body, &r)
if err != nil {
return nil, err
}
return txn, nil
return &Brc20MintTransaction{
CommitId: r.CommitId,
Commit: r.CommitPsbt,
Reveal: r.RevealTxs,
Inscription: r.RevealIds,

CommitCustom: r.CommitCustom,

NetworkFee: r.NetworkFee,
SatpointFee: r.SatpointFee,
ServiceFee: r.ServiceFee,
CommitFee: r.CommitFee,
CommitVsize: r.CommitVsize,
}, nil
}
49 changes: 33 additions & 16 deletions core/btc/brc20_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/coming-chat/wallet-SDK/core/base"
"github.com/coming-chat/wallet-SDK/pkg/httpUtil"
Expand All @@ -13,6 +15,9 @@ import (
type Brc20TransferTransaction struct {
Transaction string `json:"transaction"`
NetworkFee int64 `json:"network_fee"`
CommitId string `json:"commit_id"`
CommitFee int64 `json:"commit_fee"`
CommitVsize int64 `json:"commit_vsize"`

CommitCustom *Brc20CommitCustom `json:"commit_custom"`
}
Expand Down Expand Up @@ -48,27 +53,24 @@ func (c *Chain) BuildBrc20TransferTransaction(
if inscriptionIds == nil || inscriptionIds.Count() <= 0 {
return nil, errors.New("no inscriptions")
}
insIds := inscriptionIds.AnyArray
insIds := inscriptionIds.AnyArray[:]
for idx, id := range insIds {
insIds[idx] = strings.Replace(id, "i", ":", 1)
}

host, err := comingOrdHost(c.Chainnet)
if err != nil {
return
}
url := fmt.Sprintf("%v/transfer", host)
url := fmt.Sprintf("%v/TransferOutpoints", host)
header := map[string]string{"Content-Type": "application/json"}
requestBody := map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "transfer",
"params": map[string]any{
"source": sender,
"destination": receiver,
"fee_rate": feeRate,
"op_return": opReturn,
"outgoing": insIds[0],
"addition_outgoing": insIds[1:],
"brc20_transfer": true,
},
"commit_fee_rate": strconv.FormatInt(feeRate, 10),
"source": sender,
"destination": receiver,
"outpoint": insIds,
"postage": 546,
"op_return": opReturn,
}
requestBytes, _ := json.Marshal(requestBody)
resp, err := httpUtil.Request(http.MethodPost, url, header, requestBytes)
Expand All @@ -79,9 +81,24 @@ func (c *Chain) BuildBrc20TransferTransaction(
return nil, fmt.Errorf("code: %d, body: %s", resp.Code, string(resp.Body))
}

err = json.Unmarshal(resp.Body, &txn)
var r struct {
Commit_id string `json:"commit_id"`
Commit_psbt string `json:"commit_psbt"`
Commit_fee int64 `json:"commit_fee"`
Commit_vsize int64 `json:"commit_vsize"`
Commit_custom *Brc20CommitCustom `json:"commit_custom"`
Network_fee int64 `json:"network_fee"`
}
err = json.Unmarshal(resp.Body, &r)
if err != nil {
return nil, err
}
return txn, nil
return &Brc20TransferTransaction{
Transaction: r.Commit_psbt,
NetworkFee: r.Network_fee,
CommitFee: r.Commit_fee,
CommitVsize: r.Commit_vsize,
CommitId: r.Commit_id,
CommitCustom: r.Commit_custom,
}, nil
}
4 changes: 2 additions & 2 deletions core/btc/chainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func zeroWalletHost(chainnet string) (string, error) {
func comingOrdHost(chainnet string) (string, error) {
switch chainnet {
case ChainMainnet, ChainBitcoin:
return "https://bitcoin.coming.chat/ord", nil
return "https://ord.bevm.io/mainnet", nil
case ChainTestnet:
return "https://bitcoin.coming.chat/ord_testnet", nil
return "https://ord.bevm.io/testnet", nil
}
return "", ErrUnsupportedChain
}
Expand Down

0 comments on commit c320810

Please sign in to comment.