Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track errors #71

Merged
merged 5 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ERRORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Common Biconomy error responses

### code=UNPREDICTABLE_GAS_LIMIT:

```
{"timestamp":"{timestamp}","kind":"ERROR","system":"transactions-server","message":"Error: An error occurred trying to send the meta transaction.
Response: {\"log\":\"Error while gas estimation with message cannot estimate gas; transaction may fail or may require manual gas limit [...]
\\\\\\\"id\\\\\\\":{id},\\\\\\\"jsonrpc\\\\\\\":\\\\\\\"2.0\\\\\\\"}\\\",\\\"requestMethod\\\":\\\"POST\\\",\\\"url\\\":\\\
"https://rpc-biconomy-mainnet.maticvigil.com/v1/{key}\\\"}, method=\\\"estimateGas\\\", transaction={\\\"from\\\":\\\"{{from}}\\\",
\\\"to\\\":\\\"{to}\\\",\\\"data\\\":\\\"{data}\\\", code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.4.0)\",\"flag\":417,\"code\":417}.\n
```
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

Server to relay meta-transactions.

## Set up

You'll need to check the `.env.example` file and create your own `.env` file. Some properties have defaults. Once you're done, you can run the project!

# Run the project

The server's only dependency is sqlite3 which needs to be initialized first.

```bash
npm install
npm run migrate
```

After that you'll need to up check the `.env.example` file and create your own `.env` file. Some properties have defaults. Once you're done, you can run the project!

```bash
npm start # runs npm run build behind the scenes

# or
npm run start:watch # will watch for changes
```

Expand All @@ -32,3 +31,9 @@ You can also check this [`Playground`](https://web3playground.io/Qmd2WcPpBwM3NqB
### GET /transactions/:userAddress

Returns the transactions an address relayed

# Test

```bash
npm run test
```
27 changes: 22 additions & 5 deletions src/logic/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export async function sendMetaTransaction(
...transactionData,
}

metrics.increment('dcl_sent_transactions_biconomy', {
contract: transactionData.params[0],
})

const result = await fetch(biconomyAPIURL, {
headers: {
'x-api-key': biconomyAPIKey,
Expand All @@ -44,14 +40,35 @@ export async function sendMetaTransaction(
})

if (!result.ok) {
const errorMessage = await result.text()
const errorPayload = {
contract: transactionData.params[0],
data: transactionData.params[1],
from: transactionData.from,
}
if (errorMessage.includes('UNPREDICTABLE_GAS_LIMIT')) {
// This error happens when the contract execution will fail. See https://github.com/decentraland/transactions-server/blob/2e5d833f672a87a7acf0ff761f986421676c4ec9/ERRORS.md
metrics.increment(
'dcl_error_cannot_estimate_gas_transactions_biconomy',
errorPayload
)
} else {
// Any other error is related to the Biconomy API
metrics.increment('dcl_error_relay_transactions_biconomy', errorPayload)
}

throw new MetaTransactionError(
`An error occurred trying to send the meta transaction. Response: ${await result.text()}.
`An error occurred trying to send the meta transaction. Response: ${errorMessage}.
${result.statusText}`
)
}

const data: MetaTransactionResponse = await result.json()

metrics.increment('dcl_sent_transactions_biconomy', {
contract: transactionData.params[0],
})

return data.txHash!
}

Expand Down
11 changes: 8 additions & 3 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ export const metricDeclarations = {
type: IMetricsComponent.CounterType,
labelNames: ['contract'],
},
dcl_sent_amount_biconomy: {
help: 'Count transactions sent to BICONOMY',
dcl_error_cannot_estimate_gas_transactions_biconomy: {
help: 'Count errors of cannot estimate gas when trying to relay a transaction to BICONOMY',
type: IMetricsComponent.CounterType,
labelNames: ['contract', 'data', 'from'],
},
dcl_error_relay_transactions_biconomy: {
help: 'Count errors of BICONOMY Api when trying to relay a transaction to BICONOMY',
type: IMetricsComponent.CounterType,
labelNames: ['contract', 'amount'],
labelNames: ['contract', 'data', 'from'],
},
}

Expand Down
5 changes: 1 addition & 4 deletions test/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { createDotEnvConfigComponent } from '@well-known-components/env-config-p
import {
createServerComponent,
createStatusCheckComponent,
IFetchComponent,
} from '@well-known-components/http-server'
import { createLogComponent } from '@well-known-components/logger'
import { createMetricsComponent } from '@well-known-components/metrics'
import { metricDeclarations } from '../src/metrics'
import { createDatabaseComponent } from '../src/ports/database/component'
import {
createFetchComponent,
createTestFetchComponent,
} from '../src/ports/fetcher'
import { AppComponents, GlobalContext, TestComponents } from '../src/types'
import { GlobalContext, TestComponents } from '../src/types'
import { main } from '../src/service'
import { createRunner } from '@well-known-components/test-helpers'
import { RequestInfo, RequestInit } from 'node-fetch'

// start TCP port for listeners
let lastUsedPort = 19000 + parseInt(process.env.JEST_WORKER_ID || '1') * 1000
Expand Down
36 changes: 34 additions & 2 deletions test/tests/biconomy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test } from '../components'
import { RequestInit, Response } from 'node-fetch'
import { future } from 'fp-future'
import { Response } from 'node-fetch'
import { sendMetaTransaction } from '../../src/logic/transaction'
import {
MetaTransactionResponse,
Expand Down Expand Up @@ -65,5 +64,38 @@ test('biconomy flow test 1', function ({ components, stubComponents }) {
await expect(() =>
sendMetaTransaction({ metrics, fetcher, config }, tx)
).rejects.toThrow(/An error occurred trying to send the meta transaction/)

expect(
metrics.increment.calledOnceWith('dcl_error_relay_transactions_biconomy', {
contract: tx.params[0],
data: tx.params[1],
from: tx.from
})
).toEqual(true)
})

it('UNPREDICTABLE_GAS_LIMIT', async () => {
const { config } = components
const { metrics, fetcher } = stubComponents

const tx: TransactionData = { from: '0x1', params: ['1', '2'] }

const url = await config.requireString('BICONOMY_API_URL')

fetcher.fetch
.withArgs(url)
.returns(Promise.resolve(new Response('code=UNPREDICTABLE_GAS_LIMIT', { status: 503 })))

await expect(() =>
sendMetaTransaction({ metrics, fetcher, config }, tx)
).rejects.toThrow(/An error occurred trying to send the meta transaction/)

expect(
metrics.increment.calledOnceWith('dcl_error_cannot_estimate_gas_transactions_biconomy', {
contract: tx.params[0],
data: tx.params[1],
from: tx.from
})
).toEqual(true)
})
})