|
| 1 | +import { Cardano, StakePoolProvider } from '@cardano-sdk/core'; |
| 2 | +import { PersonalWallet, SharedWallet } from '@cardano-sdk/wallet'; |
| 3 | +import { buildSharedWallets } from '../wallet/SharedWallet/ultils'; |
| 4 | +import { filter, firstValueFrom, map, take } from 'rxjs'; |
| 5 | +import { |
| 6 | + getEnv, |
| 7 | + getTxConfirmationEpoch, |
| 8 | + getWallet, |
| 9 | + runningAgainstLocalNetwork, |
| 10 | + submitAndConfirm, |
| 11 | + waitForEpoch, |
| 12 | + walletReady, |
| 13 | + walletVariables |
| 14 | +} from '../../src'; |
| 15 | +import { isNotNil } from '@cardano-sdk/util'; |
| 16 | +import { logger } from '@cardano-sdk/util-dev'; |
| 17 | +import { waitForWalletStateSettle } from '../../../wallet/test/util'; |
| 18 | + |
| 19 | +const env = getEnv(walletVariables); |
| 20 | + |
| 21 | +const submitDelegationTx = async ( |
| 22 | + alice: SharedWallet, |
| 23 | + bob: SharedWallet, |
| 24 | + charlotte: SharedWallet, |
| 25 | + pool: Cardano.PoolId |
| 26 | +) => { |
| 27 | + logger.info(`Creating delegation tx at epoch #${(await firstValueFrom(alice.currentEpoch$)).epochNo}`); |
| 28 | + let tx = (await alice.createTxBuilder().delegateFirstStakeCredential(pool).build().sign()).tx; |
| 29 | + |
| 30 | + tx = await bob.updateWitness({ sender: { id: 'e2e' }, tx }); |
| 31 | + tx = await charlotte.updateWitness({ sender: { id: 'e2e' }, tx }); |
| 32 | + await alice.submitTx(tx); |
| 33 | + |
| 34 | + const { epochNo } = await firstValueFrom(alice.currentEpoch$); |
| 35 | + logger.info(`Delegation tx ${tx.id} submitted at epoch #${epochNo}`); |
| 36 | + |
| 37 | + return tx; |
| 38 | +}; |
| 39 | + |
| 40 | +const generateTxs = async (sendingWallet: PersonalWallet, receivingWallet: SharedWallet) => { |
| 41 | + logger.info('Sending 100 txs to generate reward fees'); |
| 42 | + |
| 43 | + const tAdaToSend = 5_000_000n; |
| 44 | + const [{ address: receivingAddress }] = await firstValueFrom(receivingWallet.addresses$); |
| 45 | + |
| 46 | + for (let i = 0; i < 100; i++) { |
| 47 | + const txBuilder = sendingWallet.createTxBuilder(); |
| 48 | + const txOut = await txBuilder.buildOutput().address(receivingAddress).coin(tAdaToSend).build(); |
| 49 | + const { tx: signedTx } = await txBuilder.addOutput(txOut).build().sign(); |
| 50 | + await sendingWallet.submitTx(signedTx); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +const buildSpendRewardTx = async ( |
| 55 | + alice: SharedWallet, |
| 56 | + bob: SharedWallet, |
| 57 | + charlotte: SharedWallet, |
| 58 | + receivingWallet: PersonalWallet |
| 59 | +) => { |
| 60 | + const tAdaToSend = 5_000_000n; |
| 61 | + const [{ address: receivingAddress }] = await firstValueFrom(receivingWallet.addresses$); |
| 62 | + const txBuilder = alice.createTxBuilder(); |
| 63 | + const txOut = await txBuilder.buildOutput().address(receivingAddress).coin(tAdaToSend).build(); |
| 64 | + const tx = txBuilder.addOutput(txOut).build(); |
| 65 | + |
| 66 | + const { body } = await tx.inspect(); |
| 67 | + logger.debug('Body of tx before sign'); |
| 68 | + logger.debug(body); |
| 69 | + let signedTx = (await tx.sign()).tx; |
| 70 | + |
| 71 | + signedTx = await bob.updateWitness({ sender: { id: 'e2e' }, tx: signedTx }); |
| 72 | + signedTx = await charlotte.updateWitness({ sender: { id: 'e2e' }, tx: signedTx }); |
| 73 | + |
| 74 | + logger.debug('Body of tx after sign'); |
| 75 | + logger.debug(signedTx.body); |
| 76 | + |
| 77 | + return signedTx; |
| 78 | +}; |
| 79 | + |
| 80 | +const getPoolIds = async (stakePoolProvider: StakePoolProvider, count: number) => { |
| 81 | + const activePools = await stakePoolProvider.queryStakePools({ |
| 82 | + filters: { pledgeMet: true, status: [Cardano.StakePoolStatus.Active] }, |
| 83 | + pagination: { limit: count, startAt: 0 } |
| 84 | + }); |
| 85 | + expect(activePools.totalResultCount).toBeGreaterThanOrEqual(count); |
| 86 | + const poolIds = activePools.pageResults.map(({ id }) => id); |
| 87 | + expect(poolIds.every((poolId) => poolId !== undefined)).toBeTruthy(); |
| 88 | + logger.info('Wallet funds will be staked to pools:', poolIds); |
| 89 | + return poolIds; |
| 90 | +}; |
| 91 | + |
| 92 | +describe('delegation rewards', () => { |
| 93 | + let fundingTx: Cardano.Tx<Cardano.TxBody>; |
| 94 | + let faucetWallet: PersonalWallet; |
| 95 | + let faucetAddress: Cardano.PaymentAddress; |
| 96 | + let aliceMultiSigWallet: SharedWallet; |
| 97 | + let bobMultiSigWallet: SharedWallet; |
| 98 | + let charlotteMultiSigWallet: SharedWallet; |
| 99 | + let stakePoolProvider: StakePoolProvider; |
| 100 | + |
| 101 | + const initialFunds = 10_000_000n; |
| 102 | + |
| 103 | + beforeAll(async () => { |
| 104 | + jest.setTimeout(180_000); |
| 105 | + |
| 106 | + ({ |
| 107 | + wallet: faucetWallet, |
| 108 | + providers: { stakePoolProvider } |
| 109 | + } = await getWallet({ env, logger, name: 'Sending Wallet', polling: { interval: 50 } })); |
| 110 | + |
| 111 | + // Make sure the wallet has sufficient funds to run this test |
| 112 | + await walletReady(faucetWallet, initialFunds); |
| 113 | + |
| 114 | + faucetAddress = (await firstValueFrom(faucetWallet.addresses$))[0].address; |
| 115 | + |
| 116 | + ({ aliceMultiSigWallet, bobMultiSigWallet, charlotteMultiSigWallet } = await buildSharedWallets( |
| 117 | + env, |
| 118 | + await firstValueFrom(faucetWallet.genesisParameters$), |
| 119 | + logger |
| 120 | + )); |
| 121 | + |
| 122 | + await Promise.all([ |
| 123 | + waitForWalletStateSettle(aliceMultiSigWallet), |
| 124 | + waitForWalletStateSettle(bobMultiSigWallet), |
| 125 | + waitForWalletStateSettle(charlotteMultiSigWallet) |
| 126 | + ]); |
| 127 | + |
| 128 | + const [{ address: receivingAddress }] = await firstValueFrom(aliceMultiSigWallet.addresses$); |
| 129 | + |
| 130 | + logger.info(`Address ${faucetAddress} will send ${initialFunds} lovelace to address ${receivingAddress}.`); |
| 131 | + |
| 132 | + // Send 10 tADA to the shared wallet. |
| 133 | + const txBuilder = faucetWallet.createTxBuilder(); |
| 134 | + const txOutput = await txBuilder.buildOutput().address(receivingAddress).coin(initialFunds).build(); |
| 135 | + fundingTx = (await txBuilder.addOutput(txOutput).build().sign()).tx; |
| 136 | + await faucetWallet.submitTx(fundingTx); |
| 137 | + |
| 138 | + logger.info( |
| 139 | + `Submitted transaction id: ${fundingTx.id}, inputs: ${JSON.stringify( |
| 140 | + fundingTx.body.inputs.map((txIn) => [txIn.txId, txIn.index]) |
| 141 | + )} and outputs:${JSON.stringify( |
| 142 | + fundingTx.body.outputs.map((txOut) => [txOut.address, Number.parseInt(txOut.value.coins.toString())]) |
| 143 | + )}.` |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + afterAll(() => { |
| 148 | + faucetWallet.shutdown(); |
| 149 | + aliceMultiSigWallet.shutdown(); |
| 150 | + bobMultiSigWallet.shutdown(); |
| 151 | + charlotteMultiSigWallet.shutdown(); |
| 152 | + faucetWallet.shutdown(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('will receive rewards for delegated tADA and can spend them', async () => { |
| 156 | + if (!(await runningAgainstLocalNetwork())) { |
| 157 | + return logger.fatal( |
| 158 | + "Skipping test 'will receive rewards for delegated tADA' as it should only run with a fast test network" |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + const txFoundInHistory = await firstValueFrom( |
| 163 | + aliceMultiSigWallet.transactions.history$.pipe( |
| 164 | + map((txs) => txs.find((tx) => tx.id === fundingTx.id)), |
| 165 | + filter(isNotNil), |
| 166 | + take(1) |
| 167 | + ) |
| 168 | + ); |
| 169 | + |
| 170 | + expect(txFoundInHistory?.id).toEqual(fundingTx.id); |
| 171 | + |
| 172 | + // Arrange |
| 173 | + const [poolId] = await getPoolIds(stakePoolProvider, 1); |
| 174 | + |
| 175 | + // Stake and wait for reward |
| 176 | + const signedTx = await submitDelegationTx(aliceMultiSigWallet, bobMultiSigWallet, charlotteMultiSigWallet, poolId); |
| 177 | + |
| 178 | + const delegationTxConfirmedAtEpoch = await getTxConfirmationEpoch(aliceMultiSigWallet, signedTx); |
| 179 | + |
| 180 | + logger.info(`Delegation tx confirmed at epoch #${delegationTxConfirmedAtEpoch}`); |
| 181 | + |
| 182 | + await waitForEpoch(aliceMultiSigWallet, delegationTxConfirmedAtEpoch + 2); |
| 183 | + |
| 184 | + await generateTxs(faucetWallet, aliceMultiSigWallet); |
| 185 | + await waitForEpoch(aliceMultiSigWallet, delegationTxConfirmedAtEpoch + 4); |
| 186 | + |
| 187 | + // Check reward |
| 188 | + await waitForWalletStateSettle(aliceMultiSigWallet); |
| 189 | + const rewards = await firstValueFrom(aliceMultiSigWallet.balance.rewardAccounts.rewards$); |
| 190 | + expect(rewards).toBeGreaterThan(0n); |
| 191 | + |
| 192 | + logger.info(`Generated rewards: ${rewards} tLovelace`); |
| 193 | + |
| 194 | + // Spend reward |
| 195 | + const spendRewardTx = await buildSpendRewardTx( |
| 196 | + aliceMultiSigWallet, |
| 197 | + bobMultiSigWallet, |
| 198 | + charlotteMultiSigWallet, |
| 199 | + faucetWallet |
| 200 | + ); |
| 201 | + expect(spendRewardTx.body.withdrawals?.length).toBeGreaterThan(0); |
| 202 | + await submitAndConfirm(aliceMultiSigWallet, spendRewardTx); |
| 203 | + }); |
| 204 | +}); |
0 commit comments