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 wallet and starts the auction
  • Creates another wallet 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,
generateWallet,
waitTillCompleted,
} from "@nilfoundation/niljs";
import { type Abi, encodeFunctionData } from "viem";

Deploy all contracts

To create a new wallet 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 wallet = await generateWallet({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});
const gasPrice = await client.getGasPrice(1);

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

const receiptsNFT = await waitTillCompleted(client, hashNFT);

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

const receiptsAuction = await waitTillCompleted(client, hashAuction);

Participating in the auction

To start the auction using the 'owner' wallet:

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

const receiptsStart = await waitTillCompleted(client, startAuctionHash);

To place a bid using another wallet:

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

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

const receiptsBid = await waitTillCompleted(client, bidHash);

To end the auction and check for its results:


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

const receiptsEnd = await waitTillCompleted(client, endHash);

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

console.log(result);