Skip to content

Commit

Permalink
Merge pull request #44 from rarimo/feat/abstraction
Browse files Browse the repository at this point in the history
Abstraction account support
  • Loading branch information
Zaptoss authored Sep 24, 2024
2 parents 81c8dbd + 037e405 commit 3e00720
Show file tree
Hide file tree
Showing 26 changed files with 2,015 additions and 1,587 deletions.
16 changes: 16 additions & 0 deletions docs/spec/components/schemas/AbstractionAccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
allOf:
- $ref: '#/components/schemas/AbstractionAccountKey'
- type: object
required:
- attributes
properties:
attributes:
type: object
required:
- address
properties:
address:
type: string
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{40}$'
description: Account abstraction address.
13 changes: 13 additions & 0 deletions docs/spec/components/schemas/AbstractionAccountKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type: object
required:
- id
- type
properties:
id:
type: string
description: Abstraction account address
example: "0x123...abc"
pattern: '^0x[0-9a-fA-F]{40}$'
type:
type: string
enum: [ abstraction_account ]
16 changes: 16 additions & 0 deletions docs/spec/components/schemas/CreateAbstractionAccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
allOf:
- $ref: '#/components/schemas/AbstractionAccountKey'
- type: object
x-go-is-request: true
required:
- attributes
properties:
attributes:
type: object
required:
- proof
properties:
proof:
type: object
format: types.ZKProof
description: Query ZK passport verification proof.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
post:
tags:
- Abstraction
summary: Create abstraction account
description: |
Create account for abstraction associated with nullifier.
User must provide query proof.
operationId: createAbstractionAccount
requestBody:
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/CreateAbstractionAccount'
responses:
201:
description: Created
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/AbstractionAccount'
400:
$ref: '#/components/responses/invalidParameter'
401:
$ref: '#/components/responses/invalidAuth'
409:
description: Abstraction account already exists for provided nullifier
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
500:
$ref: '#/components/responses/internalError'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
get:
tags:
- Abstraction
summary: Get abstraction acccount
description: Get abstraction account associated with provided nullifier.
operationId: getAbstractionAccount
parameters:
- $ref: '#/components/parameters/pathNullifier'
responses:
200:
description: Success
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/AbstractionAccount'
400:
$ref: '#/components/responses/invalidParameter'
401:
$ref: '#/components/responses/invalidAuth'
404:
$ref: '#/components/responses/notFound'
500:
$ref: '#/components/responses/internalError'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ post:
- Points balance
summary: Withdraw points
description: |
Convert points to RariMarket points by exchange rate and withdraw to user wallet.
Convert points to abstract points by exchange rate and withdraw to user wallet.
Updated balance with new rank is returned.
operationId: withdrawPoints
parameters:
Expand Down
56 changes: 28 additions & 28 deletions internal/config/rarimarket.go → internal/config/abstraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
vaultapi "github.com/hashicorp/vault/api"
accountFactory "github.com/rarimo/geo-points-svc/internal/contracts/abstractionaccountfactory"
pointTokens "github.com/rarimo/geo-points-svc/internal/contracts/points"
accountFactory "github.com/rarimo/geo-points-svc/internal/contracts/rarimarketaccountfactory"

"gitlab.com/distributed_lab/dig"
"gitlab.com/distributed_lab/figure/v3"
Expand All @@ -24,22 +24,22 @@ import (

var ZeroAddress = common.HexToAddress("0x0000000000000000000000000000000000000000")

type Rarimarket interface {
RarimarketConfig() *RarimarketConfig
type Abstraction interface {
AbstractionConfig() *AbstractionConfig
}

func NewRarimarketConfig(getter kv.Getter) Rarimarket {
return &rarimarketConfig{
func NewAbstractionConfig(getter kv.Getter) Abstraction {
return &abstractionConfig{
getter: getter,
}
}

type rarimarketConfig struct {
type abstractionConfig struct {
once comfig.Once
getter kv.Getter
}

type RarimarketConfig struct {
type AbstractionConfig struct {
RPC *ethclient.Client
AccountFactory common.Address
PointTokens common.Address
Expand All @@ -49,7 +49,7 @@ type RarimarketConfig struct {
privateKey *ecdsa.PrivateKey
}

func (c *rarimarketConfig) RarimarketConfig() *RarimarketConfig {
func (c *abstractionConfig) AbstractionConfig() *AbstractionConfig {
return c.once.Do(func() interface{} {
var cfg struct {
RPC *ethclient.Client `fig:"rpc,required"`
Expand All @@ -63,11 +63,11 @@ func (c *rarimarketConfig) RarimarketConfig() *RarimarketConfig {
}

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "rarimarket")).
From(kv.MustGetStringMap(c.getter, "abstraction")).
With(figure.EthereumHooks, figure.BaseHooks).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure out rarimarket config: %w", err))
panic(fmt.Errorf("failed to figure out abstraction config: %w", err))
}

privateKey := cfg.PrivateKey
Expand All @@ -85,7 +85,7 @@ func (c *rarimarketConfig) RarimarketConfig() *RarimarketConfig {
cfg.PointPrice = int64(math.Pow10(9))
}

return &RarimarketConfig{
return &AbstractionConfig{
RPC: cfg.RPC,
AccountFactory: cfg.AccountFactory,
PointTokens: cfg.PointTokens,
Expand All @@ -98,23 +98,23 @@ func (c *rarimarketConfig) RarimarketConfig() *RarimarketConfig {

privateKey: privateKey,
}
}).(*RarimarketConfig)
}).(*AbstractionConfig)
}

func (r *RarimarketConfig) CreateAccount(ctx context.Context, nullifier [32]byte) (common.Address, error) {
func (r *AbstractionConfig) CreateAccount(ctx context.Context, nullifier [32]byte) (common.Address, error) {
signerOpts, err := bind.NewKeyedTransactorWithChainID(r.privateKey, r.ChainID)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get keyed transactor: %w", err)
}

accountFactoryInstance, err := accountFactory.NewRarimarketAccountFactory(r.AccountFactory, r.RPC)
accountFactoryInstance, err := accountFactory.NewAbstractionAccountFactory(r.AccountFactory, r.RPC)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get account factory: %w", err)
}

tx, err := accountFactoryInstance.DeployRarimarketAccount(signerOpts, nullifier)
tx, err := accountFactoryInstance.DeployAbstractionAccount(signerOpts, nullifier)
if err != nil {
return common.Address{}, fmt.Errorf("failed to deploy rarimarket account: %w", err)
return common.Address{}, fmt.Errorf("failed to deploy abstraction account: %w", err)
}

ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
Expand All @@ -125,20 +125,20 @@ func (r *RarimarketConfig) CreateAccount(ctx context.Context, nullifier [32]byte
return common.Address{}, fmt.Errorf("failed to wait mined transaction: %w", err)
}

abi, err := accountFactory.RarimarketAccountFactoryMetaData.GetAbi()
abi, err := accountFactory.AbstractionAccountFactoryMetaData.GetAbi()
if err != nil {
return common.Address{}, fmt.Errorf("failed to get contract abi: %w", err)
}

rarimarketAccountDeployedTopic := abi.Events["RarimarketAccountDeployed"].ID
abstractionAccountDeployedTopic := abi.Events["AbstractionAccountDeployed"].ID

var event *accountFactory.RarimarketAccountFactoryRarimarketAccountDeployed
var event *accountFactory.AbstractionAccountFactoryAbstractionAccountDeployed
for _, log := range rec.Logs {
if !bytes.Equal(log.Topics[0][:], rarimarketAccountDeployedTopic[:]) {
if !bytes.Equal(log.Topics[0][:], abstractionAccountDeployedTopic[:]) {
continue
}

event, err = accountFactoryInstance.ParseRarimarketAccountDeployed(*log)
event, err = accountFactoryInstance.ParseAbstractionAccountDeployed(*log)
if err != nil {
return common.Address{}, fmt.Errorf("failed to unpack log: %w", err)
}
Expand All @@ -148,34 +148,34 @@ func (r *RarimarketConfig) CreateAccount(ctx context.Context, nullifier [32]byte
return event.Account, nil
}

func (r *RarimarketConfig) GetAccount(nullifier [32]byte) (common.Address, error) {
accountFactoryInstance, err := accountFactory.NewRarimarketAccountFactory(r.AccountFactory, r.RPC)
func (r *AbstractionConfig) GetAccount(nullifier [32]byte) (common.Address, error) {
accountFactoryInstance, err := accountFactory.NewAbstractionAccountFactory(r.AccountFactory, r.RPC)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get account factory: %w", err)
}

accountAddress, err := accountFactoryInstance.GetRarimarketAccount(nil, nullifier)
accountAddress, err := accountFactoryInstance.GetAbstractionAccount(nil, nullifier)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get rarimarket account: %w", err)
return common.Address{}, fmt.Errorf("failed to get abstraction account: %w", err)
}

return accountAddress, nil
}

func (r *RarimarketConfig) Mint(ctx context.Context, account common.Address, amount *big.Int) (common.Hash, error) {
func (r *AbstractionConfig) Mint(ctx context.Context, account common.Address, amount *big.Int) (common.Hash, error) {
signerOpts, err := bind.NewKeyedTransactorWithChainID(r.privateKey, r.ChainID)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get keyed transactor: %w", err)
}

pointTokensInstance, err := pointTokens.NewPoints(r.PointTokens, r.RPC)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get account factory: %w", err)
return common.Hash{}, fmt.Errorf("failed to get points instance: %w", err)
}

tx, err := pointTokensInstance.Mint(signerOpts, account, amount)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to deploy rarimarket account: %w", err)
return common.Hash{}, fmt.Errorf("failed to mint points: %w", err)
}

ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
Expand Down
6 changes: 3 additions & 3 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Config interface {
evtypes.EventTypeser
hmacsig.SigCalculatorProvider
PollVerifierer
Rarimarket
Abstraction

Levels() *Levels
Verifiers() Verifiers
Expand All @@ -36,7 +36,7 @@ type config struct {
evtypes.EventTypeser
hmacsig.SigCalculatorProvider
PollVerifierer
Rarimarket
Abstraction

passport root.VerifierProvider

Expand All @@ -58,6 +58,6 @@ func New(getter kv.Getter) Config {
passport: root.NewVerifierProvider(getter, root.PoseidonSMT),
EventTypeser: evtypes.NewConfig(getter),
SigCalculatorProvider: hmacsig.NewCalculatorProvider(getter),
Rarimarket: NewRarimarketConfig(getter),
Abstraction: NewAbstractionConfig(getter),
}
}
Loading

0 comments on commit 3e00720

Please sign in to comment.