2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-27 02:55:09 +00:00
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
package dai
|
2018-08-27 02:55:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-08-31 19:48:43 +00:00
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
|
2018-09-19 17:22:05 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
2018-08-27 02:55:09 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-08-31 19:48:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/helpers"
|
2018-08-27 02:55:09 +00:00
|
|
|
)
|
|
|
|
|
2018-08-28 16:45:31 +00:00
|
|
|
// Converter converts a raw event log into its corresponding entity
|
|
|
|
// and can subsequently convert the entity into a model
|
|
|
|
|
2018-08-27 02:55:09 +00:00
|
|
|
type ERC20ConverterInterface interface {
|
|
|
|
ToTransferEntity(watchedEvent core.WatchedEvent) (*TransferEntity, error)
|
2018-09-19 03:15:49 +00:00
|
|
|
ToTransferModel(entity *TransferEntity) *event_triggered.TransferModel
|
2018-08-27 02:55:09 +00:00
|
|
|
ToApprovalEntity(watchedEvent core.WatchedEvent) (*ApprovalEntity, error)
|
2018-09-19 03:15:49 +00:00
|
|
|
ToApprovalModel(entity *ApprovalEntity) *event_triggered.ApprovalModel
|
2018-08-27 02:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ERC20Converter struct {
|
2018-09-19 17:22:05 +00:00
|
|
|
config shared.ContractConfig
|
2018-08-27 02:55:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 17:22:05 +00:00
|
|
|
func NewERC20Converter(config shared.ContractConfig) (*ERC20Converter, error) {
|
2018-08-31 19:48:43 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
config.ParsedAbi, err = geth.ParseAbi(config.Abi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
converter := &ERC20Converter{
|
2018-08-27 02:55:09 +00:00
|
|
|
config: config,
|
|
|
|
}
|
2018-08-31 19:48:43 +00:00
|
|
|
|
|
|
|
return converter, nil
|
2018-08-27 02:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c ERC20Converter) ToTransferEntity(watchedEvent core.WatchedEvent) (*TransferEntity, error) {
|
|
|
|
result := &TransferEntity{}
|
|
|
|
contract := bind.NewBoundContract(common.HexToAddress(c.config.Address), c.config.ParsedAbi, nil, nil, nil)
|
|
|
|
event := helpers.ConvertToLog(watchedEvent)
|
2018-08-31 19:48:43 +00:00
|
|
|
err := contract.UnpackLog(result, constants.TransferEvent.String(), event)
|
2018-08-27 02:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
result.TokenName = c.config.Name
|
|
|
|
result.TokenAddress = common.HexToAddress(c.config.Address)
|
|
|
|
result.Block = watchedEvent.BlockNumber
|
|
|
|
result.TxHash = watchedEvent.TxHash
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
func (c ERC20Converter) ToTransferModel(entity *TransferEntity) *event_triggered.TransferModel {
|
2018-08-31 19:48:43 +00:00
|
|
|
to := entity.Dst.String()
|
|
|
|
from := entity.Src.String()
|
|
|
|
tokens := entity.Wad.String()
|
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
return &event_triggered.TransferModel{
|
2018-08-27 02:55:09 +00:00
|
|
|
TokenName: c.config.Name,
|
|
|
|
TokenAddress: c.config.Address,
|
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Tokens: tokens,
|
2018-08-31 19:48:43 +00:00
|
|
|
Block: entity.Block,
|
|
|
|
TxHash: entity.TxHash,
|
2018-08-27 02:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ERC20Converter) ToApprovalEntity(watchedEvent core.WatchedEvent) (*ApprovalEntity, error) {
|
|
|
|
result := &ApprovalEntity{}
|
|
|
|
contract := bind.NewBoundContract(common.HexToAddress(c.config.Address), c.config.ParsedAbi, nil, nil, nil)
|
|
|
|
event := helpers.ConvertToLog(watchedEvent)
|
2018-08-31 19:48:43 +00:00
|
|
|
err := contract.UnpackLog(result, constants.ApprovalEvent.String(), event)
|
2018-08-27 02:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
result.TokenName = c.config.Name
|
|
|
|
result.TokenAddress = common.HexToAddress(c.config.Address)
|
|
|
|
result.Block = watchedEvent.BlockNumber
|
|
|
|
result.TxHash = watchedEvent.TxHash
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
func (c ERC20Converter) ToApprovalModel(entity *ApprovalEntity) *event_triggered.ApprovalModel {
|
2018-08-31 19:48:43 +00:00
|
|
|
tokenOwner := entity.Src.String()
|
|
|
|
spender := entity.Guy.String()
|
|
|
|
tokens := entity.Wad.String()
|
|
|
|
|
2018-09-19 03:15:49 +00:00
|
|
|
return &event_triggered.ApprovalModel{
|
2018-08-27 02:55:09 +00:00
|
|
|
TokenName: c.config.Name,
|
|
|
|
TokenAddress: c.config.Address,
|
|
|
|
Owner: tokenOwner,
|
|
|
|
Spender: spender,
|
|
|
|
Tokens: tokens,
|
2018-08-31 19:48:43 +00:00
|
|
|
Block: entity.Block,
|
|
|
|
TxHash: entity.TxHash,
|
2018-08-27 02:55:09 +00:00
|
|
|
}
|
|
|
|
}
|