Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/api/types/LaunchpadSale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,

Check warning on line 31 in src/api/types/LaunchpadSale.ts

View workflow job for this annotation

GitHub Actions / Run Tests

'IsPositive' is defined but never used
IsString,
Min,
ValidateNested
Expand Down Expand Up @@ -63,6 +63,11 @@
@IsInt()
public saleStartTime?: number;

@IsOptional()
@IsInt()
@Min(0)
public timeUntilLaunch?: number;

@IsNotEmpty()
@ValidateNested()
@Type(() => TokenInstanceKey)
Expand Down
37 changes: 36 additions & 1 deletion src/chaincode/launchpad/createSale.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TokenBalance, TokenClass, TokenInstance, randomUniqueKey } from "@gala-chain/api";
import {
GalaChainResponse,
TokenBalance,
TokenClass,
TokenInstance,
ValidationFailedError,
randomUniqueKey
} from "@gala-chain/api";
import { fixture, users } from "@gala-chain/test";
import BigNumber from "bignumber.js";
import { plainToInstance } from "class-transformer";
Expand Down Expand Up @@ -170,4 +177,32 @@ describe("createSale", () => {
expect(response.Status).toBe(1);
expect(response.Data?.image).toBe("https://cdn.example.com/token-logo.png");
});

it("should reject sale start time in the past", async () => {
// Given
const { ctx, contract } = fixture(LaunchpadContract).registeredUsers(users.testUser1);

const createSaleDto = new CreateTokenSaleDTO(
"Past Start Token",
"PAST",
"A token with past sale start time",
"https://example.com/past.png",
new BigNumber(0),
"PastCollection",
"PastCategory",
undefined,
ctx.txUnixTime - 1
);
createSaleDto.uniqueKey = randomUniqueKey();

const signedDto = createSaleDto.signed(users.testUser1.privateKey);

// When
const response = await contract.CreateSale(ctx, signedDto);

// Then
expect(response).toEqual(
GalaChainResponse.Error(new ValidationFailedError("Sale start time must be in the future."))
);
});
});
6 changes: 5 additions & 1 deletion src/chaincode/launchpad/createSale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ConflictError, TokenInstanceKey, asValidUserAlias } from "@gala-chain/api";
import { ConflictError, TokenInstanceKey, ValidationFailedError, asValidUserAlias } from "@gala-chain/api";
import {
GalaChainContext,
createTokenClass,
Expand Down Expand Up @@ -63,6 +63,10 @@ export async function createSale(

launchpadDetails.tokenSymbol = launchpadDetails.tokenSymbol.toUpperCase();

if (launchpadDetails.saleStartTime !== undefined && launchpadDetails.saleStartTime < ctx.txUnixTime) {
throw new ValidationFailedError("Sale start time must be in the future.");
}

// Define the token class key
const tokenInstanceKey = new TokenInstanceKey();
tokenInstanceKey.collection = `${launchpadDetails.tokenCollection}`;
Expand Down
19 changes: 19 additions & 0 deletions src/chaincode/launchpad/fetchSaleDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ describe("fetchSaleDetails", () => {
expect(response.Data?.sellingToken).toEqual(launchpadGalaInstance.instanceKeyObj());
});

it("should include timeUntilLaunch when saleStartTime is set", async () => {
// Given
sale.saleStartTime = Math.floor(Date.now() / 1000) + 60;
const { ctx, contract } = fixture(LaunchpadContract).registeredUsers(users.testUser1).savedState(sale);

const fetchSaleDto = new FetchSaleDto(vaultAddress);
fetchSaleDto.uniqueKey = randomUniqueKey();

const signedDto = fetchSaleDto.signed(users.testUser1.privateKey);

// When
const response = await contract.FetchSaleDetails(ctx, signedDto);

// Then
const expected = Math.max(0, (sale.saleStartTime - ctx.txUnixTime) * 1000);
expect(response.Status).toBe(1);
expect(response.Data?.timeUntilLaunch).toBe(expected);
});

it("should handle sale with existing trades", async () => {
// Given
sale.buyToken(new BigNumber("100"), new BigNumber("0.01"));
Expand Down
4 changes: 4 additions & 0 deletions src/chaincode/launchpad/fetchSaleDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ export async function fetchSaleDetails(
throw new NotFoundError("Sale record not found.");
}

const saleStartTime = sale.saleStartTime ?? 0;
const remainingSeconds = saleStartTime > 0 ? saleStartTime - ctx.txUnixTime : 0;
sale.timeUntilLaunch = Math.max(0, remainingSeconds) * 1000;

return sale;
}
Loading