@@ -182,3 +182,57 @@ func (s *Substrate) UpdateTwin(identity Identity, relay string, pk []byte) (uint
182182
183183 return s.GetTwinByPubKey(identity.PublicKey())
184184}
185+
186+ // BondTwinAccount bonds a twin with a stash/savings account
187+ // The stash account should initiate the bond between the twin and the account address
188+ func (s *Substrate) BondTwinAccount(identity Identity, twinID uint32) error {
189+ cl, meta, err := s.GetClient()
190+ if err != nil {
191+ return err
192+ }
193+
194+ c, err := types.NewCall(meta, "TfgridModule.bond_twin_account", twinID)
195+ if err != nil {
196+ return errors.Wrap(err, "failed to create call")
197+ }
198+
199+ if _, err := s.Call(cl, meta, identity, c); err != nil {
200+ return errors.Wrap(err, "failed to bond twin account")
201+ }
202+
203+ return nil
204+ }
205+
206+ // GetTwinBondedAccount gets the bonded/stash account for a twin
207+ func (s *Substrate) GetTwinBondedAccount(twinID uint32) (*AccountID, error) {
208+ cl, meta, err := s.GetClient()
209+ if err != nil {
210+ return nil, err
211+ }
212+
213+ bytes, err := Encode(twinID)
214+ if err != nil {
215+ return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
216+ }
217+
218+ key, err := types.CreateStorageKey(meta, "TfgridModule", "TwinBoundedAccountID", bytes, nil)
219+ if err != nil {
220+ return nil, errors.Wrap(err, "failed to create substrate query key")
221+ }
222+
223+ raw, err := cl.RPC.State.GetStorageRawLatest(key)
224+ if err != nil {
225+ return nil, errors.Wrap(err, "failed to lookup twin bonded account")
226+ }
227+
228+ if len(*raw) == 0 {
229+ return nil, nil // No bonded account found
230+ }
231+
232+ var accountID AccountID
233+ if err := Decode(*raw, &accountID); err != nil {
234+ return nil, errors.Wrap(err, "failed to decode account")
235+ }
236+
237+ return &accountID, nil
238+ }
0 commit comments