Skip to content

Commit 62c2f0f

Browse files
committed
add stash account functions
1 parent b950278 commit 62c2f0f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

clients/tfchain-client-go/twin.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

clients/tfchain-client-go/twin_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,37 @@ func TestTwin(t *testing.T) {
2323

2424
require.Equal(t, uint32(twin.ID), id)
2525
}
26+
27+
func TestBondTwinAccount(t *testing.T) {
28+
cl := startLocalConnection(t)
29+
defer cl.Close()
30+
31+
twinID := assertCreateTwin(t, cl, AccountBob)
32+
33+
// Get Alice Stash identity to act as the bonded account
34+
stashUser := Accounts[AccountAliceStash]
35+
stashIdentity, err := NewIdentityFromSr25519Phrase(stashUser.Phrase)
36+
require.NoError(t, err)
37+
38+
err = cl.BondTwinAccount(stashIdentity, twinID)
39+
require.NoError(t, err)
40+
41+
bondedAccount, err := cl.GetTwinBondedAccount(twinID)
42+
require.NoError(t, err)
43+
require.NotNil(t, bondedAccount)
44+
45+
stashAccount, err := FromAddress(stashUser.Address)
46+
require.NoError(t, err)
47+
require.Equal(t, stashAccount.PublicKey(), bondedAccount.PublicKey())
48+
}
49+
50+
func TestGetTwinBondedAccount_NotBonded(t *testing.T) {
51+
cl := startLocalConnection(t)
52+
defer cl.Close()
53+
54+
twinID := assertCreateTwin(t, cl, AccountAlice)
55+
56+
bondedAccount, err := cl.GetTwinBondedAccount(twinID)
57+
require.NoError(t, err)
58+
require.Nil(t, bondedAccount)
59+
}

0 commit comments

Comments
 (0)