Skip to content

Commit f78655c

Browse files
authored
Merge pull request #432 from trustlines-protocol/update_eip1559
Try to make test pass with EIP1559
2 parents 06004a3 + 7611423 commit f78655c

31 files changed

Lines changed: 3131 additions & 272 deletions

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ COPY --from=builder /usr/local/bin/yarn /usr/local/bin/yarn
1414
COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
1515
COPY --from=builder /clientlib /clientlib
1616

17+
ENV RPC_PROVIDER_URL="http://node:8545"
18+
ENV SAFE_RELAY_PROVIDER_URL="http://nginx:8000/api"
1719
ENV RELAY_HOSTNAME relay
1820
WORKDIR /clientlib
1921
CMD ["yarn", "test:e2e"]

filterContracts/filterContracts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { writeFile } from 'fs'
33

44
const requiredContracts = ['CurrencyNetwork', 'Exchange', 'Identity', 'UnwEth']
55
const pathContractsModule =
6-
'node_modules/trustlines-contracts-abi/contracts.json'
6+
'node_modules/@trustlines/trustlines-contracts-abi/contracts.json'
77

88
const filteredAbi = Object.fromEntries(
99
Object.entries(TrustlinesContractsAbi['default']).filter(([key, value]) =>

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prettier:check": "prettier --check './{src,tests}/**/*.ts'",
1616
"prettier:big-tabs": "prettier --write --print-width 120 --no-semi --single-quote --tab-width 4 './{src,tests}/**/*.ts'",
1717
"test": "mocha --timeout 30000 --require isomorphic-fetch -r ts-node/register tests/**/**.test.ts",
18-
"test:e2e": "nyc mocha --timeout 30000 --require isomorphic-fetch -r ts-node/register tests/e2e/**.test.ts",
18+
"test:e2e": "nyc mocha --timeout 240000 --require isomorphic-fetch -r ts-node/register tests/e2e/**.test.ts",
1919
"test:integration": "nyc mocha --timeout 5000 --require isomorphic-fetch -r ts-node/register tests/integration/**.test.ts",
2020
"test:unit": "nyc mocha --timeout 30000 --require isomorphic-fetch -r ts-node/register tests/unit/**.test.ts",
2121
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
@@ -69,7 +69,7 @@
6969
"awesome-typescript-loader": "^5.2.0",
7070
"chai": "^4.1.2",
7171
"chai-as-promised": "^7.1.1",
72-
"codecov": "^3.5.0",
72+
"codecov": "^3.8.3",
7373
"es6-promise": "^4.1.1",
7474
"fetch-mock": "^7.2.5",
7575
"fluid-publish": "^2.2.0",
@@ -90,11 +90,12 @@
9090
"webpack-cli": "^3.0.8"
9191
},
9292
"dependencies": {
93+
"@gnosis.pm/safe-core-sdk": "^3.1.1",
9394
"@msgpack/msgpack": "^1.12.1",
9495
"@trustlines/trustlines-contracts-abi": "^2.0.0",
9596
"bignumber.js": "7.2.1",
9697
"ethereumjs-util": "^5.2.0",
97-
"ethers": "^4.0.23",
98+
"ethers": "5.7.1",
9899
"reconnecting-websocket": "^4.1.10",
99100
"rxjs": "^5.4.1",
100101
"simple-jsonrpc-js": "0.0.10",

src/TLNetwork.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ import { EthersWallet } from './wallets/EthersWallet'
2222
import {
2323
TLWallet,
2424
WALLET_TYPE_ETHERS,
25-
WALLET_TYPE_IDENTITY
25+
WALLET_TYPE_IDENTITY,
26+
WALLET_TYPE_SAFE
2627
} from './wallets/TLWallet'
2728

2829
import utils from './utils'
2930

31+
import { SafeRelayProvider } from './providers/SafeRelayProvider'
3032
import { NonceMechanism, ProviderUrl, TLNetworkConfig } from './typings'
3133
import { IdentityWallet } from './wallets/IdentityWallet'
34+
import { SafeWallet } from './wallets/SafeWallet'
3235

3336
/**
3437
* The TLNetwork class is the single entry-point into the trustlines-clientlib.
@@ -63,6 +66,7 @@ export class TLNetwork {
6366
path: url.path || defaultUrlParameters.path
6467
})
6568
}
69+
6670
/**
6771
* User instance containing all user/keystore related methods.
6872
*/
@@ -128,6 +132,10 @@ export class TLNetwork {
128132
* @hidden
129133
*/
130134
public messagingProvider: Provider
135+
/**
136+
* @hidden
137+
*/
138+
public safeRelayProvider: SafeRelayProvider
131139

132140
/**
133141
* Initiates a new TLNetwork instance that provides the public interface to trustlines-clientlib.
@@ -137,9 +145,12 @@ export class TLNetwork {
137145
const {
138146
relayUrl = {},
139147
messagingUrl = {},
148+
safeRelayUrl = {},
140149
web3Provider,
141150
identityFactoryAddress,
142151
identityImplementationAddress,
152+
gnosisSafeL2Address,
153+
gnosisSafeProxyFactoryAddress,
143154
walletType = WALLET_TYPE_ETHERS,
144155
chainId,
145156
nonceMechanism = NonceMechanism.Random
@@ -160,15 +171,22 @@ export class TLNetwork {
160171
new Provider(
161172
TLNetwork.getApiUrl(messagingUrl, defaultUrlParameters),
162173
TLNetwork.getWsUrl(messagingUrl, defaultUrlParameters)
174+
),
175+
new SafeRelayProvider(
176+
TLNetwork.getApiUrl(safeRelayUrl, defaultUrlParameters),
177+
TLNetwork.getWsUrl(safeRelayUrl, defaultUrlParameters)
163178
)
164179
)
165180

166181
this.setWallet(
167182
walletType,
168183
this.relayProvider,
184+
this.safeRelayProvider,
169185
chainId,
170186
identityFactoryAddress,
171187
identityImplementationAddress,
188+
gnosisSafeL2Address,
189+
gnosisSafeProxyFactoryAddress,
172190
nonceMechanism
173191
)
174192
this.setSigner(web3Provider, this.wallet)
@@ -233,13 +251,15 @@ export class TLNetwork {
233251
*/
234252
public setProviders(
235253
relayProvider: TLProvider,
236-
messagingProvider: Provider
254+
messagingProvider: Provider,
255+
safeRelayProvider: SafeRelayProvider
237256
): void {
238257
if (!(relayProvider instanceof RelayProvider)) {
239258
throw new Error('Provider not supported.')
240259
}
241260
this.relayProvider = relayProvider
242261
this.messagingProvider = messagingProvider
262+
this.safeRelayProvider = safeRelayProvider
243263
}
244264

245265
/**
@@ -254,7 +274,8 @@ export class TLNetwork {
254274
!(
255275
signer instanceof Web3Signer ||
256276
signer instanceof EthersWallet ||
257-
signer instanceof IdentityWallet
277+
signer instanceof IdentityWallet ||
278+
signer instanceof SafeWallet
258279
)
259280
) {
260281
throw new Error('Signer not supported.')
@@ -268,9 +289,12 @@ export class TLNetwork {
268289
public setWallet(
269290
walletType: string,
270291
provider: TLProvider,
292+
safeRelayProvider: SafeRelayProvider,
271293
chainId: number,
272294
identityFactoryAddress: string,
273295
identityImplementationAddress: string,
296+
gnosisSafeL2Address,
297+
gnosisSafeProxyFactoryAddress,
274298
nonceMechanism: NonceMechanism
275299
): void {
276300
let wallet: TLWallet
@@ -283,6 +307,17 @@ export class TLNetwork {
283307
identityImplementationAddress,
284308
nonceMechanism
285309
)
310+
} else if (walletType === WALLET_TYPE_SAFE) {
311+
wallet = new SafeWallet(
312+
provider,
313+
safeRelayProvider,
314+
chainId,
315+
identityFactoryAddress,
316+
identityImplementationAddress,
317+
gnosisSafeL2Address,
318+
gnosisSafeProxyFactoryAddress,
319+
nonceMechanism
320+
)
286321
} else if (walletType === WALLET_TYPE_ETHERS) {
287322
wallet = new EthersWallet(provider)
288323
} else {

src/Transaction.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { Interface } from '@ethersproject/abi'
2+
import { AddressZero } from '@ethersproject/constants'
3+
import { Logger } from '@ethersproject/logger'
14
import * as TrustlinesContractsAbi from '@trustlines/trustlines-contracts-abi'
25
import { BigNumber } from 'bignumber.js'
3-
import { ethers } from 'ethers'
4-
import { AddressZero } from 'ethers/constants'
56

67
import { TLProvider } from './providers/TLProvider'
78
import { TLSigner } from './signers/TLSigner'
@@ -22,12 +23,12 @@ import { CurrencyNetwork } from './CurrencyNetwork'
2223

2324
// Ethers will otherwise warn for every call on `updateTrustline` due to function overloading
2425
// see https://github.com/ethers-io/ethers.js/issues/407
25-
ethers.errors.setLogLevel('error')
26+
Logger.setLogLevel(Logger.levels.ERROR)
2627

2728
const ETH_DECIMALS = 18
2829
export const GAS_LIMIT_MULTIPLIER = 1.2
2930
// Value taken from the contracts gas tests
30-
export const GAS_LIMIT_IDENTITY_OVERHEAD = new BigNumber(27_000)
31+
export const GAS_LIMIT_IDENTITY_OVERHEAD = new BigNumber(50_000)
3132
export const GAS_LIMIT_VALUE_TRANSACTION = new BigNumber(21_000)
3233
.plus(GAS_LIMIT_IDENTITY_OVERHEAD.multipliedBy(GAS_LIMIT_MULTIPLIER))
3334
.integerValue(BigNumber.ROUND_DOWN)
@@ -73,11 +74,9 @@ export class Transaction {
7374
args: any[],
7475
options: TxOptionsInternal = {}
7576
): Promise<TxObjectInternal> {
76-
const abi = new ethers.utils.Interface(
77-
TrustlinesContractsAbi[contractName].abi
78-
)
77+
const abi = new Interface(TrustlinesContractsAbi[contractName].abi)
7978
const rawTx: RawTxObject = {
80-
data: abi.functions[functionName].encode(args),
79+
data: abi.encodeFunctionData(functionName, args),
8180
from: userAddress,
8281
to: contractAddress,
8382
gasLimit: options.gasLimit || GAS_LIMIT_DEFAULT_CONTRACT_TRANSACTION,

src/providers/Provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class Provider {
2929
public async postToEndpoint<T>(endpoint: string, data: any): Promise<T> {
3030
const options = {
3131
body: JSON.stringify(data),
32-
headers: new Headers({ 'Content-Type': 'application/json' }),
32+
headers: { 'Content-Type': 'application/json' },
3333
method: 'POST'
3434
}
3535
return this.fetchEndpoint<T>(endpoint, options)

src/providers/SafeRelayProvider.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { AddressZero } from '@ethersproject/constants'
2+
import {
3+
Amount,
4+
EstimationResponse,
5+
SafeMetaTransaction,
6+
SafeMultisigTxResponse,
7+
SafeTransactionFees,
8+
TransactionStatus,
9+
TransactionStatusObject
10+
} from '../typings'
11+
import utils from '../utils'
12+
import { Provider } from './Provider'
13+
14+
export class SafeRelayProvider extends Provider {
15+
public ApiUrl: string
16+
public WsApiUrl: string
17+
18+
public async plainFetch(endpoint: string) {
19+
const response = await fetch(`${this.ApiUrl}/${endpoint}`)
20+
return response
21+
}
22+
23+
/**
24+
* Returns a JSON response from the REST API of the server.
25+
*
26+
* We overload this method since the safe relay server from gnosis doesn't like
27+
* urls with a trimmed slash at the end.
28+
*
29+
* @param endpoint Endpoint to fetch.
30+
* @param options Optional fetch options.
31+
*/
32+
public async fetchEndpoint<T>(
33+
endpoint: string,
34+
options?: object
35+
): Promise<T> {
36+
return utils.fetchUrl<T>(`${this.ApiUrl}/${endpoint}`, options)
37+
}
38+
39+
public async getSafeNonce(safeAddress: string): Promise<number> {
40+
const response = await this.plainFetch(`v1/safes/${safeAddress}/`)
41+
42+
if (response.status === 200) {
43+
const json = await response.json()
44+
return json.nonce
45+
}
46+
47+
throw new Error(`Error fetching safe nonce: ${response.status}`)
48+
}
49+
50+
/**
51+
* Returns the fees the provider would be willing to pay for the transaction
52+
* @param metaTransaction Meta transaction to be relayed
53+
* @returns The fees value and currency network of fees for given meta transaction
54+
*/
55+
public async getMetaTxFees(
56+
metaTransaction: SafeMetaTransaction
57+
): Promise<SafeTransactionFees> {
58+
const data = {
59+
safe: metaTransaction.safe,
60+
to: metaTransaction.to,
61+
value: metaTransaction.value,
62+
data: metaTransaction.data,
63+
operation: metaTransaction.operation
64+
}
65+
66+
const response: EstimationResponse = await this.postToEndpoint(
67+
`v2/safes/${metaTransaction.safe}/transactions/estimate/`,
68+
data
69+
)
70+
71+
return {
72+
safeTxGas: response.safeTxGas,
73+
baseGas: response.baseGas,
74+
gasPrice: response.gasPrice,
75+
refundReceiver: response.refundReceiver,
76+
gasToken: response.gasToken || AddressZero
77+
}
78+
}
79+
80+
/**
81+
* Send the given signed meta-transaction to a relay server to execute it on the
82+
* blockchain and returns a `Promise` with the transaction hash.
83+
* @param signedMetaTransaction Signed meta-transaction to be sent to the relay server
84+
* @returns The hash of the transaction sent by the relay server, not to be confused with the hash of the meta-transaction
85+
*/
86+
public async sendSignedMetaTransaction(
87+
signedMetaTransaction: SafeMetaTransaction
88+
): Promise<any> {
89+
const response: any = await this.postToEndpoint(
90+
`v1/safes/${signedMetaTransaction.safe}/transactions/`,
91+
signedMetaTransaction
92+
)
93+
94+
return response.txHash
95+
}
96+
97+
/**
98+
* Returns implementation address of identity with given address
99+
* @param address Address of identity
100+
* @returns the implementation address currently in use by the given identity
101+
*/
102+
public async getIdentityImplementationAddress(
103+
address: string
104+
): Promise<string> {
105+
const { masterCopy } = await this.fetchEndpoint<any>(`v1/safes/${address}`)
106+
return masterCopy
107+
}
108+
109+
public async getMetaTxStatus(
110+
safeAddress: string,
111+
transactionHash: string
112+
): Promise<TransactionStatusObject> {
113+
try {
114+
const response: SafeMultisigTxResponse = await this.fetchEndpoint(
115+
`/v1/safes/${safeAddress}/transactions/?safe_tx_hash=${transactionHash}`
116+
)
117+
118+
if (response.results.length) {
119+
const tx = response.results[0]
120+
const status = tx.metaTxSuccessful
121+
return {
122+
status: status ? TransactionStatus.Success : TransactionStatus.Failure
123+
}
124+
}
125+
} catch (e) {
126+
if (e.message.includes('Status 404')) {
127+
return { status: TransactionStatus.NotFound }
128+
}
129+
return { status: TransactionStatus.Failure }
130+
}
131+
}
132+
133+
/**
134+
* Returns balance of given address.
135+
* @param address Address to determine balance for.
136+
*/
137+
public async getBalance(address: string): Promise<Amount> {
138+
// return utils.formatToAmount(0, 18)
139+
try {
140+
const response: Array<{
141+
tokenAddress: string | null
142+
balance: string
143+
}> = await this.fetchEndpoint(`/v1/safes/${address}/balances`)
144+
145+
return utils.formatToAmount(response[0].balance, 18)
146+
} catch (e) {
147+
return utils.formatToAmount(0, 18)
148+
}
149+
}
150+
}

src/signers/TLSigner.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Amount,
33
MetaTransactionFees,
44
RawTxObject,
5+
SafeTransactionFees,
56
Signature,
67
TransactionStatusObject,
78
TxObjectRaw
@@ -19,5 +20,7 @@ export interface TLSigner {
1920
confirm(rawTx: RawTxObject): Promise<string>
2021
prepareTransaction(rawTx: RawTxObject): Promise<TxObjectRaw>
2122
getTxStatus(txHash: string | RawTxObject): Promise<TransactionStatusObject>
22-
getMetaTxFees(rawTx: RawTxObject): Promise<MetaTransactionFees>
23+
getMetaTxFees(
24+
rawTx: RawTxObject
25+
): Promise<MetaTransactionFees | SafeTransactionFees>
2326
}

0 commit comments

Comments
 (0)