ethNotification

package module
v0.0.0-...-aa5b2d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2018 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golden Ethereum Notification

A Go engine to track transaction and push notifications to mobile application (Firebase Cloud Message).

Features

  • Tracking pending transaction.
  • Tracking new block.
  • Support ERC20 transaction.
  • Send message to mobile application.

Installation

Golden-eth-notifications can be installed easily by running

go get -u github.com/goldennetwork/golden-eth-notifications

Then you can import it in your project

import "github.com/goldennetwork/golden-eth-notifications"

Setting up a simple subscriber

Simple Usage
// file: main.go

package main
import (
	ethPush "github.com/goldennetwork/golden-eth-notifications"
)

func main() {
    engine := ethPush.NewEngine(ethPush.EngineConfig{
        WSURL: YOUR_WS_URL_FULLNODE,
        FCM_PUSH_KEY: YOUR_FIREBASE_PUSH_KEY,
        ENABLE_PUSH_PENDING_TX: true, // (*)
    }
    
    engine.SubscribeWallet("WALLET_NAME 1", "YOUR_WALLET_ADDRESS 1", "YOUR_DEVICE_TOKEN 1")
    engine.SubscribeWallet("WALLET_NAME 2", "YOUR_WALLET_ADDRESS 2", "YOUR_DEVICE_TOKEN 2")
    engine.Start()
}

Important Note: Setting "ENABLE_PUSH_PENDING_TX: true" require your full node run with option --rpcapi miner,txpool

Tracking and Custom message
    engine.OnBeforeSendMessage(func(tran *ethPush.Transaction, ws ethPush.WalletSubscriber, pushMess ethPush.PushMessage) {
	// do something with your transaction and message
    })
    
    engine.SetMessageTitle(func(tran *ethPush.Transaction, ws ethPush.WalletSubscriber) string {
    	// Custom push message title
	return fmt.Sprintf("Wallet %s received %s from %s", ws.WalletName, tran.Value, tran.From)
    })
    
    engine.SetMessagePayload(func(tran *ethPush.Transaction, ws ethPush.WalletSubscriber) map[string]interface{} {
    	return map[string]interface{}{
		"address": ws.WalletAddress,
		"value":   tran.Value,
		"wallet":  ws.WalletName,
		"tx":      tran,
	}
    })
    
    engine.SetAllowSendMessage(func(tran *ethPush.Transaction, ws ethPush.WalletSubscriber, pushMess ethPush.PushMessage) bool {
    	return true
    })
    
    engine.OnAfterSendMessage(func(tran *ethPush.Transaction, ws ethPush.WalletSubscriber, pushMess ethPush.PushMessage) {
    	// do something with your transaction and message
    })
Custom Data Source

By default the engine use a simple data source to store and fetch wallet subscribers. To use your custom data source by implementing this interface:

type EngineDataSource interface {
	FindWalletSubscribers(transactions []Transaction) []WalletSubscriberResult
	SubscribeWallet(walletName, walletAddress, deviceToken string)
	UnsubscribeWallet(walletAddress, deviceToken string)
	UnsubscribeWalletAllDevice(walletAddress string)
}
type CustomDataSource struct {
}

func (cds *CustomDataSource) FindWalletSubscribers(transactions []Transaction) []WalletSubscriberResult{
	// Query your database
}

func (cds *CustomDataSource) SubscribeWallet(walletName, walletAddress, deviceToken string) {
	// Insert into your database
}

func (cds *CustomDataSource) UnsubscribeWallet(walletAddress, deviceToken string) {
	// Delete data from your database
}

func (cds *CustomDataSource) UnsubscribeWalletAllDevice(walletAddress string) {
	// Delete all wallet subscribers from your database
}

engine.SetDataSource(CustomDataSource)
Custom Token Data Source
type EngineTokenDataSource interface {
	FindTokens(tokenAddress []string) []TokenContract
}
type TokenDataSource struct {
}

func (ds TokenDataSource) FindTokens(tokenAddress []string) []TokenContract {
	// Query your database
}

engine.SetTokenDataSource(TokenDataSource)
Custom Cache Data Source
type EngineCache interface {
	Get(txHash string) (CacheData, error)
	Set(txHash string, ws []WalletSubscriber, txInfo Transaction)
	Remove(txHash string)
}
type Cache struct {
}

func (c *Cache) Get(txHash string) (CacheData, error) {
}
	
func (c *Cache) Set(txHash string, ws []WalletSubscriber, txInfo Transaction) {
}
	
func (c *Cache) Remove(txHash string) {
}

engine.SetEngineCache(Cache)

License

MIT License

Copyright (C) 2018 Skylab Technology Cooperation

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTransactionNotFound = errors.New("Transaction not found")
	ErrBlockNotFound       = errors.New("Block not found")
)

Functions

func CoinToNumberInString

func CoinToNumberInString(value *big.Int, decimal int, number_precision int) string

*

  • @author: thienthongthai

func ConvertHexStringToBigInt

func ConvertHexStringToBigInt(str string) (*big.Int, error)

func ConvertInputValueWithDecimal

func ConvertInputValueWithDecimal(valStr string, decimals int8) string

func NewBlockHashHandler

func NewBlockHashHandler(e *Engine, hash string) blockHashHandler

func NewTxHashHandler

func NewTxHashHandler(e *Engine, hash string) txHashHandler

Types

type Block

type Block struct {
	Hash         string        `json:"hash"`
	Number       string        `json:"number"`
	Transactions []Transaction `json:"transactions"`
}

type CacheData

type CacheData struct {
	Transaction       Transaction
	WalletSubscribers []WalletSubscriber
}

type Engine

type Engine struct {
	ChainName string
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(config EngineConfig) Engine

func (*Engine) OnAfterSendMessage

func (e *Engine) OnAfterSendMessage(hdl func(*Transaction, WalletSubscriber, PushMessage))

func (*Engine) OnBeforeSendMessage

func (e *Engine) OnBeforeSendMessage(hdl func(*Transaction, WalletSubscriber, PushMessage))

Hook

func (*Engine) SetAllowSendMessage

func (e *Engine) SetAllowSendMessage(hdl func(*Transaction, WalletSubscriber, PushMessage) bool)

func (*Engine) SetDataSource

func (e *Engine) SetDataSource(ds EngineDataSource)

func (*Engine) SetEngineCache

func (e *Engine) SetEngineCache(ec EngineCache)

func (*Engine) SetMessagePayload

func (e *Engine) SetMessagePayload(hdl func(*Transaction, WalletSubscriber) map[string]interface{})

func (*Engine) SetMessageTitle

func (e *Engine) SetMessageTitle(hdl func(*Transaction, WalletSubscriber) string)

func (*Engine) SetTokenDataSource

func (e *Engine) SetTokenDataSource(etds EngineTokenDataSource)

func (*Engine) Start

func (e *Engine) Start()

func (*Engine) Stop

func (e *Engine) Stop()

func (*Engine) SubscribeWallet

func (e *Engine) SubscribeWallet(walletName, address, deviceToken string)

func (*Engine) UnsubscribeWallet

func (e *Engine) UnsubscribeWallet(address string)

type EngineCache

type EngineCache interface {
	Get(txHash string) (CacheData, error)
	Set(txHash string, ws []WalletSubscriber, txInfo Transaction)
	Remove(txHash string)
}

type EngineConfig

type EngineConfig struct {
	WSURL                  string
	FCM_PUSH_KEY           string
	FCM_PUSH_TITLE         string
	ENABLE_PUSH_PENDING_TX bool
	CHAIN_NAME             string
}

type EngineDataSource

type EngineDataSource interface {
	FindWalletSubscribers(transactions []Transaction) []WalletSubscriberResult
	SubscribeWallet(walletName, walletAddress, deviceToken string)
	UnsubscribeWallet(walletAddress, deviceToken string)
	UnsubscribeWalletAllDevice(walletAddress string)
}

type EngineTokenDataSource

type EngineTokenDataSource interface {
	FindTokens(tokenAddress []string) []TokenContract
}

type InputData

type InputData struct {
	MethodID          string `json:"method_id" bson:"method_id"`
	ToAddress         string `json:"to_address" bson:"to_address"`
	Value             string `json:"value" bson:"value"`
	ValueWithDecimals string `json:"value_with_dcms" bson:"value_with_dcms"`
}

func ParseInputTx

func ParseInputTx(input string, decimals int8) *InputData

type Log

type Log struct {
	Address          string   `json:"address" bson:"address"`
	Topics           []string `json:"topics" bson:"topics"`
	Data             string   `json:"data" bson:"data"`
	BlockNumber      string   `json:"blockNumber" bson:"blockNumber"`
	TransactionHash  string   `json:"transactionHash" bson:"transactionHash"`
	TransactionIndex string   `json:"transactionIndex" bson:"transactionIndex"`
	BlockHash        string   `json:"blockHash" bson:"blockHash"`
	LogIndex         string   `json:"logIndex" bson:"logIndex"`
	Removed          bool     `json:"removed" bson:"removed"`
}

type MessageHook

type MessageHook struct {
	MessageTitle   func(*Transaction, WalletSubscriber) string
	MessagePayload func(*Transaction, WalletSubscriber) map[string]interface{}
	BeforeSend     func(*Transaction, WalletSubscriber, PushMessage)
	AllowSend      func(*Transaction, WalletSubscriber, PushMessage) bool
	AfterSend      func(*Transaction, WalletSubscriber, PushMessage)
}

type MethodID

type MethodID int8
const (
	MethodIDTransferERC20Token MethodID = iota
)

func (MethodID) String

func (m MethodID) String() string

type PushMessage

type PushMessage struct {
	Title        string                 `json:"title" bson:"title"`
	Sound        string                 `json:"sound" bson:"sound"`
	Content      string                 `json:"content" bson:"content"`
	Badge        string                 `json:"badge" bson:"badge"`
	DeviceTokens []string               `json:"device_tokens" bson:"device_tokens"`
	Payload      map[string]interface{} `json:"payload" bson:"payload"`
	Responses    []PushResult           `json:"results,omitempty" bson:"results,omitempty"`
}

type PushResult

type PushResult struct {
	DeviceToken string            `json:"device_token" bson:"device_token"`
	Result      map[string]string `json:"result,omitempty" bson:"result,omitempty"`
}

type TokenContract

type TokenContract struct {
	Name            string `json:"name" bson:"name"`
	Symbol          string `json:"symbol" bson:"symbol"`
	TotalSupply     string `json:"total_supply" bson:"total_supply"`
	Decimals        int8   `json:"decimals" bson:"decimals"`
	ContractAddress string `json:"contract_address" bson:"contract_address"`
	ContractCreator string `json:"contract_creator" bson:"contract_creator"`
}

type Transaction

type Transaction struct {
	BlockHash        string   `json:"blockHash" bson:"block_hash"`
	BlockNumber      string   `json:"blockNumber" bson:"block_number"`
	From             string   `json:"from" bson:"from"`
	To               string   `json:"to" bson:"to"`
	Value            string   `json:"value" bson:"value"`
	Gas              string   `json:"gas" bson:"gas"`
	GasPrice         string   `json:"gasPrice" bson:"gas_price"`
	Hash             string   `json:"hash" bson:"hash"`
	Input            string   `json:"input" bson:"input"`
	TransactionIndex string   `json:"transactionIndex" bson:"tx_index"`
	TimeStamp        string   `json:"timeStamp" bson:"time_stamp"`
	ContractAddress  string   `json:"contractAddress" bson:"contract_address"`
	TokenName        string   `json:"tokenName" bson:"token_name"`
	TokenSymbol      string   `json:"tokenSymbol" bson:"token_symbol"`
	TokenDecimal     int      `json:"tokenDecimal" bson:"token_decimal"`
	Status           TxStatus `json:"-" bson:"status"`
	IsSeft           bool     `json:"isSeft"`
	ChainName        string   `json:"chainName"`
	Receipt          TransactionReceipt
}

func (*Transaction) IsNormalTx

func (t *Transaction) IsNormalTx() bool

type TransactionReceipt

type TransactionReceipt struct {
	ContractAddress   string `json:"contractAddress" bson:"contract_address"`
	CumulativeGasUsed string `json:"cumulativeGasUsed" bson:"cumulativeGasUsed"`
	From              string `json:"from" bson:"from"`
	To                string `json:"to" bson:"to"`
	GasUsed           string `json:"gasUsed" bson:"gasUsed"`
	Logs              []Log  `json:"logs" bson:"logs"`
	LogsBloom         string `json:"logsBloom" bson:"logsBloom"`
	Status            string `json:"status" bson:"status"`
}

type TxStatus

type TxStatus int8
const (
	Pending TxStatus = iota
	Success
	Failure
)

func (TxStatus) String

func (ts TxStatus) String() string

type Wallet

type Wallet struct {
	ID      string `json:"id" bson:"id,omitempty"`
	Name    string `json:"name" bson:"name"`
	Address string `json:"address" bson:"address"`
	Type    string `json:"type" bson:"type"`
}

type WalletPush

type WalletPush struct {
	Wallet
	DeviceUDID  string `json:"device_udid" bson:"_id"`
	DeviceToken string `json:"device_token" bson:"device_token"`
}

type WalletSubscriber

type WalletSubscriber struct {
	WalletName    string
	WalletAddress string
	DeviceToken   string
}

type WalletSubscriberResult

type WalletSubscriberResult struct {
	Transaction *Transaction
	Subscribers []WalletSubscriber
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址