Skip to main content

Deploy and test the 'English Auction'

This test of the EnglishAuction contract does the following:

  • Deploys the auction contract and the NFT contract
  • Creates a smart account and starts the auction
  • Creates another smart account and makes a bid
  • Ends the auction and tracks whether the NFT has been transferred to the winner

Import statements

Import the following components to follow the test:

import { HttpTransport, PublicClient, generateSmartAccount } from "@nilfoundation/niljs";
import { type Abi, encodeFunctionData } from "viem";

Deploy all contracts

To create a new smart account and deploy both contracts:

const SALT = BigInt(Math.floor(Math.random() * 10000));

const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});

const smartAccount = await generateSmartAccount({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

const gasPrice = await client.getGasPrice(1);

const { address: addressNFT, tx: txNFT } = await smartAccount.deployContract({
salt: SALT,
shardId: 1,
bytecode: NFT_BYTECODE,
abi: NFT_ABI,
args: [],
feeCredit: 3_000_000n * gasPrice,
});

const receiptsNFT = await txNFT.wait();

const { address: addressAuction, tx: txAuction } = await smartAccount.deployContract({
salt: SALT,
shardId: 3,
bytecode: AUCTION_BYTECODE,
value: 50_000n,
abi: AUCTION_ABI,
args: [addressNFT],
feeCredit: 5_000_000n * gasPrice,
});

const receiptsAuction = await txAuction.wait();

Participating in the auction

To transfer the ownership of the NFT to the auction and start the auction:

const changeOwnershipTx = await smartAccount.sendTransaction({
to: addressNFT,
feeCredit: 500_000n * gasPrice,
data: encodeFunctionData({
abi: NFT_ABI,
functionName: "changeOwnershipToAuction",
args: [addressAuction],
}),
});

const receiptsOwnership = await changeOwnershipTx.wait();

const startAuctionTx = await smartAccount.sendTransaction({
to: addressAuction,
feeCredit: 1_000_000n * gasPrice,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "start",
args: [],
}),
});

const receiptsStart = await startAuctionTx.wait();

To place a bid using another smart account:

const smartAccountTwo = await generateSmartAccount({
shardId: 2,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

const bidTx = await smartAccountTwo.sendTransaction({
to: addressAuction,
feeCredit: 1_000_000n * gasPrice,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "bid",
args: [],
}),
value: 300_000n,
});

const receiptsBid = await bidTx.wait();

To end the auction and check for its results:


const endTx = await smartAccount.sendTransaction({
to: addressAuction,
feeCredit: 1_000_000n * gasPrice,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "end",
args: [],
}),
});

const receiptsEnd = await endTx.wait();

const result = await client.getTokens(smartAccountTwo.address, "latest");

console.log(result);