Skip to content

Commit

Permalink
fix starknet-events
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Sep 13, 2024
1 parent d4a8431 commit 9d20961
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
16 changes: 15 additions & 1 deletion starknet-events/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,22 @@ type Contract struct {
PaddedAddress string
}

func (c *Contract) AddressWithoutLead() string {
return c.PaddedAddress[2:]
}

func (c *Contract) Identifier() string { return c.Name }
func (c *Contract) IdentifierCapitalize() string {
if len(c.Name) == 0 {
return c.Name
}

if len(c.Name) == 1 {
return strings.ToUpper(c.Name)
}

return strings.ToUpper(string(c.Name[0])) + c.Name[1:]
}
func (c *Contract) SetAliases() {
events := c.Abi.decodedAbi.EventsBySelector

Expand Down Expand Up @@ -108,7 +122,7 @@ func (c *Contract) fetchABI(config *ChainConfig) (string, error) {
func (c *Contract) handleContractAddress(inputAddress string) {
// ADDRESS ALREADY PADDED
if len(inputAddress) == 66 {
c.Address = inputAddress[2:] + strings.TrimLeft(inputAddress[2:], "0")
c.Address = inputAddress[0:2] + strings.TrimLeft(inputAddress[2:], "0")
c.PaddedAddress = inputAddress

return
Expand Down
19 changes: 18 additions & 1 deletion starknet-events/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,27 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return loop.Seq(cmd(MsgInvalidContractAddress{err}), cmd(AskContractAddress{}))
}

contract.Address = inputAddress
contract.handleContractAddress(inputAddress)

return c.NextStep()

case AskConfirmContractABI:
return c.Action(InputConfirmContractABI{}).
Confirm("Do you want to proceed with this ABI?", "Yes", "No").
Cmd()

case InputConfirmContractABI:
if msg.Affirmative {
return c.NextStep()
}
contract := c.contextContract()
if contract == nil {
return QuitInvalidContext
}
contract.RawABI = nil
contract.abiFetchedInThisSession = false
return cmd(AskContractABI{})

case codegen.InputProjectName:
c.State.Name = msg.Value
return c.NextStep()
Expand Down
6 changes: 3 additions & 3 deletions starknet-events/templates/src/lib.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod abi;

use pb::starknet::v1::*;
{{- range $i, $contract := .Contracts }}
use crate::abi::{{ $contract.Identifier }}_contract::Event as {{ toUpper $contract.Identifier}}Event;
use crate::abi::{{ $contract.Identifier }}_contract::Event as {{ $contract.IdentifierCapitalize }}Event;
{{- end }}

use substreams::Hex;
Expand All @@ -26,7 +26,7 @@ fn map_{{ $contract.Identifier }}_events(transactions: Transactions) -> Result<E
for event in data_events {
let event_from_address = Hex(event.from_address.as_slice()).to_string();

if event_from_address != "{{ $contract.Address }}" {
if event_from_address != "{{ $contract.AddressWithoutLead }}" {
continue;
}

Expand All @@ -51,7 +51,7 @@ fn map_{{ $contract.Identifier }}_events(transactions: Transactions) -> Result<E
transaction_hash: Felt::default(),
};

let {{ $contract.Identifier}}_event = {{ toUpper $contract.Identifier}}Event::try_from(emitted_event).unwrap();
let {{ $contract.Identifier}}_event = {{ $contract.IdentifierCapitalize }}Event::try_from(emitted_event).unwrap();

let event_json = serde_json::to_string(&{{ $contract.Identifier}}_event).unwrap();
let event = Event {
Expand Down
2 changes: 1 addition & 1 deletion starknet-events/templates/substreams.yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ modules:
inputs:
- map: starknet:filtered_transactions
output:
type: proto:sf.substreams.starknet.v1.Events
type: proto:starknet.v1.Events
{{- end }}

network: {{ .ChainName }}
Expand Down

0 comments on commit 9d20961

Please sign in to comment.