Skip to content

Commit

Permalink
fix: set correct autoRenrewAccountId (#2217)
Browse files Browse the repository at this point in the history
* fix: set correct autoRenrewAccountId

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: update releated integration tests

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: integration test

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: TokenCreateTransaction integration tests

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: unit test

Signed-off-by: svetoslav-nikol0v <[email protected]>

* chore: formatting

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: network tag to 0.48.0

Signed-off-by: svetoslav-nikol0v <[email protected]>

* chore: fix typo

Signed-off-by: svetoslav-nikol0v <[email protected]>

---------

Signed-off-by: svetoslav-nikol0v <[email protected]>
  • Loading branch information
svetoslav-nikol0v authored Apr 15, 2024
1 parent f067bbb commit def492a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
id: start-local-node
if: ${{ steps.build-sdk.conclusion == 'success' && !cancelled() && always() }}
run: |
npx @hashgraph/hedera-local start -d --network local --balance=100000
npx @hashgraph/hedera-local start -d --network-tag=0.48.0 --balance=100000
# Wait for the network to fully start
sleep 30
Expand Down
32 changes: 6 additions & 26 deletions src/token/TokenCreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ export default class TokenCreateTransaction extends Transaction {
* @private
* @type {?Timestamp}
*/
this._expirationTime = null;
this._expirationTime = new Timestamp(
Math.floor(
Date.now() / 1000 + DEFAULT_AUTO_RENEW_PERIOD.toNumber(),
),
0,
);

/**
* @private
Expand Down Expand Up @@ -657,7 +662,6 @@ export default class TokenCreateTransaction extends Transaction {
*/
setExpirationTime(time) {
this._requireNotFrozen();
this._autoRenewPeriod = null;
this._expirationTime =
time instanceof Timestamp ? time : Timestamp.fromDate(time);

Expand Down Expand Up @@ -791,30 +795,6 @@ export default class TokenCreateTransaction extends Transaction {
return this;
}

/**
* @override
* @param {AccountId} accountId
*/
_freezeWithAccountId(accountId) {
super._freezeWithAccountId(accountId);

if (this._autoRenewPeriod != null && accountId != null) {
this._autoRenewAccountId = accountId;
}
}

/**
* @param {?import("../client/Client.js").default<Channel, *>} client
* @returns {this}
*/
freezeWith(client) {
if (client != null && client.operatorAccountId != null) {
this._freezeWithAccountId(client.operatorAccountId);
}

return super.freezeWith(client);
}

/**
* @param {Client} client
*/
Expand Down
106 changes: 94 additions & 12 deletions test/integration/TokenCreateIntegrationTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
PrivateKey,
Status,
Timestamp,
TokenCreateTransaction,
TokenDeleteTransaction,
TokenInfoQuery,
Expand Down Expand Up @@ -59,12 +60,8 @@ describe("TokenCreate", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;
});

Expand Down Expand Up @@ -101,12 +98,8 @@ describe("TokenCreate", function () {
expect(info.defaultFreezeStatus).to.be.null;
expect(info.defaultKycStatus).to.be.null;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;

let err = false;
Expand All @@ -126,6 +119,95 @@ describe("TokenCreate", function () {
}
});

it("when autoRenewAccountId is set", async function () {
this.timeout(120000);

const operatorId = env.operatorId;

const response = await new TokenCreateTransaction()
.setTokenName("ffff")
.setTokenSymbol("F")
.setTreasuryAccountId(operatorId)
.setAutoRenewAccountId(operatorId)
.execute(env.client);

const tokenId = (await response.getReceipt(env.client)).tokenId;

const info = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(env.client);

expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
});

it("when expirationTime is set", async function () {
this.timeout(120000);

const operatorId = env.operatorId;
const DAYS_45_IN_SECONDS = 3888000;
const expirationTime = new Timestamp(
Math.floor(Date.now() / 1000 + DAYS_45_IN_SECONDS),
0,
);

const response = await new TokenCreateTransaction()
.setTokenName("ffff")
.setTokenSymbol("F")
.setTreasuryAccountId(operatorId)
.setExpirationTime(expirationTime)
.execute(env.client);

const tokenId = (await response.getReceipt(env.client)).tokenId;

const info = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(env.client);

expect(info.expirationTime).to.be.not.null;
expect(info.expirationTime.toString()).to.be.eql(
expirationTime.toString(),
);
});

it("when autoRenewAccountId and expirationTime are set", async function () {
this.timeout(120000);

const operatorId = env.operatorId;
const DAYS_90_IN_SECONDS = 7776000;
const expirationTime = new Timestamp(
Math.floor(Date.now() / 1000 + DAYS_90_IN_SECONDS),
0,
);

const response = await new TokenCreateTransaction()
.setTokenName("ffff")
.setTokenSymbol("F")
.setTreasuryAccountId(operatorId)
.setExpirationTime(expirationTime)
.setAutoRenewAccountId(operatorId)
.execute(env.client);

const tokenId = (await response.getReceipt(env.client)).tokenId;

const info = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(env.client);

expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.expirationTime).to.be.not.null;
expect(info.expirationTime.toString()).to.be.eql(
expirationTime.toString(),
);
});

it("should error when token name is not set", async function () {
this.timeout(120000);

Expand Down
16 changes: 4 additions & 12 deletions test/integration/TokenInfoIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ describe("TokenInfo", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;
});

Expand Down Expand Up @@ -101,12 +97,8 @@ describe("TokenInfo", function () {
expect(info.defaultFreezeStatus).to.be.null;
expect(info.defaultKycStatus).to.be.null;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;
});

Expand Down
32 changes: 8 additions & 24 deletions test/integration/TokenUpdateIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ describe("TokenUpdate", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;

await (
Expand Down Expand Up @@ -104,12 +100,8 @@ describe("TokenUpdate", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;
});

Expand Down Expand Up @@ -171,12 +163,8 @@ describe("TokenUpdate", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;

await (
Expand Down Expand Up @@ -219,12 +207,8 @@ describe("TokenUpdate", function () {
expect(info.defaultFreezeStatus).to.be.false;
expect(info.defaultKycStatus).to.be.false;
expect(info.isDeleted).to.be.false;
expect(info.autoRenewAccountId).to.be.not.null;
expect(info.autoRenewAccountId.toString()).to.be.eql(
operatorId.toString(),
);
expect(info.autoRenewPeriod).to.be.not.null;
expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000);
expect(info.autoRenewAccountId).to.be.null;
expect(info.autoRenewPeriod).to.be.null;
expect(info.expirationTime).to.be.not.null;
});

Expand Down
11 changes: 10 additions & 1 deletion test/unit/TokenCreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Timestamp,
} from "../../src/index.js";
import Long from "long";
import { DEFAULT_AUTO_RENEW_PERIOD } from "../../src/transaction/Transaction.js";

describe("TokenCreateTransaction", function () {
it("encodes to correct protobuf", function () {
Expand Down Expand Up @@ -34,6 +35,13 @@ describe("TokenCreateTransaction", function () {
const autoRenewAccountId = new AccountId(10);
const treasuryAccountId = new AccountId(11);

const expirationTime = new Timestamp(
Math.floor(
Date.now() / 1000 + DEFAULT_AUTO_RENEW_PERIOD.toNumber(),
),
0,
);

const transaction = new TokenCreateTransaction()
.setMaxTransactionFee(new Hbar(30))
.setTransactionId(
Expand All @@ -48,6 +56,7 @@ describe("TokenCreateTransaction", function () {
.setDecimals(7)
.setTreasuryAccountId(treasuryAccountId)
.setAutoRenewAccountId(autoRenewAccountId)
.setExpirationTime(expirationTime)
.setAdminKey(key1)
.setKycKey(key2)
.setFreezeKey(key3)
Expand Down Expand Up @@ -77,7 +86,7 @@ describe("TokenCreateTransaction", function () {
autoRenewPeriod: {
seconds: Long.fromValue(7776000),
},
expiry: null,
expiry: expirationTime._toProtobuf(),
treasury: treasuryAccountId._toProtobuf(),
adminKey: {
ed25519: key1.publicKey.toBytesRaw(),
Expand Down

0 comments on commit def492a

Please sign in to comment.