/Smart Contract ↔ Backend Integration
🔗
Smart Contract ↔ Backend Integration
Day 1 · Smart Contract + Backend Integration · 45 min
ABI Encoding/Decoding from Go:
import "github.com/ethereum/go-ethereum/accounts/abi"
// Parse contract ABI
parsedABI, _ := abi.JSON(strings.NewReader(contractABIJson))
// Encode function call
data, _ := parsedABI.Pack("settle", paymentId, amount)
// Decode return values / event data
results, _ := parsedABI.Unpack("getBalance", returnData)
go-ethereum Bindings: Use abigen to generate type-safe Go bindings from Solidity ABI.
abigen --abi Settlement.abi --pkg settlement --out settlement.go
This generates a Go struct with methods mirroring every contract function. Compile-time type safety, auto-completion, and no manual ABI packing.
Event Log Parsing:
// Subscribe to events
query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddr},
Topics: [][]common.Hash{{settledEventSig}},
}
logs, _ := client.FilterLogs(ctx, query)
for _, log := range logs {
event, _ := parsedABI.Unpack("PaymentSettled", log.Data)
// event contains decoded paymentId, amount, timestamp
// log.Topics[1] contains indexed paymentId
}
Safe Transaction Submission from Backend:
1. Build the inner transaction (target, value, data)
Calculate Safe transaction hash (EIP-712 typed data)
Collect M-of-N signatures (backend HSM + co-signers)
Submit to Safe Transaction Service API
Or call execTransaction() directly if threshold met
For Gnosis Pay: the backend uses the Safe Roles Module for scoped delegation (EURe only, issuer Safe only, daily limit) and Gelato Relay for gasless execution — no per-tx user signature needed for card settlements.Key Points
- ▸Use abigen for type-safe Go contract bindings
- ▸ABI.Pack/Unpack for manual encoding when needed
- ▸FilterLogs + ABI.Unpack for event parsing
- ▸Safe tx submission: build → hash → sign → submit
- ▸Backend holds a limited signing key for routine operations