Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to subscribe to starknet contract events #444

Closed
Subbitoooo opened this issue Oct 20, 2023 · 3 comments
Closed

How to subscribe to starknet contract events #444

Subbitoooo opened this issue Oct 20, 2023 · 3 comments

Comments

@Subbitoooo
Copy link

Import the StarkNet Go Library:
Import the StarkNet Go library at the beginning of your Go file.

go
Copy code
import "starknetgo"
Connect to the StarkNet Node:
Establish a connection to the StarkNet node using its endpoint.

go
Copy code
client, err := starknetgo.Dial("http://starknet-node-endpoint")
if err != nil {
log.Fatal(err)
}
defer client.Close()
Subscribe to Contract Events:
Use the StarkNet Go library to subscribe to events emitted by the smart contract. You would need the contract ABI and address.

go
Copy code
contractAddress := "0x1234567890abcdef1234567890abcdef12345678" // Replace with your contract address

// Subscribe to the event
subscription, err := client.SubscribeToContractEvents(context.TODO(), &starknetgo.ContractEventSubscription{
ContractAddress: common.HexToAddress(contractAddress),
})
if err != nil {
log.Fatal(err)
}

// Listen for events
for {
select {
case event := <-subscription.Events:
// Handle the event
log.Printf("Received event: %+v\n", event)
case err := <-subscription.Errors:
log.Fatal(err)
}
}
Replace the contract address with the actual address of your deployed contract. Additionally, you might need to replace ContractEventSubscription with the actual struct or type provided by the StarkNet Go library for event subscriptions.

Handle Events:
Inside the event handling block, you can define how your application should respond to each event emitted by the smart contract.

@cicr99
Copy link
Contributor

cicr99 commented Oct 26, 2023

I believe this is a duplicate of issue #443 , am I right? Could we close this one?

@Subbitoooo
Copy link
Author

Sorry ;( didn't see that, You're right. Have a nice day

@cicr99
Copy link
Contributor

cicr99 commented Oct 26, 2023

Thanks! No worries

@cicr99 cicr99 closed this as completed Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants