2023-04-03 16:58:45 +00:00
|
|
|
package e2e_test
|
|
|
|
|
|
|
|
import (
|
2023-04-06 17:51:13 +00:00
|
|
|
"context"
|
|
|
|
"math/big"
|
2023-04-03 16:58:45 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
sdkerrors "cosmossdk.io/errors"
|
2023-04-03 16:58:45 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2023-04-06 17:51:13 +00:00
|
|
|
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
cdptypes "github.com/0glabs/0g-chain/x/cdp/types"
|
|
|
|
evmutiltypes "github.com/0glabs/0g-chain/x/evmutil/types"
|
2023-04-03 16:58:45 +00:00
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/tests/e2e/contracts/greeter"
|
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-04-03 16:58:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestEthCallToGreeterContract() {
|
|
|
|
// this test manipulates state of the Greeter contract which means other tests shouldn't use it.
|
|
|
|
|
|
|
|
// setup funded account to interact with contract
|
2023-06-26 22:03:51 +00:00
|
|
|
user := suite.Kava.NewFundedAccount("greeter-contract-user", sdk.NewCoins(ukava(1e6)))
|
2023-04-03 16:58:45 +00:00
|
|
|
|
|
|
|
greeterAddr := suite.Kava.ContractAddrs["greeter"]
|
|
|
|
contract, err := greeter.NewGreeter(greeterAddr, suite.Kava.EvmClient)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
beforeGreeting, err := contract.Greet(nil)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
updatedGreeting := "look at me, using the evm"
|
|
|
|
tx, err := contract.SetGreeting(user.EvmAuth, updatedGreeting)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
_, err = util.WaitForEvmTxReceipt(suite.Kava.EvmClient, tx.Hash(), 10*time.Second)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
afterGreeting, err := contract.Greet(nil)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.Equal("what's up!", beforeGreeting)
|
|
|
|
suite.Equal(updatedGreeting, afterGreeting)
|
|
|
|
}
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestEthCallToErc20() {
|
|
|
|
randoReceiver := util.SdkToEvmAddress(app.RandomAddress())
|
2023-06-30 05:30:02 +00:00
|
|
|
amount := big.NewInt(1)
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
// make unauthenticated eth_call query to check balance
|
2023-06-26 22:03:51 +00:00
|
|
|
beforeBalance := suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, randoReceiver)
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
// make authenticate eth_call to transfer tokens
|
|
|
|
res := suite.FundKavaErc20Balance(randoReceiver, amount)
|
|
|
|
suite.NoError(res.Err)
|
|
|
|
|
|
|
|
// make another unauthenticated eth_call query to check new balance
|
2023-06-26 22:03:51 +00:00
|
|
|
afterBalance := suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, randoReceiver)
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
suite.BigIntsEqual(big.NewInt(0), beforeBalance, "expected before balance to be zero")
|
|
|
|
suite.BigIntsEqual(amount, afterBalance, "unexpected post-transfer balance")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
|
|
|
|
// create new funded account
|
2023-06-26 22:03:51 +00:00
|
|
|
sender := suite.Kava.NewFundedAccount("eip712-msgSend", sdk.NewCoins(ukava(2e4)))
|
2023-04-06 17:51:13 +00:00
|
|
|
receiver := app.RandomAddress()
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// setup message for sending some kava to random receiver
|
2023-04-06 17:51:13 +00:00
|
|
|
msgs := []sdk.Msg{
|
2023-06-26 22:03:51 +00:00
|
|
|
banktypes.NewMsgSend(sender.SdkAddress, receiver, sdk.NewCoins(ukava(1e3))),
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create tx
|
|
|
|
tx := suite.NewEip712TxBuilder(
|
|
|
|
sender,
|
|
|
|
suite.Kava,
|
|
|
|
1e6,
|
|
|
|
sdk.NewCoins(ukava(1e4)),
|
|
|
|
msgs,
|
|
|
|
"this is a memo",
|
|
|
|
).GetTx()
|
|
|
|
|
|
|
|
txBytes, err := suite.Kava.EncodingConfig.TxConfig.TxEncoder()(tx)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// broadcast tx
|
2023-12-13 17:17:37 +00:00
|
|
|
res, err := suite.Kava.Grpc.Query.Tx.BroadcastTx(context.Background(), &txtypes.BroadcastTxRequest{
|
2023-04-06 17:51:13 +00:00
|
|
|
TxBytes: txBytes,
|
|
|
|
Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
|
|
|
|
})
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(sdkerrors.SuccessABCICode, res.TxResponse.Code)
|
|
|
|
|
2023-12-13 17:17:37 +00:00
|
|
|
_, err = util.WaitForSdkTxCommit(suite.Kava.Grpc.Query.Tx, res.TxResponse.TxHash, 6*time.Second)
|
2023-04-06 17:51:13 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// check that the message was processed & the kava is transferred.
|
2023-12-13 17:17:37 +00:00
|
|
|
balRes, err := suite.Kava.Grpc.Query.Bank.Balance(context.Background(), &banktypes.QueryBalanceRequest{
|
2023-04-06 17:51:13 +00:00
|
|
|
Address: receiver.String(),
|
|
|
|
Denom: "ukava",
|
|
|
|
})
|
|
|
|
suite.NoError(err)
|
2023-06-26 22:03:51 +00:00
|
|
|
suite.Equal(sdk.NewInt(1e3), balRes.Balance.Amount)
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:52:48 +00:00
|
|
|
// Note that this test works because the deployed erc20 is configured in evmutil & cdp params.
|
|
|
|
// This test matches the webapp's "USDT Earn" workflow
|
|
|
|
func (suite *IntegrationTestSuite) TestEip712ConvertToCoinAndDepositToLend() {
|
|
|
|
// cdp requires minimum of $11 collateral
|
|
|
|
amount := sdk.NewInt(11e6) // 11 USDT
|
|
|
|
principal := sdk.NewCoin("usdx", sdk.NewInt(10e6))
|
2023-06-26 22:03:51 +00:00
|
|
|
sdkDenom := suite.DeployedErc20.CosmosDenom
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
// create new funded account
|
2023-08-02 21:52:48 +00:00
|
|
|
depositor := suite.Kava.NewFundedAccount("eip712-lend-depositor", sdk.NewCoins(ukava(1e5)))
|
2023-04-06 17:51:13 +00:00
|
|
|
// give them erc20 balance to deposit
|
|
|
|
fundRes := suite.FundKavaErc20Balance(depositor.EvmAddress, amount.BigInt())
|
|
|
|
suite.NoError(fundRes.Err)
|
|
|
|
|
|
|
|
// setup messages for convert to coin & deposit into earn
|
|
|
|
convertMsg := evmutiltypes.NewMsgConvertERC20ToCoin(
|
|
|
|
evmutiltypes.NewInternalEVMAddress(depositor.EvmAddress),
|
|
|
|
depositor.SdkAddress,
|
2023-06-26 22:03:51 +00:00
|
|
|
evmutiltypes.NewInternalEVMAddress(suite.DeployedErc20.Address),
|
2023-04-06 17:51:13 +00:00
|
|
|
amount,
|
|
|
|
)
|
2023-08-02 21:52:48 +00:00
|
|
|
depositMsg := cdptypes.NewMsgCreateCDP(
|
|
|
|
depositor.SdkAddress,
|
2023-04-06 17:51:13 +00:00
|
|
|
sdk.NewCoin(sdkDenom, amount),
|
2023-08-02 21:52:48 +00:00
|
|
|
principal,
|
|
|
|
suite.DeployedErc20.CdpCollateralType,
|
2023-04-06 17:51:13 +00:00
|
|
|
)
|
|
|
|
msgs := []sdk.Msg{
|
|
|
|
// convert to coin
|
|
|
|
&convertMsg,
|
2023-08-02 21:52:48 +00:00
|
|
|
// deposit into cdp (Mint), take out USDX
|
|
|
|
&depositMsg,
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create tx
|
|
|
|
tx := suite.NewEip712TxBuilder(
|
|
|
|
depositor,
|
|
|
|
suite.Kava,
|
|
|
|
1e6,
|
|
|
|
sdk.NewCoins(ukava(1e4)),
|
|
|
|
msgs,
|
2023-08-02 21:52:48 +00:00
|
|
|
"doing the USDT Earn workflow! erc20 -> sdk.Coin -> USDX hard deposit",
|
2023-04-06 17:51:13 +00:00
|
|
|
).GetTx()
|
|
|
|
|
|
|
|
txBytes, err := suite.Kava.EncodingConfig.TxConfig.TxEncoder()(tx)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// broadcast tx
|
2023-12-13 17:17:37 +00:00
|
|
|
res, err := suite.Kava.Grpc.Query.Tx.BroadcastTx(context.Background(), &txtypes.BroadcastTxRequest{
|
2023-04-06 17:51:13 +00:00
|
|
|
TxBytes: txBytes,
|
|
|
|
Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
|
|
|
|
})
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(sdkerrors.SuccessABCICode, res.TxResponse.Code)
|
|
|
|
|
2023-12-13 17:17:37 +00:00
|
|
|
_, err = util.WaitForSdkTxCommit(suite.Kava.Grpc.Query.Tx, res.TxResponse.TxHash, 6*time.Second)
|
2023-08-02 21:52:48 +00:00
|
|
|
suite.Require().NoError(err)
|
2023-04-06 17:51:13 +00:00
|
|
|
|
|
|
|
// check that depositor no longer has erc20 balance
|
2023-06-26 22:03:51 +00:00
|
|
|
balance := suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, depositor.EvmAddress)
|
2023-04-06 17:51:13 +00:00
|
|
|
suite.BigIntsEqual(big.NewInt(0), balance, "expected no erc20 balance")
|
|
|
|
|
2023-08-02 21:52:48 +00:00
|
|
|
// check that account has cdp
|
2023-12-13 17:17:37 +00:00
|
|
|
cdpRes, err := suite.Kava.Grpc.Query.Cdp.Cdp(context.Background(), &cdptypes.QueryCdpRequest{
|
2023-08-02 21:52:48 +00:00
|
|
|
CollateralType: suite.DeployedErc20.CdpCollateralType,
|
|
|
|
Owner: depositor.SdkAddress.String(),
|
2023-04-06 17:51:13 +00:00
|
|
|
})
|
|
|
|
suite.NoError(err)
|
2023-08-02 21:52:48 +00:00
|
|
|
suite.True(cdpRes.Cdp.Collateral.Amount.Equal(amount))
|
|
|
|
suite.True(cdpRes.Cdp.Principal.Equal(principal))
|
2023-06-30 05:30:02 +00:00
|
|
|
|
|
|
|
// withdraw deposit & convert back to erc20 (this allows refund to recover erc20s used in test)
|
2023-08-02 21:52:48 +00:00
|
|
|
withdraw := cdptypes.NewMsgRepayDebt(
|
|
|
|
depositor.SdkAddress,
|
|
|
|
suite.DeployedErc20.CdpCollateralType,
|
|
|
|
principal,
|
2023-06-30 05:30:02 +00:00
|
|
|
)
|
|
|
|
convertBack := evmutiltypes.NewMsgConvertCoinToERC20(
|
|
|
|
depositor.SdkAddress.String(),
|
|
|
|
depositor.EvmAddress.Hex(),
|
|
|
|
sdk.NewCoin(sdkDenom, amount),
|
|
|
|
)
|
|
|
|
withdrawAndConvertBack := util.KavaMsgRequest{
|
2023-08-02 21:52:48 +00:00
|
|
|
Msgs: []sdk.Msg{&withdraw, &convertBack},
|
|
|
|
GasLimit: 1e6,
|
|
|
|
FeeAmount: sdk.NewCoins(ukava(1000)),
|
|
|
|
Data: "withdrawing from mint & converting back to erc20",
|
2023-06-30 05:30:02 +00:00
|
|
|
}
|
|
|
|
lastRes := depositor.SignAndBroadcastKavaTx(withdrawAndConvertBack)
|
|
|
|
suite.NoError(lastRes.Err)
|
2023-08-02 21:52:48 +00:00
|
|
|
|
|
|
|
balance = suite.Kava.GetErc20Balance(suite.DeployedErc20.Address, depositor.EvmAddress)
|
|
|
|
suite.BigIntsEqual(amount.BigInt(), balance, "expected returned erc20 balance")
|
2023-04-06 17:51:13 +00:00
|
|
|
}
|