diff --git a/two-party-pol-covenant/justfile b/two-party-pol-covenant/justfile index 967520c4..877524b4 100644 --- a/two-party-pol-covenant/justfile +++ b/two-party-pol-covenant/justfile @@ -43,4 +43,4 @@ simtest: optimize ictest: go clean -testcache - cd tests/interchaintest/ && go test -timeout 20m -v ./... \ No newline at end of file + cd tests/interchaintest/ && go test -timeout 30m -v ./... \ No newline at end of file diff --git a/two-party-pol-covenant/tests/interchaintest/two_party_pol_test.go b/two-party-pol-covenant/tests/interchaintest/two_party_pol_test.go index b5796471..d566da0e 100644 --- a/two-party-pol-covenant/tests/interchaintest/two_party_pol_test.go +++ b/two-party-pol-covenant/tests/interchaintest/two_party_pol_test.go @@ -734,294 +734,863 @@ func TestTwoPartyPol(t *testing.T) { println("neutronUser lp token bal: ", neutronUserLPTokenBal) }) - t.Run("instantiate covenant", func(t *testing.T) { - timeouts := Timeouts{ - IcaTimeout: "100", // sec - IbcTransferTimeout: "100", // sec - } + // t.Run("two party POL happy path", func(t *testing.T) { + + // tickClock := func() { + // println("\ntick") + // cmd := []string{"neutrond", "tx", "wasm", "execute", clockAddress, + // `{"tick":{}}`, + // "--gas-prices", "0.0untrn", + // "--gas-adjustment", `1.5`, + // "--output", "json", + // "--home", "/var/cosmos-chain/neutron-2", + // "--node", neutron.GetRPCAddress(), + // "--home", neutron.HomeDir(), + // "--chain-id", neutron.Config().ChainID, + // "--from", neutronUser.KeyName, + // "--gas", "1500000", + // "--keyring-backend", keyring.BackendTest, + // "-y", + // } + + // resp, _, err := cosmosNeutron.Exec(ctx, cmd, nil) + // require.NoError(t, err) + // println("tick response: ", string(resp), "\n") + // err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) + // require.NoError(t, err, "failed to wait for blocks") + // } + + // t.Run("instantiate covenant", func(t *testing.T) { + // timeouts := Timeouts{ + // IcaTimeout: "100", // sec + // IbcTransferTimeout: "100", // sec + // } + + // depositBlock := Block(500) + // lockupBlock := Block(500) + + // lockupConfig := Expiration{ + // AtHeight: &lockupBlock, + // } + // depositDeadline := Expiration{ + // AtHeight: &depositBlock, + // } + // presetIbcFee := PresetIbcFee{ + // AckFee: "10000", + // TimeoutFee: "10000", + // } + + // atomCoin := Coin{ + // Denom: cosmosAtom.Config().Denom, + // Amount: strconv.FormatUint(atomContributionAmount, 10), + // } + + // osmoCoin := Coin{ + // Denom: cosmosOsmosis.Config().Denom, + // Amount: strconv.FormatUint(osmoContributionAmount, 10), + // } + + // partyAConfig := CovenantPartyConfig{ + // ControllerAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), + // HostAddr: hubNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), + // Contribution: atomCoin, + // IbcDenom: neutronAtomIbcDenom, + // PartyToHostChainChannelId: testCtx.GaiaTransferChannelIds[cosmosNeutron.Config().Name], + // HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosAtom.Config().Name], + // PartyReceiverAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), + // PartyChainConnectionId: neutronAtomIBCConnId, + // IbcTransferTimeout: timeouts.IbcTransferTimeout, + // } + // partyBConfig := CovenantPartyConfig{ + // ControllerAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), + // HostAddr: osmoNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), + // Contribution: osmoCoin, + // IbcDenom: neutronOsmoIbcDenom, + // PartyToHostChainChannelId: testCtx.OsmoTransferChannelIds[cosmosNeutron.Config().Name], + // HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosOsmosis.Config().Name], + // PartyReceiverAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), + // PartyChainConnectionId: neutronOsmosisIBCConnId, + // IbcTransferTimeout: timeouts.IbcTransferTimeout, + // } + // codeIds := ContractCodeIds{ + // IbcForwarderCode: ibcForwarderCodeId, + // InterchainRouterCode: routerCodeId, + // ClockCode: clockCodeId, + // HolderCode: holderCodeId, + // LiquidPoolerCode: lperCodeId, + // } + + // ragequitTerms := RagequitTerms{ + // Penalty: "0.1", + // } + + // ragequitConfig := RagequitConfig{ + // Enabled: &ragequitTerms, + // } + + // poolAddress := stableswapAddress + // pairType := PairType{ + // Stable: struct{}{}, + // } + + // covenantMsg := CovenantInstantiateMsg{ + // Label: "two-party-pol-covenant", + // Timeouts: timeouts, + // PresetIbcFee: presetIbcFee, + // ContractCodeIds: codeIds, + // LockupConfig: lockupConfig, + // PartyAConfig: partyAConfig, + // PartyBConfig: partyBConfig, + // PoolAddress: poolAddress, + // RagequitConfig: &ragequitConfig, + // DepositDeadline: depositDeadline, + // PartyAShare: "50", + // PartyBShare: "50", + // ExpectedPoolRatio: "0.1", + // AcceptablePoolRatioDelta: "0.09", + // PairType: pairType, + // } + // str, err := json.Marshal(covenantMsg) + // require.NoError(t, err, "Failed to marshall CovenantInstantiateMsg") + // instantiateMsg := string(str) + + // println("instantiation message: ", instantiateMsg) + // cmd := []string{"neutrond", "tx", "wasm", "instantiate", covenantCodeIdStr, + // instantiateMsg, + // "--label", "two-party-pol-covenant", + // "--no-admin", + // "--from", neutronUser.KeyName, + // "--output", "json", + // "--home", neutron.HomeDir(), + // "--node", neutron.GetRPCAddress(), + // "--chain-id", neutron.Config().ChainID, + // "--gas", "90009000", + // "--keyring-backend", keyring.BackendTest, + // "-y", + // } + + // _, _, err = neutron.Exec(ctx, cmd, nil) + // require.NoError(t, err) + // require.NoError(t, testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis)) + + // queryCmd := []string{"neutrond", "query", "wasm", + // "list-contract-by-code", covenantCodeIdStr, + // "--output", "json", + // "--home", neutron.HomeDir(), + // "--node", neutron.GetRPCAddress(), + // "--chain-id", neutron.Config().ChainID, + // } + + // queryResp, _, err := neutron.Exec(ctx, queryCmd, nil) + // require.NoError(t, err, "failed to query") + + // type QueryContractResponse struct { + // Contracts []string `json:"contracts"` + // Pagination any `json:"pagination"` + // } + + // contactsRes := QueryContractResponse{} + // require.NoError(t, json.Unmarshal(queryResp, &contactsRes), "failed to unmarshal contract response") + + // covenantAddress = contactsRes.Contracts[len(contactsRes.Contracts)-1] + + // println("covenant address: ", covenantAddress) + // }) + + // t.Run("query covenant contracts", func(t *testing.T) { + // routerQueryPartyA := InterchainRouterQuery{ + // Party: Party{ + // Party: "party_a", + // }, + // } + // routerQueryPartyB := InterchainRouterQuery{ + // Party: Party{ + // Party: "party_b", + // }, + // } + // forwarderQueryPartyA := IbcForwarderQuery{ + // Party: Party{ + // Party: "party_a", + // }, + // } + // forwarderQueryPartyB := IbcForwarderQuery{ + // Party: Party{ + // Party: "party_b", + // }, + // } + + // var response CovenantAddressQueryResponse + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, ClockAddressQuery{}, &response) + // require.NoError(t, err, "failed to query instantiated clock address") + // clockAddress = response.Data + // println("clock addr: ", clockAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, HolderAddressQuery{}, &response) + // require.NoError(t, err, "failed to query instantiated holder address") + // holderAddress = response.Data + // println("holder addr: ", holderAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, LiquidPoolerQuery{}, &response) + // require.NoError(t, err, "failed to query instantiated liquid pooler address") + // liquidPoolerAddress = response.Data + // println("liquid pooler addr: ", liquidPoolerAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyA, &response) + // require.NoError(t, err, "failed to query instantiated party a router address") + // partyARouterAddress = response.Data + // println("partyARouterAddress: ", partyARouterAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyB, &response) + // require.NoError(t, err, "failed to query instantiated party b router address") + // partyBRouterAddress = response.Data + // println("partyBRouterAddress: ", partyBRouterAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyA, &response) + // require.NoError(t, err, "failed to query instantiated party a forwarder address") + // partyAIbcForwarderAddress = response.Data + // println("partyAIbcForwarderAddress: ", partyAIbcForwarderAddress) + + // err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyB, &response) + // require.NoError(t, err, "failed to query instantiated party b forwarder address") + // partyBIbcForwarderAddress = response.Data + // println("partyBIbcForwarderAddress: ", partyBIbcForwarderAddress) + // }) + + // t.Run("fund contracts with neutron", func(t *testing.T) { + // err := neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: partyAIbcForwarderAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to partyAIbcForwarder contract") + + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: partyBIbcForwarderAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to partyBIbcForwarder contract") + + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: clockAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to clock contract") + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: partyARouterAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to party a router") + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: partyBRouterAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to party b router") + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: holderAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to holder") + // err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + // Address: liquidPoolerAddress, + // Amount: 5000000001, + // Denom: nativeNtrnDenom, + // }) + // require.NoError(t, err, "failed to send funds from neutron user to holder") + + // err = testutil.WaitForBlocks(ctx, 2, atom, neutron, osmosis) + // require.NoError(t, err, "failed to wait for blocks") + + // bal, err := neutron.GetBalance(ctx, partyAIbcForwarderAddress, nativeNtrnDenom) + // require.NoError(t, err) + // require.Equal(t, int64(5000000001), bal) + // bal, err = neutron.GetBalance(ctx, partyBIbcForwarderAddress, nativeNtrnDenom) + // require.NoError(t, err) + // require.Equal(t, int64(5000000001), bal) + // bal, err = neutron.GetBalance(ctx, clockAddress, nativeNtrnDenom) + // require.NoError(t, err) + // require.Equal(t, int64(5000000001), bal) + // bal, err = neutron.GetBalance(ctx, partyARouterAddress, nativeNtrnDenom) + // require.NoError(t, err) + // require.Equal(t, int64(5000000001), bal) + // bal, err = neutron.GetBalance(ctx, partyBRouterAddress, nativeNtrnDenom) + // require.NoError(t, err) + // require.Equal(t, int64(5000000001), bal) + // }) + + // t.Run("tick until forwarders create ICA", func(t *testing.T) { + // require.NoError(t, testutil.WaitForBlocks(ctx, 15, atom, neutron, osmosis), "failed to wait for blocks") + // for { + // tickClock() + // var response CovenantAddressQueryResponse + // type ContractState struct{} + // type ContractStateQuery struct { + // ContractState ContractState `json:"contract_state"` + // } + // contractStateQuery := ContractStateQuery{ + // ContractState: ContractState{}, + // } + + // require.NoError(t, + // cosmosNeutron.QueryContract(ctx, partyAIbcForwarderAddress, contractStateQuery, &response), + // "failed to query forwarder A state") + // forwarderAState := response.Data + + // require.NoError(t, + // cosmosNeutron.QueryContract(ctx, partyBIbcForwarderAddress, contractStateQuery, &response), + // "failed to query forwarder B state") + // forwarderBState := response.Data + + // if forwarderAState == forwarderBState && forwarderBState == "ica_created" { + // require.NoError(t, testutil.WaitForBlocks(ctx, 15, atom, neutron, osmosis), "failed to wait for blocks") + + // var depositAddressResponse CovenantAddressQueryResponse + + // type DepositAddress struct{} + // type DepositAddressQuery struct { + // DepositAddress DepositAddress `json:"deposit_address"` + // } + // depositAddressQuery := DepositAddressQuery{ + // DepositAddress: DepositAddress{}, + // } + + // err := cosmosNeutron.QueryContract(ctx, partyAIbcForwarderAddress, depositAddressQuery, &depositAddressResponse) + // require.NoError(t, err, "failed to query party a forwarder deposit address") + // partyADepositAddress = depositAddressResponse.Data + + // err = cosmosNeutron.QueryContract(ctx, partyBIbcForwarderAddress, depositAddressQuery, &depositAddressResponse) + // require.NoError(t, err, "failed to query party b forwarder deposit address") + // partyBDepositAddress = depositAddressResponse.Data + // println("both parties icas created: ", partyADepositAddress, " , ", partyBDepositAddress) + // break + // } + // } + // }) + + // t.Run("fund the forwarders with sufficient funds", func(t *testing.T) { + + // err := cosmosOsmosis.SendFunds(ctx, osmoUser.KeyName, ibc.WalletAmount{ + // Address: partyBDepositAddress, + // Denom: nativeOsmoDenom, + // Amount: int64(osmoContributionAmount + 1), + // }) + // require.NoError(t, err, "failed to fund osmo forwarder") + // err = cosmosAtom.SendFunds(ctx, gaiaUser.KeyName, ibc.WalletAmount{ + // Address: partyADepositAddress, + // Denom: nativeAtomDenom, + // Amount: int64(atomContributionAmount + 1), + // }) + // require.NoError(t, err, "failed to fund gaia forwarder") + + // err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) + // require.NoError(t, err, "failed to wait for blocks") + + // bal, err := cosmosAtom.GetBalance(ctx, partyADepositAddress, nativeAtomDenom) + // require.NoError(t, err, "failed to query bal") + // require.Equal(t, int64(atomContributionAmount+1), bal) + // bal, err = cosmosOsmosis.GetBalance(ctx, partyBDepositAddress, nativeOsmoDenom) + // require.NoError(t, err, "failed to query bal") + // require.Equal(t, int64(osmoContributionAmount+1), bal) + // }) + + // t.Run("tick until forwarders forward the funds to holder", func(t *testing.T) { + // for { + // holderOsmoBal, err := cosmosNeutron.GetBalance(ctx, holderAddress, neutronOsmoIbcDenom) + // require.NoError(t, err, "failed to query holder osmo bal") + // holderAtomBal, err := cosmosNeutron.GetBalance(ctx, holderAddress, neutronAtomIbcDenom) + // require.NoError(t, err, "failed to query holder atom bal") + // // liquidPoolerOsmoBal, err := cosmosNeutron.GetBalance(ctx, liquidPoolerAddress, neutronOsmoIbcDenom) + // // require.NoError(t, err, "failed to query liquidPooler osmo bal") + // // liquidPoolerAtomBal, err := cosmosNeutron.GetBalance(ctx, liquidPoolerAddress, neutronAtomIbcDenom) + // // require.NoError(t, err, "failed to query liquidPooler atom bal") + // println("holder atom bal: ", holderAtomBal) + // println("holder osmo bal: ", holderOsmoBal) + + // var response CovenantAddressQueryResponse + // type ContractState struct{} + // type ContractStateQuery struct { + // ContractState ContractState `json:"contract_state"` + // } + // contractStateQuery := ContractStateQuery{ + // ContractState: ContractState{}, + // } + + // require.NoError(t, + // cosmosNeutron.QueryContract(ctx, holderAddress, contractStateQuery, &response), + // "failed to query holder state") + // holderState := response.Data + // println("holder state: ", holderState) + + // if holderAtomBal == int64(atomContributionAmount) && holderOsmoBal == int64(osmoContributionAmount) || holderState == "active" { + // println("\nholder/liquidpooler received atom & osmo\n") + // break + // } else { + // tickClock() + // } + // } + // }) + + // t.Run("tick until holder sends the funds to LPer", func(t *testing.T) { + // for { + // liquidPoolerOsmoBal, err := cosmosNeutron.GetBalance(ctx, liquidPoolerAddress, neutronOsmoIbcDenom) + // require.NoError(t, err, "failed to query liquidPooler osmo bal") + // liquidPoolerAtomBal, err := cosmosNeutron.GetBalance(ctx, liquidPoolerAddress, neutronAtomIbcDenom) + // require.NoError(t, err, "failed to query liquidPooler atom bal") + // holderLpTokenBal := queryLpTokenBalance(liquidityTokenAddress, holderAddress) + + // println("liquid pooler atom bal: ", liquidPoolerAtomBal) + // println("liquid pooler osmo bal: ", liquidPoolerOsmoBal) + // println("holder lp token balance: ", holderLpTokenBal) + + // if liquidPoolerOsmoBal == int64(osmoContributionAmount) && liquidPoolerAtomBal == int64(atomContributionAmount) { + // break + // } else { + // tickClock() + // } + // } + // }) + + // t.Run("tick until holder receives LP tokens", func(t *testing.T) { + // for { + // holderLpTokenBal := queryLpTokenBalance(liquidityTokenAddress, holderAddress) + // println("holder lp token balance: ", holderLpTokenBal) + // holderLpBal, err := strconv.ParseUint(holderLpTokenBal, 10, 64) + // if err != nil { + // panic(err) + // } + + // if holderLpBal == 0 { + // tickClock() + // } else { + // break + // } + // } + // }) + + // t.Run("tick until holder expires", func(t *testing.T) { + // for { + // neutronHeight, err := cosmosNeutron.Height(ctx) + // require.NoError(t, err) + + // if neutronHeight >= 515 { + // println("neutron height: ", neutronHeight) + // break + // } else { + // tickClock() + // } + // } + // }) + + // t.Run("party A claims and router receives the funds", func(t *testing.T) { + + // cmd := []string{"neutrond", "tx", "wasm", "execute", holderAddress, + // `{"claim":{}}`, + // "--from", hubNeutronAccount.GetKeyName(), + // "--gas-prices", "0.0untrn", + // "--gas-adjustment", `1.5`, + // "--output", "json", + // "--node", neutron.GetRPCAddress(), + // "--home", neutron.HomeDir(), + // "--chain-id", neutron.Config().ChainID, + // "--gas", "42069420", + // "--keyring-backend", keyring.BackendTest, + // "-y", + // } + // println("hub claim msg: ", strings.Join(cmd, " ")) + + // for { + // routerAtomBalA, err := cosmosNeutron.GetBalance(ctx, partyARouterAddress, neutronAtomIbcDenom) + // require.NoError(t, err) + + // routerOsmoBalA, err := cosmosNeutron.GetBalance(ctx, partyARouterAddress, neutronOsmoIbcDenom) + // require.NoError(t, err) + + // println("routerAtomBalA: ", routerAtomBalA) + // println("routerOsmoBalA: ", routerOsmoBalA) + + // if routerAtomBalA != 0 && routerOsmoBalA != 0 { + // break + // } else { + // tickClock() + // _, _, err = cosmosNeutron.Exec(ctx, cmd, nil) + // require.NoError(t, err, "party A claim failed") + + // err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) + // require.NoError(t, err, "failed to wait for blocks") + // } + // } + // }) + + // t.Run("party B claims and router receives the funds", func(t *testing.T) { + + // cmd := []string{"neutrond", "tx", "wasm", "execute", holderAddress, + // `{"claim":{}}`, + // "--from", osmoNeutronAccount.GetKeyName(), + // "--gas-prices", "0.0untrn", + // "--gas-adjustment", `1.8`, + // "--output", "json", + // "--node", neutron.GetRPCAddress(), + // "--home", neutron.HomeDir(), + // "--chain-id", neutron.Config().ChainID, + // "--gas", "42069420", + // "--keyring-backend", keyring.BackendTest, + // "-y", + // } + + // println("osmo claim msg: ", strings.Join(cmd, " ")) + // _, _, err := cosmosNeutron.Exec(ctx, cmd, nil) + // require.NoError(t, err, "party B claim failed") + + // err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) + // require.NoError(t, err, "failed to wait for blocks") + + // for { + // routerAtomBalB, err := cosmosNeutron.GetBalance(ctx, partyBRouterAddress, neutronAtomIbcDenom) + // require.NoError(t, err) + + // routerOsmoBalB, err := cosmosNeutron.GetBalance(ctx, partyBRouterAddress, neutronOsmoIbcDenom) + // require.NoError(t, err) + + // println("routerAtomBalB: ", routerAtomBalB) + // println("routerOsmoBalB: ", routerOsmoBalB) + + // if routerAtomBalB != 0 || routerOsmoBalB != 0 { + // break + // } else { + // tickClock() + // } + // } + // }) + + // t.Run("tick routers until both parties receive their funds", func(t *testing.T) { + // for { + // osmoBalPartyA, err := cosmosAtom.GetBalance( + // ctx, gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), gaiaNeutronOsmoIbcDenom, + // ) + // require.NoError(t, err) + + // osmoBalPartyB, err := cosmosOsmosis.GetBalance( + // ctx, osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), cosmosOsmosis.Config().Denom, + // ) + // require.NoError(t, err) + + // atomBalPartyA, err := cosmosAtom.GetBalance( + // ctx, gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), cosmosAtom.Config().Denom, + // ) + // require.NoError(t, err) + + // atomBalPartyB, err := cosmosOsmosis.GetBalance( + // ctx, osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), osmoNeutronAtomIbcDenom, + // ) + // require.NoError(t, err) + + // println("party A osmo bal: ", osmoBalPartyA) + // println("party A atom bal: ", atomBalPartyA) + // println("party B osmo bal: ", osmoBalPartyB) + // println("party B atom bal: ", atomBalPartyB) + + // if osmoBalPartyA != 0 && atomBalPartyA != 0 && osmoBalPartyB != 0 && atomBalPartyB != 0 { + // break + // } + + // tickClock() + // } + // }) + // }) + + t.Run("two party POL ragequit path", func(t *testing.T) { - depositBlock := Block(500) - lockupBlock := Block(500) + tickClock := func() { + println("\ntick") + cmd := []string{"neutrond", "tx", "wasm", "execute", clockAddress, + `{"tick":{}}`, + "--gas-prices", "0.0untrn", + "--gas-adjustment", `1.5`, + "--output", "json", + "--home", "/var/cosmos-chain/neutron-2", + "--node", neutron.GetRPCAddress(), + "--home", neutron.HomeDir(), + "--chain-id", neutron.Config().ChainID, + "--from", neutronUser.KeyName, + "--gas", "1500000", + "--keyring-backend", keyring.BackendTest, + "-y", + } - lockupConfig := Expiration{ - AtHeight: &lockupBlock, - } - depositDeadline := Expiration{ - AtHeight: &depositBlock, - } - presetIbcFee := PresetIbcFee{ - AckFee: "10000", - TimeoutFee: "10000", + resp, _, err := cosmosNeutron.Exec(ctx, cmd, nil) + require.NoError(t, err) + println("tick response: ", string(resp), "\n") + err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) + require.NoError(t, err, "failed to wait for blocks") } - atomCoin := Coin{ - Denom: cosmosAtom.Config().Denom, - Amount: strconv.FormatUint(atomContributionAmount, 10), - } + t.Run("instantiate covenant", func(t *testing.T) { + timeouts := Timeouts{ + IcaTimeout: "100", // sec + IbcTransferTimeout: "100", // sec + } - osmoCoin := Coin{ - Denom: cosmosOsmosis.Config().Denom, - Amount: strconv.FormatUint(osmoContributionAmount, 10), - } + depositBlock := Block(500) + lockupBlock := Block(1000) - partyAConfig := CovenantPartyConfig{ - ControllerAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), - HostAddr: hubNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), - Contribution: atomCoin, - IbcDenom: neutronAtomIbcDenom, - PartyToHostChainChannelId: testCtx.GaiaTransferChannelIds[cosmosNeutron.Config().Name], - HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosAtom.Config().Name], - PartyReceiverAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), - PartyChainConnectionId: neutronAtomIBCConnId, - IbcTransferTimeout: timeouts.IbcTransferTimeout, - } - partyBConfig := CovenantPartyConfig{ - ControllerAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), - HostAddr: osmoNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), - Contribution: osmoCoin, - IbcDenom: neutronOsmoIbcDenom, - PartyToHostChainChannelId: testCtx.OsmoTransferChannelIds[cosmosNeutron.Config().Name], - HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosOsmosis.Config().Name], - PartyReceiverAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), - PartyChainConnectionId: neutronOsmosisIBCConnId, - IbcTransferTimeout: timeouts.IbcTransferTimeout, - } - codeIds := ContractCodeIds{ - IbcForwarderCode: ibcForwarderCodeId, - InterchainRouterCode: routerCodeId, - ClockCode: clockCodeId, - HolderCode: holderCodeId, - LiquidPoolerCode: lperCodeId, - } + lockupConfig := Expiration{ + AtHeight: &lockupBlock, + } + depositDeadline := Expiration{ + AtHeight: &depositBlock, + } + presetIbcFee := PresetIbcFee{ + AckFee: "10000", + TimeoutFee: "10000", + } - ragequitTerms := RagequitTerms{ - Penalty: "0.1", - } + atomCoin := Coin{ + Denom: cosmosAtom.Config().Denom, + Amount: strconv.FormatUint(atomContributionAmount, 10), + } - ragequitConfig := RagequitConfig{ - Enabled: &ragequitTerms, - } + osmoCoin := Coin{ + Denom: cosmosOsmosis.Config().Denom, + Amount: strconv.FormatUint(osmoContributionAmount, 10), + } - poolAddress := stableswapAddress - pairType := PairType{ - Stable: struct{}{}, - } + partyAConfig := CovenantPartyConfig{ + ControllerAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), + HostAddr: hubNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), + Contribution: atomCoin, + IbcDenom: neutronAtomIbcDenom, + PartyToHostChainChannelId: testCtx.GaiaTransferChannelIds[cosmosNeutron.Config().Name], + HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosAtom.Config().Name], + PartyReceiverAddr: gaiaUser.Bech32Address(cosmosAtom.Config().Bech32Prefix), + PartyChainConnectionId: neutronAtomIBCConnId, + IbcTransferTimeout: timeouts.IbcTransferTimeout, + } + partyBConfig := CovenantPartyConfig{ + ControllerAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), + HostAddr: osmoNeutronAccount.Bech32Address(cosmosNeutron.Config().Bech32Prefix), + Contribution: osmoCoin, + IbcDenom: neutronOsmoIbcDenom, + PartyToHostChainChannelId: testCtx.OsmoTransferChannelIds[cosmosNeutron.Config().Name], + HostToPartyChainChannelId: testCtx.NeutronTransferChannelIds[cosmosOsmosis.Config().Name], + PartyReceiverAddr: osmoUser.Bech32Address(cosmosOsmosis.Config().Bech32Prefix), + PartyChainConnectionId: neutronOsmosisIBCConnId, + IbcTransferTimeout: timeouts.IbcTransferTimeout, + } + codeIds := ContractCodeIds{ + IbcForwarderCode: ibcForwarderCodeId, + InterchainRouterCode: routerCodeId, + ClockCode: clockCodeId, + HolderCode: holderCodeId, + LiquidPoolerCode: lperCodeId, + } - covenantMsg := CovenantInstantiateMsg{ - Label: "two-party-pol-covenant", - Timeouts: timeouts, - PresetIbcFee: presetIbcFee, - ContractCodeIds: codeIds, - LockupConfig: lockupConfig, - PartyAConfig: partyAConfig, - PartyBConfig: partyBConfig, - PoolAddress: poolAddress, - RagequitConfig: &ragequitConfig, - DepositDeadline: depositDeadline, - PartyAShare: "50", - PartyBShare: "50", - ExpectedPoolRatio: "0.1", - AcceptablePoolRatioDelta: "0.09", - PairType: pairType, - } - str, err := json.Marshal(covenantMsg) - require.NoError(t, err, "Failed to marshall CovenantInstantiateMsg") - instantiateMsg := string(str) - - println("instantiation message: ", instantiateMsg) - cmd := []string{"neutrond", "tx", "wasm", "instantiate", covenantCodeIdStr, - instantiateMsg, - "--label", "two-party-pol-covenant", - "--no-admin", - "--from", neutronUser.KeyName, - "--output", "json", - "--home", neutron.HomeDir(), - "--node", neutron.GetRPCAddress(), - "--chain-id", neutron.Config().ChainID, - "--gas", "90009000", - "--keyring-backend", keyring.BackendTest, - "-y", - } + ragequitTerms := RagequitTerms{ + Penalty: "0.1", + } - _, _, err = neutron.Exec(ctx, cmd, nil) - require.NoError(t, err) - require.NoError(t, testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis)) + ragequitConfig := RagequitConfig{ + Enabled: &ragequitTerms, + } - queryCmd := []string{"neutrond", "query", "wasm", - "list-contract-by-code", covenantCodeIdStr, - "--output", "json", - "--home", neutron.HomeDir(), - "--node", neutron.GetRPCAddress(), - "--chain-id", neutron.Config().ChainID, - } + poolAddress := stableswapAddress + pairType := PairType{ + Stable: struct{}{}, + } - queryResp, _, err := neutron.Exec(ctx, queryCmd, nil) - require.NoError(t, err, "failed to query") + covenantMsg := CovenantInstantiateMsg{ + Label: "two-party-pol-covenant", + Timeouts: timeouts, + PresetIbcFee: presetIbcFee, + ContractCodeIds: codeIds, + LockupConfig: lockupConfig, + PartyAConfig: partyAConfig, + PartyBConfig: partyBConfig, + PoolAddress: poolAddress, + RagequitConfig: &ragequitConfig, + DepositDeadline: depositDeadline, + PartyAShare: "50", + PartyBShare: "50", + ExpectedPoolRatio: "0.1", + AcceptablePoolRatioDelta: "0.09", + PairType: pairType, + } + str, err := json.Marshal(covenantMsg) + require.NoError(t, err, "Failed to marshall CovenantInstantiateMsg") + instantiateMsg := string(str) + + println("instantiation message: ", instantiateMsg) + cmd := []string{"neutrond", "tx", "wasm", "instantiate", covenantCodeIdStr, + instantiateMsg, + "--label", "two-party-pol-covenant", + "--no-admin", + "--from", neutronUser.KeyName, + "--output", "json", + "--home", neutron.HomeDir(), + "--node", neutron.GetRPCAddress(), + "--chain-id", neutron.Config().ChainID, + "--gas", "90009000", + "--keyring-backend", keyring.BackendTest, + "-y", + } - type QueryContractResponse struct { - Contracts []string `json:"contracts"` - Pagination any `json:"pagination"` - } + _, _, err = neutron.Exec(ctx, cmd, nil) + require.NoError(t, err) + require.NoError(t, testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis)) - contactsRes := QueryContractResponse{} - require.NoError(t, json.Unmarshal(queryResp, &contactsRes), "failed to unmarshal contract response") + queryCmd := []string{"neutrond", "query", "wasm", + "list-contract-by-code", covenantCodeIdStr, + "--output", "json", + "--home", neutron.HomeDir(), + "--node", neutron.GetRPCAddress(), + "--chain-id", neutron.Config().ChainID, + } - covenantAddress = contactsRes.Contracts[len(contactsRes.Contracts)-1] + queryResp, _, err := neutron.Exec(ctx, queryCmd, nil) + require.NoError(t, err, "failed to query") - println("covenant address: ", covenantAddress) - }) + type QueryContractResponse struct { + Contracts []string `json:"contracts"` + Pagination any `json:"pagination"` + } - t.Run("query covenant contracts", func(t *testing.T) { - routerQueryPartyA := InterchainRouterQuery{ - Party: Party{ - Party: "party_a", - }, - } - routerQueryPartyB := InterchainRouterQuery{ - Party: Party{ - Party: "party_b", - }, - } - forwarderQueryPartyA := IbcForwarderQuery{ - Party: Party{ - Party: "party_a", - }, - } - forwarderQueryPartyB := IbcForwarderQuery{ - Party: Party{ - Party: "party_b", - }, - } + contactsRes := QueryContractResponse{} + require.NoError(t, json.Unmarshal(queryResp, &contactsRes), "failed to unmarshal contract response") - var response CovenantAddressQueryResponse - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, ClockAddressQuery{}, &response) - require.NoError(t, err, "failed to query instantiated clock address") - clockAddress = response.Data - println("clock addr: ", clockAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, HolderAddressQuery{}, &response) - require.NoError(t, err, "failed to query instantiated holder address") - holderAddress = response.Data - println("holder addr: ", holderAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, LiquidPoolerQuery{}, &response) - require.NoError(t, err, "failed to query instantiated liquid pooler address") - liquidPoolerAddress = response.Data - println("liquid pooler addr: ", liquidPoolerAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyA, &response) - require.NoError(t, err, "failed to query instantiated party a router address") - partyARouterAddress = response.Data - println("partyARouterAddress: ", partyARouterAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyB, &response) - require.NoError(t, err, "failed to query instantiated party b router address") - partyBRouterAddress = response.Data - println("partyBRouterAddress: ", partyBRouterAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyA, &response) - require.NoError(t, err, "failed to query instantiated party a forwarder address") - partyAIbcForwarderAddress = response.Data - println("partyAIbcForwarderAddress: ", partyAIbcForwarderAddress) - - err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyB, &response) - require.NoError(t, err, "failed to query instantiated party b forwarder address") - partyBIbcForwarderAddress = response.Data - println("partyBIbcForwarderAddress: ", partyBIbcForwarderAddress) - }) + covenantAddress = contactsRes.Contracts[len(contactsRes.Contracts)-1] - t.Run("fund contracts with neutron", func(t *testing.T) { - err := neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: partyAIbcForwarderAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, + println("covenant address: ", covenantAddress) }) - require.NoError(t, err, "failed to send funds from neutron user to partyAIbcForwarder contract") - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: partyBIbcForwarderAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, - }) - require.NoError(t, err, "failed to send funds from neutron user to partyBIbcForwarder contract") + t.Run("query covenant contracts", func(t *testing.T) { + routerQueryPartyA := InterchainRouterQuery{ + Party: Party{ + Party: "party_a", + }, + } + routerQueryPartyB := InterchainRouterQuery{ + Party: Party{ + Party: "party_b", + }, + } + forwarderQueryPartyA := IbcForwarderQuery{ + Party: Party{ + Party: "party_a", + }, + } + forwarderQueryPartyB := IbcForwarderQuery{ + Party: Party{ + Party: "party_b", + }, + } - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: clockAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, - }) - require.NoError(t, err, "failed to send funds from neutron user to clock contract") - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: partyARouterAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, - }) - require.NoError(t, err, "failed to send funds from neutron user to party a router") - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: partyBRouterAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, - }) - require.NoError(t, err, "failed to send funds from neutron user to party b router") - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: holderAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, + var response CovenantAddressQueryResponse + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, ClockAddressQuery{}, &response) + require.NoError(t, err, "failed to query instantiated clock address") + clockAddress = response.Data + println("clock addr: ", clockAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, HolderAddressQuery{}, &response) + require.NoError(t, err, "failed to query instantiated holder address") + holderAddress = response.Data + println("holder addr: ", holderAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, LiquidPoolerQuery{}, &response) + require.NoError(t, err, "failed to query instantiated liquid pooler address") + liquidPoolerAddress = response.Data + println("liquid pooler addr: ", liquidPoolerAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyA, &response) + require.NoError(t, err, "failed to query instantiated party a router address") + partyARouterAddress = response.Data + println("partyARouterAddress: ", partyARouterAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, routerQueryPartyB, &response) + require.NoError(t, err, "failed to query instantiated party b router address") + partyBRouterAddress = response.Data + println("partyBRouterAddress: ", partyBRouterAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyA, &response) + require.NoError(t, err, "failed to query instantiated party a forwarder address") + partyAIbcForwarderAddress = response.Data + println("partyAIbcForwarderAddress: ", partyAIbcForwarderAddress) + + err = cosmosNeutron.QueryContract(ctx, covenantAddress, forwarderQueryPartyB, &response) + require.NoError(t, err, "failed to query instantiated party b forwarder address") + partyBIbcForwarderAddress = response.Data + println("partyBIbcForwarderAddress: ", partyBIbcForwarderAddress) }) - require.NoError(t, err, "failed to send funds from neutron user to holder") - err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ - Address: liquidPoolerAddress, - Amount: 5000000001, - Denom: nativeNtrnDenom, - }) - require.NoError(t, err, "failed to send funds from neutron user to holder") - err = testutil.WaitForBlocks(ctx, 2, atom, neutron, osmosis) - require.NoError(t, err, "failed to wait for blocks") + t.Run("fund contracts with neutron", func(t *testing.T) { + err := neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: partyAIbcForwarderAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to partyAIbcForwarder contract") - bal, err := neutron.GetBalance(ctx, partyAIbcForwarderAddress, nativeNtrnDenom) - require.NoError(t, err) - require.Equal(t, int64(5000000001), bal) - bal, err = neutron.GetBalance(ctx, partyBIbcForwarderAddress, nativeNtrnDenom) - require.NoError(t, err) - require.Equal(t, int64(5000000001), bal) - bal, err = neutron.GetBalance(ctx, clockAddress, nativeNtrnDenom) - require.NoError(t, err) - require.Equal(t, int64(5000000001), bal) - bal, err = neutron.GetBalance(ctx, partyARouterAddress, nativeNtrnDenom) - require.NoError(t, err) - require.Equal(t, int64(5000000001), bal) - bal, err = neutron.GetBalance(ctx, partyBRouterAddress, nativeNtrnDenom) - require.NoError(t, err) - require.Equal(t, int64(5000000001), bal) - }) + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: partyBIbcForwarderAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to partyBIbcForwarder contract") - t.Run("two party POL", func(t *testing.T) { + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: clockAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to clock contract") + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: partyARouterAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to party a router") + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: partyBRouterAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to party b router") + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: holderAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to holder") + err = neutron.SendFunds(ctx, neutronUser.KeyName, ibc.WalletAmount{ + Address: liquidPoolerAddress, + Amount: 5000000001, + Denom: nativeNtrnDenom, + }) + require.NoError(t, err, "failed to send funds from neutron user to holder") - tickClock := func() { - println("\ntick") - cmd := []string{"neutrond", "tx", "wasm", "execute", clockAddress, - `{"tick":{}}`, - "--gas-prices", "0.0untrn", - "--gas-adjustment", `1.5`, - "--output", "json", - "--home", "/var/cosmos-chain/neutron-2", - "--node", neutron.GetRPCAddress(), - "--home", neutron.HomeDir(), - "--chain-id", neutron.Config().ChainID, - "--from", neutronUser.KeyName, - "--gas", "1500000", - "--keyring-backend", keyring.BackendTest, - "-y", - } + err = testutil.WaitForBlocks(ctx, 2, atom, neutron, osmosis) + require.NoError(t, err, "failed to wait for blocks") - resp, _, err := cosmosNeutron.Exec(ctx, cmd, nil) + bal, err := neutron.GetBalance(ctx, partyAIbcForwarderAddress, nativeNtrnDenom) require.NoError(t, err) - println("tick response: ", string(resp), "\n") - err = testutil.WaitForBlocks(ctx, 5, atom, neutron, osmosis) - require.NoError(t, err, "failed to wait for blocks") - } + require.Equal(t, int64(5000000001), bal) + bal, err = neutron.GetBalance(ctx, partyBIbcForwarderAddress, nativeNtrnDenom) + require.NoError(t, err) + require.Equal(t, int64(5000000001), bal) + bal, err = neutron.GetBalance(ctx, clockAddress, nativeNtrnDenom) + require.NoError(t, err) + require.Equal(t, int64(5000000001), bal) + bal, err = neutron.GetBalance(ctx, partyARouterAddress, nativeNtrnDenom) + require.NoError(t, err) + require.Equal(t, int64(5000000001), bal) + bal, err = neutron.GetBalance(ctx, partyBRouterAddress, nativeNtrnDenom) + require.NoError(t, err) + require.Equal(t, int64(5000000001), bal) + }) t.Run("tick until forwarders create ICA", func(t *testing.T) { require.NoError(t, testutil.WaitForBlocks(ctx, 15, atom, neutron, osmosis), "failed to wait for blocks") @@ -1172,12 +1741,12 @@ func TestTwoPartyPol(t *testing.T) { } }) - t.Run("tick until holder expires", func(t *testing.T) { + t.Run("tick a bit", func(t *testing.T) { for { neutronHeight, err := cosmosNeutron.Height(ctx) require.NoError(t, err) - if neutronHeight >= 515 { + if neutronHeight >= 500 { println("neutron height: ", neutronHeight) break } else { @@ -1186,10 +1755,10 @@ func TestTwoPartyPol(t *testing.T) { } }) - t.Run("party A claims and router receives the funds", func(t *testing.T) { + t.Run("party A ragequits", func(t *testing.T) { cmd := []string{"neutrond", "tx", "wasm", "execute", holderAddress, - `{"claim":{}}`, + `{"ragequit":{}}`, "--from", hubNeutronAccount.GetKeyName(), "--gas-prices", "0.0untrn", "--gas-adjustment", `1.5`, @@ -1201,7 +1770,7 @@ func TestTwoPartyPol(t *testing.T) { "--keyring-backend", keyring.BackendTest, "-y", } - println("hub claim msg: ", strings.Join(cmd, " ")) + println("hub ragequit msg: ", strings.Join(cmd, " ")) for { routerAtomBalA, err := cosmosNeutron.GetBalance(ctx, partyARouterAddress, neutronAtomIbcDenom)