Skip to content

Commit

Permalink
introduce support for RPC v0.2.1 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregoryguillou committed Dec 8, 2022
1 parent cdccbf5 commit c8063aa
Show file tree
Hide file tree
Showing 75 changed files with 7,179 additions and 65,534 deletions.
5 changes: 1 addition & 4 deletions .bin/devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ export ACCOUNT_ADDRESS=0x06bb9425718d801fd06f144abb82eced725f0e81db61d2f9f4c9a26

while true; do
i=$((i + 1))
curl --fail localhost:5050/is_alive 2>/dev/null 2>&1 && \
curl --fail localhost:5051/is_alive 2>/dev/null 2>&1
curl --fail localhost:5050/is_alive 2>/dev/null 2>&1
result=$?
if [ $result -eq 0 ]; then
sleep 5
curl --fail -H 'Content-Type: application/json' -XPOST http://localhost:5050/mint \
-d '{ "address": "'${ACCOUNT_ADDRESS}'", "amount": 1000000000000000}'
curl --fail -H 'Content-Type: application/json' -XPOST http://localhost:5051/mint \
-d '{ "address": "'${ACCOUNT_ADDRESS}'", "amount": 1000000000000000}'
exit 0
fi
if [ $i -gt 10 ]; then
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '*'
jobs:
goreleaser:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:

- name: Checkout
Expand Down
69 changes: 69 additions & 0 deletions .github/workflows/rpcv02.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: rpcv02

on:
push:
branches:
- '**'
tags-ignore:
- '**'

jobs:
build:
runs-on: ubuntu-22.04

services:
devnet:
image: shardlabs/starknet-devnet:latest
ports:
- 5050:5050

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: wait for devnet to be up...
run: ./.bin/devnet.sh

- name: Test contracts deployment and execute
run: echo "skip for now"
# run: cd contracts && go test -env devnet -v -run "^TestRPCv02" .

- name: Preinstall RPC v0.2 contracts on devnet
run: echo "skip for now"
# run: cd rpcv02 && go test -timeout 600s -v -env devnet -run TestDeployTransaction . && sleep 5
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "http://localhost:5050/rpc"

- name: Test Accounts (Caigo) on devnet
run: echo "skip for now"
# run: go test -timeout 600s -v -env devnet -run "^(TestGateway|TestRPCv02|TestGeneral)" .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "http://localhost:5050/rpc"

- name: Test RPC v0.2 with mocks
run: cd rpcv02 && go test -v .

- name: Test RPC v0.2 on devnet
run: echo "skip for now"
# run: cd rpcv02 && go test -timeout 600s -v -env devnet .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "http://localhost:5050/rpc"

- name: Test RPC v0.2 on testnet
run: cd rpcv02 && go test -timeout 1200s -v -env testnet .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "https://starknet-goerli.cartridge.gg/"

- name: Test RPC v0.2 with mainnet
run: cd rpcv02 && go test -timeout 600s -v -env mainnet .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "https://starknet.cartridge.gg/"
13 changes: 4 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ name: test
on:
push:
branches:
- '**'
- '**'
tags-ignore:
- '**'
schedule:
- cron: "42 2 * * *"

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

services:
devnet:
image: shardlabs/starknet-devnet:0.3.4
ports:
- 5050:5050

devnetv2:
image: shardlabs/starknet-devnet:0.4.2
ports:
- 5051:5050

steps:
- uses: actions/checkout@v3

Expand All @@ -36,7 +31,7 @@ jobs:
run: ./.bin/devnet.sh

- name: Test contracts deployment and execute
run: cd contracts && go test -env devnet -v .
run: cd contracts && go test -env devnet -v -run "^(TestGateway|TestRPCv01)" .

- name: Test RPC v0.1 with mocks
run: cd rpcv01 && go test -v .
Expand All @@ -48,7 +43,7 @@ jobs:
INTEGRATION_BASE: "http://localhost:5050/rpc"

- name: Test Accounts (Caigo) on devnet
run: go test -timeout 600s -v -env devnet .
run: go test -timeout 600s -v -env devnet -run "^(TestGateway|TestRPCv01|TestGeneral)" .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "http://localhost:5050/rpc"
Expand Down
137 changes: 128 additions & 9 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/dontpanicdao/caigo/gateway"
"github.com/dontpanicdao/caigo/rpcv01"
"github.com/dontpanicdao/caigo/rpcv02"
"github.com/dontpanicdao/caigo/types"
)

Expand Down Expand Up @@ -41,11 +42,13 @@ type ProviderType string

const (
ProviderRPCv01 ProviderType = "rpcv01"
ProviderRPCv02 ProviderType = "rpcv02"
ProviderGateway ProviderType = "gateway"
)

type Account struct {
rpcv01 *rpcv01.Provider
rpcv02 *rpcv02.Provider
sequencer *gateway.GatewayProvider
provider ProviderType
chainId string
Expand Down Expand Up @@ -101,19 +104,37 @@ func newAccount(private, address string, options ...AccountOptionFunc) (*Account
}, nil
}

func NewRPCAccount(private, address string, provider *rpcv01.Provider, options ...AccountOptionFunc) (*Account, error) {
account, err := newAccount(private, address, options...)
if err != nil {
return nil, err
func setAccountProvider(account *Account, provider interface{}) error {
switch p := provider.(type) {
case *rpcv01.Provider:
chainID, err := p.ChainID(context.Background())
if err != nil {
return err
}
account.chainId = chainID
account.provider = ProviderRPCv01
account.rpcv01 = p
return nil
case *rpcv02.Provider:
chainID, err := p.ChainID(context.Background())
if err != nil {
return err
}
account.chainId = chainID
account.provider = ProviderRPCv02
account.rpcv02 = p
return nil
}
chainID, err := provider.ChainID(context.Background())
return errors.New("unsupported provider")
}

func NewRPCAccount[Provider *rpcv01.Provider | *rpcv02.Provider](private, address string, provider Provider, options ...AccountOptionFunc) (*Account, error) {
account, err := newAccount(private, address, options...)
if err != nil {
return nil, err
}
account.chainId = chainID
account.provider = ProviderRPCv01
account.rpcv01 = provider
return account, nil
err = setAccountProvider(account, provider)
return account, err
}

func NewGatewayAccount(private, address string, provider *gateway.GatewayProvider, options ...AccountOptionFunc) (*Account, error) {
Expand All @@ -138,6 +159,11 @@ func (account *Account) Call(ctx context.Context, call types.FunctionCall) ([]st
return nil, ErrUnsupportedAccount
}
return account.rpcv01.Call(ctx, call, rpcv01.WithBlockTag("latest"))
case ProviderRPCv02:
if account.rpcv02 == nil {
return nil, ErrUnsupportedAccount
}
return account.rpcv02.Call(ctx, call, rpcv02.WithBlockTag("latest"))
case ProviderGateway:
if account.sequencer == nil {
return nil, ErrUnsupportedAccount
Expand Down Expand Up @@ -264,6 +290,27 @@ func (account *Account) Nonce(ctx context.Context) (*big.Int, error) {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderRPCv02:
nonce, err := account.rpcv02.Call(
ctx,
types.FunctionCall{
ContractAddress: types.HexToHash(account.AccountAddress),
EntryPointSelector: "get_nonce",
Calldata: []string{},
},
rpcv02.WithBlockTag("latest"),
)
if err != nil {
return nil, err
}
if len(nonce) == 0 {
return nil, errors.New("nonce error")
}
n, ok := big.NewInt(0).SetString(nonce[0], 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderGateway:
return account.sequencer.AccountNonce(ctx, types.HexToHash(account.AccountAddress))
}
Expand All @@ -282,6 +329,20 @@ func (account *Account) Nonce(ctx context.Context) (*big.Int, error) {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderRPCv02:
nonce, err := account.rpcv02.Nonce(
ctx,
rpcv02.WithBlockTag("latest"),
types.HexToHash(account.AccountAddress),
)
if err != nil {
return nil, err
}
n, ok := big.NewInt(0).SetString(*nonce, 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderGateway:
return account.sequencer.Nonce(ctx, account.AccountAddress, "latest")
}
Expand Down Expand Up @@ -386,6 +447,35 @@ func (account *Account) EstimateFee(ctx context.Context, calls []types.FunctionC
switch account.provider {
case ProviderRPCv01:
return account.rpcv01.EstimateFee(ctx, *call, rpcv01.WithBlockTag("latest"))
case ProviderRPCv02:
signature := []string{}
for _, v := range call.Signature {
signature = append(signature, fmt.Sprintf("0x%s", v.Text(16)))
}
switch account.version {
case 0:
return account.rpcv02.EstimateFee(ctx, rpcv02.BroadcastedInvokeV0Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV0,
Signature: signature,
Type: "INVOKE",
},
FunctionCall: call.FunctionCall,
}, rpcv02.WithBlockTag("latest"))
case 1:
return account.rpcv02.EstimateFee(ctx, rpcv02.BroadcastedInvokeV1Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV1,
Signature: signature,
Nonce: call.Nonce,
Type: "INVOKE",
},
Calldata: call.FunctionCall.Calldata,
SenderAddress: types.HexToHash(account.AccountAddress),
}, rpcv02.WithBlockTag("latest"))
}
case ProviderGateway:
return account.sequencer.EstimateFee(ctx, *call, "latest")
}
Expand Down Expand Up @@ -425,6 +515,35 @@ func (account *Account) Execute(ctx context.Context, calls []types.FunctionCall,
fmt.Sprintf("0x%d", account.version),
call.Nonce,
)
case ProviderRPCv02:
signature := []string{}
for _, v := range call.Signature {
signature = append(signature, fmt.Sprintf("0x%s", v.Text(16)))
}
switch account.version {
case 0:
return account.rpcv02.AddInvokeTransaction(ctx, rpcv02.BroadcastedInvokeV0Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV0,
Signature: signature,
Type: "INVOKE",
},
FunctionCall: call.FunctionCall,
})
case 1:
return account.rpcv02.AddInvokeTransaction(ctx, rpcv02.BroadcastedInvokeV1Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV1,
Signature: signature,
Nonce: call.Nonce,
Type: "INVOKE",
},
SenderAddress: types.HexToHash(account.AccountAddress),
Calldata: call.FunctionCall.Calldata,
})
}
case ProviderGateway:
return account.sequencer.Invoke(
context.Background(),
Expand Down
Loading

0 comments on commit c8063aa

Please sign in to comment.