Skip to content

Commit

Permalink
Fix #397: Remove need to specify 'stake_token' in ppss.yaml (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmc authored Dec 18, 2023
1 parent bbfef0d commit a4c117b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 61 deletions.
37 changes: 19 additions & 18 deletions pdr_backend/models/predictoor_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ def get_price(self) -> int:

def get_current_epoch(self) -> int:
"""
curEpoch returns the timestamp of current candle start
this function returns the 'epoch number' that increases
Whereas curEpoch returns the timestamp of current candle start...
*This* function returns the 'epoch number' that increases
by one each secondsPerEpoch seconds
"""
current_epoch_ts = self.get_current_epoch_ts()
Expand Down Expand Up @@ -238,34 +237,36 @@ def soonest_timestamp_to_predict(self, timestamp: int) -> int:
def submit_prediction(
self,
predicted_value: bool,
stake_amount: float,
stake_amt: float,
prediction_ts: int,
wait_for_receipt=True,
):
"""
Submits a prediction with the specified stake amount, to the contract.
@description
Submits a prediction with the specified stake amount, to the contract.
@param predicted_value: The predicted value (True or False)
@param stake_amount: The amount of ETH to be staked on the prediction
@param prediction_ts: The prediction timestamp == start a candle.
@param wait_for_receipt:
If True, waits for tx receipt after submission.
If False, immediately after sending the transaction.
Default is True.
@arguments
predicted_value: The predicted value (True or False)
stake_amt: The amount of OCEAN to stake in prediction (in ETH, not wei)
prediction_ts: The prediction timestamp == start a candle.
wait_for_receipt:
If True, waits for tx receipt after submission.
If False, immediately after sending the transaction.
Default is True.
@return:
If wait_for_receipt is True, returns the tx receipt.
If False, returns the tx hash immediately after sending.
If an exception occurs during the process, returns None.
"""
amount_wei = to_wei(stake_amount)
stake_amt_wei = to_wei(stake_amt)

# Check allowance first, only approve if needed
if self.last_allowance <= 0:
self.last_allowance = self.token.allowance(
self.config.owner, self.contract_address
)
if self.last_allowance < amount_wei:
if self.last_allowance < stake_amt_wei:
try:
self.token.approve(self.contract_address, MAX_UINT)
self.last_allowance = MAX_UINT
Expand All @@ -279,15 +280,15 @@ def submit_prediction(
if is_sapphire_network(self.config.w3.eth.chain_id):
self.contract_instance.encodeABI(
fn_name="submitPredval",
args=[predicted_value, amount_wei, prediction_ts],
args=[predicted_value, stake_amt_wei, prediction_ts],
)
sender = self.config.owner
receiver = self.contract_instance.address
pk = self.config.account.key.hex()[2:]
res, txhash = send_encrypted_tx(
self.contract_instance,
"submitPredval",
[predicted_value, amount_wei, prediction_ts],
[predicted_value, stake_amt_wei, prediction_ts],
pk,
sender,
receiver,
Expand All @@ -300,10 +301,10 @@ def submit_prediction(
print("Encrypted transaction status code:", res)
else:
tx = self.contract_instance.functions.submitPredval(
predicted_value, amount_wei, prediction_ts
predicted_value, stake_amt_wei, prediction_ts
).transact(call_params)
txhash = tx.hex()
self.last_allowance -= amount_wei
self.last_allowance -= stake_amt_wei
print(f"Submitted prediction, txhash: {txhash}")
if not wait_for_receipt:
return txhash
Expand Down
60 changes: 39 additions & 21 deletions pdr_backend/models/test/test_predictoor_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pdr_backend.models.token import Token
from pdr_backend.models.predictoor_contract import mock_predictoor_contract
from pdr_backend.util.contract import get_address
from pdr_backend.util.mathutil import from_wei, to_wei


@enforce_types
Expand Down Expand Up @@ -93,40 +94,57 @@ def test_get_trueValSubmitTimeout(predictoor_contract):


@enforce_types
def test_submit_prediction_aggpredval_payout(predictoor_contract, ocean_token: Token):
def test_submit_prediction_trueval_payout(
predictoor_contract,
ocean_token: Token,
):
OCEAN = ocean_token
w3 = predictoor_contract.config.w3
owner_addr = predictoor_contract.config.owner
balance_before = ocean_token.balanceOf(owner_addr)
current_epoch = predictoor_contract.get_current_epoch_ts()
soonest_timestamp = predictoor_contract.soonest_timestamp_to_predict(current_epoch)
receipt = predictoor_contract.submit_prediction(True, 1, soonest_timestamp, True)
OCEAN_before = from_wei(OCEAN.balanceOf(owner_addr))
cur_epoch = predictoor_contract.get_current_epoch_ts()
soonest_ts = predictoor_contract.soonest_timestamp_to_predict(cur_epoch)
predval = True
stake_amt = 1.0
receipt = predictoor_contract.submit_prediction(
predval,
stake_amt,
soonest_ts,
wait_for_receipt=True,
)
assert receipt["status"] == 1

balance_after = ocean_token.balanceOf(owner_addr)
assert balance_before - balance_after == 1e18
OCEAN_after = from_wei(OCEAN.balanceOf(owner_addr))
assert (OCEAN_before - OCEAN_after) == approx(stake_amt, 1e-8)

prediction = predictoor_contract.get_prediction(
soonest_timestamp, predictoor_contract.config.owner
pred_tup = predictoor_contract.get_prediction(
soonest_ts,
predictoor_contract.config.owner,
)
assert prediction[0]
assert prediction[1] == 1e18

predictoor_contract.config.w3.provider.make_request(
"evm_increaseTime", [S_PER_EPOCH * 2]
assert pred_tup[0] == predval
assert pred_tup[1] == to_wei(stake_amt)

w3.provider.make_request("evm_increaseTime", [S_PER_EPOCH * 2])
w3.provider.make_request("evm_mine", [])
trueval = True
receipt = predictoor_contract.submit_trueval(
trueval,
soonest_ts,
cancel_round=False,
wait_for_receipt=True,
)
predictoor_contract.config.w3.provider.make_request("evm_mine", [])
receipt = predictoor_contract.submit_trueval(True, soonest_timestamp, False, True)
assert receipt["status"] == 1

receipt = predictoor_contract.payout(soonest_timestamp, True)
receipt = predictoor_contract.payout(soonest_ts, wait_for_receipt=True)
assert receipt["status"] == 1
balance_final = ocean_token.balanceOf(owner_addr)
assert balance_before / 1e18 == approx(balance_final / 1e18, 2.0) # + sub revenue
OCEAN_final = from_wei(OCEAN.balanceOf(owner_addr))
assert OCEAN_before == approx(OCEAN_final, 2.0) # + sub revenue


@enforce_types
def test_redeem_unused_slot_revenue(predictoor_contract):
current_epoch = predictoor_contract.get_current_epoch_ts() - S_PER_EPOCH * 123
receipt = predictoor_contract.redeem_unused_slot_revenue(current_epoch, True)
cur_epoch = predictoor_contract.get_current_epoch_ts() - S_PER_EPOCH * 123
receipt = predictoor_contract.redeem_unused_slot_revenue(cur_epoch, True)
assert receipt["status"] == 1


Expand Down
3 changes: 0 additions & 3 deletions pdr_backend/ppss/test/test_web3_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
"address_file": "address.json 1",
"rpc_url": "rpc url 1",
"subgraph_url": "subgraph url 1",
"stake_token": "0xStake1",
"owner_addrs": "0xOwner1",
}
_D2 = {
"address_file": "address.json 2",
"rpc_url": "rpc url 2",
"subgraph_url": "subgraph url 2",
"stake_token": "0xStake2",
"owner_addrs": "0xOwner2",
}
_D = {
Expand Down Expand Up @@ -58,7 +56,6 @@ def test_web3_pp__yaml_dict(monkeypatch):
assert pp.address_file == "address.json 1"
assert pp.rpc_url == "rpc url 1"
assert pp.subgraph_url == "subgraph url 1"
assert pp.stake_token == "0xStake1"
assert pp.owner_addrs == "0xOwner1"

# network2
Expand Down
19 changes: 9 additions & 10 deletions pdr_backend/ppss/web3_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ def rpc_url(self) -> str:
def subgraph_url(self) -> str:
return self.dn["subgraph_url"] # type: ignore[index]

@property
def stake_token(self) -> str:
return self.dn["stake_token"] # type: ignore[index]

@property
def owner_addrs(self) -> str:
return self.dn["owner_addrs"] # type: ignore[index]
Expand Down Expand Up @@ -169,7 +165,6 @@ def mock_web3_pp(network: str) -> Web3PP:
"address_file": "address.json 1",
"rpc_url": "http://example.com/rpc",
"subgraph_url": "http://example.com/subgraph",
"stake_token": "0xStake1",
"owner_addrs": "0xOwner1",
}
D = {
Expand Down Expand Up @@ -241,12 +236,16 @@ def get_secondsPerEpoch(self) -> int:
return self.s_per_epoch

def submit_prediction(
self, predval: bool, stake: float, timestamp: int, wait: bool = True
self,
predicted_value: bool,
stake_amt: float,
prediction_ts: int,
wait_for_receipt: bool = True,
): # pylint: disable=unused-argument
assert stake <= 3
if timestamp in self._prediction_slots:
print(f" (Replace prev pred at time slot {timestamp})")
self._prediction_slots.append(timestamp)
assert stake_amt <= 3
if prediction_ts in self._prediction_slots:
print(f" (Replace prev pred at time slot {prediction_ts})")
self._prediction_slots.append(prediction_ts)


@enforce_types
Expand Down
7 changes: 6 additions & 1 deletion pdr_backend/predictoor/base_predictoor_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ def take_step(self):

# submit prediction to chain
print("Submit predict tx to chain...")
self.feed_contract.submit_prediction(predval, stake, target_slot, True)
self.feed_contract.submit_prediction(
predval,
stake,
target_slot,
wait_for_receipt=True,
)
self.prev_submit_epochs.append(submit_epoch)
print("-> Submit predict tx result: success.")
print("" + "=" * 180)
Expand Down
8 changes: 0 additions & 8 deletions ppss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,31 @@ web3_pp:
address_file: "~/.ocean/ocean-contracts/artifacts/address.json"
rpc_url: https://testnet.sapphire.oasis.dev
subgraph_url: https://v4.subgraph.sapphire-testnet.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph
stake_token: "0x973e69303259B0c2543a38665122b773D28405fB" # (fake) OCEAN token address
owner_addrs: "0xe02a421dfc549336d47efee85699bd0a3da7d6ff" # OPF deployer address

sapphire-mainnet:
address_file: "~/.ocean/ocean-contracts/artifacts/address.json"
rpc_url: https://sapphire.oasis.io
subgraph_url: https://v4.subgraph.sapphire-mainnet.oceanprotocol.com/subgraphs/name/oceanprotocol/ocean-subgraph
stake_token: "0x39d22B78A7651A76Ffbde2aaAB5FD92666Aca520" # OCEAN token address
owner_addrs: "0x4ac2e51f9b1b0ca9e000dfe6032b24639b172703" # OPF deployer address

development:
address_file: "~/.ocean/ocean-contracts/artifacts/address.json"
rpc_url: http://localhost:8545
subgraph_url: http://localhost:9000/subgraphs/name/oceanprotocol/ocean-subgraph
# get by: grep --after-context=10 development ~/.ocean/ocean-contracts/artifacts/address.json|grep Ocean|sed -e 's/.*0x/stake_token: \"0x/'| sed -e 's/",//'
stake_token: "0x282d8efCe846A88B159800bd4130ad77443Fa1A1" # this value is just a placeholder; set it with above
owner_addrs: "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" # OPF deployer address. Taken from ocean.py setup-local.md FACTORY_DEPLOYER_PRIVATE_KEY

barge-predictoor-bot:
address_file: "~/barge-predictoor-bot.address.json"
private_key: "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58" # address is 0xe2DD...
rpc_url: http://4.245.224.119:8545 # from VPS
subgraph_url: http://4.245.224.119:9000/subgraphs/name/oceanprotocol/ocean-subgraph # from VPS
# get by: grep --after-context=10 development ~/barge-predictoor-bot.address.json|grep Ocean|sed -e 's/.*0x/stake_token: \"0x/'| sed -e 's/",//'
stake_token: "0x2473f4F7bf40ed9310838edFCA6262C17A59DF64" # this value is just a placeholder; set it with above
owner_addrs: "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" # OPF deployer address. Taken from ocean.py setup-local.md FACTORY_DEPLOYER_PRIVATE_KEY

barge-pytest:
address_file: "~/barge-pytest.address.json"
private_key: "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58"
rpc_url: http://74.234.16.165:8545
subgraph_url: http://74.234.16.165:9000/subgraphs/name/oceanprotocol/ocean-subgraph
# get by: grep --after-context=10 development ~/barge-pytest.address.json|grep Ocean|sed -e 's/.*0x/stake_token: \"0x/'| sed -e 's/",//'
stake_token: "0x2473f4F7bf40ed9310838edFCA6262C17A59DF64" # this value is just a placeholder; set it with above
owner_addrs: "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" # OPF deployer address. Taken from ocean.py setup-local.md FACTORY_DEPLOYER_PRIVATE_KEY

0 comments on commit a4c117b

Please sign in to comment.