Skip to content

Commit

Permalink
add iniital onchain policy sync
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaszimmermann committed Dec 23, 2024
1 parent 7a2283c commit 30b653f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

### Foundry/forge
out/
broadcast/
cache/

### Python ###
# Byte-compiled / optimized / DLL files
Expand Down
9 changes: 7 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,17 @@ anvil --mnemonic $ETH_MNEMONIC

1. Open a new shell
1. Run deploy script with `forge script` using the new private key in the `.env` file
1. Record the address of the newly deployed contract (used as `<contract-address>` below)
1. Record the address of the newly deployed token and product contracts (used as `<contract-address>` below)

```shell
forge clean
forge script script/MockSetup.s.sol --fork-url http://127.0.0.1:8545 --broadcast --private-key $(grep ETH_PRIVATE_KEY .env | cut -d= -f2)
```

### Interact with the Counter Contract
4. Update `PRODUCT_CONTRACT_ADDRESS` and `TOKEN_CONTRACT_ADDRESS` in `./app/.env` accordingly
5. Set OPERATOR_WALLET_MNEMONIC to ETH_MNEMONIC

### Interact with the Mock Contracts

1. set the `ETH_MNEMONIC` variable using the `.env` file
1. Start a python shell
Expand Down Expand Up @@ -403,6 +406,8 @@ uv run python3 app/main.py
Open the browser at `http://localhost:8000`.
The actual port is shown in the ports tab of VSCode.

To check/modify data use MongoDB Compass at `mongodb://localhost:27017`.
The DB `mongo` holds the collections `policies`, `risks`, etc.

### Initial Project Setup

Expand Down
8 changes: 4 additions & 4 deletions app/server/sync/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def sync_config_onchain(config: ConfigOut):

logger.info(f"synching config {config.id} onchain")

id = helper.toStr(config.id)
id = product.toStr(config.id)
year = config.year
name = helper.toStr(config.name)
season_start = helper.toStr(config.name)
season_end = helper.toStr(config.name)
name = product.toStr(config.name)
season_start = product.toStr(config.name)
season_end = product.toStr(config.name)
season_days = config.seasonDays

# execute transaction
Expand Down
10 changes: 8 additions & 2 deletions app/server/sync/policy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from util.logging import get_logger

from server.mongo import find_in_collection
from server.mongo import find_in_collection, update_in_collection
from server.model.person import PersonOut
from server.model.policy import PolicyOut
from server.model.risk import RiskOut
Expand Down Expand Up @@ -37,8 +37,14 @@ def sync_policy_onchain(policy: PolicyOut):
sum_insured = int(policy.sumInsuredAmount)
premium = int(policy.premiumAmount)


activate_at = int()
tx = product.createPolicy(policy_holder, risk_id, activate_at, sum_insured, premium, {'from': operator})

logger.info(f"{tx} onchain policy {policy.id} created")

# update policy with tx and nft id
logs = product.get_logs({'fromBlock':'latest'})
log_policy = product.contract.events.LogCropPolicyCreated.process_log(logs[0])
policy.nft = log_policy.args['policyNftId']
policy.tx = tx
update_in_collection(policy, PolicyOut)
99 changes: 0 additions & 99 deletions broadcast/MockSetup.s.sol/31337/run-latest.json

This file was deleted.

9 changes: 7 additions & 2 deletions src/CropProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {Amount, Location, NftId, RiskId, Str, Timestamp} from "./Types.sol";

contract CropProduct {

event LogCropPolicyCreated(NftId policyNftId);

error StringTooLong(string str);
error InvalidShortString();

uint256 public riskCounter;
uint96 public policyNftCounter = 100101;
uint96 public policyNftCounter = 100;

mapping(Str id => RiskId riskId) internal _riskId;

Expand Down Expand Up @@ -63,7 +65,10 @@ contract CropProduct {
returns (NftId policyNftId)
{
policyNftCounter++;
return NftId.wrap(policyNftCounter);
policyNftId = NftId.wrap(1000 * policyNftCounter + 101);
emit LogCropPolicyCreated(policyNftId);

return policyNftId;
}

function getRiskId(Str id)
Expand Down

0 comments on commit 30b653f

Please sign in to comment.