From c18ca3df48e4a6a9f93c8a6c6b64abd194026cac Mon Sep 17 00:00:00 2001 From: huyao Date: Mon, 20 Nov 2023 18:04:09 +0800 Subject: [PATCH] add list_liquidity api for indexer & add readme for indexer/sdk --- .prettierrc | 1 + README.md | 118 ++++++++++++++++++++++++++++- graphql/README.md | 83 ++++++++++++++++++++ graphql/graph/generated.go | 122 ++++++++++++++++++++++++++++++ graphql/graph/model/models_gen.go | 2 +- graphql/graph/schema.graphqls | 1 + graphql/graph/schema.resolvers.go | 20 ++++- graphql/graph/utils/utils.go | 13 ++++ package.json | 5 ++ 9 files changed, 358 insertions(+), 7 deletions(-) create mode 100644 .prettierrc create mode 100644 graphql/graph/utils/utils.go create mode 100644 package.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/.prettierrc @@ -0,0 +1 @@ +{} diff --git a/README.md b/README.md index cc57979..c9519be 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,117 @@ -## InstaSwap +# InstaSwap + +### initialize sdk + +To initialize sdk,fill the config first + +```js + +import { Provider, constants } from "starknet"; +import { useAccount } from "@starknet-react/core"; +.... + +const provider = new Provider({ + sequencer: { network: constants.NetworkName.SN_GOERLI }, + }); + +const config = { + erc1155Address: "0x03467674358c444d5868e40b4de2c8b08f0146cbdb4f77242bd7619efcf3c0a6", + werc20Address: "0x06b09e4c92a08076222b392c77e7eab4af5d127188082713aeecbe9013003bf4", + erc20Address: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", + ekuboPositionAddress: "0x73fa8432bf59f8ed535f29acfd89a7020758bda7be509e00dfed8a9fde12ddc", + ekuboCoreAddress: "0x031e8a7ab6a6a556548ac85cbb8b5f56e8905696e9f13e9a858142b8ee0cc221", + quoterAddress: "0x042aa743335663ed9c7b52b331ab7f81cc8d65280d311506653f9b5cc22be7cb", + provider: provider, + account: useAccount(), +}; + +const wrap = new Wrap(config); +... +``` + +### Initialize pool + +```js +const initialize_tick = { mag: 0n, sign: false }; + +const { transaction_hash } = await wrap.mayInitializePool( + FeeAmount.LOWEST, + initialize_tick, +); +``` + +### Add liquidity + +```js +const params = { + erc1155Amount: [erc1155 amount], + erc20Amount: [erc20 amount], + fee: [fee], + lowerPrice: [lowerBound], + upperPrice: [upperBound], + }; + +wrap.addLiquidity(params); + +``` + + + +### Withdraw Liquidity + +```js +wrap.withdrawLiquidity(positionId,liquidity); +``` + + + + +### Simple wrap + +- from erc115 to erc20 + +```js + +const params = { + amountIn: [erc1155 amount for swap], + minERC20AmountOut: [min amount], + simpleSwapperAddress: [simple swapper address], + userAddress:[user address], + fee: [fee], + slippage: [slippage], +}; + +wrap.swapSimple( + SwapDirection.ERC1155_TO_ERC20, + params, +); + + +``` + +- from erc20 to erc1155 + +```js + const params = { + amountIn: [erc20 amount for swap], + minERC20AmountOut: [min amount], + simpleSwapperAddress: [simple swapper address], + userAddress:[user address], + fee: [fee], + slippage: [slippage], +}; + +wrap.swapSimple( + SwapDirection.ERC20_TO_ERC1155, + params, +); + +``` + +### Withdraw erc1155 + +```js +wrap.withdraw(erc1155Num); +``` + -## \ No newline at end of file diff --git a/graphql/README.md b/graphql/README.md index d7de409..3dcd45f 100644 --- a/graphql/README.md +++ b/graphql/README.md @@ -22,5 +22,88 @@ copy the config example file,then replace the database information then visit http://localhost:8088/ for GraphQL playground +## Queries + +```graphql +query getPoolKeys { + pool_keys{ + fee + key_hash + token0 + token1 + tick_spacing + extension + } +} + +query getPoolKeyByHash { + pool_key( + key_hash: "[hash]" + ) { + fee + key_hash + token0 + token1 + tick_spacing + extension + } +} + +query getPositionDeposits { + position_deposits { + block_number + transaction_index + event_index + transaction_hash + token_id + lower_bound + upper_bound + pool_key_hash + liquidity + delta0 + delta1 + } +} + + +query getPositionDepositByHash { + position_deposit(transaction_hash:"[hash]") { + block_number + transaction_index + event_index + transaction_hash + token_id + lower_bound + upper_bound + pool_key_hash + liquidity + delta0 + delta1 + } +} + + +query getSwapByHash { + swap(transaction_hash:"[hash]") { + transaction_index + event_index + transaction_hash + locker + pool_key_hash + delta0 + delta1 + sqrt_ratio_after + tick_after + liquidity_after + } +} + + +query getListLiquidity { + list_liquidity(account:"0x0112C1E020708b84aaC85983734A6ffB5fCe89891e8414e4E54F94CE75c06a90"){ + token_id + } +} +``` diff --git a/graphql/graph/generated.go b/graphql/graph/generated.go index 9af4aad..b61bf41 100644 --- a/graphql/graph/generated.go +++ b/graphql/graph/generated.go @@ -80,6 +80,7 @@ type ComplexityRoot struct { } Query struct { + ListLiquidity func(childComplexity int, account string) int PoolKey func(childComplexity int, keyHash string) int PoolKeys func(childComplexity int) int PositionDeposit func(childComplexity int, transactionHash string) int @@ -109,6 +110,7 @@ type QueryResolver interface { PoolKey(ctx context.Context, keyHash string) (*model.PoolKey, error) PositionTransfers(ctx context.Context) ([]*model.PositionTransfer, error) PositionTransfer(ctx context.Context, transactionHash string) (*model.PositionTransfer, error) + ListLiquidity(ctx context.Context, account string) ([]*model.PositionTransfer, error) PositionDeposits(ctx context.Context) ([]*model.PositionDeposit, error) PositionDeposit(ctx context.Context, transactionHash string) (*model.PositionDeposit, error) Swap(ctx context.Context, transactionHash string) (*model.Swap, error) @@ -301,6 +303,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.PositionTransfer.TransactionIndex(childComplexity), true + case "Query.list_liquidity": + if e.complexity.Query.ListLiquidity == nil { + break + } + + args, err := ec.field_Query_list_liquidity_args(context.TODO(), rawArgs) + if err != nil { + return 0, false + } + + return e.complexity.Query.ListLiquidity(childComplexity, args["account"].(string)), true + case "Query.pool_key": if e.complexity.Query.PoolKey == nil { break @@ -570,6 +584,21 @@ func (ec *executionContext) field_Query___type_args(ctx context.Context, rawArgs return args, nil } +func (ec *executionContext) field_Query_list_liquidity_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { + var err error + args := map[string]interface{}{} + var arg0 string + if tmp, ok := rawArgs["account"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("account")) + arg0, err = ec.unmarshalNString2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["account"] = arg0 + return args, nil +} + func (ec *executionContext) field_Query_pool_key_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -1955,6 +1984,77 @@ func (ec *executionContext) fieldContext_Query_position_transfer(ctx context.Con return fc, nil } +func (ec *executionContext) _Query_list_liquidity(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Query_list_liquidity(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().ListLiquidity(rctx, fc.Args["account"].(string)) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.([]*model.PositionTransfer) + fc.Result = res + return ec.marshalNPositionTransfer2ᚕᚖgithubᚗcomᚋmetaforoᚋindexerᚑgraphqlᚋgraphᚋmodelᚐPositionTransferᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Query_list_liquidity(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Query", + Field: field, + IsMethod: true, + IsResolver: true, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "block_number": + return ec.fieldContext_PositionTransfer_block_number(ctx, field) + case "transaction_index": + return ec.fieldContext_PositionTransfer_transaction_index(ctx, field) + case "event_index": + return ec.fieldContext_PositionTransfer_event_index(ctx, field) + case "transaction_hash": + return ec.fieldContext_PositionTransfer_transaction_hash(ctx, field) + case "token_id": + return ec.fieldContext_PositionTransfer_token_id(ctx, field) + case "from_address": + return ec.fieldContext_PositionTransfer_from_address(ctx, field) + case "to_address": + return ec.fieldContext_PositionTransfer_to_address(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type PositionTransfer", field.Name) + }, + } + defer func() { + if r := recover(); r != nil { + err = ec.Recover(ctx, r) + ec.Error(ctx, err) + } + }() + ctx = graphql.WithFieldContext(ctx, fc) + if fc.Args, err = ec.field_Query_list_liquidity_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { + ec.Error(ctx, err) + return fc, err + } + return fc, nil +} + func (ec *executionContext) _Query_position_deposits(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Query_position_deposits(ctx, field) if err != nil { @@ -4864,6 +4964,28 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) } + out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) }) + case "list_liquidity": + field := field + + innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_list_liquidity(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&fs.Invalids, 1) + } + return res + } + + rrm := func(ctx context.Context) graphql.Marshaler { + return ec.OperationContext.RootResolverMiddleware(ctx, + func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) + } + out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) }) case "position_deposits": field := field diff --git a/graphql/graph/model/models_gen.go b/graphql/graph/model/models_gen.go index 48d0c12..199cf3a 100644 --- a/graphql/graph/model/models_gen.go +++ b/graphql/graph/model/models_gen.go @@ -12,7 +12,7 @@ type PoolKey struct { } type PositionDeposit struct { - tableName struct{} `pg:"position_deposit"` + tableName struct{} `pg:"position_deposit"` BlockNumber *int `json:"block_number,omitempty"` TransactionIndex *int `json:"transaction_index,omitempty"` EventIndex *int `json:"event_index,omitempty"` diff --git a/graphql/graph/schema.graphqls b/graphql/graph/schema.graphqls index b36a360..a0198f4 100644 --- a/graphql/graph/schema.graphqls +++ b/graphql/graph/schema.graphqls @@ -58,6 +58,7 @@ type Query { pool_key(key_hash: String!): PoolKey! position_transfers:[PositionTransfer!]! position_transfer(transaction_hash: String!):PositionTransfer + list_liquidity(account: String!):[PositionTransfer!]! position_deposits:[PositionDeposit!]! position_deposit(transaction_hash: String!): PositionDeposit swap(transaction_hash: String!):Swap! diff --git a/graphql/graph/schema.resolvers.go b/graphql/graph/schema.resolvers.go index c60918d..9de9afe 100644 --- a/graphql/graph/schema.resolvers.go +++ b/graphql/graph/schema.resolvers.go @@ -7,6 +7,7 @@ package graph import ( "context" "github.com/metaforo/indexer-graphql/graph/model" + "github.com/metaforo/indexer-graphql/graph/utils" ) // PoolKeys is the resolver for the pool_keys field. @@ -24,7 +25,7 @@ func (r *queryResolver) PoolKeys(ctx context.Context) ([]*model.PoolKey, error) func (r *queryResolver) PoolKey(ctx context.Context, keyHash string) (*model.PoolKey, error) { pool_key := &model.PoolKey{} - err := r.DB.Model(pool_key).Where("key_hash=?", keyHash).Select() + err := r.DB.Model(pool_key).Where("key_hash=?", utils.Hex2BigNum(keyHash)).Select() if err != nil { return nil, err } @@ -46,13 +47,24 @@ func (r *queryResolver) PositionTransfers(ctx context.Context) ([]*model.Positio func (r *queryResolver) PositionTransfer(ctx context.Context, transactionHash string) (*model.PositionTransfer, error) { position_transfer := &model.PositionTransfer{} - err := r.DB.Model(position_transfer).Where("transaction_hash=?", transactionHash).Select() + err := r.DB.Model(position_transfer).Where("transaction_hash=?", utils.Hex2BigNum(transactionHash)).Select() if err != nil { return nil, err } return position_transfer, nil } +// ListLiquidity is the resolver for the list_liquidity field. +func (r *queryResolver) ListLiquidity(ctx context.Context, account string) ([]*model.PositionTransfer, error) { + var position_transfers []*model.PositionTransfer + + err := r.DB.Model(&position_transfers).Where("to_address=?",utils.Hex2BigNum(account)).Select() + if err != nil { + return nil, err + } + return position_transfers, nil +} + // PositionDeposits is the resolver for the position_deposits field. func (r *queryResolver) PositionDeposits(ctx context.Context) ([]*model.PositionDeposit, error) { var position_deposits []*model.PositionDeposit @@ -68,7 +80,7 @@ func (r *queryResolver) PositionDeposits(ctx context.Context) ([]*model.Position func (r *queryResolver) PositionDeposit(ctx context.Context, transactionHash string) (*model.PositionDeposit, error) { position_deposit := &model.PositionDeposit{} - err := r.DB.Model(position_deposit).Where("transaction_hash=?", transactionHash).Select() + err := r.DB.Model(position_deposit).Where("transaction_hash=?", utils.Hex2BigNum(transactionHash)).Select() if err != nil { return nil, err } @@ -79,7 +91,7 @@ func (r *queryResolver) PositionDeposit(ctx context.Context, transactionHash str func (r *queryResolver) Swap(ctx context.Context, transactionHash string) (*model.Swap, error) { swap := &model.Swap{} - err := r.DB.Model(swap).Where("transaction_hash=?", transactionHash).Select() + err := r.DB.Model(swap).Where("transaction_hash=?", utils.Hex2BigNum(transactionHash)).Select() if err != nil { return nil, err } diff --git a/graphql/graph/utils/utils.go b/graphql/graph/utils/utils.go new file mode 100644 index 0000000..c626d61 --- /dev/null +++ b/graphql/graph/utils/utils.go @@ -0,0 +1,13 @@ +package utils + +import "math/big" + +func Hex2BigNum(hexStr string)string{ + if len(hexStr) > 2 && hexStr[:2] == "0x" { + hexStr = hexStr[2:] + } + + bigNum := new(big.Int) + bigNum.SetString(hexStr, 16) + return bigNum.String() +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..debdc5f --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "prettier": "3.0.3" + } +}