Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
import os
from web3 import Web3


class ChainId(Enum):
Expand Down Expand Up @@ -160,6 +161,8 @@ class OperatorCategory(Enum):
},
}

DEFAULT_AURORA_GAS_PRICE = Web3.to_wei(0.07, "gwei")


SUBGRAPH_API_KEY_PLACEHOLDER = "[SUBGRAPH_API_KEY]"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_w3_with_priv_key(priv_key: str):
from web3.types import TxParams
from eth_utils import abi

from human_protocol_sdk.utils import validate_url
from human_protocol_sdk.utils import validate_url, apply_tx_defaults
from human_protocol_sdk.decorators import requires_signer

LOG = logging.getLogger("human_protocol_sdk.escrow")
Expand Down Expand Up @@ -278,7 +278,7 @@ def get_w3_with_priv_key(priv_key: str):
try:
tx_hash = self.factory_contract.functions.createEscrow(
token_address, trusted_handlers, job_requester_id
).transact(tx_options or {})
).transact(apply_tx_defaults(self.w3, tx_options))
receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
event = next(
(
Expand Down Expand Up @@ -366,7 +366,7 @@ def get_w3_with_priv_key(priv_key: str):
escrow_config.manifest,
escrow_config.hash,
)
.transact(tx_options or {})
.transact(apply_tx_defaults(self.w3, tx_options))
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down Expand Up @@ -431,7 +431,7 @@ def get_w3_with_priv_key(priv_key: str):
try:
tx_hash = token_contract.functions.transfer(
escrow_address, amount
).transact(tx_options or {})
).transact(apply_tx_defaults(self.w3, tx_options))
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
handle_error(e, EscrowClientError)
Expand Down Expand Up @@ -497,7 +497,7 @@ def get_w3_with_priv_key(priv_key: str):
tx_hash = (
self._get_escrow_contract(escrow_address)
.functions.storeResults(url, hash)
.transact(tx_options or {})
.transact(apply_tx_defaults(self.w3, tx_options))
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down Expand Up @@ -550,7 +550,7 @@ def get_w3_with_priv_key(priv_key: str):
tx_hash = (
self._get_escrow_contract(escrow_address)
.functions.complete()
.transact(tx_options or {})
.transact(apply_tx_defaults(self.w3, tx_options))
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down Expand Up @@ -646,7 +646,7 @@ def get_w3_with_priv_key(priv_key: str):
recipients, amounts, final_results_url, final_results_hash, txId
)
)
tx_hash = contract_func.transact(tx_options or {})
tx_hash = contract_func.transact(apply_tx_defaults(self.w3, tx_options))
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
handle_error(e, EscrowClientError)
Expand Down Expand Up @@ -874,7 +874,7 @@ def get_w3_with_priv_key(priv_key: str):
tx_hash = (
self._get_escrow_contract(escrow_address)
.functions.cancel()
.transact(tx_options or {})
.transact(apply_tx_defaults(self.w3, tx_options))
)
receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def get_w3_with_priv_key(priv_key: str):
get_kvstore_interface,
handle_error,
validate_url,
apply_tx_defaults,
)
from web3 import Web3
from web3.middleware import ExtraDataToPOAMiddleware
Expand Down Expand Up @@ -159,7 +160,7 @@ def get_w3_with_priv_key(priv_key: str):

try:
tx_hash = self.kvstore_contract.functions.set(key, value).transact(
tx_options or {}
apply_tx_defaults(self.w3, tx_options)
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down Expand Up @@ -216,7 +217,7 @@ def get_w3_with_priv_key(priv_key: str):

try:
tx_hash = self.kvstore_contract.functions.setBulk(keys, values).transact(
tx_options or {}
apply_tx_defaults(self.w3, tx_options)
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down Expand Up @@ -275,7 +276,7 @@ def get_w3_with_priv_key(priv_key: str):
try:
tx_hash = self.kvstore_contract.functions.setBulk(
[key, key + "_hash"], [url, content_hash]
).transact(tx_options or {})
).transact(apply_tx_defaults(self.w3, tx_options))
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
handle_error(e, KVStoreClientError)
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get_w3_with_priv_key(priv_key: str):
get_factory_interface,
get_staking_interface,
handle_error,
apply_tx_defaults,
)

LOG = logging.getLogger("human_protocol_sdk.staking")
Expand Down Expand Up @@ -178,7 +179,7 @@ def get_w3_with_priv_key(priv_key: str):
try:
tx_hash = self.hmtoken_contract.functions.approve(
self.network["staking_address"], amount
).transact(tx_options or {})
).transact(apply_tx_defaults(self.w3, tx_options))
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
handle_error(e, StakingClientError)
Expand Down Expand Up @@ -230,7 +231,7 @@ def get_w3_with_priv_key(priv_key: str):
raise StakingClientError("Amount to stake must be greater than 0")
try:
tx_hash = self.staking_contract.functions.stake(amount).transact(
tx_options or {}
apply_tx_defaults(self.w3, tx_options)
)
self.w3.eth.wait_for_transaction_receipt(tx_hash)
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
from web3.types import TxParams
from web3.middleware import SignAndSendRawMiddlewareBuilder

from human_protocol_sdk.constants import ARTIFACTS_FOLDER, SUBGRAPH_API_KEY_PLACEHOLDER
from human_protocol_sdk.constants import (
ARTIFACTS_FOLDER,
SUBGRAPH_API_KEY_PLACEHOLDER,
ChainId,
DEFAULT_AURORA_GAS_PRICE,
)

logger = logging.getLogger("human_protocol_sdk.utils")

Expand Down Expand Up @@ -319,3 +324,22 @@ def validate_json(data: str) -> bool:
return True
except (ValueError, TypeError):
return False


def apply_tx_defaults(w3: Web3, tx_options: Optional[TxParams]) -> TxParams:
"""Apply network specific default transaction parameters.

Aurora networks enforce a fixed gas price. We always override any user supplied
gasPrice with DEFAULT_AURORA_GAS_PRICE when on Aurora Testnet.
EIP-1559 fields are removed to avoid conflicts.

:param w3: Web3 instance (used to read chain id)
:param tx_options: Original transaction options (can be None)
:return: Mutated tx options with enforced defaults
"""
opts: TxParams = dict(tx_options) if tx_options else {}
if w3.eth.chain_id == ChainId.AURORA_TESTNET.value:
opts["gasPrice"] = DEFAULT_AURORA_GAS_PRICE
opts.pop("maxFeePerGas", None)
opts.pop("maxPriorityFeePerGas", None)
return opts
24 changes: 23 additions & 1 deletion packages/sdk/typescript/human-protocol-sdk/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ContractRunner } from 'ethers';
import { ContractRunner, Overrides } from 'ethers';
import { NetworkData } from './types';
import { ChainId } from './enums';
import { DEFAULT_AURORA_GAS_PRICE } from './constants';

/**
* ## Introduction
Expand All @@ -21,4 +23,24 @@ export abstract class BaseEthersClient {
this.networkData = networkData;
this.runner = runner;
}

/**
* Internal helper to enrich transaction overrides with network specific defaults.
*
* Aurora networks use a fixed gas price. We always override any user provided
* gasPrice with the canonical DEFAULT_AURORA_GAS_PRICE to avoid mismatches
* or tx failures due to an unexpected value. For other networks the user
* supplied fee parameters are left untouched.
*/
protected applyTxDefaults(txOptions: Overrides = {}): Overrides {
if (this.networkData.chainId === ChainId.AURORA_TESTNET) {
return {
...txOptions,
gasPrice: DEFAULT_AURORA_GAS_PRICE,
maxFeePerGas: undefined,
maxPriorityFeePerGas: undefined,
} as Overrides;
}
return txOptions;
}
}
3 changes: 3 additions & 0 deletions packages/sdk/typescript/human-protocol-sdk/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ethers } from 'ethers';
import { ChainId } from './enums';
import { NetworkData } from './types';

Expand All @@ -11,6 +12,8 @@ export const DEFAULT_PORT = 9000;

export const DEFAULT_USE_SSL = false;

export const DEFAULT_AURORA_GAS_PRICE = ethers.parseUnits('0.07', 'gwei');

export enum HttpStatus {
OK = 200,
CREATED = 201,
Expand Down
39 changes: 29 additions & 10 deletions packages/sdk/typescript/human-protocol-sdk/src/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class EscrowClient extends BaseEthersClient {
tokenAddress,
trustedHandlers,
jobRequesterId,
txOptions
this.applyTxDefaults(txOptions)
)
).wait();

Expand Down Expand Up @@ -380,7 +380,7 @@ export class EscrowClient extends BaseEthersClient {
exchangeOracleFee,
manifest,
manifestHash,
txOptions
this.applyTxDefaults(txOptions)
)
).wait();

Expand Down Expand Up @@ -444,7 +444,11 @@ export class EscrowClient extends BaseEthersClient {
this.runner
);
await (
await tokenContract.transfer(escrowAddress, amount, txOptions)
await tokenContract.transfer(
escrowAddress,
amount,
this.applyTxDefaults(txOptions)
)
).wait();

return;
Expand Down Expand Up @@ -511,7 +515,13 @@ export class EscrowClient extends BaseEthersClient {
try {
const escrowContract = this.getEscrowContract(escrowAddress);

await (await escrowContract.storeResults(url, hash, txOptions)).wait();
await (
await escrowContract.storeResults(
url,
hash,
this.applyTxDefaults(txOptions)
)
).wait();

return;
} catch (e) {
Expand Down Expand Up @@ -561,7 +571,9 @@ export class EscrowClient extends BaseEthersClient {
try {
const escrowContract = this.getEscrowContract(escrowAddress);

await (await escrowContract.complete(txOptions)).wait();
await (
await escrowContract.complete(this.applyTxDefaults(txOptions))
).wait();
return;
} catch (e) {
return throwError(e);
Expand Down Expand Up @@ -638,7 +650,7 @@ export class EscrowClient extends BaseEthersClient {
finalResultsHash,
txId,
forceComplete,
txOptions
this.applyTxDefaults(txOptions)
)
).wait();
} else {
Expand All @@ -651,7 +663,7 @@ export class EscrowClient extends BaseEthersClient {
finalResultsUrl,
finalResultsHash,
txId,
txOptions
this.applyTxDefaults(txOptions)
)
).wait();
}
Expand Down Expand Up @@ -704,7 +716,7 @@ export class EscrowClient extends BaseEthersClient {
const escrowContract = this.getEscrowContract(escrowAddress);

const transactionReceipt = await (
await escrowContract.cancel(txOptions)
await escrowContract.cancel(this.applyTxDefaults(txOptions))
).wait();

let amountTransferred: bigint | undefined = undefined;
Expand Down Expand Up @@ -801,7 +813,10 @@ export class EscrowClient extends BaseEthersClient {
const escrowContract = this.getEscrowContract(escrowAddress);

await (
await escrowContract.addTrustedHandlers(trustedHandlers, txOptions)
await escrowContract.addTrustedHandlers(
trustedHandlers,
this.applyTxDefaults(txOptions)
)
).wait();
return;
} catch (e) {
Expand Down Expand Up @@ -861,7 +876,10 @@ export class EscrowClient extends BaseEthersClient {
const escrowContract = this.getEscrowContract(escrowAddress);

const transactionReceipt = await (
await escrowContract.withdraw(tokenAddress, txOptions)
await escrowContract.withdraw(
tokenAddress,
this.applyTxDefaults(txOptions)
)
).wait();

let amountTransferred: bigint | undefined = undefined;
Expand Down Expand Up @@ -953,6 +971,7 @@ export class EscrowClient extends BaseEthersClient {
forceComplete = false,
txOptions: Overrides = {}
): Promise<TransactionLikeWithNonce> {
txOptions = this.applyTxDefaults(txOptions);
await this.ensureCorrectBulkPayoutInput(
escrowAddress,
recipients,
Expand Down
14 changes: 11 additions & 3 deletions packages/sdk/typescript/human-protocol-sdk/src/kvstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ export class KVStoreClient extends BaseEthersClient {
): Promise<void> {
if (key === '') throw ErrorKVStoreEmptyKey;
try {
await (await this.contract.set(key, value, txOptions)).wait();
await (
await this.contract.set(key, value, this.applyTxDefaults(txOptions))
).wait();
} catch (e) {
if (e instanceof Error) throw Error(`Failed to set value: ${e.message}`);
}
Expand Down Expand Up @@ -220,7 +222,13 @@ export class KVStoreClient extends BaseEthersClient {
if (keys.includes('')) throw ErrorKVStoreEmptyKey;

try {
await (await this.contract.setBulk(keys, values, txOptions)).wait();
await (
await this.contract.setBulk(
keys,
values,
this.applyTxDefaults(txOptions)
)
).wait();
} catch (e) {
if (e instanceof Error)
throw Error(`Failed to set bulk values: ${e.message}`);
Expand Down Expand Up @@ -273,7 +281,7 @@ export class KVStoreClient extends BaseEthersClient {
await this.contract.setBulk(
[urlKey, hashKey],
[url, contentHash],
txOptions
this.applyTxDefaults(txOptions)
)
).wait();
} catch (e) {
Expand Down
Loading
Loading