Skip to content

Commit

Permalink
Add delete nft allowances and add delegate spender functionality (#1452)
Browse files Browse the repository at this point in the history
* small refactoring

Signed-off-by: Petar Tonev <[email protected]>

* Fix/update mirror endpoints (#1448)

* Fix _maxAutomaticTokenAssociations in contract create and contract up… (#1444)

* Fix _maxAutomaticTokenAssociations in contract create and contract update transactions

Signed-off-by: ochikov <[email protected]>

* set the fields default to null

Signed-off-by: ochikov <[email protected]>

---------

Signed-off-by: ochikov <[email protected]>

* Update the mirror node endpoints

Signed-off-by: ochikov <[email protected]>

---------

Signed-off-by: ochikov <[email protected]>

* add missing functionalities from hip-336 nft allowances

Signed-off-by: Petar Tonev <[email protected]>

* small description typo

Signed-off-by: Petar Tonev <[email protected]>

---------

Signed-off-by: Petar Tonev <[email protected]>
Signed-off-by: ochikov <[email protected]>
Co-authored-by: ochikov <[email protected]>
  • Loading branch information
petreze and ochikov authored Feb 8, 2023
1 parent 5448744 commit a8d914e
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/get-address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function main() {

if (process.env.HEDERA_NETWORK.toLowerCase() === "mainnet") {
client
.setMirrorNetwork(["mainnet-public.mirrornode.hedera.com:5600"])
.setMirrorNetwork(["mainnet-public.mirrornode.hedera.com:443"])
.setTransportSecurity(true);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/proto/src/proto
2 changes: 1 addition & 1 deletion scripts/update-address-books.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function main() {
const networks = [
{ name: "previewnet" },
{ name: "testnet" },
{ name: "mainnet", url: "mainnet-public.mirrornode.hedera.com:5600" },
{ name: "mainnet", url: "mainnet-public.mirrornode.hedera.com:443" },
];

for (const network of networks) {
Expand Down
2 changes: 2 additions & 0 deletions src/account/AccountAllowanceAdjustTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export default class AccountAllowanceAdjustTransaction extends Transaction {
serialNumbers: [nftId.serial],
ownerAccountId: owner,
allSerials: false,
delegatingSpender: null,
})
);
}
Expand Down Expand Up @@ -438,6 +439,7 @@ export default class AccountAllowanceAdjustTransaction extends Transaction {
: spenderAccountId,
serialNumbers: null,
allSerials,
delegatingSpender: null,
})
);

Expand Down
76 changes: 65 additions & 11 deletions src/account/AccountAllowanceApproveTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
}

/**
* @deprecated
* @deprecated - Use `approveHbarAllowance()` instead
* @param {AccountId | string} spenderAccountId
* @param {number | string | Long | LongObject | BigNumber | Hbar} amount
* @returns {AccountAllowanceApproveTransaction}
Expand Down Expand Up @@ -227,7 +227,7 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
}

/**
* @deprecated
* @deprecated - Use `approveTokenAllowance()` instead
* @param {TokenId | string} tokenId
* @param {AccountId | string} spenderAccountId
* @param {Long | number} amount
Expand Down Expand Up @@ -258,13 +258,18 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
}

/**
* @deprecated
* @deprecated - Use `approveTokenNftAllowance()` instead
* @param {NftId | string} nftId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceApproveTransaction}
*/
addTokenNftAllowance(nftId, spenderAccountId) {
return this._approveTokenNftAllowance(nftId, null, spenderAccountId);
return this._approveTokenNftAllowance(
nftId,
null,
spenderAccountId,
null
);
}

/**
Expand All @@ -278,9 +283,15 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
* @param {NftId | string} nftId
* @param {AccountId | string | null} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {AccountId | string | null} delegatingSpender
* @returns {AccountAllowanceApproveTransaction}
*/
_approveTokenNftAllowance(nftId, ownerAccountId, spenderAccountId) {
_approveTokenNftAllowance(
nftId,
ownerAccountId,
spenderAccountId,
delegatingSpender
) {
this._requireNotFrozen();

const id = typeof nftId === "string" ? NftId.fromString(nftId) : nftId;
Expand Down Expand Up @@ -308,16 +319,17 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
this._nftApprovals.push(
new TokenNftAllowance({
tokenId: id.tokenId,
spenderAccountId:
typeof spenderAccountId === "string"
? AccountId.fromString(spenderAccountId)
: spenderAccountId,
spenderAccountId: spender,
ownerAccountId:
typeof ownerAccountId === "string"
? AccountId.fromString(ownerAccountId)
: ownerAccountId,
serialNumbers: [id.serial],
allSerials: false,
delegatingSpender:
typeof delegatingSpender === "string"
? AccountId.fromString(delegatingSpender)
: delegatingSpender,
})
);
}
Expand All @@ -335,7 +347,29 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
return this._approveTokenNftAllowance(
nftId,
ownerAccountId,
spenderAccountId
spenderAccountId,
null
);
}

/**
* @param {NftId | string} nftId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {AccountId | string} delegatingSpender
* @returns {AccountAllowanceApproveTransaction}
*/
approveTokenNftAllowanceWithDelegatingSpender(
nftId,
ownerAccountId,
spenderAccountId,
delegatingSpender
) {
return this._approveTokenNftAllowance(
nftId,
ownerAccountId,
spenderAccountId,
delegatingSpender
);
}

Expand Down Expand Up @@ -370,14 +404,15 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
: ownerAccountId,
serialNumbers: null,
allSerials,
delegatingSpender: null,
})
);

return this;
}

/**
* @deprecated
* @deprecated - Use `approveTokenNftAllowanceAllSerials()` instead
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
Expand Down Expand Up @@ -411,6 +446,25 @@ export default class AccountAllowanceApproveTransaction extends Transaction {
);
}

/**
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceApproveTransaction}
*/
deleteTokenNftAllowanceAllSerials(
tokenId,
ownerAccountId,
spenderAccountId
) {
return this._approveAllTokenNftAllowance(
tokenId,
ownerAccountId,
spenderAccountId,
false
);
}

/**
* @param {Client} client
*/
Expand Down
3 changes: 3 additions & 0 deletions src/account/AccountAllowanceDeleteTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export default class AccountAllowanceDeleteTransaction extends Transaction {
}

/**
* @description If you want to remove allowance for all serials of a NFT
* - use AccountAllowanceApproveTransaction().deleteTokenNftAllowanceAllSerials()
* @param {NftId | string} nftId
* @param {AccountId | string} ownerAccountId
* @returns {AccountAllowanceDeleteTransaction}
Expand Down Expand Up @@ -147,6 +149,7 @@ export default class AccountAllowanceDeleteTransaction extends Transaction {
serialNumbers: [id.serial],
ownerAccountId: owner,
allSerials: false,
delegatingSpender: null,
})
);
}
Expand Down
23 changes: 23 additions & 0 deletions src/account/TokenNftAllowance.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class TokenNftAllowance {
* @param {AccountId | null} props.ownerAccountId
* @param {Long[] | null} props.serialNumbers
* @param {boolean | null} props.allSerials
* @param {AccountId | null} props.delegatingSpender
*/
constructor(props) {
/**
Expand Down Expand Up @@ -81,6 +82,14 @@ export default class TokenNftAllowance {
*/
this.allSerials = props.allSerials;

/**
* The account ID of the spender who is granted approvedForAll allowance and granting
* approval on an NFT serial to another spender.
*
* @readonly
*/
this.delegatingSpender = props.delegatingSpender;

Object.freeze(this);
}

Expand Down Expand Up @@ -121,6 +130,14 @@ export default class TokenNftAllowance {
)
: [],
allSerials,
delegatingSpender:
allowance.delegatingSpender != null
? AccountId._fromProtobuf(
/**@type {HashgraphProto.proto.IAccountID}*/ (
allowance.delegatingSpender
)
)
: null,
});
}

Expand All @@ -143,6 +160,7 @@ export default class TokenNftAllowance {
ownerAccountId,
serialNumbers: [],
allSerials: null,
delegatingSpender: null,
});
}

Expand Down Expand Up @@ -172,6 +190,7 @@ export default class TokenNftAllowance {
)
: [],
allSerials: null,
delegatingSpender: null,
});
}

Expand All @@ -193,6 +212,10 @@ export default class TokenNftAllowance {
approvedForAll:
this.serialNumbers == null ? { value: this.allSerials } : null,
serialNumbers: this.serialNumbers,
delegatingSpender:
this.delegatingSpender != null
? this.delegatingSpender._toProtobuf()
: null,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/NodeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export const MirrorNetwork = {
},

MAINNET: ["mainnet-public.mirrornode.hedera.com:443"],
TESTNET: ["hcs.testnet.mirrornode.hedera.com:5600"],
PREVIEWNET: ["hcs.previewnet.mirrornode.hedera.com:5600"],
TESTNET: ["testnet.mirrornode.hedera.com:443"],
PREVIEWNET: ["previewnet.mirrornode.hedera.com:443"],
LOCAL_NODE: ["127.0.0.1:5600"],
};

Expand Down
3 changes: 3 additions & 0 deletions test/unit/AccountAllowanceApproveTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ describe("AccountAllowanceApproveTransaction", function () {
spender: spenderAccountId1._toProtobuf(),
tokenId: tokenId2._toProtobuf(),
approvedForAll: null,
delegatingSpender: null,
},
{
owner: ownerAccountId._toProtobuf(),
serialNumbers: [serialNumber2],
spender: spenderAccountId2._toProtobuf(),
tokenId: tokenId2._toProtobuf(),
approvedForAll: null,
delegatingSpender: null,
},
{
owner: ownerAccountId._toProtobuf(),
serialNumbers: null,
spender: spenderAccountId1._toProtobuf(),
tokenId: tokenId1._toProtobuf(),
approvedForAll: { value: true },
delegatingSpender: null,
},
],
tokenAllowances: [
Expand Down
16 changes: 8 additions & 8 deletions test/unit/ManagedNodeAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ describe("ManagedNodeAddress", function () {
);

const mirrorNodeAddress = ManagedNodeAddress.fromString(
"hcs.testnet.mirrornode.hedera.com:5600"
"testnet.mirrornode.hedera.com:443"
);
expect(mirrorNodeAddress.address).to.be.equal(
"hcs.testnet.mirrornode.hedera.com"
"testnet.mirrornode.hedera.com"
);
expect(mirrorNodeAddress.port).to.be.equal(5600);
expect(mirrorNodeAddress.port).to.be.equal(443);
expect(mirrorNodeAddress.toString()).to.be.equal(
"hcs.testnet.mirrornode.hedera.com:5600"
"testnet.mirrornode.hedera.com:443"
);

const mirrorNodeAddressSecure = mirrorNodeAddress.toSecure();
expect(mirrorNodeAddressSecure.address).to.be.equal(
"hcs.testnet.mirrornode.hedera.com"
"testnet.mirrornode.hedera.com"
);
expect(mirrorNodeAddressSecure.port).to.be.equal(443);
expect(mirrorNodeAddressSecure.toString()).to.be.equal(
"hcs.testnet.mirrornode.hedera.com:443"
"testnet.mirrornode.hedera.com:443"
);

const mirrorNodeAddressInsecure = mirrorNodeAddressSecure.toInsecure();
expect(mirrorNodeAddressInsecure.address).to.be.equal(
"hcs.testnet.mirrornode.hedera.com"
"testnet.mirrornode.hedera.com"
);
expect(mirrorNodeAddressInsecure.port).to.be.equal(5600);
expect(mirrorNodeAddressInsecure.toString()).to.be.equal(
"hcs.testnet.mirrornode.hedera.com:5600"
"testnet.mirrornode.hedera.com:5600"
);

let err = false;
Expand Down
8 changes: 4 additions & 4 deletions test/unit/NodeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe("Client", function () {
});

it("should correctly construct and update mirror network", async function () {
let nodes = ["hcs.testnet.mirrornode.hedera.com:5600"];
let nodes = ["testnet.mirrornode.hedera.com:443"];

const client = Client.forNetwork(
{},
Expand All @@ -157,14 +157,14 @@ describe("Client", function () {
let network = client.mirrorNetwork;

expect(network.length).to.be.equal(1);
expect(network.includes("hcs.testnet.mirrornode.hedera.com:5600")).to.be
expect(network.includes("testnet.mirrornode.hedera.com:443")).to.be
.true;

client.setMirrorNetwork(nodes);
network = client.mirrorNetwork;

expect(network.length).to.be.equal(1);
expect(network.includes("hcs.testnet.mirrornode.hedera.com:5600")).to.be
expect(network.includes("testnet.mirrornode.hedera.com:443")).to.be
.true;

nodes.push("hcs.testnet1.mirrornode.hedera.com:5600");
Expand All @@ -173,7 +173,7 @@ describe("Client", function () {
network = client.mirrorNetwork;

expect(network.length).to.be.equal(2);
expect(network.includes("hcs.testnet.mirrornode.hedera.com:5600")).to.be
expect(network.includes("testnet.mirrornode.hedera.com:443")).to.be
.true;
expect(network.includes("hcs.testnet1.mirrornode.hedera.com:5600")).to
.be.true;
Expand Down
Loading

0 comments on commit a8d914e

Please sign in to comment.