Skip to content

Commit 84383a4

Browse files
authored
chore: allow builtin revision querying (#1097)
1 parent 1205dd7 commit 84383a4

7 files changed

Lines changed: 121 additions & 35 deletions

File tree

thorclient/builtin/authority.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
type Authority struct {
2121
contract bind.Contract
22+
revision string
2223
}
2324

2425
func NewAuthority(client *thorclient.Client) (*Authority, error) {
@@ -31,6 +32,14 @@ func NewAuthority(client *thorclient.Client) (*Authority, error) {
3132
}, nil
3233
}
3334

35+
// Revision creates a new Authority instance with the specified revision.
36+
func (a *Authority) Revision(rev string) *Authority {
37+
return &Authority{
38+
contract: a.contract,
39+
revision: rev,
40+
}
41+
}
42+
3443
func (a *Authority) Raw() bind.Contract {
3544
return a.contract
3645
}
@@ -39,7 +48,7 @@ func (a *Authority) Raw() bind.Contract {
3948
func (a *Authority) First() (thor.Address, error) {
4049
// todo add sanity check on the methods and arguments
4150
out := new(common.Address)
42-
if err := a.contract.Method("first").Call().ExecuteInto(&out); err != nil {
51+
if err := a.contract.Method("first").Call().AtRevision(a.revision).ExecuteInto(&out); err != nil {
4352
return thor.Address{}, err
4453
}
4554
return thor.Address(*out), nil
@@ -48,7 +57,7 @@ func (a *Authority) First() (thor.Address, error) {
4857
// Next returns the next authority node after the given node master
4958
func (a *Authority) Next(nodeMaster thor.Address) (thor.Address, error) {
5059
out := new(common.Address)
51-
if err := a.contract.Method("next", common.Address(nodeMaster)).Call().ExecuteInto(&out); err != nil {
60+
if err := a.contract.Method("next", common.Address(nodeMaster)).Call().AtRevision(a.revision).ExecuteInto(&out); err != nil {
5261
return thor.Address{}, err
5362
}
5463
return thor.Address(*out), nil
@@ -57,7 +66,7 @@ func (a *Authority) Next(nodeMaster thor.Address) (thor.Address, error) {
5766
// Executor returns the executor address
5867
func (a *Authority) Executor() (thor.Address, error) {
5968
out := new(common.Address)
60-
if err := a.contract.Method("executor").Call().ExecuteInto(&out); err != nil {
69+
if err := a.contract.Method("executor").Call().AtRevision(a.revision).ExecuteInto(&out); err != nil {
6170
return thor.Address{}, err
6271
}
6372
return thor.Address(*out), nil
@@ -78,7 +87,7 @@ func (a *Authority) Get(nodeMaster thor.Address) (*AuthorityNode, error) {
7887
out[2] = new(common.Hash)
7988
out[3] = new(bool)
8089

81-
if err := a.contract.Method("get", common.Address(nodeMaster)).Call().ExecuteInto(&out); err != nil {
90+
if err := a.contract.Method("get", common.Address(nodeMaster)).Call().AtRevision(a.revision).ExecuteInto(&out); err != nil {
8291
return nil, err
8392
}
8493

thorclient/builtin/energy.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
// Energy is a type-safe smart contract wrapper of VTHO.
2323
type Energy struct {
2424
contract bind.Contract
25+
revision string
2526
}
2627

2728
func NewEnergy(client *thorclient.Client) (*Energy, error) {
@@ -34,14 +35,22 @@ func NewEnergy(client *thorclient.Client) (*Energy, error) {
3435
}, nil
3536
}
3637

38+
// Revision creates a new Energy instance with the specified revision.
39+
func (e *Energy) Revision(rev string) *Energy {
40+
return &Energy{
41+
contract: e.contract,
42+
revision: rev,
43+
}
44+
}
45+
3746
func (e *Energy) Raw() bind.Contract {
3847
return e.contract
3948
}
4049

4150
// Name returns the name of the token
4251
func (e *Energy) Name() (string, error) {
4352
var name string
44-
if err := e.contract.Method("name").Call().ExecuteInto(&name); err != nil {
53+
if err := e.contract.Method("name").Call().AtRevision(e.revision).ExecuteInto(&name); err != nil {
4554
return "", err
4655
}
4756
return name, nil
@@ -50,7 +59,7 @@ func (e *Energy) Name() (string, error) {
5059
// Symbol returns the symbol of the token
5160
func (e *Energy) Symbol() (string, error) {
5261
var symbol string
53-
if err := e.contract.Method("symbol").Call().ExecuteInto(&symbol); err != nil {
62+
if err := e.contract.Method("symbol").Call().AtRevision(e.revision).ExecuteInto(&symbol); err != nil {
5463
return "", err
5564
}
5665
return symbol, nil
@@ -59,7 +68,7 @@ func (e *Energy) Symbol() (string, error) {
5968
// Decimals returns the number of decimals the token uses
6069
func (e *Energy) Decimals() (uint8, error) {
6170
var decimals uint8
62-
if err := e.contract.Method("decimals").Call().ExecuteInto(&decimals); err != nil {
71+
if err := e.contract.Method("decimals").Call().AtRevision(e.revision).ExecuteInto(&decimals); err != nil {
6372
return 0, err
6473
}
6574
return decimals, nil
@@ -68,7 +77,7 @@ func (e *Energy) Decimals() (uint8, error) {
6877
// TotalSupply returns the total token supply
6978
func (e *Energy) TotalSupply() (*big.Int, error) {
7079
totalSupply := new(big.Int)
71-
if err := e.contract.Method("totalSupply").Call().ExecuteInto(&totalSupply); err != nil {
80+
if err := e.contract.Method("totalSupply").Call().AtRevision(e.revision).ExecuteInto(&totalSupply); err != nil {
7281
return nil, err
7382
}
7483
return totalSupply, nil
@@ -77,7 +86,7 @@ func (e *Energy) TotalSupply() (*big.Int, error) {
7786
// TotalBurned returns the total amount of burned tokens
7887
func (e *Energy) TotalBurned() (*big.Int, error) {
7988
totalBurned := new(big.Int)
80-
if err := e.contract.Method("totalBurned").Call().ExecuteInto(&totalBurned); err != nil {
89+
if err := e.contract.Method("totalBurned").Call().AtRevision(e.revision).ExecuteInto(&totalBurned); err != nil {
8190
return nil, err
8291
}
8392
return totalBurned, nil
@@ -86,7 +95,7 @@ func (e *Energy) TotalBurned() (*big.Int, error) {
8695
// BalanceOf returns the token balance of the specified address
8796
func (e *Energy) BalanceOf(owner thor.Address) (*big.Int, error) {
8897
balanceOf := new(big.Int)
89-
if err := e.contract.Method("balanceOf", owner).Call().ExecuteInto(&balanceOf); err != nil {
98+
if err := e.contract.Method("balanceOf", owner).Call().AtRevision(e.revision).ExecuteInto(&balanceOf); err != nil {
9099
return nil, err
91100
}
92101
return balanceOf, nil
@@ -95,7 +104,7 @@ func (e *Energy) BalanceOf(owner thor.Address) (*big.Int, error) {
95104
// Allowance returns the amount of tokens approved by the owner to be spent by the spender
96105
func (e *Energy) Allowance(owner, spender thor.Address) (*big.Int, error) {
97106
allowance := new(big.Int)
98-
if err := e.contract.Method("allowance", owner, spender).Call().ExecuteInto(&allowance); err != nil {
107+
if err := e.contract.Method("allowance", owner, spender).Call().AtRevision(e.revision).ExecuteInto(&allowance); err != nil {
99108
return nil, err
100109
}
101110
return allowance, nil

thorclient/builtin/energy_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,20 @@ func TestEnergy(t *testing.T) {
112112
require.True(t, found, "Transfer event should be found in the logs")
113113
})
114114
}
115+
116+
func TestEnergy_Revision(t *testing.T) {
117+
chain, client := newChain(t, false)
118+
119+
energy, err := NewEnergy(client)
120+
require.NoError(t, err)
121+
122+
require.NoError(t, chain.MintBlock(genesis.DevAccounts()[0]))
123+
require.NoError(t, chain.MintBlock(genesis.DevAccounts()[1]))
124+
125+
supplyBlock1, err := energy.Revision("1").TotalSupply()
126+
require.NoError(t, err)
127+
supplyBlock2, err := energy.Revision("2").TotalSupply()
128+
require.NoError(t, err)
129+
130+
require.Greater(t, supplyBlock2.Cmp(supplyBlock1), 0, "Total supply should increase with each block")
131+
}

thorclient/builtin/executor.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
type Executor struct {
2121
contract bind.Contract
22+
revision string
2223
}
2324

2425
func NewExecutor(client *thorclient.Client) (*Executor, error) {
@@ -31,6 +32,14 @@ func NewExecutor(client *thorclient.Client) (*Executor, error) {
3132
}, nil
3233
}
3334

35+
// Revision creates a new Executor instance with the specified revision.
36+
func (e *Executor) Revision(rev string) *Executor {
37+
return &Executor{
38+
contract: e.contract,
39+
revision: rev,
40+
}
41+
}
42+
3443
func (e *Executor) Raw() bind.Contract {
3544
return e.contract
3645
}
@@ -45,7 +54,7 @@ func (e *Executor) Approvers(address thor.Address) (*Approver, error) {
4554
out[0] = new(common.Hash)
4655
out[1] = new(bool)
4756

48-
if err := e.contract.Method("approvers", address).Call().ExecuteInto(&out); err != nil {
57+
if err := e.contract.Method("approvers", address).Call().AtRevision(e.revision).ExecuteInto(&out); err != nil {
4958
return nil, fmt.Errorf("failed to call approvers: %w", err)
5059
}
5160

@@ -75,7 +84,7 @@ func (e *Executor) Proposals(proposalID thor.Bytes32) (*Proposal, error) {
7584
out[5] = new(common.Address)
7685
out[6] = new([]byte)
7786

78-
if err := e.contract.Method("proposals", proposalID).Call().ExecuteInto(&out); err != nil {
87+
if err := e.contract.Method("proposals", proposalID).Call().AtRevision(e.revision).ExecuteInto(&out); err != nil {
7988
return nil, fmt.Errorf("failed to call proposals: %w", err)
8089
}
8190

@@ -92,7 +101,7 @@ func (e *Executor) Proposals(proposalID thor.Bytes32) (*Proposal, error) {
92101

93102
func (e *Executor) ApproverCount() (uint8, error) {
94103
var count uint8
95-
if err := e.contract.Method("approverCount").Call().ExecuteInto(&count); err != nil {
104+
if err := e.contract.Method("approverCount").Call().AtRevision(e.revision).ExecuteInto(&count); err != nil {
96105
return 0, fmt.Errorf("failed to call approverCount: %w", err)
97106
}
98107
return count, nil

thorclient/builtin/params.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
type Params struct {
2222
contract bind.Contract
23+
revision string
2324
}
2425

2526
func NewParams(client *thorclient.Client) (*Params, error) {
@@ -32,6 +33,14 @@ func NewParams(client *thorclient.Client) (*Params, error) {
3233
}, nil
3334
}
3435

36+
// Revision creates a new Params instance with the specified revision.
37+
func (p *Params) Revision(rev string) *Params {
38+
return &Params{
39+
contract: p.contract,
40+
revision: rev,
41+
}
42+
}
43+
3544
func (p *Params) Raw() bind.Contract {
3645
return p.contract
3746
}
@@ -42,7 +51,7 @@ func (p *Params) Set(key thor.Bytes32, value *big.Int) bind.MethodBuilder {
4251

4352
func (p *Params) Get(key thor.Bytes32) (*big.Int, error) {
4453
out := new(big.Int)
45-
if err := p.contract.Method("get", key).Call().ExecuteInto(&out); err != nil {
54+
if err := p.contract.Method("get", key).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
4655
return nil, err
4756
}
4857
return out, nil

thorclient/builtin/prototype.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
type Prototype struct {
2020
contract bind.Contract
21+
revision string
2122
}
2223

2324
func NewPrototype(client *thorclient.Client) (*Prototype, error) {
@@ -30,14 +31,22 @@ func NewPrototype(client *thorclient.Client) (*Prototype, error) {
3031
}, nil
3132
}
3233

34+
// Revision creates a new Prototype instance with the specified revision.
35+
func (p *Prototype) Revision(rev string) *Prototype {
36+
return &Prototype{
37+
contract: p.contract,
38+
revision: rev,
39+
}
40+
}
41+
3342
func (p *Prototype) Raw() bind.Contract {
3443
return p.contract
3544
}
3645

3746
// Master returns the master address for the given contract
3847
func (p *Prototype) Master(contract thor.Address) (thor.Address, error) {
3948
out := new(common.Address)
40-
if err := p.contract.Method("master", contract).Call().ExecuteInto(&out); err != nil {
49+
if err := p.contract.Method("master", contract).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
4150
return thor.Address{}, err
4251
}
4352
return thor.Address(*out), nil
@@ -51,7 +60,7 @@ func (p *Prototype) SetMaster(self thor.Address, newMaster thor.Address) bind.Me
5160
// IsUser checks if the given address is a user of the contract
5261
func (p *Prototype) IsUser(self thor.Address, user thor.Address) (bool, error) {
5362
out := new(bool)
54-
if err := p.contract.Method("isUser", self, user).Call().ExecuteInto(&out); err != nil {
63+
if err := p.contract.Method("isUser", self, user).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
5564
return false, err
5665
}
5766
return *out, nil
@@ -70,7 +79,7 @@ func (p *Prototype) RemoveUser(self thor.Address, user thor.Address) bind.Method
7079
// UserCredit returns the credit amount for a specific user
7180
func (p *Prototype) UserCredit(self thor.Address, user thor.Address) (*big.Int, error) {
7281
out := new(big.Int)
73-
if err := p.contract.Method("userCredit", self, user).Call().ExecuteInto(&out); err != nil {
82+
if err := p.contract.Method("userCredit", self, user).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
7483
return nil, err
7584
}
7685
return out, nil
@@ -81,7 +90,7 @@ func (p *Prototype) CreditPlan(self thor.Address) (*big.Int, *big.Int, error) {
8190
var out = [2]any{}
8291
out[0] = new(*big.Int)
8392
out[1] = new(*big.Int)
84-
if err := p.contract.Method("creditPlan", self).Call().ExecuteInto(&out); err != nil {
93+
if err := p.contract.Method("creditPlan", self).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
8594
return nil, nil, err
8695
}
8796
return *(out[0].(**big.Int)), *(out[1].(**big.Int)), nil
@@ -95,7 +104,7 @@ func (p *Prototype) SetCreditPlan(self thor.Address, credit *big.Int, recoveryRa
95104
// CurrentSponsor returns the current sponsor address
96105
func (p *Prototype) CurrentSponsor(self thor.Address) (thor.Address, error) {
97106
out := new(common.Address)
98-
if err := p.contract.Method("currentSponsor", self).Call().ExecuteInto(&out); err != nil {
107+
if err := p.contract.Method("currentSponsor", self).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
99108
return thor.Address{}, err
100109
}
101110
return thor.Address(*out), nil
@@ -104,7 +113,7 @@ func (p *Prototype) CurrentSponsor(self thor.Address) (thor.Address, error) {
104113
// IsSponsor checks if the given address is a sponsor
105114
func (p *Prototype) IsSponsor(self thor.Address, sponsor thor.Address) (bool, error) {
106115
out := new(bool)
107-
if err := p.contract.Method("isSponsor", self, sponsor).Call().ExecuteInto(&out); err != nil {
116+
if err := p.contract.Method("isSponsor", self, sponsor).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
108117
return false, err
109118
}
110119
return *out, nil
@@ -128,7 +137,7 @@ func (p *Prototype) Unsponsor(self thor.Address) bind.MethodBuilder {
128137
// Balance returns the balance at a specific block number
129138
func (p *Prototype) Balance(self thor.Address, blockNumber *big.Int) (*big.Int, error) {
130139
out := new(big.Int)
131-
if err := p.contract.Method("balance", self, blockNumber).Call().ExecuteInto(&out); err != nil {
140+
if err := p.contract.Method("balance", self, blockNumber).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
132141
return nil, err
133142
}
134143
return out, nil
@@ -137,7 +146,7 @@ func (p *Prototype) Balance(self thor.Address, blockNumber *big.Int) (*big.Int,
137146
// Energy returns the energy at a specific block number
138147
func (p *Prototype) Energy(self thor.Address, blockNumber *big.Int) (*big.Int, error) {
139148
out := new(big.Int)
140-
if err := p.contract.Method("energy", self, blockNumber).Call().ExecuteInto(&out); err != nil {
149+
if err := p.contract.Method("energy", self, blockNumber).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
141150
return nil, err
142151
}
143152
return out, nil
@@ -146,7 +155,7 @@ func (p *Prototype) Energy(self thor.Address, blockNumber *big.Int) (*big.Int, e
146155
// HasCode checks if the contract has code
147156
func (p *Prototype) HasCode(self thor.Address) (bool, error) {
148157
out := new(bool)
149-
if err := p.contract.Method("hasCode", self).Call().ExecuteInto(&out); err != nil {
158+
if err := p.contract.Method("hasCode", self).Call().AtRevision(p.revision).ExecuteInto(&out); err != nil {
150159
return false, err
151160
}
152161
return *out, nil
@@ -155,7 +164,7 @@ func (p *Prototype) HasCode(self thor.Address) (bool, error) {
155164
// StorageFor returns the storage value for a given key
156165
func (p *Prototype) StorageFor(self thor.Address, key thor.Bytes32) (thor.Bytes32, error) {
157166
out := new(common.Hash)
158-
if err := p.contract.Method("storageFor", self, key).Call().ExecuteInto(out); err != nil {
167+
if err := p.contract.Method("storageFor", self, key).Call().AtRevision(p.revision).ExecuteInto(out); err != nil {
159168
return thor.Bytes32{}, err
160169
}
161170
return thor.Bytes32(*out), nil

0 commit comments

Comments
 (0)