github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/tests/test_utils/strategy_utils.go (about)

     1  package test_utils
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strconv"
     7  
     8  	"github.com/DapperCollectives/CAST/backend/main/models"
     9  	"github.com/DapperCollectives/CAST/backend/main/shared"
    10  )
    11  
    12  type dummyBalance struct {
    13  	Primary     uint64
    14  	Staking     uint64
    15  	BlockHeight uint64
    16  }
    17  
    18  func (otu *OverflowTestUtils) GenerateListOfVotes(proposalId int, count int) []*models.VoteWithBalance {
    19  	votes := make([]*models.VoteWithBalance, count)
    20  	choices := []string{"a", "b"}
    21  	for i := 0; i < count; i++ {
    22  		addr := "0x" + strconv.Itoa(i+1)
    23  		randomNumber := rand.Intn(2)
    24  		choice := choices[randomNumber]
    25  		v := models.Vote{
    26  			Proposal_id: proposalId, Addr: addr, Choice: choice,
    27  		}
    28  
    29  		// Balance is 1 FLOW * index
    30  		balance := 100000000 * (i + 1)
    31  		dummyBal := createDummyBalance(balance)
    32  
    33  		vote := &models.VoteWithBalance{
    34  			Vote:                  v,
    35  			PrimaryAccountBalance: &dummyBal.Primary,
    36  			StakingBalance:        &dummyBal.Staking,
    37  			BlockHeight:           &dummyBal.BlockHeight,
    38  		}
    39  		votes[i] = vote
    40  	}
    41  
    42  	return votes
    43  }
    44  
    45  func (otu *OverflowTestUtils) GenerateCheatVote(proposalId int, count int) *[]models.VoteWithBalance {
    46  	votes := make([]models.VoteWithBalance, count)
    47  	choices := []string{"a", "b"}
    48  	for i := 0; i < count; i++ {
    49  		addr := "0x" + strconv.Itoa(i)
    50  		randomNumber := rand.Intn(2)
    51  		choice := choices[randomNumber]
    52  		v := models.Vote{
    53  			Proposal_id: proposalId, Addr: addr, Choice: choice,
    54  		}
    55  		// Balance is 1 FLOW * index
    56  		balance := 100000000 * (i + 1)
    57  		dummyBal := createDummyBalance(balance)
    58  
    59  		vote := models.VoteWithBalance{
    60  			Vote:                  v,
    61  			PrimaryAccountBalance: &dummyBal.Primary,
    62  			StakingBalance:        &dummyBal.Staking,
    63  			BlockHeight:           &dummyBal.BlockHeight,
    64  		}
    65  
    66  		votes[i] = vote
    67  	}
    68  
    69  	return &votes
    70  }
    71  
    72  func (otu *OverflowTestUtils) GenerateListOfVotesWithNFTs(
    73  	proposalId int,
    74  	count int,
    75  	contract *shared.Contract,
    76  ) ([]*models.VoteWithBalance, error) {
    77  
    78  	mintParams := shared.MintParams{
    79  		Recipient:            "user1",
    80  		Description:          "the best NFT",
    81  		Cuts:                 []float64{0.8},
    82  		RoyaltyDescriptions:  []string{"the best NFT"},
    83  		RoyaltyBeneficiaries: []string{"0xf8d6e0586b0a20c7"},
    84  	}
    85  
    86  	var votes []*models.VoteWithBalance
    87  	choices := []string{"a", "b"}
    88  
    89  	otu.SetupAccountForFlow("account")
    90  	otu.SetupToReceiveRoyalty("account")
    91  
    92  	for i := 0; i < count; i++ {
    93  		accountName := "user" + strconv.Itoa(i+1)
    94  
    95  		mintParams.Name = accountName
    96  		mintParams.Recipient = accountName
    97  
    98  		otu.SetupAccountForNFTs(accountName)
    99  		otu.SetupAccountForFlow(accountName)
   100  		otu.SetupToReceiveRoyalty(accountName)
   101  		otu.MintNFT(mintParams)
   102  
   103  		addr := otu.ResolveUser(i + 1)
   104  		randomNumber := rand.Intn(2)
   105  		choice := choices[randomNumber]
   106  		v := models.Vote{
   107  			Proposal_id: proposalId, Addr: addr, Choice: choice,
   108  		}
   109  
   110  		scriptPath := "./main/cadence/scripts/get_nfts_ids.cdc"
   111  		nftIds, err := otu.Adapter.GetNFTIds(
   112  			addr,
   113  			contract,
   114  			scriptPath,
   115  		)
   116  		if err != nil {
   117  			return nil, err
   118  		}
   119  
   120  		vote := otu.CreateNFTVote(v, nftIds, contract)
   121  
   122  		balance := 100000000 * (i + 1)
   123  		dummyBal := createDummyBalance(balance)
   124  
   125  		vote.PrimaryAccountBalance = &dummyBal.Primary
   126  		vote.StakingBalance = &dummyBal.Staking
   127  		vote.BlockHeight = &dummyBal.BlockHeight
   128  
   129  		votes = append(votes, &vote)
   130  	}
   131  
   132  	return votes, nil
   133  }
   134  
   135  func createDummyBalance(balance int) dummyBalance {
   136  	primary := uint64(balance)
   137  	staking := uint64(balance * 5)
   138  	blockHeight := uint64(9)
   139  
   140  	return dummyBalance{
   141  		Primary:     primary,
   142  		Staking:     staking,
   143  		BlockHeight: blockHeight,
   144  	}
   145  }
   146  
   147  func (otu *OverflowTestUtils) GenerateSingleVoteWithNFT(
   148  	proposalId int,
   149  	accountNumber int,
   150  	contract *shared.Contract,
   151  ) (*models.VoteWithBalance, error) {
   152  	addr := otu.ResolveUser(1)
   153  	randomNumber := rand.Intn(2)
   154  	choices := []string{"a", "b"}
   155  	choice := choices[randomNumber]
   156  	v := models.Vote{
   157  		Proposal_id: proposalId, Addr: addr, Choice: choice,
   158  	}
   159  
   160  	scriptPath := "./main/cadence/scripts/get_nfts_ids.cdc"
   161  	nftIds, err := otu.Adapter.GetNFTIds(
   162  		addr,
   163  		contract,
   164  		scriptPath,
   165  	)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	vote := otu.CreateNFTVote(v, nftIds, contract)
   171  	balance := 100000000 * (accountNumber)
   172  	primary := uint64(balance)
   173  	staking := uint64(balance * 5)
   174  	blockHeight := uint64(0)
   175  
   176  	vote.PrimaryAccountBalance = &primary
   177  	vote.StakingBalance = &staking
   178  	vote.BlockHeight = &blockHeight
   179  
   180  	return &vote, nil
   181  }
   182  
   183  func (otu *OverflowTestUtils) CreateNFTVote(v models.Vote, ids []interface{}, contract *shared.Contract) models.VoteWithBalance {
   184  	nfts := []*models.NFT{}
   185  
   186  	for _, id := range ids {
   187  		idUint, err := strconv.ParseUint(id.(string), 10, 64)
   188  		if err != nil {
   189  			fmt.Println(err)
   190  			return models.VoteWithBalance{}
   191  		}
   192  
   193  		NFT := models.NFT{
   194  			ID:            idUint,
   195  			Contract_addr: *contract.Addr,
   196  		}
   197  		nfts = append(nfts, &NFT)
   198  	}
   199  
   200  	vote := models.VoteWithBalance{
   201  		Vote: v,
   202  		NFTs: nfts,
   203  	}
   204  
   205  	return vote
   206  }
   207  
   208  func (otu *OverflowTestUtils) SetupAccountForNFTs(account string) {
   209  	otu.O.TransactionFromFile("setup_account_nfts").
   210  		SignProposeAndPayAs(account).
   211  		RunPrintEventsFull()
   212  }
   213  
   214  func (otu *OverflowTestUtils) SetupToReceiveRoyalty(account string) {
   215  	otu.O.TransactionFromFile("setup_account_to_receive_royalty").
   216  		SignProposeAndPayAs(account).
   217  		Args(otu.O.Arguments()).
   218  		RunPrintEventsFull()
   219  }
   220  
   221  func (otu *OverflowTestUtils) SetupAccountForFlow(account string) {
   222  	otu.O.TransactionFromFile("setup_flow_token_account").
   223  		SignProposeAndPayAs(account).
   224  		RunPrintEventsFull()
   225  }
   226  
   227  func (otu *OverflowTestUtils) MintNFT(p shared.MintParams) {
   228  	otu.O.TransactionFromFile("mint_nft").
   229  		SignProposeAndPayAsService().
   230  		Args(otu.O.Arguments().
   231  			Account(p.Recipient).
   232  			String(p.Name).
   233  			String(p.Description).
   234  			String(p.Thumbnail).
   235  			UFix64Array(0.8).
   236  			StringArray("royalties").
   237  			AccountArray(p.Recipient)).
   238  		RunPrintEventsFull()
   239  }
   240  
   241  func (otu *OverflowTestUtils) TransferNFT(signer, recipient string, id uint64) {
   242  	otu.O.TransactionFromFile("transfer_nft").
   243  		SignProposeAndPayAs(signer).
   244  		Args(otu.O.Arguments().
   245  			Account(recipient).
   246  			UInt64(id)).
   247  		RunPrintEventsFull()
   248  }