-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(erc20signersvc): add erc20 reward signer svc
- Loading branch information
Showing
8 changed files
with
642 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package signersvc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/kwilteam/kwil-db/core/client" | ||
clientTypes "github.com/kwilteam/kwil-db/core/client/types" | ||
"github.com/kwilteam/kwil-db/core/types" | ||
) | ||
|
||
// TODO: use the type from Ext? | ||
type Epoch struct { | ||
ID types.UUID | ||
StartHeight int64 | ||
EndHeight int64 | ||
TotalRewards types.Decimal | ||
//MtreeJson string | ||
RewardRoot []byte | ||
SafeNonce int64 | ||
SignHash []byte // SignHash is Chain aware, it's from GnosisSafeTx | ||
ContractID types.UUID | ||
BlockHash []byte | ||
CreatedAt int64 | ||
Voters []string | ||
Finalized bool // TODO: so signerSvc can use this to see it can skip, or, in the SQL, we have option to only return not-finalized epoch | ||
} | ||
|
||
// TODO: use the type from Ext? | ||
type FinalizedReward struct { | ||
ID types.UUID | ||
Voters []string | ||
Signatures [][]byte | ||
EpochID types.UUID | ||
CreatedAt int64 | ||
// | ||
StartHeight int64 | ||
EndHeight int64 | ||
TotalRewards types.Decimal | ||
RewardRoot []byte | ||
SafeNonce int64 | ||
SignHash []byte | ||
ContractID types.UUID | ||
BlockHash []byte | ||
} | ||
|
||
// rewardExtAPI defines the ERC20 reward extension API used by SignerSvc. | ||
type rewardExtAPI interface { | ||
GetTarget() string | ||
SetTarget(ns string) | ||
ListEpochs(ctx context.Context, afterHeight int64, limit int) ([]*Epoch, error) | ||
FetchLatestRewards(ctx context.Context, limit int) ([]*FinalizedReward, error) | ||
VoteEpoch(ctx context.Context, signHash []byte, signature []byte) (string, error) | ||
} | ||
|
||
type erc20rwExtApi struct { | ||
clt *client.Client | ||
target string | ||
} | ||
|
||
var _ rewardExtAPI = (*erc20rwExtApi)(nil) | ||
|
||
func newERC20RWExtAPI(clt *client.Client, ns string) *erc20rwExtApi { | ||
return &erc20rwExtApi{ | ||
clt: clt, | ||
target: ns, | ||
} | ||
} | ||
|
||
func (k *erc20rwExtApi) GetTarget() string { | ||
return k.target | ||
} | ||
|
||
func (k *erc20rwExtApi) SetTarget(ns string) { | ||
k.target = ns | ||
} | ||
|
||
func (k *erc20rwExtApi) ListEpochs(ctx context.Context, startHeight int64, limit int) ([]*Epoch, error) { | ||
procedure := "list_epochs" | ||
input := []any{startHeight, limit} | ||
|
||
res, err := k.clt.Call(ctx, k.target, procedure, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(res.QueryResult.Values) == 0 { | ||
return nil, nil | ||
} | ||
|
||
ers := make([]*Epoch, len(res.QueryResult.Values)) | ||
for i, v := range res.QueryResult.Values { | ||
er := &Epoch{} | ||
err = types.ScanTo(v, &er.ID, &er.StartHeight, &er.EndHeight, &er.TotalRewards, | ||
&er.RewardRoot, &er.SafeNonce, &er.SignHash, &er.ContractID, &er.BlockHash, &er.CreatedAt, &er.Voters) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ers[i] = er | ||
} | ||
|
||
return ers, nil | ||
} | ||
|
||
func (k *erc20rwExtApi) FetchLatestRewards(ctx context.Context, limit int) ([]*FinalizedReward, error) { | ||
procedure := "latest_finalized" | ||
input := []any{limit} | ||
|
||
res, err := k.clt.Call(ctx, k.target, procedure, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(res.QueryResult.Values) == 0 { | ||
return nil, nil | ||
} | ||
|
||
frs := make([]*FinalizedReward, len(res.QueryResult.Values)) | ||
for i, v := range res.QueryResult.Values { | ||
fr := &FinalizedReward{} | ||
err = types.ScanTo(v, &fr.ID, &fr.Voters, &fr.Signatures, &fr.EpochID, | ||
&fr.CreatedAt, &fr.StartHeight, &fr.EndHeight, &fr.TotalRewards, | ||
&fr.RewardRoot, &fr.SafeNonce, &fr.SignHash, &fr.ContractID, &fr.BlockHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
frs[i] = fr | ||
} | ||
|
||
return frs, nil | ||
} | ||
|
||
func (k *erc20rwExtApi) VoteEpoch(ctx context.Context, signHash []byte, signature []byte) (string, error) { | ||
procedure := "vote_epoch" | ||
input := [][]any{{signHash, signature}} | ||
|
||
res, err := k.clt.Execute(ctx, k.target, procedure, input, clientTypes.WithSyncBroadcast(true)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return res.String(), nil | ||
} |
Oops, something went wrong.