Skip to content

Commit

Permalink
refactor(reef): adapt queries to latest refactoring on reef's graphql…
Browse files Browse the repository at this point in the history
… schemas
  • Loading branch information
guidiaz committed Apr 4, 2023
1 parent 546c066 commit aa358ec
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 120 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"optimism:mainnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_LIMIT_FACTOR=1.1 ETHERS_MOCK_FILTERS=true node dist/bin/ethers https://optimism-mainnet.infura.io/v3/6f4dade254984586b9d6ae727e384ca4 9520",
"polygon:goerli": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=100000000000 ETHERS_GAS_LIMIT=15000000 node dist/bin/ethers https://matic-mumbai.chainstacklabs.com 8535",
"polygon:mainnet": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=true ETHERS_GAS_PRICE=300000000000 ETHERS_GAS_PRICE_FACTOR=1.1 ETHERS_GAS_LIMIT=25000000 node dist/bin/ethers https://polygon-mainnet.infura.io/v3/6f4dade254984586b9d6ae727e384ca4 9535",
"reef:testnet": "cross-env-shell node dist/bin/reef wss://rpc-testnet.reefscan.com/ws https://testnet.reefscan.com/graphql 8532",
"reef:testnet": "cross-env-shell node dist/bin/reef wss://rpc-testnet.reefscan.com/ws https://squid.subsquid.io/reef-explorer-testnet/graphql 8532",
"reef:mainnet": "cross-env-shell node dist/bin/reef wss://rpc.reefscan.info/ws https://squid.subsquid.io/reef-explorer/graphql 9532",
"scroll:goerli": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=0 ETHERS_ESTIMATE_GAS_PRICE=true ETHERS_ESTIMATE_GAS_LIMIT=false ETHERS_GAS_LIMIT=8000000 ETHERS_GAS_PRICE_FACTOR=2.0 node dist/bin/ethers https://alpha-rpc.scroll.io/l2 8514",
"smartbch:amber": "cross-env-shell EVM_CALL_INTERLEAVE_BLOCKS=5 node dist/bin/ethers http://35.220.203.194:8545 8525",
Expand Down Expand Up @@ -105,7 +105,7 @@
"dependencies": {
"@celo-tools/celo-ethers-wrapper": "~0.1.1",
"@celo/contractkit": "~2.0.0",
"@reef-defi/evm-provider": "^1.0.9",
"@reef-defi/evm-provider": "~1.0.10",
"body-parser": "1.19.0",
"cors": "2.8.5",
"cross-env": "7.0.3",
Expand Down
7 changes: 3 additions & 4 deletions src/reef/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ export class WalletMiddlewareServer {
} catch (exception: any) {
if (!exception.code) {
// if no error code is specified,
// assume the Conflux provider is actually reporting an execution error:
// assume the Reef provider is actually reporting an execution error:
exception = {
reason: exception.toString(),
body: {
error: {
code: -32015,
message: exception.toString(),
data: exception
}
}
}
Expand All @@ -137,11 +136,11 @@ export class WalletMiddlewareServer {
error: {
code: exception.code || -32099,
message: `"${message}"`,
data: exception.data
}
})
body = typeof body !== 'string' ? JSON.stringify(body) : body
try {
response = { ...header, error: body.error }
response = { ...header, error: JSON.parse(body).error }
} catch (e) {
logger.log({
level: 'error',
Expand Down
203 changes: 93 additions & 110 deletions src/reef/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,56 +259,56 @@ export class WalletWrapper {
})
const queryBlock = gql`
{
block(
order_by: { id: desc_nulls_last }
limit: 1
where: { finalized: { _eq: true } }
blocks(
limit: 1,
orderBy: height_DESC,
where: { finalized_eq: true }
) {
id
height
author
hash
parent_hash
parentHash
finalized
extrinsic_root
state_root
extrinsicRoot
stateRoot
timestamp
}
}
`
let res = null
let data = await request(this.graphUrl, queryBlock)
const block = data?.block[0]
if (block?.id) {
const block = data?.blocks[0]
if (block?.height) {
const queryBlockExtrinsics = gql`
{
extrinsic (
extrinsics (
offset: 0,
limit: 256,
where: {
block_id: {
_eq: ${block.id}
block: {
height_eq: ${block.height}
}
}
) {
hash
events (
where: {
section: {
_eq: "evm"
}
}
offset: 0,
limit: 1,
where: { section_eq: "EVM" }
) {
method
}
}
}
`
data = await request(this.graphUrl, queryBlockExtrinsics)
const extrinsics: any[] = data?.extrinsic
const unixTs = Math.round(new Date(block.timestamp).getTime()) / 1000
const extrinsics: any[] = data?.extrinsics
const unixTs = Math.round(new Date(block.timestamp).getTime() / 1000)
res = {
hash: block.hash,
parentHash: block.parent_hash,
number: block.id,
stateRoot: block.state_root,
parentHash: block.parentHash,
number: block.height,
stateRoot: block.stateRoot,
timestamp: unixTs,
nonce: '0x0000000000000000',
difficulty: 0,
Expand Down Expand Up @@ -343,34 +343,29 @@ export class WalletWrapper {
): Promise<any> {
const query = gql`
{
extrinsic (
where: {
hash: {
_eq: "${txHash}"
}
}
extrinsics (
offset: 0,
limit: 1,
where: { hash_eq: "${txHash}" }
) {
id
args
signedData
status
timestamp
block {
id
hash
height
finalized
}
status
timestamp
events (
where: {
section: {
_eq: "evm"
}
}
offset: 0,
limit: 256,
where: { section_eq: "EVM" }
) {
data
method
index
}
args
signed_data
}
}
`
Expand All @@ -379,7 +374,7 @@ export class WalletWrapper {
message: `=> querying data to ${this.graphUrl} ...`
})
const data = await request(this.graphUrl, query)
const extrinsic = data?.extrinsic[0]
const extrinsic = data?.extrinsics[0]
let res = null
if (extrinsic && extrinsic.block.finalized) {
try {
Expand All @@ -388,13 +383,13 @@ export class WalletWrapper {
from,
extrinsic.block.hash
)
const gas = BigNumber.from(extrinsic.signed_data.fee.weight)
const fee = BigNumber.from(extrinsic.signed_data.fee.partialFee)
const gas = BigNumber.from(extrinsic.signedData.fee.weight)
const fee = BigNumber.from(extrinsic.signedData.fee.partialFee)
res = {
hash: txHash,
nonce: BigNumber.from(nonce).toHexString(),
blockHash: extrinsic.block.hash,
blockNumber: BigNumber.from(extrinsic.block.id).toHexString(),
blockNumber: BigNumber.from(extrinsic.block.height).toHexString(),
transactionIndex: `0x${extrinsic.events[0]!.index.toString(16)}`,
from,
to:
Expand All @@ -420,113 +415,101 @@ export class WalletWrapper {
): Promise<any> {
const query = gql`
{
extrinsic (
where: {
hash: {
_eq: "${txHash}"
}
}
extrinsics (
offset: 0,
limit: 1,
where: { hash_eq: "${txHash}" }
) {
args
id
index
signedData
status
timestamp
block {
id
hash
height
finalized
}
events (
offset: 0,
limit: 1,
where: {
_and: [
{ section: { _eq: "evm" }},
{
_or: [
{ method: { _eq: "Executed" }},
{ method: { _eq: "Created" }}
]
}
AND: [
{ section_eq: "EVM" },
{ OR: [
{ method_eq: "Executed" },
{ method_eq: "Created" },
]}
]
}
) {
data
index
method
}
index
signed_data
status
timestamp
}
}
`
const logsQuery = gql`
{
extrinsic (
where: {
hash: {
_eq: "${txHash}"
}
}
) {
events (
order_by: { index: asc },
where: {
_and: [
{ section: { _eq: "evm" }},
{ method: { _eq: "Log" }}
const data = await request(this.graphUrl, query)
const extrinsic = data?.extrinsics[0]
console.log("extrinsic =>", extrinsic)
console.log("extrinsic.events[0] =>", extrinsic?.events[0])
let res = null
if (extrinsic && extrinsic.block.finalized) {
const logsQuery = gql`
{
evmEvents (
offset: 0,
limit: 50,
where: {
AND: [
{ block: { hash_eq: "${extrinsic?.block?.hash}" }},
{ extrinsicIndex_eq: ${extrinsic?.index}}
]
}
) {
data
eventIndex
dataRaw
}
}
}
`
logger.verbose({
socket,
message: `=> querying data to ${this.graphUrl} ...`
})
const data = await request(this.graphUrl, query)
const extrinsic = data?.extrinsic[0]
let res = null
if (extrinsic && extrinsic.block.finalized) {
`
const logsData = await request(this.graphUrl, logsQuery)
const events: any[] = logsData?.extrinsic[0].events
const events: any[] = logsData?.evmEvents
try {
const gas = BigNumber.from(extrinsic.signed_data.fee.weight)
const fee = BigNumber.from(extrinsic.signed_data.fee.partialFee)
const gas = BigNumber.from(extrinsic.signedData.fee.weight)
const fee = BigNumber.from(extrinsic.signedData.fee.partialFee)
res = {
transactionHash: txHash,
transactionIndex: `0x${extrinsic.events[0]!.index.toString(16)}`,
transactionIndex: `0x${extrinsic?.index?.toString(16)}`,
blockHash: extrinsic.block.hash,
blockNumber: BigNumber.from(extrinsic.block.id).toHexString(),
blockNumber: BigNumber.from(extrinsic.block.height).toHexString(),
cumulativeGasUsed: gas.toHexString(),
gasUsed: gas.toHexString(),
contractAddress:
extrinsic.events[0]!.method === 'Created'
? extrinsic.events[0]!.data[1]
contractAddress:
extrinsic.events[0]?.method === 'Created'
? extrinsic.events[0]!.data[0][1]
: null,
status: extrinsic.status === 'success' ? '0x1' : '0x0',
logs: events?.map((event: any, index) => {
const log = event.data[0]
logs: events?.map((event: any) => {
const rawData = event.dataRaw
return {
removed: false,
logIndex: `0x${index.toString(16)}`,
transactionIndex: `0x${extrinsic.index}`,
logIndex: `0x${event?.eventIndex.toString(16)}`,
transactionIndex: `0x${extrinsic?.index}`,
transactionHash: txHash,
blockHash: extrinsic.block.hash,
blockNumber: BigNumber.from(extrinsic.block.id).toHexString(),
address: log.address,
data: log.data,
topics: log.topics
blockNumber: BigNumber.from(extrinsic.block.height).toHexString(),
address: rawData?.address,
data: rawData?.data,
topics: rawData?.topics || [],
}
}),
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
from: extrinsic.events[0]!.data[0],
to:
extrinsic.events[0]!.method === 'Created'
from: extrinsic.events[0]!.data[0][0],
to: extrinsic.events[0]?.method === 'Created'
? null
: extrinsic.events[0]!.data[1],
: extrinsic.events[0]?.data[0][1],
effectiveGasPrice: fee.div(gas).toHexString(),
type: '0x0'
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2994,10 +2994,10 @@
"@reef-defi/api-derive" "^1.1.0"
"@reef-defi/types" "^1.1.0"

"@reef-defi/evm-provider@^1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@reef-defi/evm-provider/-/evm-provider-1.0.9.tgz#3f089f8f94dc4945ff51c11fd6b4e52a681cf716"
integrity sha512-TYUA0FdM0QaHuTadYvVVUK86jWZZRQd2+ulG4XU9JEqgDz44YnmMuoPUS3QDQix/Kaf01mJFcpRZy+6TmCf7bA==
"@reef-defi/evm-provider@~1.0.10":
version "1.0.10"
resolved "https://registry.yarnpkg.com/@reef-defi/evm-provider/-/evm-provider-1.0.10.tgz#f2d755f37950f8ca2c6942e13ceb29974b6e1f54"
integrity sha512-IQl1imac+YXt4hOi2Q8qrwRt+4TjZPTC8SxfNK3vILaBW7N+SRA07lh5cvZ0htHvY6PErRoKNNKV+c6sKHTB1g==
dependencies:
"@open-web3/dev-config" "0.1.13"
"@open-web3/scanner" "1.0.1"
Expand Down

0 comments on commit aa358ec

Please sign in to comment.