diff --git a/README.md b/README.md index ad752b3..56da8ce 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Example session inside the Brownie console * Deployment of a GIF instance * Deployment and usage of Test oracle and product -```bash +```python # --- imports --- import uuid from scripts.product import GifInstance, GifTestOracle, GifTestProduct, GifTestRiskpool @@ -133,7 +133,7 @@ txPolicy2 = product.applyForPolicy(premium, sumInsured, metaData, applicationDat Brownie console commands to deploy/use the example product -```shell +```python from scripts.area_yield_index import GifAreaYieldIndexOracle, GifAreaYieldIndexProduct from scripts.setup import fund_riskpool, fund_customer @@ -235,7 +235,7 @@ owner.address ``` Use Polygon test [faucet](https://faucet.polygon.technology/) to fund the owner address -```bash +```python from scripts.instance import GifInstance # publishes source code to the network @@ -255,21 +255,85 @@ brownie run scripts/instance.py dump_sources 0x2852593b21796b549555d09873155B252 ## Full Deployment with Example Product -```bash -# --- ganache accounts setup ------------- -instanceOperator=accounts[0] -instanceWallet=accounts[1] -oracleProvider=accounts[2] -chainlinkNodeOperator=accounts[3] -riskpoolKeeper=accounts[4] -riskpoolWallet=accounts[5] -investor=accounts[6] -productOwner=accounts[7] -insurer=accounts[8] -customer=accounts[9] -customer2=accounts[10] +Before attempting to deploy the setup on a life chain ensure that the +`instanceOperator` has sufficient funds to cover the setup. + +For testnets faucet funds may be used + +* [avax-test (Fuji (C-Chain))](https://faucet.avax.network/) +* [polygon-test](https://faucet.polygon.technology/) + +Using the ganache scenario shown below ensures that all addresses used are sufficiently funded. + +```python +from scripts.deploy_ayii import ( + stakeholders_accounts_ganache, + check_funds, + amend_funds, + deploy, + from_registry, + from_component, +) + +from scripts.instance import ( + GifInstance, + dump_sources +) + +from scripts.util import ( + s2b, + b2s, + contract_from_address, +) + +# for ganche the command below may be used +# for other chains, use accounts.add() and record the mnemonics +a = stakeholders_accounts_ganache() + +# check_funds checks which stakeholder accounts need funding for the deploy +# also, it checks if the instanceOperator has a balance that allows to provided +# the missing funds for the other accounts +check_funds(a) + +# amend_funds transfers missing funds to stakeholder addresses using the +# avaulable balance of the instanceOperator +amend_funds(a) + +publishSource=False +d = deploy(a, publishSource) + +( +componentOwnerService,customer1,customer2,erc20Token,instance,instanceOperator,instanceOperatorService,instanceService, +instanceWallet,insurer,investor,oracle,oracleProvider,processId1,processId2,product,productOwner,riskId1,riskId2, +riskpool,riskpoolKeeper,riskpoolWallet +)=( +d['componentOwnerService'],d['customer1'],d['customer2'],d['erc20Token'],d['instance'],d['instanceOperator'],d['instanceOperatorService'],d['instanceService'], +d['instanceWallet'],d['insurer'],d['investor'],d['oracle'],d['oracleProvider'],d['processId1'],d['processId2'],d['product'],d['productOwner'],d['riskId1'],d['riskId2'], +d['riskpool'],d['riskpoolKeeper'],d['riskpoolWallet'] +) + +# the deployed setup can now be used +# example usage +instanceOperator +instance.getRegistry() +instanceService.getInstanceId() + +product.getId() +b2s(product.getName()) + +customer1 +instanceService.getMetadata(processId1) +instanceService.getApplication(processId1) +instanceService.getPolicy(processId1) +``` + +For a first time setup on a live chain the setup below can be used. -# --- test net accounts setup ------------- +IMPORTANT: Make sure to write down the generated mnemonics for the +stakeholder accounts. To reuse the same accounts replace `accounts.add` +with `accounts.from_mnemonic` using the recorded mnemonics. + +```python instanceOperator=accounts.add() instanceWallet=accounts.add() oracleProvider=accounts.add() @@ -279,108 +343,47 @@ riskpoolWallet=accounts.add() investor=accounts.add() productOwner=accounts.add() insurer=accounts.add() -customer=accounts.add() +customer1=accounts.add() customer2=accounts.add() -# --- optional erc20 token deploy ------------- -# skip this if the value token for the product and -# riskpool is already deployed - -erc20Token = TestCoin.deploy({'from': instanceOperator}) - -# --- gif instance deploy deploy ------------- -# if the gif instance is already deployed replace -# the command below with the following line -# instance = GifInstance(registryAddress='0x...') -from scripts.instance import GifInstance - -instance = GifInstance(instanceOperator, instanceWallet=instanceWallet) -instanceService = instance.getInstanceService() - -instance.getRegistry() - -# --- example product deploy deploy ------------- -from scripts.ayii_product import GifAyiiProductComplete - -ayiiDeploy = GifAyiiProductComplete(instance, productOwner, insurer, oracleProvider, chainlinkNodeOperator, riskpoolKeeper, investor, erc20Token, riskpoolWallet) - -ayiiProduct = ayiiDeploy.getProduct() -ayiiOracle = ayiiProduct.getOracle() -ayiiRiskpool = ayiiProduct.getRiskpool() - -product = ayiiProduct.getContract() -oracle = ayiiOracle.getContract() -riskpool = ayiiRiskpool.getContract() +a = { + 'instanceOperator': instanceOperator, + 'instanceWallet': instanceWallet, + 'oracleProvider': oracleProvider, + 'chainlinkNodeOperator': chainlinkNodeOperator, + 'riskpoolKeeper': riskpoolKeeper, + 'riskpoolWallet': riskpoolWallet, + 'investor': investor, + 'productOwner': productOwner, + 'insurer': insurer, + 'customer1': customer1, + 'customer2': customer2, +} ``` -## Interact with Example Product - -```bash -from scripts.util import s2b32 - -#--- setup risk (group policy) definition ----------------- -projectId = s2b32('test-project') -uaiId = s2b32('some-region-id') -cropId = s2b32('maize') +To interact with an existing setup use the following helper methods as shown below. -multiplier = product.getPercentageMultiplier() -trigger = 0.75 * multiplier -exit_ = 0.1 * multiplier # exit is needed to exit the console -tsi = 0.9 * multiplier -aph = 2.0 * multiplier +```python +from scripts.deploy_ayii import ( + from_registry, + from_component, +) -tx = product.createRisk(projectId, uaiId, cropId, trigger, exit_, tsi, aph, - {'from': insurer}) -riskId = tx.return_value +from scripts.instance import ( + GifInstance, +) -#--- fund investor which in turn creates a risk bundle --- -bundleInitialFunding=1000000 -erc20Token.transfer(investor, bundleInitialFunding, {'from': instanceOperator}) -erc20Token.approve(instance.getTreasury(), bundleInitialFunding, {'from': investor}) +from scripts.util import ( + s2b, + b2s, + contract_from_address, +) -applicationFilter = bytes(0) -riskpool.createBundle(applicationFilter, bundleInitialFunding, {'from': investor}) +# for the case of a known registry address, +# eg '0xE7eD6747FaC5360f88a2EFC03E00d25789F69291' +(instance, product, oracle, riskpool) = from_registry('0xE7eD6747FaC5360f88a2EFC03E00d25789F69291') -instanceService.getBundle(1) -erc20Token.balanceOf(riskpoolWallet.address) -erc20Token.balanceOf(instanceWallet.address) - -# approvel for payouts/defunding -maxUint256 = 2**256-1 -erc20Token.approve(instance.getTreasury(), maxUint256, {'from': riskpoolWallet}) - -#--- fund customer which in turn applies for a policy --- -customerFunding=1000 -erc20Token.transfer(customer, customerFunding, {'from': instanceOperator}) - -premium = 100 -sumInsured = 20000 - -tx = product.applyForPolicy(customer, premium, sumInsured, riskId, - {'from': insurer}) -policyId = tx.return_value - -# print data for bundle and newly created policy -riskpool.getTotalValueLocked() # shows the 20000 of locked capital to cover the sum insurance -instanceService.getBundle(1) # bundle state, locked capital (20000 to cover sum insured) -instanceService.getMetadata(policyId) # policy owner and product id -instanceService.getApplication(policyId) # premium, sum insurec, risk id -instanceService.getPolicy(policyId) # policy state, premium payed (=0 for now) - -# premium payment in bits, allowance set for full premium -erc20Token.approve(instance.getTreasury(), 100, {'from': customer}) - -# first installment -product.collectPremium(policyId, 40, {'from':insurer}) -instanceService.getPolicy(policyId) - -# second installment -product.collectPremium(policyId, 60, {'from':insurer}) -instanceService.getPolicy(policyId) - -# TODO add: -# - oracle call -# - policy processing -# - payout to customer -# - definding back to inverster +# or for a known address of a component, eg +# eg product address '0xF039D8acecbB47763c67937D66A254DB48c87757' +(instance, product, oracle, riskpool) = from_component('0xF039D8acecbB47763c67937D66A254DB48c87757') ``` \ No newline at end of file diff --git a/contracts/modules/ComponentController.sol b/contracts/modules/ComponentController.sol index 53339bf..f657b00 100644 --- a/contracts/modules/ComponentController.sol +++ b/contracts/modules/ComponentController.sol @@ -4,6 +4,9 @@ pragma solidity ^0.8.0; import "../shared/CoreController.sol"; import "@etherisc/gif-interface/contracts/components/IComponent.sol"; +import "@etherisc/gif-interface/contracts/components/IOracle.sol"; +import "@etherisc/gif-interface/contracts/components/IProduct.sol"; +import "@etherisc/gif-interface/contracts/components/IRiskpool.sol"; import "@etherisc/gif-interface/contracts/modules/IComponentEvents.sol"; contract ComponentController is @@ -202,6 +205,18 @@ contract ComponentController is return _componentState[id]; } + function getOracleId(uint256 idx) public view returns (uint256 oracleId) { + return _oracles[idx]; + } + + function getRiskpoolId(uint256 idx) public view returns (uint256 riskpoolId) { + return _riskpools[idx]; + } + + function getProductId(uint256 idx) public view returns (uint256 productId) { + return _products[idx]; + } + function getRequiredRole(IComponent.ComponentType componentType) external returns (bytes32) { if (componentType == IComponent.ComponentType.Product) { return _access.getProductOwnerRole(); } else if (componentType == IComponent.ComponentType.Oracle) { return _access.getOracleProviderRole(); } diff --git a/contracts/services/InstanceService.sol b/contracts/services/InstanceService.sol index 84b8fd9..e65386c 100644 --- a/contracts/services/InstanceService.sol +++ b/contracts/services/InstanceService.sol @@ -10,6 +10,9 @@ import "../shared/CoreController.sol"; import "../services/InstanceOperatorService.sol"; import "@etherisc/gif-interface/contracts/components/IComponent.sol"; +import "@etherisc/gif-interface/contracts/components/IOracle.sol"; +import "@etherisc/gif-interface/contracts/components/IProduct.sol"; +import "@etherisc/gif-interface/contracts/components/IRiskpool.sol"; import "@etherisc/gif-interface/contracts/modules/IPolicy.sol"; import "@etherisc/gif-interface/contracts/modules/IRegistry.sol"; import "@etherisc/gif-interface/contracts/services/IComponentOwnerService.sol"; @@ -137,10 +140,6 @@ contract InstanceService is return _component.getComponentId(componentAddress); } - function getComponent(uint256 id) external override view returns(IComponent) { - return _component.getComponent(id); - } - function getComponentType(uint256 componentId) external override view @@ -157,6 +156,22 @@ contract InstanceService is componentState = _component.getComponentState(componentId); } + function getComponent(uint256 id) external override view returns(IComponent) { + return _component.getComponent(id); + } + + function getOracleId(uint256 idx) public view returns (uint256 oracleId) { + return _component.getOracleId(idx); + } + + function getRiskpoolId(uint256 idx) public view returns (uint256 riskpoolId) { + return _component.getRiskpoolId(idx); + } + + function getProductId(uint256 idx) public view returns (uint256 productId) { + return _component.getProductId(idx); + } + /* service staking */ function getStakingRequirements(uint256 id) external override diff --git a/scripts/ayii_product.py b/scripts/ayii_product.py index 5cd57fb..56f1379 100644 --- a/scripts/ayii_product.py +++ b/scripts/ayii_product.py @@ -139,13 +139,15 @@ def __init__(self, self.clToken = ClToken.deploy( clTokenOwner, clTokenSupply, - {'from': oracleProvider}) + {'from': oracleProvider}, + publish_source=publishSource) # 2b) chainlink operator deploy self.chainlinkOperator = ClOperator.deploy( self.clToken, chainlinkNodeOperator, - {'from': oracleProvider}) + {'from': oracleProvider}, + publish_source=publishSource) # set oracleProvider as authorized to call fullfill on operator self.chainlinkOperator.setAuthorizedSenders([chainlinkNodeOperator]) @@ -163,9 +165,8 @@ def __init__(self, chainLinkOracleAddress, chainLinkJobId, chainLinkPaymentAmount, - {'from': oracleProvider}) - # {'from': oracleProvider}, - # publish_source=publishSource) + {'from': oracleProvider}, + publish_source=publishSource) # 3) oracle owner proposes oracle to instance componentOwnerService.propose( @@ -277,7 +278,6 @@ def getPolicy(self, policyId: str): return self.policy.getPolicy(policyId) - class GifAyiiProductComplete(object): def __init__(self, diff --git a/scripts/component.py b/scripts/component.py new file mode 100644 index 0000000..b7b50e7 --- /dev/null +++ b/scripts/component.py @@ -0,0 +1,42 @@ +from web3 import Web3 + +from brownie import Contract +from brownie.convert import to_bytes +from brownie.network import accounts +from brownie.network.account import Account + +from brownie import ( + Wei, + interface, + Contract, + PolicyController, + OracleService, + ComponentOwnerService, + InstanceOperatorService, + InstanceService, +) + +from scripts.util import ( + get_account, + contractFromAddress, + s2b32, +) + +from scripts.instance import GifInstance + + +class GifComponent(object): + + def __init__(self, + componentAddress: Account, + ): + self.component = contractFromAddress(interface.IComponent, componentAddress) + self.instance = GifInstance(registryAddress=self.component.getRegistry()) + + instanceService = self.instance.getInstanceService() + instanceOperatorService = self.instance.getInstanceOperatorService() + componentOwnerService = self.instance.getComponentOwnerService() + riskpoolService = self.instance.getRiskpoolService() + + self.component = contractFromAddress(interface.IComponent, componentAddress) + self.instance = GifInstance(registryAddress=self.component.getRegistry()) \ No newline at end of file diff --git a/scripts/deploy_ayii.py b/scripts/deploy_ayii.py new file mode 100644 index 0000000..652f592 --- /dev/null +++ b/scripts/deploy_ayii.py @@ -0,0 +1,296 @@ +from brownie.network import accounts +from brownie.network.account import Account + +from brownie import ( + interface, + TestCoin, + InstanceService, + InstanceOperatorService, + ComponentOwnerService, + AyiiProduct, + AyiiOracle, + AyiiRiskpool +) + +from scripts.ayii_product import GifAyiiProductComplete +from scripts.instance import GifInstance +from scripts.util import contract_from_address, s2b32 + +INSTANCE_OPERATOR = 'instanceOperator' +INSTANCE_WALLET = 'instanceWallet' +ORACLE_PROVIDER = 'oracleProvider' +NODE_OPERATOR = 'chainlinkNodeOperator' +RISKPOOL_KEEPER = 'riskpoolKeeper' +RISKPOOL_WALLET = 'riskpoolWallet' +INVESTOR = 'investor' +PRODUCT_OWNER = 'productOwner' +INSURER = 'insurer' +CUSTOMER1 = 'customer1' +CUSTOMER2 = 'customer2' + +ERC20_TOKEM = 'erc20Token' +INSTANCE = 'instance' +INSTANCE_SERVICE = 'instanceService' +INSTANCE_OPERATOR_SERVICE = 'instanceOperatorService' +COMPONENT_OWNER_SERVICE = 'componentOwnerService' +PRODUCT = 'product' +ORACLE = 'oracle' +RISKPOOL = 'riskpool' + +RISK_ID1 = 'riskId1' +RISK_ID2 = 'riskId2' +PROCESS_ID1 = 'processId1' +PROCESS_ID2 = 'processId2' + +REQUIRED_FUNDS_S = 50000000000000000 +REQUIRED_FUNDS_M = 150000000000000000 +REQUIRED_FUNDS_L = 1500000000000000000 + +REQUIRED_FUNDS = { + INSTANCE_OPERATOR: REQUIRED_FUNDS_L, + INSTANCE_WALLET: REQUIRED_FUNDS_S, + PRODUCT_OWNER: REQUIRED_FUNDS_M, + INSURER: REQUIRED_FUNDS_S, + ORACLE_PROVIDER: REQUIRED_FUNDS_M, + RISKPOOL_KEEPER: REQUIRED_FUNDS_M, + RISKPOOL_WALLET: REQUIRED_FUNDS_S, + INVESTOR: REQUIRED_FUNDS_S, + CUSTOMER1: REQUIRED_FUNDS_S, + CUSTOMER2: REQUIRED_FUNDS_S, +} + +def stakeholders_accounts_ganache(): + # define stakeholder accounts + instanceOperator=accounts[0] + instanceWallet=accounts[1] + oracleProvider=accounts[2] + chainlinkNodeOperator=accounts[3] + riskpoolKeeper=accounts[4] + riskpoolWallet=accounts[5] + investor=accounts[6] + productOwner=accounts[7] + insurer=accounts[8] + customer=accounts[9] + customer2=accounts[10] + + return { + INSTANCE_OPERATOR: instanceOperator, + INSTANCE_WALLET: instanceWallet, + ORACLE_PROVIDER: oracleProvider, + NODE_OPERATOR: chainlinkNodeOperator, + RISKPOOL_KEEPER: riskpoolKeeper, + RISKPOOL_WALLET: riskpoolWallet, + INVESTOR: investor, + PRODUCT_OWNER: productOwner, + INSURER: insurer, + CUSTOMER1: customer, + CUSTOMER2: customer2, + } + + +def check_funds(stakeholders_accounts): + a = stakeholders_accounts + fundsMissing = 0 + for accountName, requiredAmount in REQUIRED_FUNDS.items(): + if a[accountName].balance() >= REQUIRED_FUNDS[accountName]: + print('{} funding ok'.format(accountName)) + else: + fundsMissing += REQUIRED_FUNDS[accountName] - a[accountName].balance() + print('{} needs {} but has {}'.format( + accountName, + REQUIRED_FUNDS[accountName], + a[accountName].balance() + )) + + if fundsMissing > 0: + if a[INSTANCE_OPERATOR].balance() >= REQUIRED_FUNDS[INSTANCE_OPERATOR] + fundsMissing: + print('{} sufficiently funded to cover missing funds'.format(INSTANCE_OPERATOR)) + else: + print('{} needs additional funding of {} to cover missing funds'.format( + INSTANCE_OPERATOR, + REQUIRED_FUNDS[INSTANCE_OPERATOR] + fundsMissing - a[INSTANCE_OPERATOR].balance() + )) + + +def amend_funds(stakeholders_accounts): + a = stakeholders_accounts + for accountName, requiredAmount in REQUIRED_FUNDS.items(): + if a[accountName].balance() < REQUIRED_FUNDS[accountName]: + missingAmount = REQUIRED_FUNDS[accountName] - a[accountName].balance() + print('funding {} with {}'.format(accountName, missingAmount)) + a[INSTANCE_OPERATOR].transfer(a[accountName], missingAmount) + + +def deploy( + stakeholders_accounts, + publishSource=False +): + + # define stakeholder accounts + a = stakeholders_accounts + instanceOperator=a[INSTANCE_OPERATOR] + instanceWallet=a[INSTANCE_WALLET] + oracleProvider=a[ORACLE_PROVIDER] + chainlinkNodeOperator=a[NODE_OPERATOR] + riskpoolKeeper=a[RISKPOOL_KEEPER] + riskpoolWallet=a[RISKPOOL_WALLET] + investor=a[INVESTOR] + productOwner=a[PRODUCT_OWNER] + insurer=a[INSURER] + customer=a[CUSTOMER1] + customer2=a[CUSTOMER2] + + # token definition, funding of investor and customer + erc20Token = TestCoin.deploy({'from': instanceOperator}) + + # gif instance deployment + instance = GifInstance(instanceOperator, instanceWallet=instanceWallet) + instanceService = instance.getInstanceService() + instanceOperatorService = instance.getInstanceOperatorService() + componentOwnerService = instance.getComponentOwnerService() + + # ayii deployment + ayiiDeploy = GifAyiiProductComplete(instance, productOwner, insurer, oracleProvider, chainlinkNodeOperator, riskpoolKeeper, investor, erc20Token, riskpoolWallet) + + ayiiProduct = ayiiDeploy.getProduct() + ayiiOracle = ayiiProduct.getOracle() + ayiiRiskpool = ayiiProduct.getRiskpool() + + product = ayiiProduct.getContract() + oracle = ayiiOracle.getContract() + riskpool = ayiiRiskpool.getContract() + + # investor funding and bundle creation + bundleInitialFunding=1000000 + erc20Token.transfer(investor, bundleInitialFunding, {'from': instanceOperator}) + erc20Token.approve(instance.getTreasury(), bundleInitialFunding, {'from': investor}) + + maxUint256 = 2**256-1 + erc20Token.approve(instance.getTreasury(), maxUint256, {'from': riskpoolWallet}) + + # create bundle for investor + applicationFilter = bytes(0) + riskpool.createBundle( + applicationFilter, + bundleInitialFunding, + {'from': investor}) + + # create risks + projectId = s2b32('2022.kenya.wfp.ayii') + uaiId = [s2b32('1234'), s2b32('2345')] + cropId = s2b32('mixed') + + triggerFloat = 0.75 + exitFloat = 0.1 + tsiFloat = 0.9 + aphFloat = [2.0, 1.8] + + multiplier = product.getPercentageMultiplier() + trigger = multiplier * triggerFloat + exit_ = multiplier * exitFloat + tsi = multiplier * tsiFloat + aph = [multiplier * aphFloat[0], multiplier * aphFloat[1]] + + tx = [None, None] + tx[0] = product.createRisk(projectId, uaiId[0], cropId, trigger, exit_, tsi, aph[0], {'from': insurer}) + tx[1] = product.createRisk(projectId, uaiId[1], cropId, trigger, exit_, tsi, aph[1], {'from': insurer}) + + riskId1 = tx[0].events['LogAyiiRiskDataCreated']['riskId'] + riskId2 = tx[1].events['LogAyiiRiskDataCreated']['riskId'] + + # customer funding + customerFunding=1000 + erc20Token.transfer(customer, customerFunding, {'from': instanceOperator}) + erc20Token.approve(instance.getTreasury(), customerFunding, {'from': customer}) + + # policy creation + premium = [300, 400] + sumInsured = [2000, 3000] + + tx[0] = product.applyForPolicy(customer, premium[0], sumInsured[0], riskId1, {'from': insurer}) + tx[1] = product.applyForPolicy(customer2, premium[1], sumInsured[1], riskId2, {'from': insurer}) + + processId1 = tx[0].events['LogAyiiPolicyCreated']['policyId'] + processId2 = tx[1].events['LogAyiiPolicyCreated']['policyId'] + + return { + INSTANCE_OPERATOR: instanceOperator, + INSTANCE_WALLET: instanceWallet, + ORACLE_PROVIDER: oracleProvider, + NODE_OPERATOR: chainlinkNodeOperator, + RISKPOOL_KEEPER: riskpoolKeeper, + RISKPOOL_WALLET: riskpoolWallet, + INVESTOR: investor, + PRODUCT_OWNER: productOwner, + INSURER: insurer, + CUSTOMER1: customer, + CUSTOMER2: customer2, + ERC20_TOKEM: contract_from_address(TestCoin, erc20Token), + INSTANCE: instance, + INSTANCE_SERVICE: contract_from_address(InstanceService, instanceService), + INSTANCE_OPERATOR_SERVICE: contract_from_address(InstanceOperatorService, instanceOperatorService), + COMPONENT_OWNER_SERVICE: contract_from_address(ComponentOwnerService, componentOwnerService), + PRODUCT: contract_from_address(AyiiProduct, product), + ORACLE: contract_from_address(AyiiOracle, oracle), + RISKPOOL: contract_from_address(AyiiRiskpool, riskpool), + RISK_ID1: riskId1, + RISK_ID2: riskId2, + PROCESS_ID1: processId1, + PROCESS_ID2: processId2, + } + + +def from_component(componentAddress): + component = contract_from_address(interface.IComponent, componentAddress) + return from_registry(component.getRegistry()) + + +def from_registry(registryAddress): + instance = GifInstance(registryAddress=registryAddress) + instanceService = instance.getInstanceService() + + products = instanceService.products() + oracles = instanceService.oracles() + riskpools = instanceService.riskpools() + + product = None + oracle = None + riskpool = None + + if products >= 1: + if products > 1: + print('1 product expected, {} product available'.format(products)) + print('returning last product available') + + componentId = instanceService.getProductId(products-1) + component = instanceService.getComponent(componentId) + product = contract_from_address(AyiiProduct, component) + else: + print('1 product expected, no producta available') + print('no product returned (None)') + + if oracles >= 1: + if oracles > 1: + print('1 oracle expected, {} oracles available'.format(oracles)) + print('returning last oracle available') + + componentId = instanceService.getOracleId(oracles-1) + component = instanceService.getComponent(componentId) + oracle = contract_from_address(AyiiOracle, component) + else: + print('1 oracle expected, no oracles available') + print('no oracle returned (None)') + + if riskpools >= 1: + if riskpools > 1: + print('1 riskpool expected, {} riskpools available'.format(riskpools)) + print('returning last riskpool available') + + componentId = instanceService.getRiskpoolId(riskpools-1) + component = instanceService.getComponent(componentId) + riskpool = contract_from_address(AyiiRiskpool, component) + else: + print('1 riskpool expected, no riskpools available') + print('no riskpool returned (None)') + + return (instance, product, oracle, riskpool) diff --git a/scripts/instance.py b/scripts/instance.py index c096037..5084c91 100644 --- a/scripts/instance.py +++ b/scripts/instance.py @@ -271,23 +271,54 @@ def dump_sources(registryAddress=None): instance = GifInstance(registryAddress=registryAddress) contracts = [] - contracts.append(dump_single(Registry, instance)) - contracts.append(dump_single(RegistryController, instance)) - - contracts.append(dump_single(License, instance)) - contracts.append(dump_single(LicenseController, instance)) - contracts.append(dump_single(Policy, instance)) - contracts.append(dump_single(PolicyController, instance)) - contracts.append(dump_single(Query, instance)) - contracts.append(dump_single(QueryController, instance)) - contracts.append(dump_single(Pool, instance)) - contracts.append(dump_single(PoolController, instance)) - - contracts.append(dump_single(PolicyFlowDefault, instance)) - contracts.append(dump_single(ProductService, instance)) - contracts.append(dump_single(ComponentOwnerService, instance)) - contracts.append(dump_single(OracleService, instance)) - contracts.append(dump_single(InstanceOperatorService, instance)) + contracts.append(dump_single(CoreProxy, "Registry", instance)) + contracts.append(dump_single(RegistryController, "RegistryController", instance)) + + contracts.append(dump_single(BundleToken, "BundleToken", instance)) + contracts.append(dump_single(RiskpoolToken, "RiskpoolToken", instance)) + + contracts.append(dump_single(CoreProxy, "Access", instance)) + contracts.append(dump_single(AccessController, "AccessController", instance)) + + contracts.append(dump_single(CoreProxy, "Component", instance)) + contracts.append(dump_single(ComponentController, "ComponentController", instance)) + + contracts.append(dump_single(CoreProxy, "Query", instance)) + contracts.append(dump_single(QueryController, "QueryController", instance)) + + contracts.append(dump_single(CoreProxy, "License", instance)) + contracts.append(dump_single(LicenseController, "LicenseController", instance)) + + contracts.append(dump_single(CoreProxy, "Policy", instance)) + contracts.append(dump_single(PolicyController, "PolicyController", instance)) + + contracts.append(dump_single(CoreProxy, "Bundle", instance)) + contracts.append(dump_single(BundleController, "BundleController", instance)) + + contracts.append(dump_single(CoreProxy, "Pool", instance)) + contracts.append(dump_single(PoolController, "PoolController", instance)) + + contracts.append(dump_single(CoreProxy, "Treasury", instance)) + contracts.append(dump_single(TreasuryModule, "TreasuryController", instance)) + + contracts.append(dump_single(PolicyDefaultFlow, "PolicyDefaultFlow", instance)) + + contracts.append(dump_single(CoreProxy, "InstanceService", instance)) + contracts.append(dump_single(InstanceService, "InstanceServiceController", instance)) + + contracts.append(dump_single(CoreProxy, "ComponentOwnerService", instance)) + contracts.append(dump_single(ComponentOwnerService, "ComponentOwnerServiceController", instance)) + + contracts.append(dump_single(CoreProxy, "OracleService", instance)) + contracts.append(dump_single(OracleService, "OracleServiceController", instance)) + + contracts.append(dump_single(CoreProxy, "RiskpoolService", instance)) + contracts.append(dump_single(RiskpoolService, "RiskpoolServiceController", instance)) + + contracts.append(dump_single(ProductService, "ProductService", instance)) + + contracts.append(dump_single(CoreProxy, "InstanceOperatorService", instance)) + contracts.append(dump_single(InstanceOperatorService, "InstanceOperatorServiceController", instance)) with open(dump_sources_summary_file,'w') as f: f.write('\n'.join(contracts)) @@ -297,7 +328,7 @@ def dump_sources(registryAddress=None): print('\nfor contract json files see directory {}'.format(dump_sources_summary_dir)) -def dump_single(contract, instance=None) -> str: +def dump_single(contract, registryName, instance=None) -> str: info = contract.get_verification_info() netw = network.show_active() @@ -309,7 +340,7 @@ def dump_single(contract, instance=None) -> str: name = info['contract_name'] if instance: - nameB32 = s2b32(contract._name) + nameB32 = s2b32(registryName) address = instance.registry.getContract(nameB32) dump_sources_contract_file = './dump_sources/{}/{}.json'.format(netw, name) diff --git a/scripts/setup.py b/scripts/setup.py index ea2b989..419d185 100644 --- a/scripts/setup.py +++ b/scripts/setup.py @@ -1,5 +1,12 @@ +from brownie.network import accounts from brownie.network.account import Account + +from brownie import ( + TestCoin, +) + from scripts.instance import GifInstance +from scripts.ayii_product import GifAyiiProductComplete def fund_riskpool( instance: GifInstance, @@ -65,4 +72,4 @@ def apply_for_policy( {'from': customer}) # returns policy id - return tx.return_value \ No newline at end of file + return tx.return_value diff --git a/scripts/util.py b/scripts/util.py index 71c6f6c..9492b64 100644 --- a/scripts/util.py +++ b/scripts/util.py @@ -172,6 +172,9 @@ def deployGifServiceV2( return service def contractFromAddress(contractClass, contractAddress): + return contract_from_address(contractClass, contractAddress) + +def contract_from_address(contractClass, contractAddress): return Contract.from_abi(contractClass._name, contractAddress, contractClass.abi) # this will deploy the TestSimpleIncrementer smart contract, increment it and wait for the trx to finish.