|
| 1 | +import { TransactionType } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
| 3 | +import { afterEach, describe, expect, it } from 'vitest' |
| 4 | +import { Auction } from './contract.algo' |
| 5 | + |
| 6 | +describe('Auction', () => { |
| 7 | + const ctx = new TestExecutionContext() |
| 8 | + afterEach(() => { |
| 9 | + ctx.reset() |
| 10 | + }) |
| 11 | + |
| 12 | + it('should be able to opt into an asset', () => { |
| 13 | + // Arrange |
| 14 | + const asset = ctx.any.asset() |
| 15 | + const contract = ctx.contract.create(Auction) |
| 16 | + contract.createApplication() |
| 17 | + |
| 18 | + // Act |
| 19 | + contract.optIntoAsset(asset) |
| 20 | + |
| 21 | + // Assert |
| 22 | + expect(contract.asa.value.id).toEqual(asset.id) |
| 23 | + const innerTxn = ctx.txn.lastGroup.lastItxnGroup().getAssetTransferInnerTxn() |
| 24 | + |
| 25 | + expect(innerTxn.assetReceiver, 'Asset receiver does not match').toEqual(ctx.ledger.getApplicationForContract(contract).address) |
| 26 | + |
| 27 | + expect(innerTxn.xferAsset, 'Transferred asset does not match').toEqual(asset) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should be able to start an auction', () => { |
| 31 | + // Arrange |
| 32 | + const contract = ctx.contract.create(Auction) |
| 33 | + contract.createApplication() |
| 34 | + |
| 35 | + const app = ctx.ledger.getApplicationForContract(contract) |
| 36 | + |
| 37 | + const latestTimestamp = ctx.any.uint64(1, 1000) |
| 38 | + const startingPrice = ctx.any.uint64() |
| 39 | + const auctionDuration = ctx.any.uint64(100, 1000) |
| 40 | + const axferTxn = ctx.any.txn.assetTransfer({ |
| 41 | + assetReceiver: app.address, |
| 42 | + assetAmount: startingPrice, |
| 43 | + }) |
| 44 | + contract.asaAmt.value = startingPrice |
| 45 | + ctx.ledger.patchGlobalData({ |
| 46 | + latestTimestamp: latestTimestamp, |
| 47 | + }) |
| 48 | + |
| 49 | + // Act |
| 50 | + contract.startAuction(startingPrice, auctionDuration, axferTxn) |
| 51 | + |
| 52 | + // Assert |
| 53 | + expect(contract.auctionEnd.value).toEqual(latestTimestamp + auctionDuration) |
| 54 | + expect(contract.previousBid.value).toEqual(startingPrice) |
| 55 | + expect(contract.asaAmt.value).toEqual(startingPrice) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should be able to bid', () => { |
| 59 | + // Arrange |
| 60 | + const account = ctx.defaultSender |
| 61 | + const auctionEnd = ctx.any.uint64(Date.now() + 10_000) |
| 62 | + const previousBid = ctx.any.uint64(1, 100) |
| 63 | + const payAmount = ctx.any.uint64() |
| 64 | + |
| 65 | + const contract = ctx.contract.create(Auction) |
| 66 | + contract.createApplication() |
| 67 | + contract.auctionEnd.value = auctionEnd |
| 68 | + contract.previousBid.value = previousBid |
| 69 | + const pay = ctx.any.txn.payment({ sender: account, amount: payAmount }) |
| 70 | + |
| 71 | + // Act |
| 72 | + contract.bid(pay) |
| 73 | + |
| 74 | + // Assert |
| 75 | + expect(contract.previousBid.value).toEqual(payAmount) |
| 76 | + expect(contract.previousBidder.value).toEqual(account) |
| 77 | + expect(contract.claimableAmount(account).value).toEqual(payAmount) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should be able to claim bids', () => { |
| 81 | + // Arrange |
| 82 | + const account = ctx.any.account() |
| 83 | + const contract = ctx.contract.create(Auction) |
| 84 | + contract.createApplication() |
| 85 | + |
| 86 | + const claimableAmount = ctx.any.uint64() |
| 87 | + contract.claimableAmount(account).value = claimableAmount |
| 88 | + |
| 89 | + contract.previousBidder.value = account |
| 90 | + const previousBid = ctx.any.uint64(undefined, claimableAmount) |
| 91 | + contract.previousBid.value = previousBid |
| 92 | + |
| 93 | + // Act |
| 94 | + ctx.txn.createScope([ctx.any.txn.applicationCall({ sender: account })]).execute(() => { |
| 95 | + contract.claimBids() |
| 96 | + }) |
| 97 | + |
| 98 | + // Assert |
| 99 | + const expectedPayment = claimableAmount - previousBid |
| 100 | + const lastInnerTxn = ctx.txn.lastGroup.lastItxnGroup().getPaymentInnerTxn() |
| 101 | + |
| 102 | + expect(lastInnerTxn.amount).toEqual(expectedPayment) |
| 103 | + expect(lastInnerTxn.receiver).toEqual(account) |
| 104 | + expect(contract.claimableAmount(account).value).toEqual(claimableAmount - expectedPayment) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should be able to claim asset', () => { |
| 108 | + // Arrange |
| 109 | + ctx.ledger.patchGlobalData({ latestTimestamp: ctx.any.uint64() }) |
| 110 | + const contract = ctx.contract.create(Auction) |
| 111 | + contract.createApplication() |
| 112 | + |
| 113 | + contract.auctionEnd.value = ctx.any.uint64(1, 100) |
| 114 | + contract.previousBidder.value = ctx.defaultSender |
| 115 | + const asaAmount = ctx.any.uint64(1000, 2000) |
| 116 | + contract.asaAmt.value = asaAmount |
| 117 | + const asset = ctx.any.asset() |
| 118 | + |
| 119 | + // Act |
| 120 | + contract.claimAsset(asset) |
| 121 | + |
| 122 | + // Assert |
| 123 | + const lastInnerTxn = ctx.txn.lastGroup.lastItxnGroup().getAssetTransferInnerTxn() |
| 124 | + expect(lastInnerTxn.xferAsset).toEqual(asset) |
| 125 | + expect(lastInnerTxn.assetCloseTo).toEqual(ctx.defaultSender) |
| 126 | + expect(lastInnerTxn.assetReceiver).toEqual(ctx.defaultSender) |
| 127 | + expect(lastInnerTxn.assetAmount).toEqual(asaAmount) |
| 128 | + }) |
| 129 | + |
| 130 | + it('should be able to delete application', () => { |
| 131 | + // Arrange |
| 132 | + const account = ctx.any.account() |
| 133 | + |
| 134 | + // Act |
| 135 | + // setting sender will determine creator |
| 136 | + let contract |
| 137 | + ctx.txn.createScope([ctx.any.txn.applicationCall({ sender: account })]).execute(() => { |
| 138 | + contract = ctx.contract.create(Auction) |
| 139 | + contract.createApplication() |
| 140 | + }) |
| 141 | + |
| 142 | + ctx.txn.createScope([ctx.any.txn.applicationCall({ onCompletion: 'DeleteApplication' })]).execute(() => { |
| 143 | + contract!.deleteApplication() |
| 144 | + }) |
| 145 | + |
| 146 | + // Assert |
| 147 | + const innerTransactions = ctx.txn.lastGroup.lastItxnGroup().getPaymentInnerTxn() |
| 148 | + expect(innerTransactions).toBeTruthy() |
| 149 | + expect(innerTransactions.type).toEqual(TransactionType.Payment) |
| 150 | + expect(innerTransactions.receiver).toEqual(account) |
| 151 | + expect(innerTransactions.closeRemainderTo).toEqual(account) |
| 152 | + }) |
| 153 | + |
| 154 | + it('should be able to call clear state program', () => { |
| 155 | + const contract = ctx.contract.create(Auction) |
| 156 | + contract.createApplication() |
| 157 | + |
| 158 | + expect(contract.clearStateProgram()).toBeTruthy() |
| 159 | + }) |
| 160 | +}) |
0 commit comments