From 995d58ea36a44051f8f9cbf2bd1d99815c6dc5ef Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Tue, 23 Jan 2024 20:14:26 +0800 Subject: [PATCH 01/10] feat: add filecoin&xrp for easynode --- blockchain/service/http_handler.go | 271 +++++++++++++++++++++++++++++ blockchain/service/xrp/xrp.go | 5 + 2 files changed, 276 insertions(+) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index 859d4d3..fd68989 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -12,6 +12,7 @@ import ( "github.com/0xcregis/easynode/blockchain" "github.com/0xcregis/easynode/blockchain/config" "github.com/0xcregis/easynode/common/chain" + "github.com/0xcregis/easynode/common/ethtypes" easyKafka "github.com/0xcregis/easynode/common/kafka" "github.com/0xcregis/easynode/common/util" "github.com/gin-gonic/gin" @@ -657,6 +658,64 @@ func (h *HttpHandler) SendRawTx1(ctx *gin.Context) { return } + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + /** + { + "/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4" + } + */ + hash := gjson.Parse(res).Get("/").String() + m["status"] = "SUCCESS" + m["hash"] = hash + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + { + "result": { + "accepted": true, + "account_sequence_available": 393, + "account_sequence_next": 393, + "applied": false, + "broadcast": false, + "engine_result": "tefPAST_SEQ", + "engine_result_code": -190, + "engine_result_message": "This sequence number has already passed.", + "kept": true, + "open_ledger_cost": "10", + "queued": false, + "status": "success", + "tx_blob": "1200002280000000240000000361D4838D7EA4C6800000000000000000000000000055534400000000004B4E9C06F24296074F7BC48F92A97916C6DC5EA968400000000000000A732103AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB74473045022100D184EB4AE5956FF600E7536EE459345C7BBCF097A84CC61A93B9AF7197EDB98702201CEA8009B7BEEBAA2AACC0359B41C427C1C5B550A4CA4B80CF2174AF2D6D5DCE81144B4E9C06F24296074F7BC48F92A97916C6DC5EA983143E9D4A2B8AA0780F682D136F7A56D6724EF53754", + "tx_json": { + "Account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + "Amount": { + "currency": "USD", + "issuer": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + "value": "1" + }, + "Destination": "ra5nK24KXen9AHvsdFTKHSANinZseWnPcX", + "Fee": "10", + "Flags": 2147483648, + "Sequence": 3, + "SigningPubKey": "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB", + "TransactionType": "Payment", + "TxnSignature": "3045022100D184EB4AE5956FF600E7536EE459345C7BBCF097A84CC61A93B9AF7197EDB98702201CEA8009B7BEEBAA2AACC0359B41C427C1C5B550A4CA4B80CF2174AF2D6D5DCE", + "hash": "82230B9D489370504B39BC2CE46216176CAC9E752E5C1774A8CBEC9FBB819208" + }, + "validated_ledger_index": 85447184 + } + } + + */ + + root := gjson.Parse(res) + status := root.Get("result.status").String() + if status == "success" { + m["status"] = "SUCCESS" + } else { + m["status"] = "FAILURE" + } + + hash := root.Get("result.tx_json.hash").String() + m["hash"] = hash } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -708,6 +767,27 @@ func (h *HttpHandler) GetBlockByHash1(ctx *gin.Context) { timestamp := root.Get("block_header.raw_data.timestamp").String() m["timestamp"] = timestamp + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + root := gjson.Parse(res) + blockHash := root.Get("Cid").String() + m["blockHash"] = blockHash + head := root.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp + Height := gjson.Parse(head).Get("Height").String() + m["blockNumber"] = Height + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + root := gjson.Parse(res).Get("result") + blockHash := root.Get("ledger_hash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledger_index").String() + m["blockNumber"] = blockNumber + timestamp := root.Get("ledger.close_time").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -757,6 +837,61 @@ func (h *HttpHandler) GetBlockByNumber1(ctx *gin.Context) { timestamp := root.Get("block_header.raw_data.timestamp").String() m["timestamp"] = timestamp + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + m["blockNumber"] = number + list := gjson.Parse(res).Array() + blocks := make([]any, 0, 2) + for _, v := range list { + mp := make(map[string]any, 2) + blockHash := v.Get("Cid").String() + mp["blockHash"] = blockHash + head := v.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + mp["timestamp"] = timestamp + blocks = append(blocks, mp) + } + m["blocks"] = blocks + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + + /** + { + "result":{ + "ledger":{ + "account_hash":"B3D7C8884773E0EA71EFD7637188DB0E284D1F17ABB15CA93B830F2B51FC3511", + "close_flags":0, + "close_time":758969601, + "close_time_human":"2024-Jan-19 08:53:21.000000000 UTC", + "close_time_iso":"2024-01-19T08:53:21Z", + "close_time_resolution":10, + "closed":true, + "ledger_hash":"8AC92BF3FC88166143F5039154B900288189682E2FA28EA1373FB497BAC155A5", + "ledger_index":"85379901", + "parent_close_time":758969600, + "parent_hash":"B60522E77C4ECCD0E936E091A020C813B8D32A159CDE52E5EB772777025CA98F", + "total_coins":"99987964714422794", + "transaction_hash":"A705F5EAB7DD7C610C239F86F16C6E721CA8C2422783A7329911B72E36C17000", + "transactions":Array[170] + }, + "ledger_hash":"8AC92BF3FC88166143F5039154B900288189682E2FA28EA1373FB497BAC155A5", + "ledger_index":85379901, + "status":"success", + "validated":true + } + } + */ + + root := gjson.Parse(res).Get("result") + blockHash := root.Get("ledger_hash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledger_index").String() + m["blockNumber"] = blockNumber + timestamp := root.Get("ledger.close_time").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -862,6 +997,95 @@ func (h *HttpHandler) GetTxByHash1(ctx *gin.Context) { m["status"] = 1 } + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + root := gjson.Parse(res).Get("result") + blockHash := root.Get("blockHash").String() + eth, _ := ethtypes.ParseEthHash(blockHash) + m["blockHash"] = eth.ToCid().String() + blockNumber := root.Get("blockNumber").String() + m["blockNumber"], _ = util.HexToInt(blockNumber) + transactionHash := root.Get("transactionHash").String() + eth, _ = ethtypes.ParseEthHash(transactionHash) + txHash := eth.ToCid().String() + m["txHash"] = txHash + from := root.Get("from").String() + addr, _ := ethtypes.ParseEthAddress(from) + fAddr, _ := addr.ToFilecoinAddress() + m["from"] = fAddr.String() + to := root.Get("to").String() + addr, _ = ethtypes.ParseEthAddress(to) + fAddr, _ = addr.ToFilecoinAddress() + m["to"] = fAddr.String() + + status := root.Get("status").String() + if status == "0x1" { + m["status"] = 1 + } else { + m["status"] = 0 + } + + gasUsed := root.Get("gasUsed").String() + gasPrice := root.Get("effectiveGasPrice").String() + bigPrice, b := new(big.Int).SetString(gasPrice, 0) + bigGas, b2 := new(big.Int).SetString(gasUsed, 0) + if b && b2 { + fee := bigPrice.Mul(bigPrice, bigGas) + m["fee"] = fee.String() + } + + blockResp, err := h.blockChainClients[blockChainCode].GetBlockByHash(blockChainCode, hash, false) + if err == nil && len(blockResp) > 1 { + root := gjson.Parse(blockResp) + head := root.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["blockTime"] = timestamp + Height := gjson.Parse(head).Get("Height").String() + m["blockNumber"] = Height + } + + m["energyUsageTotal"] = "0" + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + + { + "account":"rMZH9Lqv3ASSBAnjLFEb9PRDGbbXRqpZsj", + "date":758969601, + "destination":"rxRpSNb1VktvzBz8JF2oJC6qaww6RZ7Lw", + "fee":12, + "hash":"A7E17BB97A75A2FEF8F47179EF9329F669C0A31019F950CBEF504277444A8C40", + "ledgerIndex":85379901, + "status":"success", + "transactionIndex":0, + "transactionResult":"tesSUCCESS" + } + */ + root := gjson.Parse(res) + blockHash := root.Get("blockHash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledgerIndex").String() + m["blockNumber"], _ = util.HexToInt(blockNumber) + transactionHash := root.Get("hash").String() + m["txHash"] = transactionHash + from := root.Get("account").String() + m["from"] = from + to := root.Get("destination").String() + m["to"] = to + + status := root.Get("status").String() + if status == "success" { + m["status"] = 1 + } else { + m["status"] = 0 + } + + fee := root.Get("fee").String() + m["fee"] = fee + blockTime := root.Get("date").String() + m["blockTime"] = fmt.Sprintf("%v000", blockTime) + m["energyUsageTotal"] = "0" } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -909,6 +1133,25 @@ func (h *HttpHandler) GetBalance1(ctx *gin.Context) { balance := gjson.Parse(res).Get("balance").String() r["balance"] = balance nonce = "0" + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + //\"jsonrpc\":\"2.0\",\"result\":\"1050000000000000000\",\"id\":1} + balance := gjson.Parse(res).Get("result").String() + balance, _ = util.HexToInt(balance) + r["balance"] = balance + resNonce, err := h.blockChainClients[blockChainCode].Nonce(blockChainCode, addr, "latest") + if err == nil && len(resNonce) > 1 { + nonce = gjson.Parse(resNonce).Get("result").String() + nonce, _ = util.HexToInt(nonce) + } + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + + /** + {\"Account\":\"rKag74JVKyscABvLKLEvpQifHvuPVcx8xY\",\"Balance\":\"10598167\",\"Flags\":0,\"LedgerEntryType\":\"AccountRoot\",\"OwnerCount\":0,\"PreviousTxnID\":\"EE68D4341564502454166EF553D295E245375CFC948851CF5161AD1F81AA7ABE\",\"PreviousTxnLgrSeq\":85455870,\"Sequence\":84842028,\"index\":\"A78EFB04464466F56EC538BC5F4BCE4F928FD55EB4E0B62DB0FA51EAECC10CD0\"} + */ + balance := gjson.Parse(res).Get("Balance").String() + r["balance"] = balance + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -962,6 +1205,10 @@ func (h *HttpHandler) GetTokenBalance1(ctx *gin.Context) { balance := gjson.Parse(res).Get("balance").String() m["balance"] = balance nonce = "0" + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1007,6 +1254,12 @@ func (h *HttpHandler) GetNonce1(ctx *gin.Context) { } else if chain.GetChainCode(blockChainCode, "POLYGON", nil) { nonce = gjson.Parse(res).Get("result").String() nonce, _ = util.HexToInt(nonce) + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + //{\"jsonrpc\":\"2.0\",\"result\":38817,\"id\":1} + nonce = gjson.Parse(res).Get("result").String() + nonce, _ = util.HexToInt(nonce) + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1052,6 +1305,20 @@ func (h *HttpHandler) GetLatestBlock1(ctx *gin.Context) { number = gjson.Parse(res).Get("result").String() number, err = util.HexToInt(number) + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + //{\"jsonrpc\":\"2.0\",\"result\":\"0x36c62f\",\"id\":1} + number = gjson.Parse(res).Get("result").String() + number, err = util.HexToInt(number) + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + {\"result\":{\"ledger_hash\":\"A3B94654D20C6BE048DCBF77E90E90518BE193D45D6CA1C8487CA17BE0C06DD1\",\"ledger_index\":85467362,\"status\":\"success\"}} + */ + root := gjson.Parse(res).Get("result") + blockHash = root.Get("ledger_hash").String() + number = root.Get("ledger_index").String() + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1102,6 +1369,10 @@ func (h *HttpHandler) GasPrice1(ctx *gin.Context) { gas = util.Div(gas, 9) //gwei } else if chain.GetChainCode(blockChainCode, "TRON", nil) { //sun + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return diff --git a/blockchain/service/xrp/xrp.go b/blockchain/service/xrp/xrp.go index 885e481..fc8a8c6 100644 --- a/blockchain/service/xrp/xrp.go +++ b/blockchain/service/xrp/xrp.go @@ -166,12 +166,16 @@ func (e *XRP) GetTransactionReceiptByHash(chainCode int64, hash string) (string, r := make(map[string]any, 5) account := root.Get("Account").String() r["account"] = account + destination := root.Get("Destination").String() + r["destination"] = destination hash = root.Get("hash").String() r["hash"] = hash ledgerIndex := root.Get("ledger_index").Int() r["ledgerIndex"] = ledgerIndex date := root.Get("date").Int() r["date"] = date + fee := root.Get("Fee").Int() + r["fee"] = fee transactionIndex := root.Get("meta.TransactionIndex").Int() r["transactionIndex"] = transactionIndex transactionResult := root.Get("meta.TransactionResult").String() @@ -277,6 +281,7 @@ func (e *XRP) Balance(chainCode int64, address string, tag string) (string, erro defer func() { e.log.Printf("Balance,Duration=%v", time.Since(start)) }() + //tag may be current,validated,etc. if len(tag) < 1 { tag = "validated" } From 3b5418000ac0920f0f13e8aeaafb705b39b8a0bb Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Thu, 25 Jan 2024 18:19:45 +0800 Subject: [PATCH 02/10] fix: set nonce default value --- blockchain/service/http_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index fd68989..526689b 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -1259,7 +1259,7 @@ func (h *HttpHandler) GetNonce1(ctx *gin.Context) { nonce = gjson.Parse(res).Get("result").String() nonce, _ = util.HexToInt(nonce) } else if chain.GetChainCode(blockChainCode, "XRP", nil) { - + nonce = "0" } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return From 6683aa0bbbc5928cb8c04dda5d41db48a4a6ba49 Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Tue, 23 Jan 2024 20:14:26 +0800 Subject: [PATCH 03/10] feat: add filecoin&xrp for easynode --- blockchain/service/http_handler.go | 271 +++++++++++++++++++++++++++++ blockchain/service/xrp/xrp.go | 5 + 2 files changed, 276 insertions(+) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index 859d4d3..fd68989 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -12,6 +12,7 @@ import ( "github.com/0xcregis/easynode/blockchain" "github.com/0xcregis/easynode/blockchain/config" "github.com/0xcregis/easynode/common/chain" + "github.com/0xcregis/easynode/common/ethtypes" easyKafka "github.com/0xcregis/easynode/common/kafka" "github.com/0xcregis/easynode/common/util" "github.com/gin-gonic/gin" @@ -657,6 +658,64 @@ func (h *HttpHandler) SendRawTx1(ctx *gin.Context) { return } + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + /** + { + "/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4" + } + */ + hash := gjson.Parse(res).Get("/").String() + m["status"] = "SUCCESS" + m["hash"] = hash + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + { + "result": { + "accepted": true, + "account_sequence_available": 393, + "account_sequence_next": 393, + "applied": false, + "broadcast": false, + "engine_result": "tefPAST_SEQ", + "engine_result_code": -190, + "engine_result_message": "This sequence number has already passed.", + "kept": true, + "open_ledger_cost": "10", + "queued": false, + "status": "success", + "tx_blob": "1200002280000000240000000361D4838D7EA4C6800000000000000000000000000055534400000000004B4E9C06F24296074F7BC48F92A97916C6DC5EA968400000000000000A732103AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB74473045022100D184EB4AE5956FF600E7536EE459345C7BBCF097A84CC61A93B9AF7197EDB98702201CEA8009B7BEEBAA2AACC0359B41C427C1C5B550A4CA4B80CF2174AF2D6D5DCE81144B4E9C06F24296074F7BC48F92A97916C6DC5EA983143E9D4A2B8AA0780F682D136F7A56D6724EF53754", + "tx_json": { + "Account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + "Amount": { + "currency": "USD", + "issuer": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + "value": "1" + }, + "Destination": "ra5nK24KXen9AHvsdFTKHSANinZseWnPcX", + "Fee": "10", + "Flags": 2147483648, + "Sequence": 3, + "SigningPubKey": "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB", + "TransactionType": "Payment", + "TxnSignature": "3045022100D184EB4AE5956FF600E7536EE459345C7BBCF097A84CC61A93B9AF7197EDB98702201CEA8009B7BEEBAA2AACC0359B41C427C1C5B550A4CA4B80CF2174AF2D6D5DCE", + "hash": "82230B9D489370504B39BC2CE46216176CAC9E752E5C1774A8CBEC9FBB819208" + }, + "validated_ledger_index": 85447184 + } + } + + */ + + root := gjson.Parse(res) + status := root.Get("result.status").String() + if status == "success" { + m["status"] = "SUCCESS" + } else { + m["status"] = "FAILURE" + } + + hash := root.Get("result.tx_json.hash").String() + m["hash"] = hash } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -708,6 +767,27 @@ func (h *HttpHandler) GetBlockByHash1(ctx *gin.Context) { timestamp := root.Get("block_header.raw_data.timestamp").String() m["timestamp"] = timestamp + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + root := gjson.Parse(res) + blockHash := root.Get("Cid").String() + m["blockHash"] = blockHash + head := root.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp + Height := gjson.Parse(head).Get("Height").String() + m["blockNumber"] = Height + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + root := gjson.Parse(res).Get("result") + blockHash := root.Get("ledger_hash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledger_index").String() + m["blockNumber"] = blockNumber + timestamp := root.Get("ledger.close_time").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -757,6 +837,61 @@ func (h *HttpHandler) GetBlockByNumber1(ctx *gin.Context) { timestamp := root.Get("block_header.raw_data.timestamp").String() m["timestamp"] = timestamp + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + m["blockNumber"] = number + list := gjson.Parse(res).Array() + blocks := make([]any, 0, 2) + for _, v := range list { + mp := make(map[string]any, 2) + blockHash := v.Get("Cid").String() + mp["blockHash"] = blockHash + head := v.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + mp["timestamp"] = timestamp + blocks = append(blocks, mp) + } + m["blocks"] = blocks + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + + /** + { + "result":{ + "ledger":{ + "account_hash":"B3D7C8884773E0EA71EFD7637188DB0E284D1F17ABB15CA93B830F2B51FC3511", + "close_flags":0, + "close_time":758969601, + "close_time_human":"2024-Jan-19 08:53:21.000000000 UTC", + "close_time_iso":"2024-01-19T08:53:21Z", + "close_time_resolution":10, + "closed":true, + "ledger_hash":"8AC92BF3FC88166143F5039154B900288189682E2FA28EA1373FB497BAC155A5", + "ledger_index":"85379901", + "parent_close_time":758969600, + "parent_hash":"B60522E77C4ECCD0E936E091A020C813B8D32A159CDE52E5EB772777025CA98F", + "total_coins":"99987964714422794", + "transaction_hash":"A705F5EAB7DD7C610C239F86F16C6E721CA8C2422783A7329911B72E36C17000", + "transactions":Array[170] + }, + "ledger_hash":"8AC92BF3FC88166143F5039154B900288189682E2FA28EA1373FB497BAC155A5", + "ledger_index":85379901, + "status":"success", + "validated":true + } + } + */ + + root := gjson.Parse(res).Get("result") + blockHash := root.Get("ledger_hash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledger_index").String() + m["blockNumber"] = blockNumber + timestamp := root.Get("ledger.close_time").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["timestamp"] = timestamp + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -862,6 +997,95 @@ func (h *HttpHandler) GetTxByHash1(ctx *gin.Context) { m["status"] = 1 } + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + root := gjson.Parse(res).Get("result") + blockHash := root.Get("blockHash").String() + eth, _ := ethtypes.ParseEthHash(blockHash) + m["blockHash"] = eth.ToCid().String() + blockNumber := root.Get("blockNumber").String() + m["blockNumber"], _ = util.HexToInt(blockNumber) + transactionHash := root.Get("transactionHash").String() + eth, _ = ethtypes.ParseEthHash(transactionHash) + txHash := eth.ToCid().String() + m["txHash"] = txHash + from := root.Get("from").String() + addr, _ := ethtypes.ParseEthAddress(from) + fAddr, _ := addr.ToFilecoinAddress() + m["from"] = fAddr.String() + to := root.Get("to").String() + addr, _ = ethtypes.ParseEthAddress(to) + fAddr, _ = addr.ToFilecoinAddress() + m["to"] = fAddr.String() + + status := root.Get("status").String() + if status == "0x1" { + m["status"] = 1 + } else { + m["status"] = 0 + } + + gasUsed := root.Get("gasUsed").String() + gasPrice := root.Get("effectiveGasPrice").String() + bigPrice, b := new(big.Int).SetString(gasPrice, 0) + bigGas, b2 := new(big.Int).SetString(gasUsed, 0) + if b && b2 { + fee := bigPrice.Mul(bigPrice, bigGas) + m["fee"] = fee.String() + } + + blockResp, err := h.blockChainClients[blockChainCode].GetBlockByHash(blockChainCode, hash, false) + if err == nil && len(blockResp) > 1 { + root := gjson.Parse(blockResp) + head := root.Get("BlockHead").String() + timestamp := gjson.Parse(head).Get("Timestamp").String() + timestamp = fmt.Sprintf("%v000", timestamp) + m["blockTime"] = timestamp + Height := gjson.Parse(head).Get("Height").String() + m["blockNumber"] = Height + } + + m["energyUsageTotal"] = "0" + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + + { + "account":"rMZH9Lqv3ASSBAnjLFEb9PRDGbbXRqpZsj", + "date":758969601, + "destination":"rxRpSNb1VktvzBz8JF2oJC6qaww6RZ7Lw", + "fee":12, + "hash":"A7E17BB97A75A2FEF8F47179EF9329F669C0A31019F950CBEF504277444A8C40", + "ledgerIndex":85379901, + "status":"success", + "transactionIndex":0, + "transactionResult":"tesSUCCESS" + } + */ + root := gjson.Parse(res) + blockHash := root.Get("blockHash").String() + m["blockHash"] = blockHash + blockNumber := root.Get("ledgerIndex").String() + m["blockNumber"], _ = util.HexToInt(blockNumber) + transactionHash := root.Get("hash").String() + m["txHash"] = transactionHash + from := root.Get("account").String() + m["from"] = from + to := root.Get("destination").String() + m["to"] = to + + status := root.Get("status").String() + if status == "success" { + m["status"] = 1 + } else { + m["status"] = 0 + } + + fee := root.Get("fee").String() + m["fee"] = fee + blockTime := root.Get("date").String() + m["blockTime"] = fmt.Sprintf("%v000", blockTime) + m["energyUsageTotal"] = "0" } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -909,6 +1133,25 @@ func (h *HttpHandler) GetBalance1(ctx *gin.Context) { balance := gjson.Parse(res).Get("balance").String() r["balance"] = balance nonce = "0" + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + //\"jsonrpc\":\"2.0\",\"result\":\"1050000000000000000\",\"id\":1} + balance := gjson.Parse(res).Get("result").String() + balance, _ = util.HexToInt(balance) + r["balance"] = balance + resNonce, err := h.blockChainClients[blockChainCode].Nonce(blockChainCode, addr, "latest") + if err == nil && len(resNonce) > 1 { + nonce = gjson.Parse(resNonce).Get("result").String() + nonce, _ = util.HexToInt(nonce) + } + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + + /** + {\"Account\":\"rKag74JVKyscABvLKLEvpQifHvuPVcx8xY\",\"Balance\":\"10598167\",\"Flags\":0,\"LedgerEntryType\":\"AccountRoot\",\"OwnerCount\":0,\"PreviousTxnID\":\"EE68D4341564502454166EF553D295E245375CFC948851CF5161AD1F81AA7ABE\",\"PreviousTxnLgrSeq\":85455870,\"Sequence\":84842028,\"index\":\"A78EFB04464466F56EC538BC5F4BCE4F928FD55EB4E0B62DB0FA51EAECC10CD0\"} + */ + balance := gjson.Parse(res).Get("Balance").String() + r["balance"] = balance + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -962,6 +1205,10 @@ func (h *HttpHandler) GetTokenBalance1(ctx *gin.Context) { balance := gjson.Parse(res).Get("balance").String() m["balance"] = balance nonce = "0" + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1007,6 +1254,12 @@ func (h *HttpHandler) GetNonce1(ctx *gin.Context) { } else if chain.GetChainCode(blockChainCode, "POLYGON", nil) { nonce = gjson.Parse(res).Get("result").String() nonce, _ = util.HexToInt(nonce) + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + //{\"jsonrpc\":\"2.0\",\"result\":38817,\"id\":1} + nonce = gjson.Parse(res).Get("result").String() + nonce, _ = util.HexToInt(nonce) + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1052,6 +1305,20 @@ func (h *HttpHandler) GetLatestBlock1(ctx *gin.Context) { number = gjson.Parse(res).Get("result").String() number, err = util.HexToInt(number) + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + //{\"jsonrpc\":\"2.0\",\"result\":\"0x36c62f\",\"id\":1} + number = gjson.Parse(res).Get("result").String() + number, err = util.HexToInt(number) + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + /** + {\"result\":{\"ledger_hash\":\"A3B94654D20C6BE048DCBF77E90E90518BE193D45D6CA1C8487CA17BE0C06DD1\",\"ledger_index\":85467362,\"status\":\"success\"}} + */ + root := gjson.Parse(res).Get("result") + blockHash = root.Get("ledger_hash").String() + number = root.Get("ledger_index").String() + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return @@ -1102,6 +1369,10 @@ func (h *HttpHandler) GasPrice1(ctx *gin.Context) { gas = util.Div(gas, 9) //gwei } else if chain.GetChainCode(blockChainCode, "TRON", nil) { //sun + } else if chain.GetChainCode(blockChainCode, "FIL", nil) { + + } else if chain.GetChainCode(blockChainCode, "XRP", nil) { + } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return diff --git a/blockchain/service/xrp/xrp.go b/blockchain/service/xrp/xrp.go index 885e481..fc8a8c6 100644 --- a/blockchain/service/xrp/xrp.go +++ b/blockchain/service/xrp/xrp.go @@ -166,12 +166,16 @@ func (e *XRP) GetTransactionReceiptByHash(chainCode int64, hash string) (string, r := make(map[string]any, 5) account := root.Get("Account").String() r["account"] = account + destination := root.Get("Destination").String() + r["destination"] = destination hash = root.Get("hash").String() r["hash"] = hash ledgerIndex := root.Get("ledger_index").Int() r["ledgerIndex"] = ledgerIndex date := root.Get("date").Int() r["date"] = date + fee := root.Get("Fee").Int() + r["fee"] = fee transactionIndex := root.Get("meta.TransactionIndex").Int() r["transactionIndex"] = transactionIndex transactionResult := root.Get("meta.TransactionResult").String() @@ -277,6 +281,7 @@ func (e *XRP) Balance(chainCode int64, address string, tag string) (string, erro defer func() { e.log.Printf("Balance,Duration=%v", time.Since(start)) }() + //tag may be current,validated,etc. if len(tag) < 1 { tag = "validated" } From bc6c12178e1690fe06beda8d2837b3a3c883f928 Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Thu, 25 Jan 2024 18:19:45 +0800 Subject: [PATCH 04/10] fix: set nonce default value --- blockchain/service/http_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index fd68989..526689b 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -1259,7 +1259,7 @@ func (h *HttpHandler) GetNonce1(ctx *gin.Context) { nonce = gjson.Parse(res).Get("result").String() nonce, _ = util.HexToInt(nonce) } else if chain.GetChainCode(blockChainCode, "XRP", nil) { - + nonce = "0" } else { h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) return From 4e3fde26924968c50f2d9a1e17aae47fd6afb03a Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Fri, 26 Jan 2024 16:54:32 +0800 Subject: [PATCH 05/10] refact: format address --- blockchain/service/http_handler.go | 51 ++++++++++++++++++++++ cmd/blockchain/app.go | 4 +- cmd/blockchain/config_ether.json | 2 +- cmd/easynode/app.go | 2 + collect/service/cmd/chain/tron2/service.go | 19 +++++++- store/chain/gw.go | 2 + store/chain/tron/kafka_util.go | 13 ++++++ store/service/http_service.go | 41 ++++++++--------- store/service/ws_service.go | 2 +- store/service/ws_service_test.go | 8 ++++ 10 files changed, 118 insertions(+), 26 deletions(-) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index 526689b..dbfdac5 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -331,6 +331,32 @@ func (h *HttpHandler) GetTokenBalance(ctx *gin.Context) { h.Success(ctx, r.String(), res, ctx.Request.RequestURI) } +// GetToken get token info +func (h *HttpHandler) GetToken(ctx *gin.Context) { + b, err := io.ReadAll(ctx.Request.Body) + if err != nil { + h.Error(ctx, "", ctx.Request.RequestURI, err.Error()) + return + } + r := gjson.ParseBytes(b) + blockChainCode := r.Get("chain").Int() + codeHash := r.Get("contract").String() + abi := r.Get("abi").String() + eip := r.Get("eip").String() + + if _, ok := h.blockChainClients[blockChainCode]; !ok { + h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) + return + } + res, err := h.blockChainClients[blockChainCode].Token(blockChainCode, codeHash, abi, eip) + if err != nil { + h.Error(ctx, r.String(), ctx.Request.RequestURI, err.Error()) + return + } + + h.Success(ctx, r.String(), res, ctx.Request.RequestURI) +} + // GetNonce todo 仅适用于 ether,tron 暂不支持 func (h *HttpHandler) GetNonce(ctx *gin.Context) { b, err := io.ReadAll(ctx.Request.Body) @@ -1162,6 +1188,31 @@ func (h *HttpHandler) GetBalance1(ctx *gin.Context) { h.Success(ctx, string(b), r, ctx.Request.RequestURI) } +// GetToken1 get token info +func (h *HttpHandler) GetToken1(ctx *gin.Context) { + b, err := io.ReadAll(ctx.Request.Body) + if err != nil { + h.Error(ctx, "", ctx.Request.RequestURI, err.Error()) + return + } + r := gjson.ParseBytes(b) + blockChainCode := r.Get("chain").Int() + codeHash := r.Get("contract").String() + abi := r.Get("abi").String() + eip := r.Get("eip").String() + + if _, ok := h.blockChainClients[blockChainCode]; !ok { + h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) + return + } + res, err := h.blockChainClients[blockChainCode].Token(blockChainCode, codeHash, abi, eip) + if err != nil { + h.Error(ctx, r.String(), ctx.Request.RequestURI, err.Error()) + return + } + h.Success(ctx, r.String(), gjson.Parse(res).Value(), ctx.Request.RequestURI) +} + // GetTokenBalance1 ERC20协议代币余额,后期补充 func (h *HttpHandler) GetTokenBalance1(ctx *gin.Context) { b, err := io.ReadAll(ctx.Request.Body) diff --git a/cmd/blockchain/app.go b/cmd/blockchain/app.go index a76ebb6..13290dc 100644 --- a/cmd/blockchain/app.go +++ b/cmd/blockchain/app.go @@ -15,7 +15,7 @@ import ( func main() { var configPath string - flag.StringVar(&configPath, "blockchain", "./cmd/blockchain/config_xrp.json", "The system file of config") + flag.StringVar(&configPath, "blockchain", "./cmd/blockchain/config_ether.json", "The system file of config") flag.Parse() if len(configPath) < 1 { panic("can not find config file") @@ -53,6 +53,7 @@ func main() { origin.POST("/tx/trace", srv.GetTraceTransaction) origin.POST("/receipts/hash", srv.GetTxReceiptByHash) origin.POST("/account/balance", srv.GetBalance) + origin.POST("/account/token", srv.GetToken) origin.POST("/account/tokenBalance", srv.GetTokenBalance) origin.POST("/account/nonce", srv.GetNonce) origin.POST("/block/latest", srv.GetLatestBlock) @@ -69,6 +70,7 @@ func main() { myRoot.POST("/block/number", srv.GetBlockByNumber1) myRoot.POST("/tx/hash", srv.GetTxByHash1) myRoot.POST("/account/balance", srv.GetBalance1) + myRoot.POST("/account/token", srv.GetToken1) myRoot.POST("/account/tokenBalance", srv.GetTokenBalance1) myRoot.POST("/account/nonce", srv.GetNonce1) myRoot.POST("/block/latest", srv.GetLatestBlock1) diff --git a/cmd/blockchain/config_ether.json b/cmd/blockchain/config_ether.json index 4f5e85c..5078a79 100644 --- a/cmd/blockchain/config_ether.json +++ b/cmd/blockchain/config_ether.json @@ -9,7 +9,7 @@ }, "Nodes": [ { - "BlockChain": 200, + "BlockChain": 60, "NodeUrl": "https://docs-demo.quiknode.pro", "NodeToken": "", "Trace": true diff --git a/cmd/easynode/app.go b/cmd/easynode/app.go index 4cd3f02..7402125 100644 --- a/cmd/easynode/app.go +++ b/cmd/easynode/app.go @@ -193,6 +193,7 @@ func startBlockchain(configPath string, ctx context.Context) { origin.POST("/tx/trace", srv.GetTraceTransaction) origin.POST("/receipts/hash", srv.GetTxReceiptByHash) origin.POST("/account/balance", srv.GetBalance) + origin.POST("/account/token", srv.GetToken) origin.POST("/account/tokenBalance", srv.GetTokenBalance) origin.POST("/account/nonce", srv.GetNonce) origin.POST("/block/latest", srv.GetLatestBlock) @@ -209,6 +210,7 @@ func startBlockchain(configPath string, ctx context.Context) { myRoot.POST("/block/number", srv.GetBlockByNumber1) myRoot.POST("/tx/hash", srv.GetTxByHash1) myRoot.POST("/account/balance", srv.GetBalance1) + myRoot.POST("/account/token", srv.GetToken1) myRoot.POST("/account/tokenBalance", srv.GetTokenBalance1) myRoot.POST("/account/nonce", srv.GetNonce1) myRoot.POST("/block/latest", srv.GetLatestBlock1) diff --git a/collect/service/cmd/chain/tron2/service.go b/collect/service/cmd/chain/tron2/service.go index 7a0d14e..cdc13fa 100644 --- a/collect/service/cmd/chain/tron2/service.go +++ b/collect/service/cmd/chain/tron2/service.go @@ -14,6 +14,7 @@ import ( chainService "github.com/0xcregis/easynode/blockchain/service" "github.com/0xcregis/easynode/collect" "github.com/0xcregis/easynode/collect/config" + chainCode "github.com/0xcregis/easynode/common/chain" "github.com/0xcregis/easynode/common/util" "github.com/sirupsen/logrus" "github.com/sunjiangjun/xlog" @@ -437,8 +438,24 @@ func (s *Service) reload() { s.log.Warnf("ReloadMonitorAddress BlockChainName=%v,err=%v", s.chain.BlockChainName, err) } + //format address + code := int64(s.chain.BlockChainCode) + finalAddressList := make([]string, 0, len(addressList)) + for _, addr := range addressList { + if chainCode.GetChainCode(code, "TRON", nil) && !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { + base58Addr, err := util.Base58ToAddress(addr) + if err != nil { + continue + } + addr2 := base58Addr.Hex() + finalAddressList = append(finalAddressList, addr2) + } else { + finalAddressList = append(finalAddressList, addr) + } + } + s.lock.Lock() - s.monitorAddress = rebuildAddr(addressList) + s.monitorAddress = rebuildAddr(finalAddressList) s.lock.Unlock() } diff --git a/store/chain/gw.go b/store/chain/gw.go index 1652ee0..e6ed05d 100644 --- a/store/chain/gw.go +++ b/store/chain/gw.go @@ -129,6 +129,8 @@ func GetCoreAddress(blockChain int64, address string) string { if chainCode.GetChainCode(blockChain, "ETH", nil) { return ether.GetCoreAddr(address) } else if chainCode.GetChainCode(blockChain, "TRON", nil) { + //转换地址格式 + address = tron.FormatAddr(address) return tron.GetCoreAddr(address) } else if chainCode.GetChainCode(blockChain, "POLYGON", nil) { return polygonpos.GetCoreAddr(address) diff --git a/store/chain/tron/kafka_util.go b/store/chain/tron/kafka_util.go index 0d1f09d..3bad067 100644 --- a/store/chain/tron/kafka_util.go +++ b/store/chain/tron/kafka_util.go @@ -323,6 +323,19 @@ func ParseTx(body []byte, transferTopic string, blockchain int64) (*store.SubTx, return &r, nil } + +func FormatAddr(addr string) string { + if !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { + base58Addr, err := util.Base58ToAddress(addr) + if err != nil { + return addr + } + return base58Addr.Hex() + } else { + return addr + } +} + func GetCoreAddr(addr string) string { if strings.HasPrefix(addr, "0x41") { return strings.Replace(addr, "0x41", "", 1) //去丢41 diff --git a/store/service/http_service.go b/store/service/http_service.go index ae123de..083b6bb 100644 --- a/store/service/http_service.go +++ b/store/service/http_service.go @@ -5,11 +5,8 @@ import ( "errors" "fmt" "io" - "strings" "time" - chainCode "github.com/0xcregis/easynode/common/chain" - "github.com/0xcregis/easynode/common/util" "github.com/0xcregis/easynode/store" "github.com/0xcregis/easynode/store/config" "github.com/0xcregis/easynode/store/db" @@ -226,25 +223,25 @@ func (s *HttpHandler) MonitorAddress(c *gin.Context) { return } - //tron base58进制的地址处理 - if chainCode.GetChainCode(blockChain, "TRON", nil) && !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { - base58Addr, err := util.Base58ToAddress(addr) - if err != nil { - s.Error(c, c.Request.URL.Path, err.Error()) - return - } - addr = base58Addr.Hex() - } - - if blockChain == 0 && !util.Has0xPrefix(addr) { - //if chainCode=0 && !0x 这默认认为是tron 地址 - base58Addr, err := util.Base58ToAddress(addr) - if err != nil { - s.Error(c, c.Request.URL.Path, err.Error()) - return - } - addr = base58Addr.Hex() - } + ////tron base58进制的地址处理 + //if chainCode.GetChainCode(blockChain, "TRON", nil) && !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { + // base58Addr, err := util.Base58ToAddress(addr) + // if err != nil { + // s.Error(c, c.Request.URL.Path, err.Error()) + // return + // } + // addr = base58Addr.Hex() + //} + // + //if blockChain == 0 && !util.Has0xPrefix(addr) { + // //if chainCode=0 && !0x 这默认认为是tron 地址 + // base58Addr, err := util.Base58ToAddress(addr) + // if err != nil { + // s.Error(c, c.Request.URL.Path, err.Error()) + // return + // } + // addr = base58Addr.Hex() + //} //address:hex string addressTask := &store.MonitorAddress{BlockChain: blockChain, Address: addr, Token: token, TxType: fmt.Sprintf("%v", 0), Id: time.Now().UnixNano()} diff --git a/store/service/ws_service.go b/store/service/ws_service.go index fa20720..2d232b4 100644 --- a/store/service/ws_service.go +++ b/store/service/ws_service.go @@ -94,7 +94,7 @@ func (ws *WsHandler) pushMessage(kafkaCtx context.Context) { }(kafkaCtx, ws.writer) } -// updateMonitorAddress update monitor address to use for collect server +// updateMonitorAddress update monitor address to collect server func (ws *WsHandler) updateMonitorAddress() { go func() { for { diff --git a/store/service/ws_service_test.go b/store/service/ws_service_test.go index 4a62230..f444e13 100644 --- a/store/service/ws_service_test.go +++ b/store/service/ws_service_test.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "os/signal" + "strings" "testing" "time" @@ -74,3 +75,10 @@ func TestConn(t *testing.T) { } } } + +func Test1(t *testing.T) { + + t.Log(strings.ToLower("0xA16F524a804BEaED0d791De0aa0b5836295A2a84")) + + t.Log(strings.ToLower("0xc03fe617F10286AE63568B5c567C81ce8DF84D38")) +} From 092c588d771ac2a9fff36c91d43c591573dbe57c Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Mon, 29 Jan 2024 14:49:19 +0800 Subject: [PATCH 06/10] fix: base56 address --- .../service/polygon/polygon_pos_test.go | 11 +++++++++++ cmd/blockchain/config_polygon.json | 19 ++----------------- collect/service/cmd/chain/tron2/service.go | 7 ++----- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/blockchain/service/polygon/polygon_pos_test.go b/blockchain/service/polygon/polygon_pos_test.go index c7f035a..53382a2 100644 --- a/blockchain/service/polygon/polygon_pos_test.go +++ b/blockchain/service/polygon/polygon_pos_test.go @@ -91,6 +91,17 @@ func TestPolygonPos_GetTransactionReceiptByHash(t *testing.T) { } } +func TestPolygonPos_SendRawTransaction(t *testing.T) { + + s := Init() + resp, err := s.SendRawTransaction(64, "0xf86980834528bf82520894545f731e3ce6ab51c7a30ca08bf0f1a953e3082687038d7ea4c680008037a0b1ee34af3c3aede573f17de1789e39f2ce96dca389e1b1faeaee2595c6cb9b56a03210dd0db1cebce7e85ca5630f8ec92b16abea94ddd20f600fac0400ea0e891d") + if err != nil { + t.Error(err) + } else { + t.Log(resp) + } +} + func TestPolygonPos_GasPrice(t *testing.T) { s := Init2() resp, err := s.GasPrice(64) diff --git a/cmd/blockchain/config_polygon.json b/cmd/blockchain/config_polygon.json index 5ed5b5e..a0ff51f 100644 --- a/cmd/blockchain/config_polygon.json +++ b/cmd/blockchain/config_polygon.json @@ -9,24 +9,9 @@ }, "Nodes": [ { - "BlockChain": 66, - "NodeUrl": "https://rpc.ankr.com/base", - "NodeToken": "053a22d65617d79be4ba5f1a7a181a24b3ab258a3c506bd6f02a338713e03457" - }, - { - "BlockChain": 63, - "NodeUrl": "https://arb1.arbitrum.io/rpc", + "BlockChain": 201, + "NodeUrl": "https://polygon-bor.publicnode.com", "NodeToken": "" - }, - { - "BlockChain": 65, - "NodeUrl": "https://rpc.ankr.com/avalanche", - "NodeToken": "1f1f091ef4d9eefb93f6c0f77821cf6ab377d3d2d334d9151303e28c1ddae902" - }, - { - "BlockChain": 64, - "NodeUrl": "https://rpc.ankr.com/optimism", - "NodeToken": "1f1f091ef4d9eefb93f6c0f77821cf6ab377d3d2d334d9151303e28c1ddae902" } ] } \ No newline at end of file diff --git a/collect/service/cmd/chain/tron2/service.go b/collect/service/cmd/chain/tron2/service.go index cdc13fa..e324911 100644 --- a/collect/service/cmd/chain/tron2/service.go +++ b/collect/service/cmd/chain/tron2/service.go @@ -14,7 +14,6 @@ import ( chainService "github.com/0xcregis/easynode/blockchain/service" "github.com/0xcregis/easynode/collect" "github.com/0xcregis/easynode/collect/config" - chainCode "github.com/0xcregis/easynode/common/chain" "github.com/0xcregis/easynode/common/util" "github.com/sirupsen/logrus" "github.com/sunjiangjun/xlog" @@ -439,16 +438,14 @@ func (s *Service) reload() { } //format address - code := int64(s.chain.BlockChainCode) finalAddressList := make([]string, 0, len(addressList)) for _, addr := range addressList { - if chainCode.GetChainCode(code, "TRON", nil) && !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { + if !strings.HasPrefix(addr, "0x") && !strings.HasPrefix(addr, "41") && !strings.HasPrefix(addr, "0x41") { base58Addr, err := util.Base58ToAddress(addr) if err != nil { continue } - addr2 := base58Addr.Hex() - finalAddressList = append(finalAddressList, addr2) + finalAddressList = append(finalAddressList, base58Addr.Hex()) } else { finalAddressList = append(finalAddressList, addr) } From ec24b00459f0f7a47138cc2250235820a72bf0cf Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Sun, 18 Feb 2024 11:46:32 +0800 Subject: [PATCH 07/10] fix: not implement method --- blockchain/chain/bnb/bnb.go | 9 +-- blockchain/chain/btc/btc.go | 11 ++-- blockchain/chain/ether/ether.go | 6 +- blockchain/chain/filecoin/filecoin.go | 17 ++--- blockchain/chain/polygonpos/polygon_pos.go | 9 +-- blockchain/chain/tron/tron.go | 72 +++++++++++++++++++--- blockchain/chain/xrp/xrp.go | 16 ++--- 7 files changed, 87 insertions(+), 53 deletions(-) diff --git a/blockchain/chain/bnb/bnb.go b/blockchain/chain/bnb/bnb.go index 3f2070b..d891e8d 100644 --- a/blockchain/chain/bnb/bnb.go +++ b/blockchain/chain/bnb/bnb.go @@ -261,8 +261,7 @@ func (e *Bnb) Subscribe(host string, token string) (string, error) { } func (e *Bnb) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -274,13 +273,11 @@ func NewNFTClient() blockchain.NFT { } func (e *Bnb) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *Bnb) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Bnb) SendRequestToChain(host string, token string, query string) (string, error) { diff --git a/blockchain/chain/btc/btc.go b/blockchain/chain/btc/btc.go index 893f9d1..3c52de5 100644 --- a/blockchain/chain/btc/btc.go +++ b/blockchain/chain/btc/btc.go @@ -44,8 +44,7 @@ func (e *Btc) Subscribe(host string, token string) (string, error) { } func (e *Btc) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -56,13 +55,11 @@ func NewChainClient() blockchain.ChainConn { } func (e *Btc) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *Btc) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Btc) SendRequestToChain(host string, token string, query string) (string, error) { @@ -122,5 +119,5 @@ func (e *Btc) SendRequestToChain(host string, token string, query string) (strin } func (e *Btc) GetToken20(host string, key string, contractAddress string, userAddress string) (map[string]interface{}, error) { - return nil, nil + return nil, fmt.Errorf("not implement the method") } diff --git a/blockchain/chain/ether/ether.go b/blockchain/chain/ether/ether.go index 3370b3a..8989108 100644 --- a/blockchain/chain/ether/ether.go +++ b/blockchain/chain/ether/ether.go @@ -274,13 +274,11 @@ func NewNFTClient() blockchain.NFT { } func (e *Ether) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *Ether) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Ether) SendRequestToChain(host string, token string, query string) (string, error) { diff --git a/blockchain/chain/filecoin/filecoin.go b/blockchain/chain/filecoin/filecoin.go index 9393d98..c96751b 100644 --- a/blockchain/chain/filecoin/filecoin.go +++ b/blockchain/chain/filecoin/filecoin.go @@ -15,13 +15,11 @@ type Filecoin struct { } func (e *Filecoin) GetToken721(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Filecoin) GetToken1155(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Filecoin) Subscribe(host string, token string) (string, error) { @@ -39,8 +37,7 @@ func (e *Filecoin) Subscribe(host string, token string) (string, error) { } func (e *Filecoin) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -48,13 +45,11 @@ func NewChainClient() blockchain.ChainConn { } func (e *Filecoin) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *Filecoin) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *Filecoin) SendRequestToChain(host string, token string, query string) (string, error) { @@ -91,5 +86,5 @@ func (e *Filecoin) SendRequestToChain(host string, token string, query string) ( } func (e *Filecoin) GetToken20(host string, key string, contractAddress string, userAddress string) (map[string]interface{}, error) { - return nil, nil + return nil, fmt.Errorf("not implement the method") } diff --git a/blockchain/chain/polygonpos/polygon_pos.go b/blockchain/chain/polygonpos/polygon_pos.go index 45f1d39..d92d143 100644 --- a/blockchain/chain/polygonpos/polygon_pos.go +++ b/blockchain/chain/polygonpos/polygon_pos.go @@ -261,8 +261,7 @@ func (e *PolygonPos) Subscribe(host string, token string) (string, error) { } func (e *PolygonPos) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -274,13 +273,11 @@ func NewNFTClient() blockchain.NFT { } func (e *PolygonPos) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *PolygonPos) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *PolygonPos) SendRequestToChain(host string, token string, query string) (string, error) { diff --git a/blockchain/chain/tron/tron.go b/blockchain/chain/tron/tron.go index e2d7471..274b40c 100644 --- a/blockchain/chain/tron/tron.go +++ b/blockchain/chain/tron/tron.go @@ -17,23 +17,19 @@ type Tron struct { } func (t *Tron) GetToken721(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (t *Tron) GetToken1155(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (t *Tron) Subscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (t *Tron) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -145,6 +141,16 @@ func (t *Tron) GetToken20ByHttp(host string, token string, contractAddress strin return nil, errors.New("contract is error") } + //name, err := t.GetTokenNameByHttp(host, token, contractAddress, userAddress) + //if err == nil { + // mp["name"] = name + //} + // + //symbol, err := t.GetTokenSymbolByHttp(host, token, contractAddress, userAddress) + //if err == nil { + // mp["symbol"] = symbol + //} + return mp, nil } @@ -207,6 +213,54 @@ func (t *Tron) GetTokenBalanceByHttp2(host string, token string, contractAddress return "", errors.New("no data") } +func (t *Tron) GetTokenNameByHttp(host string, token string, contractAddress string, userAddress string) (string, error) { + //var query string + query := ` + { + "owner_address": "%v", + "contract_address": "%v", + "function_selector": "name()" + } + ` + query = fmt.Sprintf(query, userAddress, contractAddress) + resp, err := t.SendRequestToChainByHttp(host, token, query) + if err != nil { + return "", err + } + //log.Println(resp) + r := gjson.Parse(resp).Get("constant_result") + if r.Exists() { + name := r.Array()[0].String() + return fmt.Sprintf("%v", name), nil + } + + return "", errors.New("no data") +} + +func (t *Tron) GetTokenSymbolByHttp(host string, token string, contractAddress string, userAddress string) (string, error) { + //var query string + query := ` + { + "owner_address": "%v", + "contract_address": "%v", + "function_selector": "symbol()" + } + ` + query = fmt.Sprintf(query, userAddress, contractAddress) + resp, err := t.SendRequestToChainByHttp(host, token, query) + if err != nil { + return "", err + } + //log.Println(resp) + r := gjson.Parse(resp).Get("constant_result") + if r.Exists() { + symbol := r.Array()[0].String() + return fmt.Sprintf("%v", symbol), nil + } + + return "", errors.New("no data") +} + func (t *Tron) GetToken20(host string, key string, contractAddress string, userAddress string) (map[string]interface{}, error) { - return nil, nil + return nil, fmt.Errorf("not implement the method") } diff --git a/blockchain/chain/xrp/xrp.go b/blockchain/chain/xrp/xrp.go index 0d23a61..6e3ac99 100644 --- a/blockchain/chain/xrp/xrp.go +++ b/blockchain/chain/xrp/xrp.go @@ -15,13 +15,11 @@ type XRP struct { } func (e *XRP) GetToken721(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *XRP) GetToken1155(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - //TODO implement me - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *XRP) Subscribe(host string, token string) (string, error) { @@ -39,8 +37,7 @@ func (e *XRP) Subscribe(host string, token string) (string, error) { } func (e *XRP) UnSubscribe(host string, token string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func NewChainClient() blockchain.ChainConn { @@ -48,12 +45,11 @@ func NewChainClient() blockchain.ChainConn { } func (e *XRP) SendRequestToChainByHttp(host string, token string, query string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("not implement the method") } func (e *XRP) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { - panic("implement me") + return nil, fmt.Errorf("not implement the method") } func (e *XRP) SendRequestToChain(host string, token string, query string) (string, error) { @@ -92,5 +88,5 @@ func (e *XRP) SendRequestToChain(host string, token string, query string) (strin } func (e *XRP) GetToken20(host string, key string, contractAddress string, userAddress string) (map[string]interface{}, error) { - return nil, nil + return nil, fmt.Errorf("not implement the method") } From e85ce7ab0af6ba685146c0037d7afd6f1c4a14bd Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Sun, 18 Feb 2024 15:07:07 +0800 Subject: [PATCH 08/10] fix: not implement or does not support method --- blockchain/service/btc/btc.go | 22 +++++++++++----------- blockchain/service/filecoin/filecoin.go | 10 ++++------ blockchain/service/tron/tron.go | 17 +++++++---------- blockchain/service/xrp/xrp.go | 17 ++++++++--------- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/blockchain/service/btc/btc.go b/blockchain/service/btc/btc.go index 7827e50..d10ed5d 100644 --- a/blockchain/service/btc/btc.go +++ b/blockchain/service/btc/btc.go @@ -22,40 +22,40 @@ type Btc struct { } func (e *Btc) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetCode(chainCode int64, address string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetAddressType(chainCode int64, address string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) SubscribePendingTx(chainCode int64, receiverCh chan string, sendCh chan string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } // SubscribeLogs {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]} func (e *Btc) SubscribeLogs(chainCode int64, address string, topics []string, receiverCh chan string, sendCh chan string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) UnSubscribe(chainCode int64, subId string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetBlockReceiptByBlockNumber(chainCode int64, number string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetTransactionReceiptByHash(chainCode int64, hash string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) { @@ -171,11 +171,11 @@ func (e *Btc) Balance(chainCode int64, address string, tag string) (string, erro } func (e *Btc) TokenBalance(chainCode int64, address string, contractAddr string, abi string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) Nonce(chainCode int64, address string, tag string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *Btc) LatestBlock(chainCode int64) (string, error) { diff --git a/blockchain/service/filecoin/filecoin.go b/blockchain/service/filecoin/filecoin.go index 184dbbe..420a5a3 100644 --- a/blockchain/service/filecoin/filecoin.go +++ b/blockchain/service/filecoin/filecoin.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "math" "strings" "time" @@ -25,8 +24,7 @@ type FileCoin struct { } func (e *FileCoin) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func NewFileCoin(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API { @@ -139,11 +137,11 @@ func (e *FileCoin) UnSubscribe(chainCode int64, subId string) (string, error) { } func (e *FileCoin) GetBlockReceiptByBlockNumber(chainCode int64, number string) (string, error) { - return "", errors.New("does not support the method") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *FileCoin) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) { - return "", errors.New("does not support the method") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *FileCoin) GetTransactionReceiptByHash(chainCode int64, hash string) (string, error) { @@ -154,7 +152,7 @@ func (e *FileCoin) GetTransactionReceiptByHash(chainCode int64, hash string) (st eth, err := ethtypes.EthHashFromCid(cid.MustParse(hash)) if err != nil { - log.Panicln(err) + e.log.Error(err) } query := `{ diff --git a/blockchain/service/tron/tron.go b/blockchain/service/tron/tron.go index 1ba3c84..c798ec6 100644 --- a/blockchain/service/tron/tron.go +++ b/blockchain/service/tron/tron.go @@ -23,7 +23,7 @@ type Tron struct { } func (t *Tron) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - return "", nil + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) StartWDT() { @@ -70,18 +70,15 @@ func (t *Tron) GetAddressType(chainCode int64, address string) (string, error) { } func (t *Tron) SubscribePendingTx(chainCode int64, receiverCh chan string, sendCh chan string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) SubscribeLogs(chainCode int64, address string, topics []string, receiverCh chan string, sendCh chan string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) UnSubscribe(chainCode int64, subId string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) GetBlockReceiptByBlockNumber(chainCode int64, number string) (string, error) { @@ -327,7 +324,7 @@ func (t *Tron) TokenBalance(chainCode int64, address string, contractAddr string } func (t *Tron) Nonce(chainCode int64, address string, tag string) (string, error) { - return "", errors.New("unknown the method") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) LatestBlock(chainCode int64) (string, error) { @@ -354,8 +351,8 @@ func (t *Tron) SendRawTransaction(chainCode int64, signedTx string) (string, err return t.SendReq(chainCode, req, "wallet/broadcasthex") } -func (e *Tron) TraceTransaction(chainCode int64, address string) (string, error) { - return "", nil +func (t *Tron) TraceTransaction(chainCode int64, address string) (string, error) { + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (t *Tron) SendReq(blockChain int64, reqBody string, url string) (resp string, err error) { diff --git a/blockchain/service/xrp/xrp.go b/blockchain/service/xrp/xrp.go index fc8a8c6..2380339 100644 --- a/blockchain/service/xrp/xrp.go +++ b/blockchain/service/xrp/xrp.go @@ -22,29 +22,28 @@ type XRP struct { } func (e *XRP) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - //TODO implement me - panic("implement me") + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) GetCode(chainCode int64, address string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) GetAddressType(chainCode int64, address string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) SubscribePendingTx(chainCode int64, receiverCh chan string, sendCh chan string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } // SubscribeLogs {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]} func (e *XRP) SubscribeLogs(chainCode int64, address string, topics []string, receiverCh chan string, sendCh chan string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) UnSubscribe(chainCode int64, subId string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) GetBlockReceiptByBlockNumber(chainCode int64, number string) (string, error) { @@ -324,7 +323,7 @@ func (e *XRP) TokenBalance(chainCode int64, address string, contractAddr string, } func (e *XRP) Nonce(chainCode int64, address string, tag string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", chainCode) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) } func (e *XRP) LatestBlock(chainCode int64) (string, error) { @@ -351,7 +350,7 @@ func (e *XRP) SendRawTransaction(chainCode int64, signedTx string) (string, erro } func (e *XRP) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { - return "", fmt.Errorf("blockchain:%v not implement the method", blockChain) + return "", fmt.Errorf("blockchain:%v,the method has not been implemented", blockChain) } func (e *XRP) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { From 0eb55c8644e4435c512a3360da778b56373432f3 Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Mon, 19 Feb 2024 09:38:07 +0800 Subject: [PATCH 09/10] refact: lock --- collect/service/cmd/chain/tron2/service.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/collect/service/cmd/chain/tron2/service.go b/collect/service/cmd/chain/tron2/service.go index e324911..de011aa 100644 --- a/collect/service/cmd/chain/tron2/service.go +++ b/collect/service/cmd/chain/tron2/service.go @@ -451,8 +451,9 @@ func (s *Service) reload() { } } + activeAddress := rebuildAddr(finalAddressList) s.lock.Lock() - s.monitorAddress = rebuildAddr(finalAddressList) + s.monitorAddress = activeAddress s.lock.Unlock() } From 67c044e5a44dc239c1370f5093803a3287aae590 Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Mon, 19 Feb 2024 15:57:45 +0800 Subject: [PATCH 10/10] fix: get contract info to tron --- blockchain/chain/tron/tron.go | 32 ++++++++++++--------- blockchain/service/tron/tron.go | 28 +++++++++++++++++- common/util/hexutils.go | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 14 deletions(-) diff --git a/blockchain/chain/tron/tron.go b/blockchain/chain/tron/tron.go index 274b40c..26aa731 100644 --- a/blockchain/chain/tron/tron.go +++ b/blockchain/chain/tron/tron.go @@ -107,6 +107,7 @@ func (t *Tron) SendRequestToChainByHttp(host string, token string, query string) } func (t *Tron) GetToken20ByHttp(host string, token string, contractAddress string, userAddress string) (map[string]interface{}, error) { + //https://api.trongrid.io/wallet/triggerconstantcontract mp := make(map[string]interface{}, 2) if !strings.HasPrefix(userAddress, "0x41") && !strings.HasPrefix(userAddress, "41") { @@ -141,15 +142,15 @@ func (t *Tron) GetToken20ByHttp(host string, token string, contractAddress strin return nil, errors.New("contract is error") } - //name, err := t.GetTokenNameByHttp(host, token, contractAddress, userAddress) - //if err == nil { - // mp["name"] = name - //} - // - //symbol, err := t.GetTokenSymbolByHttp(host, token, contractAddress, userAddress) - //if err == nil { - // mp["symbol"] = symbol - //} + name, err := t.GetTokenNameByHttp(host, token, contractAddress, userAddress) + if err == nil { + mp["name"] = name + } + + symbol, err := t.GetTokenSymbolByHttp(host, token, contractAddress, userAddress) + if err == nil { + mp["symbol"] = symbol + } return mp, nil } @@ -206,8 +207,13 @@ func (t *Tron) GetTokenBalanceByHttp2(host string, token string, contractAddress //log.Println(resp) r := gjson.Parse(resp).Get("constant_result") if r.Exists() { - balance, _ := strconv.ParseInt(r.Array()[0].String(), 16, 64) - return fmt.Sprintf("%v", balance), nil + balance := r.Array()[0].String() + b, err := util.ParseTRC20NumericProperty(balance) + if err != nil { + return "", err + } + //balance, _ := strconv.ParseInt(r.Array()[0].String(), 16, 64) + return fmt.Sprintf("%v", b.Int64()), nil } return "", errors.New("no data") @@ -231,7 +237,7 @@ func (t *Tron) GetTokenNameByHttp(host string, token string, contractAddress str r := gjson.Parse(resp).Get("constant_result") if r.Exists() { name := r.Array()[0].String() - return fmt.Sprintf("%v", name), nil + return util.ParseTRC20StringProperty(name) } return "", errors.New("no data") @@ -255,7 +261,7 @@ func (t *Tron) GetTokenSymbolByHttp(host string, token string, contractAddress s r := gjson.Parse(resp).Get("constant_result") if r.Exists() { symbol := r.Array()[0].String() - return fmt.Sprintf("%v", symbol), nil + return util.ParseTRC20StringProperty(symbol) } return "", errors.New("no data") diff --git a/blockchain/service/tron/tron.go b/blockchain/service/tron/tron.go index c798ec6..05de075 100644 --- a/blockchain/service/tron/tron.go +++ b/blockchain/service/tron/tron.go @@ -23,7 +23,33 @@ type Tron struct { } func (t *Tron) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - return "", fmt.Errorf("blockchain:%v,the method has not been implemented", chainCode) + + cluster := t.BalanceCluster(false) + if cluster == nil { + //不存在节点 + return "", errors.New("blockchain node has not found") + } + + var resp map[string]any + var err error + if eip == "721" { + url := fmt.Sprintf("%v/%v", cluster.NodeUrl, "wallet/triggerconstantcontract") + resp, err = t.blockChainClient.GetToken721(url, cluster.NodeToken, contractAddr, contractAddr) + } else if eip == "1155" { + resp, err = t.blockChainClient.GetToken1155(cluster.NodeUrl, cluster.NodeToken, contractAddr, contractAddr) + } else if eip == "20" { + url := fmt.Sprintf("%v/%v", cluster.NodeUrl, "wallet/triggerconstantcontract") + resp, err = t.blockChainClient.GetToken20ByHttp(url, cluster.NodeToken, contractAddr, contractAddr) + } else { + return "", fmt.Errorf("unknow the eip:%v", eip) + } + + if err != nil { + cluster.ErrorCount += 1 + } + + bs, _ := json.Marshal(resp) + return string(bs), err } func (t *Tron) StartWDT() { diff --git a/common/util/hexutils.go b/common/util/hexutils.go index bd696a4..65f7d6d 100644 --- a/common/util/hexutils.go +++ b/common/util/hexutils.go @@ -1,12 +1,14 @@ package util import ( + "bytes" "encoding/hex" "errors" "fmt" "math/big" "strconv" "strings" + "unicode/utf8" ) var ( @@ -223,3 +225,52 @@ func Int2Hex(data string) (string, error) { return hexString, nil } + +func ParseTRC20NumericProperty(data string) (*big.Int, error) { + if Has0xPrefix(data) { + data = data[2:] + } + if len(data) == 64 { + var n big.Int + _, ok := n.SetString(data, 16) + if ok { + return &n, nil + } + } + + if len(data) == 0 { + return big.NewInt(0), nil + } + + return nil, fmt.Errorf("cannot parse %s", data) +} +func ParseTRC20StringProperty(data string) (string, error) { + if Has0xPrefix(data) { + data = data[2:] + } + if len(data) > 128 { + n, _ := ParseTRC20NumericProperty(data[64:128]) + if n != nil { + l := n.Uint64() + if 2*int(l) <= len(data)-128 { + b, err := hex.DecodeString(data[128 : 128+2*l]) + if err == nil { + return string(b), nil + } + } + } + } else if len(data) == 64 { + // allow string properties as 32 bytes of UTF-8 data + b, err := hex.DecodeString(data) + if err == nil { + i := bytes.Index(b, []byte{0}) + if i > 0 { + b = b[:i] + } + if utf8.Valid(b) { + return string(b), nil + } + } + } + return "", fmt.Errorf("cannot parse %s,", data) +}