Created
November 19, 2022 02:49
-
-
Save Austinhs/230fb5fff58203fb322a2a6a9cdf785f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main_test | |
| import ( | |
| "da/go-server/src/cron/jobs" | |
| "da/go-server/src/models" | |
| "da/go-server/src/utils/database" | |
| "da/go-server/src/utils/eth/observer" | |
| "math" | |
| "testing" | |
| "github.com/ethereum/go-ethereum/common" | |
| "github.com/ethereum/go-ethereum/crypto" | |
| "github.com/joho/godotenv" | |
| ) | |
| func TestCronJob(t *testing.T) { | |
| godotenv.Load() | |
| database.Connect() | |
| user := models.Leaderboards{Address: "0xe2165a834F93C39483123Ac31533780b9c679ed4"} | |
| database.DB.First(&user) | |
| totalBefore := user.OgPoints + user.WlPoints + user.SaPoints | |
| jobs.LeaderboardPoints() | |
| database.DB.First(&user) | |
| expected := totalBefore + user.OgNftCount + user.WlNftCount + user.SaNftCount | |
| totalAfter := user.OgPoints + user.WlPoints + user.SaPoints | |
| if expected != totalAfter { | |
| t.Errorf("Expected %d, got %d", expected, totalAfter) | |
| } | |
| } | |
| func TestObserver(t *testing.T) { | |
| godotenv.Load() | |
| database.Connect() | |
| type TxExamples struct { | |
| Hash string | |
| From string | |
| To string | |
| MovedAmount int | |
| } | |
| var txHashExamples = []TxExamples{ | |
| { | |
| // Single transfer wallet to wallet | |
| Hash: "0xb88e9e73f7a1abdad3f95d8429a49b8a4feecb05a0151480221b43c83418d449", | |
| From: "0xF081bdCb63379f1Ef2F9bd9C6631B2BC8F5dC65F", | |
| To: "0x165456FFcb917c82aF96B399Cb7E054c24997dfb", | |
| MovedAmount: 1, | |
| }, | |
| { | |
| // Single buy transfer opensea to wallet | |
| Hash: "0xdb7efd4de2e7bb903183bdccf180ab1758d5382af6acfc2726afee0a6ed112bc", | |
| From: "0x439755f398BFeAF6Fdc81ccC81917519Bf4cA1EE", | |
| To: "0x75F482544458fa21835c4ce0973E92871B93AE3D", | |
| MovedAmount: 1, | |
| }, | |
| { | |
| // Multi transfer wallet to wallet | |
| Hash: "0xb81792b021479b31c6a9ad53747e56fc81af0b50d210000569248dd1f37383a0", | |
| From: "0xe2165a834F93C39483123Ac31533780b9c679ed4", | |
| To: "0x87B158e1EDDB5A95BeecC3777cbA511F9df59Bff", | |
| MovedAmount: 2, | |
| }, | |
| { | |
| // Multi buy transfer opensea to wallet from multiple different wallets | |
| Hash: "0x07bb000fa2bece43daeaafa3e53aae6ff53b64781ea1f2aac801aaa235b3c888", | |
| From: "", | |
| To: "0xe2165a834F93C39483123Ac31533780b9c679ed4", | |
| MovedAmount: 8, | |
| }, | |
| } | |
| transferHash := crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)")) | |
| for _, test := range txHashExamples { | |
| ethLogs := observer.GetTxLogs(test.Hash) | |
| useLogFrom := test.From == "" | |
| totalMovedNfts := 0 | |
| for _, ethLog := range ethLogs { | |
| isTransfer := ethLog.Topics[0].String() == transferHash.String() | |
| isNftTransfer := len(ethLog.Topics) > 3 | |
| if !isTransfer { | |
| continue | |
| } | |
| if isNftTransfer { | |
| if useLogFrom { | |
| test.From = common.BytesToAddress(ethLog.Topics[1].Bytes()).Hex() | |
| } | |
| from := models.Leaderboards{Address: test.From} | |
| to := models.Leaderboards{Address: test.To} | |
| database.DB.FirstOrInit(&from, from) | |
| database.DB.FirstOrInit(&to, to) | |
| beforeFromCount := from.OgNftCount + from.WlNftCount + from.SaNftCount | |
| beforeToCount := to.OgNftCount + to.WlNftCount + to.SaNftCount | |
| err := observer.ProcessTransferNFT(ethLog) | |
| if err != nil { | |
| t.Error(err) | |
| } | |
| fromAfter := models.Leaderboards{Address: test.From} | |
| toAfter := models.Leaderboards{Address: test.To} | |
| database.DB.First(&fromAfter, fromAfter) | |
| database.DB.First(&toAfter, toAfter) | |
| afterFromCount := fromAfter.OgNftCount + fromAfter.WlNftCount + fromAfter.SaNftCount | |
| afterToCount := toAfter.OgNftCount + toAfter.WlNftCount + toAfter.SaNftCount | |
| if math.Abs(float64(beforeFromCount-afterFromCount)) != 1 { | |
| t.Errorf("Hash (%s) From (%s) count is not correct, before: %d, after: %d", | |
| test.Hash, test.From, beforeFromCount, afterFromCount) | |
| } | |
| if math.Abs(float64(beforeToCount-afterToCount)) != 1 { | |
| t.Errorf("Hash (%s) To (%s) count is not correct, before: %d, after: %d", | |
| test.Hash, test.To, beforeToCount, afterToCount) | |
| } | |
| totalMovedNfts++ | |
| } else { | |
| err := observer.ProcessTransferERC20(ethLog) | |
| if err != nil { | |
| t.Error(err) | |
| } | |
| } | |
| } | |
| if totalMovedNfts != test.MovedAmount { | |
| t.Errorf("Hash (%s) moved nfts count is not correct, expected: %d, got: %d", test.Hash, test.MovedAmount, totalMovedNfts) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment