Skip to content

Commit

Permalink
fix starknet codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Oct 18, 2024
1 parent 4bf6602 commit 7f99cab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
24 changes: 11 additions & 13 deletions starknet-events/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type Contract struct {
Abi *ABI
emptyABI bool
abiFetchedInThisSession bool

PaddedAddress string
}

func (c *Contract) Identifier() string { return c.Name }
Expand Down Expand Up @@ -89,14 +87,14 @@ func (c *Contract) fetchABI(config *ChainConfig) (string, error) {
}

emptyField := felt.Felt{}
addressToFelt, err := emptyField.SetString(c.PaddedAddress)
addressToFelt, err := emptyField.SetString(c.AddressWithoutPrefix())
if err != nil {
return "", fmt.Errorf("converting address to felt: %w", err)
}

classOutput, err := client.ClassAt(ctx, blockId, addressToFelt)
if err != nil {
return "", fmt.Errorf("calling class at for adderss: %s : %w", c.PaddedAddress, err)
return "", fmt.Errorf("calling class at for adderss: %s : %w", c.AddressWithoutPrefix(), err)
}

var contractABI string
Expand All @@ -116,19 +114,19 @@ func (c *Contract) fetchABI(config *ChainConfig) (string, error) {
// In some explorers (Ex: Starkscan) the address is padded on 66 characters with the prefix 0x
// The Contract is containing both, the padded address or the raw one without leading zeros...
func (c *Contract) handleContractAddress(inputAddress string) {
// ADDRESS ALREADY PADDED
// Address padded
if len(inputAddress) == 66 {
c.Address = inputAddress[0:2] + strings.TrimLeft(inputAddress[2:], "0")
c.PaddedAddress = inputAddress

c.Address = inputAddress
return
}

// ADDRESS NOT PADDED

c.Address = inputAddress
addressWithoutPrefix := inputAddress[2:]
// Address not padded
withoutPrefix := strings.TrimPrefix(inputAddress, "0x")
c.Address = "0x" + strings.Repeat("0", 64-len(withoutPrefix)) + withoutPrefix

c.PaddedAddress = strings.Repeat("0", 64-len(addressWithoutPrefix)) + addressWithoutPrefix
return
}

func (c *Contract) AddressWithoutPrefix() string {
return strings.TrimPrefix(c.Address, "0x")
}
2 changes: 1 addition & 1 deletion starknet-events/templates/src/lib.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.PaddedAddress }}" {
if event_from_address != "{{ $contract.AddressWithoutPrefix }}" {
continue;
}

Expand Down

0 comments on commit 7f99cab

Please sign in to comment.