Skip to content

Commit

Permalink
Burn account amount when getting events from bridge contract
Browse files Browse the repository at this point in the history
  • Loading branch information
DarianShawn committed Sep 11, 2022
1 parent 5165068 commit e0c79c5
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
70 changes: 70 additions & 0 deletions contracts/abis/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,26 @@ const BridgeJSONABI = `[
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs":
[
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Burned",
"type": "event"
},
{
"anonymous": false,
"inputs":
Expand Down Expand Up @@ -646,6 +666,21 @@ const BridgeJSONABI = `[
"name": "Withdrawn",
"type": "event"
},
{
"inputs":
[
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "addBalance",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
Expand All @@ -661,6 +696,26 @@ const BridgeJSONABI = `[
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "burn",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
Expand Down Expand Up @@ -862,6 +917,21 @@ const BridgeJSONABI = `[
"stateMutability": "view",
"type": "function"
},
{
"inputs":
[
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "subBalance",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[],
Expand Down
45 changes: 45 additions & 0 deletions contracts/bridge/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
const (
EventDeposited = "Deposited"
EventWithdrawn = "Withdrawn"
EventBurned = "Burned"

fieldReceiver = "receiver"
fieldAmount = "amount"
fieldFee = "fee"
fieldSender = "sender"
)

// Frequently used methods. Must exist.
Expand All @@ -25,6 +27,8 @@ var (
BridgeDepositedEventID = types.Hash(BridgeDepositedEvent.ID())
BridgeWithdrawnEvent = abis.BridgeABI.Events[EventWithdrawn]
BridgeWithdrawnEventID = types.Hash(BridgeWithdrawnEvent.ID())
BridgeEventBurnedEvent = abis.BridgeABI.Events[EventBurned]
BridgeBurnedEventID = types.Hash(BridgeEventBurnedEvent.ID())
)

type DepositedLog struct {
Expand Down Expand Up @@ -125,3 +129,44 @@ func getBigIntFromWithdrawnLog(log map[string]interface{}, key string) (*big.Int

return bigVal, nil
}

type BurnedLog struct {
Sender types.Address
Amount *big.Int
}

func ParseBridgeBurnedLog(log *types.Log) (*BurnedLog, error) {
topics := make([]web3.Hash, 0, len(log.Topics))
for _, topic := range log.Topics {
topics = append(topics, web3.Hash(topic))
}

w3Log, err := BridgeEventBurnedEvent.ParseLog(&web3.Log{
Address: web3.Address(log.Address),
Topics: topics,
Data: log.Data,
})
if err != nil {
return nil, err
}

sender, ok := w3Log[fieldSender]
if !ok {
return nil, errors.New("address not exists in Deposited event")
}

account, ok := sender.(web3.Address)
if !ok {
return nil, errors.New("address downcast failed")
}

amount, err := getBigIntFromWithdrawnLog(w3Log, fieldAmount)
if err != nil {
return nil, err
}

return &BurnedLog{
Sender: types.Address(account),
Amount: amount,
}, nil
}
10 changes: 10 additions & 0 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ func (t *Transition) handleBridgeLogs(msg *types.Transaction, logs []*types.Log)

// the fee goes to system Vault contract
t.state.AddBalance(systemcontracts.AddrVaultContract, parsedLog.Fee)
case bridge.BridgeBurnedEventID:
parsedLog, err := bridge.ParseBridgeBurnedLog(log)
if err != nil {
return err
}

// burn
if err := t.state.SubBalance(parsedLog.Sender, parsedLog.Amount); err != nil {
return err
}
}
}

Expand Down

0 comments on commit e0c79c5

Please sign in to comment.