|
| 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 | +} |
0 commit comments