Skip to content

Commit

Permalink
withdraw spl with non existing ata
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Nov 30, 2024
1 parent f774c0e commit c08ebfc
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestSPLDepositName,
e2etests.TestSPLDepositAndCallName,
e2etests.TestSPLWithdrawName,
e2etests.TestSPLWithdrawAndCreateReceiverAtaName,
e2etests.TestSPLWithdrawFailsIfNoReceiverAtaName,
e2etests.TestSolanaWhitelistSPLName,
}
eg.Go(solanaTestRoutine(conf, deployerRunner, verbose, solanaTests...))
Expand Down
Binary file modified contrib/localnet/solana/gateway.so
Binary file not shown.
8 changes: 4 additions & 4 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const (
TestSPLDepositName = "spl_deposit"
TestSPLDepositAndCallName = "spl_deposit_and_call"
TestSPLWithdrawName = "spl_withdraw"
TestSPLWithdrawAndCreateReceiverAtaName = "spl_withdraw_and_create_receiver_ata"
TestSPLWithdrawFailsIfNoReceiverAtaName = "spl_withdraw_fails_without_receiver_ata"

/**
* TON tests
Expand Down Expand Up @@ -448,12 +448,12 @@ var AllE2ETests = []runner.E2ETest{
TestSPLWithdraw,
),
runner.NewE2ETest(
TestSPLWithdrawAndCreateReceiverAtaName,
"withdraw SPL from ZEVM and create receiver ata",
TestSPLWithdrawFailsIfNoReceiverAtaName,
"withdraw SPL from ZEVM fails if no receiver ata",
[]runner.ArgDefinition{
{Description: "amount in spl tokens", DefaultValue: "1000000"},
},
TestSPLWithdrawAndCreateReceiverAta,
TestSPLWithdrawFailsIfNoReceiverAta,
),
runner.NewE2ETest(
TestSolanaDepositAndCallRevertName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/e2e/runner"
"github.com/zeta-chain/node/e2e/utils"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
)

// TestSPLWithdrawAndCreateReceiverAta withdraws spl, but letting gateway to create receiver ata using rent payer
// instead of providing receiver that has it already created
func TestSPLWithdrawAndCreateReceiverAta(r *runner.E2ERunner, args []string) {
// TestSPLWithdrawFailsIfNoReceiverAta fails to withdraw spl because receiver ata doesn't exist
func TestSPLWithdrawFailsIfNoReceiverAta(r *runner.E2ERunner, args []string) {
require.Len(r, args, 1)

withdrawAmount := parseBigInt(r, args[0])
Expand Down Expand Up @@ -52,28 +50,20 @@ func TestSPLWithdrawAndCreateReceiverAta(r *runner.E2ERunner, args []string) {
// withdraw
tx := r.WithdrawSPLZRC20(receiverPrivKey.PublicKey(), withdrawAmount, approvedAmount)

// wait for the cctx to be mined
// wait for the cctx to be mined and aborted
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, tx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout)
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined)
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_Aborted)

// get SPL ZRC20 balance after withdraw
zrc20BalanceAfter, err := r.SPLZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
require.NoError(r, err)
r.Logger.Info("runner balance of SPL after withdraw: %d", zrc20BalanceAfter)

// verify receiver ata was created
// verify receiver ata was not created
receiverAtaAcc, err = r.SolanaClient.GetAccountInfo(r.Ctx, receiverAta)
require.NoError(r, err)
require.NotNil(r, receiverAtaAcc)

// verify balances are updated
receiverBalanceAfter, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, receiverAta, rpc.CommitmentFinalized)
require.NoError(r, err)
r.Logger.Info("receiver balance of SPL after withdraw: %s", receiverBalanceAfter.Value.Amount)

// verify amount is added to receiver ata
require.EqualValues(r, withdrawAmount.String(), parseBigInt(r, receiverBalanceAfter.Value.Amount).String())
require.Error(r, err)
require.Nil(r, receiverAtaAcc)

// verify amount is subtracted on zrc20
require.EqualValues(r, new(big.Int).Sub(zrc20BalanceBefore, withdrawAmount).String(), zrc20BalanceAfter.String())
// verify amount is not changed on zrc20 -- TODO: cctx is aborted without revert?
// require.EqualValues(r, zrc20BalanceBefore.String(), zrc20BalanceAfter.String())
}
18 changes: 4 additions & 14 deletions zetaclient/chains/solana/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ type Signer struct {

// pda is the program derived address of the gateway program
pda solana.PublicKey

// rent payer pda is the program derived address of the gateway program to pay rent for creating atas
rentPayerPda solana.PublicKey
}

// NewSigner creates a new Solana signer
Expand All @@ -68,19 +65,12 @@ func NewSigner(
return nil, errors.Wrapf(err, "cannot parse gateway address %s", chainParams.GatewayAddress)
}

// parse rent payer PDA, used in case receiver ATA should be created in gateway
rentPayerPda, err := contracts.RentPayerPDA(gatewayID)
if err != nil {
return nil, errors.Wrapf(err, "cannot parse gateway address %s", chainParams.GatewayAddress)
}

// create Solana signer
signer := &Signer{
Signer: baseSigner,
client: solClient,
gatewayID: gatewayID,
pda: pda,
rentPayerPda: rentPayerPda,
Signer: baseSigner,
client: solClient,
gatewayID: gatewayID,
pda: pda,
}

// construct Solana private key if present
Expand Down
1 change: 0 additions & 1 deletion zetaclient/chains/solana/signer/withdraw_spl.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (signer *Signer) signWithdrawSPLTx(
solana.Meta(msg.MintAccount()),
solana.Meta(msg.To()),
solana.Meta(recipientAta).WRITE(),
solana.Meta(signer.rentPayerPda).WRITE(),
solana.Meta(solana.TokenProgramID),
solana.Meta(solana.SPLAssociatedTokenAccountProgramID),
solana.Meta(solana.SystemProgramID),
Expand Down

0 comments on commit c08ebfc

Please sign in to comment.