Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTS Precompile tests for token check methods #468

Merged
Merged
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
69 changes: 61 additions & 8 deletions packages/server/tests/acceptance/htsPrecompile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('@htsprecompile Acceptance Tests', async function () {
let HTSTokenWithCustomFeesContractAddress;

this.beforeAll(async () => {
accounts[0] = await servicesNode.createAliasAccount(170, relay.provider);
accounts[0] = await servicesNode.createAliasAccount(200, relay.provider);
accounts[1] = await servicesNode.createAliasAccount(30, relay.provider);
accounts[2] = await servicesNode.createAliasAccount(30, relay.provider);

Expand Down Expand Up @@ -704,7 +704,7 @@ describe('@htsprecompile Acceptance Tests', async function () {
describe('CryptoTransfer Tests', async function() {
let NftSerialNumber;
let NftSerialNumber2;

async function setKyc(tokenAddress) {
const grantKycTx = await baseHTSContractOwner.grantTokenKycPublic(tokenAddress, accounts[1].wallet.address, { gasLimit: 1_000_000 });
expect((await grantKycTx.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode).to.equal(TX_SUCCESS_CODE);
Expand Down Expand Up @@ -748,7 +748,7 @@ describe('@htsprecompile Acceptance Tests', async function () {
NftSerialNumber2 = serialNumbers[1];

await setKyc(NftHTSTokenContractAddress);

// setup the transfer
const tokenTransferList = [{
token: `${NftHTSTokenContractAddress}`,
Expand All @@ -774,7 +774,7 @@ describe('@htsprecompile Acceptance Tests', async function () {
expect((await txMint.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode).to.be.equal(TX_SUCCESS_CODE);
const { serialNumbers } = (await txMint.wait()).events.filter(e => e.event === 'MintedToken')[0].args;
const NftSerialNumber = serialNumbers[0];

// setup the transfer
const tokenTransferList = [{
token: `${NftHTSTokenContractAddress}`,
Expand Down Expand Up @@ -843,7 +843,7 @@ describe('@htsprecompile Acceptance Tests', async function () {
}
});

it('should fail to swap approved non-fungible tokens', async function() {
it('should fail to swap approved non-fungible tokens', async function() {
const txApprove1 = await baseHTSContract.setApprovalForAllPublic(NftHTSTokenContractAddress, accounts[1].wallet.address, true, { gasLimit: 1_000_000 });
expect((await txApprove1.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode).to.equal(TX_SUCCESS_CODE);

Expand All @@ -867,14 +867,14 @@ describe('@htsprecompile Acceptance Tests', async function () {

try{
const txXfer = await baseHTSContract.cryptoTransferPublic(tokenTransferList);
expect((await txXfer.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode).to.equal(TX_SUCCESS_CODE);
expect((await txXfer.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode).to.equal(TX_SUCCESS_CODE);
} catch (error: any) {
expect(error.code).to.equal("CALL_EXCEPTION");
expect(error.reason).to.equal("transaction failed");
}
});

it('should fail to transfer fungible and non-fungible tokens in a single tokenTransferList', async function() {
it('should fail to transfer fungible and non-fungible tokens in a single tokenTransferList', async function() {
// setup the transfer
const xferAmount = 10;
const tokenTransferList = [{
Expand All @@ -897,11 +897,64 @@ describe('@htsprecompile Acceptance Tests', async function () {
}];
try {
const txXfer = await baseHTSContract.cryptoTransferPublic(tokenTransferList);
const response = (await txXfer.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
const response = (await txXfer.wait()).events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
} catch (error: any) {
expect(error.code).to.equal("CALL_EXCEPTION");
expect(error.reason).to.equal("transaction failed");
}
});
});

describe('HTS Precompile for token check methods', async function() {
it('should return false for isToken with passed contract address', async function() {
const tx = await baseHTSContract.isTokenPublic(BaseHTSContractAddress, { gasLimit: 1000000 });
const txReceipt = await tx.wait();

const responseCode = txReceipt.events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
expect(responseCode).to.equal(TX_SUCCESS_CODE);

const isTokenFlag = txReceipt.events.filter(e => e.event === 'IsToken')[0].args.isToken;
expect(isTokenFlag).to.equal(false);
});
it('should return true for isToken with passed token address', async function() {
const tx = await baseHTSContract.isTokenPublic(HTSTokenContractAddress, { gasLimit: 1000000 });
const txReceipt = await tx.wait();

const responseCode = txReceipt.events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
expect(responseCode).to.equal(TX_SUCCESS_CODE);

const isTokenFlag = txReceipt.events.filter(e => e.event === 'IsToken')[0].args.isToken;
expect(isTokenFlag).to.equal(true);
});
it('should return 0 for getTokenType with passed FUNGIBLE_COMMON token', async function() {
const tx = await baseHTSContract.getTokenTypePublic(HTSTokenContractAddress, { gasLimit: 1000000 });
const txReceipt = await tx.wait();

const responseCode = txReceipt.events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
expect(responseCode).to.equal(TX_SUCCESS_CODE);

const tokenType = txReceipt.events.filter(e => e.event === 'TokenType')[0].args.tokenType;
expect(tokenType).to.equal(0);
});
it('should return 1 for getTokenType with passed HTS NON_FUNGIBLE_UNIQUE token', async function() {
const tx = await baseHTSContract.getTokenTypePublic(NftHTSTokenContractAddress, { gasLimit: 1000000 });
const txReceipt = await tx.wait();

const responseCode = txReceipt.events.filter(e => e.event === 'ResponseCode')[0].args.responseCode;
expect(responseCode).to.equal(TX_SUCCESS_CODE);

const tokenType = txReceipt.events.filter(e => e.event === 'TokenType')[0].args.tokenType;
expect(tokenType).to.equal(1);
});
it('should throw an exception for getTokenType with passed contract address', async function() {
let hasError = false;
try {
const tx = await baseHTSContract.getTokenTypePublic(BaseHTSContractAddress, { gasLimit: 1000000 });
await tx.wait();
} catch (e) {
hasError = true;
}
expect(hasError).to.equal(true);
});
});
});
102 changes: 88 additions & 14 deletions packages/server/tests/contracts/BaseHTS.json

Large diffs are not rendered by default.

48 changes: 36 additions & 12 deletions packages/server/tests/contracts/BaseHTS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ contract BaseHTS is FeeHelper {
event TokenDefaultKycStatus(bool defaultKycStatus);
event KycGranted(bool kycGranted);
event TokenExpiryInfo(IHederaTokenService.Expiry expiryInfo);
event IsToken(bool isToken);
event TokenType(int32 tokenType);

function createFungibleTokenPublic(
address treasury
Expand Down Expand Up @@ -271,23 +273,23 @@ contract BaseHTS is FeeHelper {
function freezeTokenPublic(address token, address account) public returns (int responseCode) {
responseCode = HederaTokenService.freezeToken(token, account);
emit ResponseCode(responseCode);
if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}
}

function unfreezeTokenPublic(address token, address account) public returns (int responseCode) {
responseCode = HederaTokenService.unfreezeToken(token, account);
emit ResponseCode(responseCode);
if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}
}

function isFrozenPublic(address token, address account) public returns (int responseCode, bool frozen) {
(responseCode, frozen) = HederaTokenService.isFrozen(token, account);
emit ResponseCode(responseCode);
if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}
emit Frozen(frozen);
Expand Down Expand Up @@ -369,7 +371,7 @@ contract BaseHTS is FeeHelper {

emit ResponseCode(responseCode);

if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}

Expand All @@ -390,45 +392,67 @@ contract BaseHTS is FeeHelper {

emit ResponseCode(responseCode);

if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}

emit TokenDefaultKycStatus(defaultKycStatus);
}

function isKycPublic(address token, address account)external returns (int64 responseCode, bool kycGranted){
function isKycPublic(address token, address account) external returns (int64 responseCode, bool kycGranted){
(responseCode, kycGranted) = this.isKyc(token, account);

emit ResponseCode(responseCode);

if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}

emit KycGranted(kycGranted);
}

function grantTokenKycPublic(address token, address account)external returns (int64 responseCode){
function grantTokenKycPublic(address token, address account) external returns (int64 responseCode){
(responseCode) = this.grantTokenKyc(token, account);

emit ResponseCode(responseCode);

if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}
}

function revokeTokenKycPublic(address token, address account)external returns (int64 responseCode){
function revokeTokenKycPublic(address token, address account) external returns (int64 responseCode){
(responseCode) = this.revokeTokenKyc(token, account);

emit ResponseCode(responseCode);

if(responseCode != HederaResponseCodes.SUCCESS) {
if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}
}

function isTokenPublic(address token) public returns (int64 responseCode, bool isTokenFlag) {
(responseCode, isTokenFlag) = HederaTokenService.isToken(token);
emit ResponseCode(responseCode);

if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}

emit IsToken(isTokenFlag);
}

function getTokenTypePublic(address token) public returns (int64 responseCode, int32 tokenType) {
(responseCode, tokenType) = HederaTokenService.getTokenType(token);
emit ResponseCode(responseCode);

if (responseCode != HederaResponseCodes.SUCCESS) {
revert();
}

emit TokenType(tokenType);
}

function getTokenExpiryInfoPublic(address token)external returns (int responseCode, IHederaTokenService.Expiry memory expiryInfo){
(responseCode, expiryInfo) = this.getTokenExpiryInfo(token);

Expand All @@ -450,4 +474,4 @@ contract BaseHTS is FeeHelper {
revert();
}
}
}
}
Loading