Skip to main content

Deploy and test the swap contract

This test of the SwapMatch contract does the follows:

  • Creates two new wallets
  • Deploys the contract
  • Mints two tokens for these wallets
  • Places two swap requests (one from Wallet 1 and another from Wallet 2)
  • Evaluates whether the swap requests were matched and if excesses were returned correctly

Create two new wallets

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

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

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

const walletOneAddress = walletOne.address;
const walletTwoAddress = walletTwo.address;

Deploy the contract

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

const { address: swapMatchAddress, hash: deploymentMessageHash } =
await wallet.deployContract({
bytecode: SWAP_MATCH_BYTECODE,
value: 0n,
feeCredit: 100_000_000n,
salt: SALT,
shardId: 4,
});

const receipts = await waitTillCompleted(client, deploymentMessageHash);

Mint two new tokens

{
const hashMessage = await walletOne.mintToken(100_000_000n);
await waitTillCompleted(client, hashMessage);
}

{
const hashMessage = await walletTwo.mintToken(100_000_000n);
await waitTillCompleted(client, hashMessage);
}

Place the first swap request

{
const gasPrice = await client.getGasPrice(walletOne.shardId);
const hashMessage = await walletOne.sendMessage({
to: swapMatchAddress,
tokens: [
{
id: walletOneAddress,
amount: 30_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [20_000_000n, walletTwoAddress],
feeCredit: gasPrice * 1_000_000_000n,
});

await waitTillCompleted(client, hashMessage);
}

Place the second swap request

{
const gasPrice = await client.getGasPrice(walletTwo.shardId);
const hashMessage = await walletTwo.sendMessage({
to: swapMatchAddress,
tokens: [
{
id: walletTwoAddress,
amount: 50_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [10_000_000n, walletOneAddress],
feeCredit: gasPrice * 1_000_000_000n,
});

await waitTillCompleted(client, hashMessage);
}

Check the token amounts

const tokensOne = await client.getTokens(walletOneAddress, "latest");
const tokensTwo = await client.getTokens(walletTwoAddress, "latest");
console.log("Wallet 1 tokens: ", tokensOne);
console.log("Wallet 2 tokens: ", tokensTwo);

Expected output:

Wallet 1 tokens:  {
TOKEN_ONE_ID: 90000000n,
TOKEN_TWO_ID: 20000000n
}
Wallet 2 tokens: {
TOKEN_ONE_ID: 10000000n,
TOKEN_TWO_ID: 80000000n
}