-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate SPL deposits (#3124)
* deposit spl integration and start with e2e test * test wip for deposit spl and call * fix up creation of ata * add balance assertions for deposit spl e2e tests * CI fixes * lint fix * add inbound parse unit test and cleanup * comment * PR comments * move inbound parsing to solana pkg * refactor e2e test zrc20 deployment * fix e2e tests * Update changelog.md Co-authored-by: Lucas Bertrand <lucas.bertrand.22@gmail.com> --------- Co-authored-by: Dmitry S <11892559+swift1337@users.noreply.github.com> Co-authored-by: Lucas Bertrand <lucas.bertrand.22@gmail.com>
- Loading branch information
1 parent
1f2c1e1
commit 13cfffe
Showing
25 changed files
with
907 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package e2etests | ||
|
||
import ( | ||
"math/big" | ||
|
||
"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" | ||
) | ||
|
||
func TestSPLDeposit(r *runner.E2ERunner, args []string) { | ||
require.Len(r, args, 1) | ||
amount := parseInt(r, args[0]) | ||
|
||
// load deployer private key | ||
privKey, err := solana.PrivateKeyFromBase58(r.Account.SolanaPrivateKey.String()) | ||
require.NoError(r, err) | ||
|
||
// get SPL balance for pda and sender atas | ||
pda := r.ComputePdaAddress() | ||
pdaAta := r.FindOrCreateAssociatedTokenAccount(privKey, pda, r.SPLAddr) | ||
|
||
pdaBalanceBefore, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, pdaAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
senderAta := r.FindOrCreateAssociatedTokenAccount(privKey, privKey.PublicKey(), r.SPLAddr) | ||
senderBalanceBefore, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, senderAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
// get zrc20 balance for recipient | ||
zrc20BalanceBefore, err := r.SPLZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress()) | ||
require.NoError(r, err) | ||
|
||
// deposit SPL tokens | ||
// #nosec G115 e2eTest - always in range | ||
sig := r.SPLDepositAndCall(&privKey, uint64(amount), r.SPLAddr, r.EVMAddress(), nil) | ||
|
||
// wait for the cctx to be mined | ||
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, sig.String(), r.CctxClient, r.Logger, r.CctxTimeout) | ||
r.Logger.CCTX(*cctx, "solana_deposit_spl") | ||
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) | ||
|
||
// verify balances are updated | ||
pdaBalanceAfter, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, pdaAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
senderBalanceAfter, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, senderAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
zrc20BalanceAfter, err := r.SPLZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress()) | ||
require.NoError(r, err) | ||
|
||
// verify amount is deposited to pda ata | ||
require.Equal(r, parseInt(r, pdaBalanceBefore.Value.Amount)+amount, parseInt(r, pdaBalanceAfter.Value.Amount)) | ||
|
||
// verify amount is subtracted from sender ata | ||
require.Equal(r, parseInt(r, senderBalanceBefore.Value.Amount)-amount, parseInt(r, senderBalanceAfter.Value.Amount)) | ||
|
||
// verify amount is minted to receiver | ||
require.Zero(r, zrc20BalanceBefore.Add(zrc20BalanceBefore, big.NewInt(int64(amount))).Cmp(zrc20BalanceAfter)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package e2etests | ||
|
||
import ( | ||
"math/big" | ||
|
||
"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" | ||
testcontract "github.com/zeta-chain/node/testutil/contracts" | ||
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" | ||
) | ||
|
||
func TestSPLDepositAndCall(r *runner.E2ERunner, args []string) { | ||
require.Len(r, args, 1) | ||
amount := parseInt(r, args[0]) | ||
|
||
// deploy an example contract in ZEVM | ||
contractAddr, _, contract, err := testcontract.DeployExample(r.ZEVMAuth, r.ZEVMClient) | ||
require.NoError(r, err) | ||
r.Logger.Info("Example contract deployed at: %s", contractAddr.String()) | ||
|
||
// load deployer private key | ||
privKey, err := solana.PrivateKeyFromBase58(r.Account.SolanaPrivateKey.String()) | ||
require.NoError(r, err) | ||
|
||
// get SPL balance for pda and sender atas | ||
pda := r.ComputePdaAddress() | ||
pdaAta := r.FindOrCreateAssociatedTokenAccount(privKey, pda, r.SPLAddr) | ||
|
||
pdaBalanceBefore, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, pdaAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
senderAta := r.FindOrCreateAssociatedTokenAccount(privKey, privKey.PublicKey(), r.SPLAddr) | ||
senderBalanceBefore, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, senderAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
// get zrc20 balance for recipient | ||
zrc20BalanceBefore, err := r.SPLZRC20.BalanceOf(&bind.CallOpts{}, contractAddr) | ||
require.NoError(r, err) | ||
|
||
// execute the deposit transaction | ||
data := []byte("hello spl tokens") | ||
// #nosec G115 e2eTest - always in range | ||
sig := r.SPLDepositAndCall(&privKey, uint64(amount), r.SPLAddr, contractAddr, data) | ||
|
||
// wait for the cctx to be mined | ||
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, sig.String(), r.CctxClient, r.Logger, r.CctxTimeout) | ||
r.Logger.CCTX(*cctx, "solana_deposit_spl_and_call") | ||
utils.RequireCCTXStatus(r, cctx, crosschaintypes.CctxStatus_OutboundMined) | ||
|
||
// check if example contract has been called, bar value should be set to amount | ||
utils.MustHaveCalledExampleContract(r, contract, big.NewInt(int64(amount))) | ||
|
||
// verify balances are updated | ||
pdaBalanceAfter, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, pdaAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
senderBalanceAfter, err := r.SolanaClient.GetTokenAccountBalance(r.Ctx, senderAta, rpc.CommitmentConfirmed) | ||
require.NoError(r, err) | ||
|
||
zrc20BalanceAfter, err := r.SPLZRC20.BalanceOf(&bind.CallOpts{}, contractAddr) | ||
require.NoError(r, err) | ||
|
||
// verify amount is deposited to pda ata | ||
require.Equal(r, parseInt(r, pdaBalanceBefore.Value.Amount)+amount, parseInt(r, pdaBalanceAfter.Value.Amount)) | ||
|
||
// verify amount is subtracted from sender ata | ||
require.Equal(r, parseInt(r, senderBalanceBefore.Value.Amount)-amount, parseInt(r, senderBalanceAfter.Value.Amount)) | ||
|
||
// verify amount is minted to receiver | ||
require.Zero(r, zrc20BalanceBefore.Add(zrc20BalanceBefore, big.NewInt(int64(amount))).Cmp(zrc20BalanceAfter)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.