From 98ca2580cdd22388f09a061a11a0a72df9b73e65 Mon Sep 17 00:00:00 2001 From: Luca Nicola Debiasi <63785793+lucanicoladebiasi@users.noreply.github.com> Date: Wed, 29 Jan 2025 10:56:10 +0000 Subject: [PATCH 1/3] docs: fix VEA_01 (#1769) --- .../src/provider/providers/vechain-provider/vechain-provider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts b/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts index c5bc3263e..f632b6784 100644 --- a/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts +++ b/packages/network/src/provider/providers/vechain-provider/vechain-provider.ts @@ -254,7 +254,7 @@ class VeChainProvider extends EventEmitter implements EIP1193ProviderMessage { this.subscriptionManager.currentBlockNumber ); - // If the block is successfully fetched (not undefined or null), update the result and increment the block number + // If the block is successfully fetched (not undefined or null), update the result. if (block !== undefined && block !== null) { result = block; // Set the fetched block as the result } From 631274e942b9b17d98fa9c725b8cafd0faee0fe3 Mon Sep 17 00:00:00 2001 From: Clayton Neal Date: Wed, 29 Jan 2025 17:37:25 +0000 Subject: [PATCH 2/3] chore: backport (#1770) --- docs/contracts.md | 3 +- docs/examples/contracts/contract-clauses.ts | 3 +- .../examples/transactions/multiple-clauses.ts | 3 +- docs/transactions.md | 3 +- packages/core/src/transaction/Clause.ts | 24 ++++---- .../tests/transaction/Clause.unit.test.ts | 55 ++++++++----------- .../hardhat-provider/hardhat-provider.ts | 2 +- .../vechain-abstract-signer.ts | 4 +- 8 files changed, 40 insertions(+), 57 deletions(-) diff --git a/docs/contracts.md b/docs/contracts.md index 7906a8d5b..bed3d6cce 100644 --- a/docs/contracts.md +++ b/docs/contracts.md @@ -37,8 +37,7 @@ const transferVetClause = Clause.transferVET( // 2. Transfer VTHO -const transferVTHOClause = Clause.transferToken( - Address.of(VTHO_ADDRESS), +const transferVTHOClause = Clause.transferVTHOToken( Address.of('0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54'), VTHO.of(300n, Units.wei) ); diff --git a/docs/examples/contracts/contract-clauses.ts b/docs/examples/contracts/contract-clauses.ts index 7bd603675..ab157b999 100644 --- a/docs/examples/contracts/contract-clauses.ts +++ b/docs/examples/contracts/contract-clauses.ts @@ -18,8 +18,7 @@ const transferVetClause = Clause.transferVET( // 2. Transfer VTHO -const transferVTHOClause = Clause.transferToken( - Address.of(VTHO_ADDRESS), +const transferVTHOClause = Clause.transferVTHOToken( Address.of('0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54'), VTHO.of(300n, Units.wei) ); diff --git a/docs/examples/transactions/multiple-clauses.ts b/docs/examples/transactions/multiple-clauses.ts index 3cfd258e6..d024e3a11 100644 --- a/docs/examples/transactions/multiple-clauses.ts +++ b/docs/examples/transactions/multiple-clauses.ts @@ -21,8 +21,7 @@ const clauses: TransactionClause[] = [ Address.of('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed'), VET.of(10000) ) as TransactionClause, - Clause.transferToken( - Address.of(VTHO_ADDRESS), + Clause.transferVTHOToken( Address.of('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed'), VTHO.of(10000) ) as TransactionClause diff --git a/docs/transactions.md b/docs/transactions.md index 26bbcfa32..ace8a8231 100644 --- a/docs/transactions.md +++ b/docs/transactions.md @@ -74,8 +74,7 @@ const clauses: TransactionClause[] = [ Address.of('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed'), VET.of(10000) ) as TransactionClause, - Clause.transferToken( - Address.of(VTHO_ADDRESS), + Clause.transferVTHOToken( Address.of('0x7567d83b7b8d80addcb281a71d54fc7b3364ffed'), VTHO.of(10000) ) as TransactionClause diff --git a/packages/core/src/transaction/Clause.ts b/packages/core/src/transaction/Clause.ts index 850c05865..3ad5b9db0 100644 --- a/packages/core/src/transaction/Clause.ts +++ b/packages/core/src/transaction/Clause.ts @@ -3,10 +3,10 @@ import { ERC721_ABI, VIP180_ABI } from '../utils'; import { ABI, ABIContract, + Address, FixedPointNumber, VET, type ABIFunction, - type Address, type HexUInt, type VTHO } from '../vcdm'; @@ -15,6 +15,7 @@ import { HexInt } from '../vcdm/HexInt'; import type { ClauseOptions } from './ClauseOptions'; import type { DeployParams } from './DeployParams'; import type { TransactionClause } from './TransactionClause'; +import { VTHO_ADDRESS } from '../utils/const/network'; /** * This class represent a transaction clause. @@ -43,7 +44,7 @@ class Clause implements TransactionClause { private static readonly TRANSFER_NFT_FUNCTION = 'transferFrom'; /** - * Used internally in {@link Clause.transferToken} method. + * Used internally in {@link Clause.transferVTHOToken} method. */ private static readonly TRANSFER_TOKEN_FUNCTION = 'transfer'; @@ -59,7 +60,7 @@ class Clause implements TransactionClause { * token in {@link Units.wei} to transfer to the destination. * * @see {Clause.callFunction} - * @see {Clause.transferToken} + * @see {Clause.transferVTHOToken} * @see {Clause.transferVET} */ readonly value: string; @@ -215,9 +216,7 @@ class Clause implements TransactionClause { } /** - * Return a new clause to transfers the specified amount of - * [VIP180](https://docs.vechain.org/introduction-to-vechain/dual-token-economic-model/vethor-vtho#vip180-vechains-fungible-token-standard) - * token. + * Return a new clause to transfers the specified amount of VTHO * * @param {Address} tokenAddress - The address of the VIP180 token. * @param {Address} recipientAddress - The address of the recipient. @@ -228,25 +227,24 @@ class Clause implements TransactionClause { * * @see VTHO.transferTokenTo */ - public static transferToken( - tokenAddress: Address, + public static transferVTHOToken( recipientAddress: Address, - amount: VTHO, - clauseOptions?: ClauseOptions + amount: VTHO ): Clause { if (amount.value.isFinite() && amount.value.isPositive()) { + const vthoAddress = Address.of(VTHO_ADDRESS); return this.callFunction( - tokenAddress, + vthoAddress, ABIContract.ofAbi(VIP180_ABI).getFunction( Clause.TRANSFER_TOKEN_FUNCTION ), [recipientAddress.toString(), amount.wei], undefined, - clauseOptions + { comment: 'Transfer VTHO' } ); } throw new InvalidDataType( - 'Clause.transferToken', + 'Clause.transferVTHOToken', 'not positive integer amount', { amount: `${amount.value}` } ); diff --git a/packages/core/tests/transaction/Clause.unit.test.ts b/packages/core/tests/transaction/Clause.unit.test.ts index c7e7a8d5c..d7208322f 100644 --- a/packages/core/tests/transaction/Clause.unit.test.ts +++ b/packages/core/tests/transaction/Clause.unit.test.ts @@ -11,7 +11,8 @@ import { type ClauseOptions, type DeployParams, type TransactionClause, - ABIContract + ABIContract, + VTHO_ADDRESS } from '../../src'; const ClauseFixture = { @@ -195,20 +196,20 @@ describe('Clause class tests', () => { }); }); - describe('transferToken method tests', () => { + describe('transferVTHOToken method tests', () => { test('Return Clause <- 1 wei VTHO', () => { const expected = { - to: ClauseFixture.token.address.toString().toLowerCase(), + to: Address.of(VTHO_ADDRESS).toString().toLowerCase(), value: `0x0`, data: `0xa9059cbb000000000000000000000000${ClauseFixture.to .toString() .toLowerCase() .slice( 2 - )}0000000000000000000000000000000000000000000000000000000000000001` + )}0000000000000000000000000000000000000000000000000000000000000001`, + comment: 'Transfer VTHO' } satisfies TransactionClause; - const actual = Clause.transferToken( - ClauseFixture.token.address, + const actual = Clause.transferVTHOToken( ClauseFixture.to, VTHO.of(1, Units.wei) ); @@ -217,17 +218,17 @@ describe('Clause class tests', () => { test('Return Clause <- 100 wei VTHO', () => { const expected = { - to: ClauseFixture.token.address.toString().toLowerCase(), + to: Address.of(VTHO_ADDRESS).toString().toLowerCase(), value: `0x0`, data: `0xa9059cbb000000000000000000000000${ClauseFixture.to .toString() .toLowerCase() .slice( 2 - )}0000000000000000000000000000000000000000000000000000000000000064` + )}0000000000000000000000000000000000000000000000000000000000000064`, + comment: 'Transfer VTHO' } satisfies TransactionClause; - const actual = Clause.transferToken( - ClauseFixture.token.address, + const actual = Clause.transferVTHOToken( ClauseFixture.to, VTHO.of(0.1, Units.kwei) ); @@ -236,17 +237,17 @@ describe('Clause class tests', () => { test('Return Clause <- 1 VTHO', () => { const expected = { - to: ClauseFixture.token.address.toString().toLowerCase(), + to: Address.of(VTHO_ADDRESS).toString().toLowerCase(), value: `0x0`, data: `0xa9059cbb000000000000000000000000${ClauseFixture.to .toString() .toLowerCase() .slice( 2 - )}0000000000000000000000000000000000000000000000000de0b6b3a7640000` + )}0000000000000000000000000000000000000000000000000de0b6b3a7640000`, + comment: 'Transfer VTHO' } satisfies TransactionClause; - const actual = Clause.transferToken( - ClauseFixture.token.address, + const actual = Clause.transferVTHOToken( ClauseFixture.to, VTHO.of(1) ); @@ -255,17 +256,17 @@ describe('Clause class tests', () => { test('Return Clause <- 500000000 VTHO', () => { const expected = { - to: ClauseFixture.token.address.toString().toLowerCase(), + to: Address.of(VTHO_ADDRESS).toString().toLowerCase(), value: `0x0`, data: `0xa9059cbb000000000000000000000000${ClauseFixture.to .toString() .toLowerCase() .slice( 2 - )}0000000000000000000000000000000000000000019d971e4fe8401e74000000` + )}0000000000000000000000000000000000000000019d971e4fe8401e74000000`, + comment: 'Transfer VTHO' } satisfies TransactionClause; - const actual = Clause.transferToken( - ClauseFixture.token.address, + const actual = Clause.transferVTHOToken( ClauseFixture.to, VTHO.of(500000000n) ); @@ -274,31 +275,19 @@ describe('Clause class tests', () => { test('Throw error <- negative amount VTHO', () => { expect(() => { - Clause.transferToken( - ClauseFixture.token.address, - ClauseFixture.to, - VTHO.of(-100) - ); + Clause.transferVTHOToken(ClauseFixture.to, VTHO.of(-100)); }).toThrow(InvalidDataType); }); test('Throw <- infinite amount VTHO', () => { expect(() => { - Clause.transferToken( - ClauseFixture.token.address, - ClauseFixture.to, - VTHO.of(Infinity) - ); + Clause.transferVTHOToken(ClauseFixture.to, VTHO.of(Infinity)); }).toThrow(InvalidDataType); }); test('Throw <- NaN amount VTHO', () => { expect(() => { - Clause.transferToken( - ClauseFixture.token.address, - ClauseFixture.to, - VTHO.of(NaN) - ); + Clause.transferVTHOToken(ClauseFixture.to, VTHO.of(NaN)); }).toThrow(InvalidDataType); }); }); diff --git a/packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts b/packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts index 6c4813d67..af22fd9a2 100644 --- a/packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts +++ b/packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts @@ -77,7 +77,7 @@ class HardhatVeChainProvider extends VeChainProvider { } /** - * Overload off the send method + * Overload of the send method * * @param method - The method to call. * @param params - The parameters to pass to the method. diff --git a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts index 4ceb062ba..a51fd591e 100644 --- a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts +++ b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts @@ -195,8 +195,8 @@ abstract class VeChainAbstractSigner implements VeChainSigner { /** * Estimates the required gas required to execute //tx// on the Blockchain. This - * will be the expected amount a transaction will require - * to successfully run all the necessary computations and store the needed state + * will be the expected amount a transaction will need + * to successfully run all the necessary computations and store the changed state * that the transaction intends. * * @param transactionToEstimate - The transaction to estimate gas for From 3eecea9b3dc55c8287b675cc933515d5732e67b2 Mon Sep 17 00:00:00 2001 From: Luca Nicola Debiasi <63785793+lucanicoladebiasi@users.noreply.github.com> Date: Thu, 30 Jan 2025 19:51:33 +0000 Subject: [PATCH 3/3] 1658 bnc - part 1 (#1777) * fix: fix BNC... * fix: fix BNC... * fix: fix BNC... * fix: fix BNC... * fix: fix BNC... * fix: fix BNC... --- docs/contracts.md | 12 ++-- .../contracts/contract-delegation-ERC20.ts | 16 ++--- docs/examples/transactions/fee-delegation.ts | 8 +-- .../full-flow-delegator-private-key.ts | 9 +-- .../transactions/full-flow-delegator-url.ts | 9 +-- docs/templates/contracts.md | 2 +- docs/transactions.md | 20 +++--- .../transaction/Transaction.unit.test.ts | 72 +++++++++---------- .../transaction/transaction.ts | 2 +- .../available-errors/transaction.unit.test.ts | 6 +- .../vechain-abstract-signer.ts | 2 +- .../vechain-private-key-signer.ts | 40 +++++------ .../helpers/delegation-handler.ts | 2 +- 13 files changed, 96 insertions(+), 104 deletions(-) diff --git a/docs/contracts.md b/docs/contracts.md index bed3d6cce..df382fea5 100644 --- a/docs/contracts.md +++ b/docs/contracts.md @@ -202,22 +202,21 @@ You can specify revisions (`best` or `finalized`) for read functions, similar to ## Delegating a Contract Call -VeChain supports delegated contract calls where fees are paid by the delegator. +VeChain supports delegated contract calls where fees are paid by the gas-payer. ```typescript { name=contract-delegation-erc20, category=example } const thorSoloClient = ThorClient.at(THOR_SOLO_URL); const provider = new VeChainProvider( thorSoloClient, new ProviderInternalBaseWallet([deployerAccount], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorPrivateKey: delegatorAccount.privateKey + delegatorPrivateKey: gasPayerAccount.privateKey } }), true ); -const signer = (await provider.getSigner( - deployerAccount.address -)) as VeChainSigner; +const signer = await provider.getSigner(deployerAccount.address); // Defining a function for deploying the ERC20 contract const setupERC20Contract = async (): Promise> => { @@ -244,8 +243,7 @@ const transferResult = await contract.transact.transfer( ); // Wait for the transfer transaction to complete and obtain its receipt -const transactionReceiptTransfer = - (await transferResult.wait()) as TransactionReceipt; +const transactionReceiptTransfer = await transferResult.wait(); // Asserting that the transaction has not been reverted expect(transactionReceiptTransfer.reverted).toEqual(false); diff --git a/docs/examples/contracts/contract-delegation-ERC20.ts b/docs/examples/contracts/contract-delegation-ERC20.ts index 6138b99b7..d5495c84e 100644 --- a/docs/examples/contracts/contract-delegation-ERC20.ts +++ b/docs/examples/contracts/contract-delegation-ERC20.ts @@ -1,4 +1,4 @@ -import { ERC20_ABI, Hex, HexUInt } from '@vechain/sdk-core'; +import { ERC20_ABI, HexUInt } from '@vechain/sdk-core'; import { type Contract, ProviderInternalBaseWallet, @@ -23,8 +23,8 @@ const deployerAccount: ProviderInternalWalletAccount = { address: '0xf02f557c753edf5fcdcbfe4c1c3a448b3cc84d54' }; -// Defining the delegator account, which has VTHO for transaction costs -const delegatorAccount = { +// Defining the gas-payer account, which has VTHO for transaction costs +const gasPayerAccount = { privateKey: '521b7793c6eb27d137b617627c6b85d57c0aa303380e9ca4e30a30302fbc6676', address: '0x062F167A905C1484DE7e75B88EDC7439f82117DE' @@ -36,15 +36,14 @@ const thorSoloClient = ThorClient.at(THOR_SOLO_URL); const provider = new VeChainProvider( thorSoloClient, new ProviderInternalBaseWallet([deployerAccount], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorPrivateKey: delegatorAccount.privateKey + delegatorPrivateKey: gasPayerAccount.privateKey } }), true ); -const signer = (await provider.getSigner( - deployerAccount.address -)) as VeChainSigner; +const signer = await provider.getSigner(deployerAccount.address); // Defining a function for deploying the ERC20 contract const setupERC20Contract = async (): Promise> => { @@ -71,8 +70,7 @@ const transferResult = await contract.transact.transfer( ); // Wait for the transfer transaction to complete and obtain its receipt -const transactionReceiptTransfer = - (await transferResult.wait()) as TransactionReceipt; +const transactionReceiptTransfer = await transferResult.wait(); // Asserting that the transaction has not been reverted expect(transactionReceiptTransfer.reverted).toEqual(false); diff --git a/docs/examples/transactions/fee-delegation.ts b/docs/examples/transactions/fee-delegation.ts index 913eaa923..851ab06e0 100644 --- a/docs/examples/transactions/fee-delegation.ts +++ b/docs/examples/transactions/fee-delegation.ts @@ -61,17 +61,17 @@ const body: TransactionBody = { // 4 - Create private keys of sender and delegate const nodeDelegate = HDKey.fromMnemonic(Mnemonic.of()); -const delegatorPrivateKey = nodeDelegate.privateKey; +const gasPayerPrivateKey = nodeDelegate.privateKey; // 5 - Get address of delegate -const delegatorAddress = Address.ofPublicKey(nodeDelegate.publicKey).toString(); +const gasPayerAddress = Address.ofPublicKey(nodeDelegate.publicKey).toString(); // 6 - Sign transaction as sender and delegate const signedTransaction = Transaction.of(body).signAsSenderAndGasPayer( HexUInt.of(senderAccount.privateKey).bytes, - HexUInt.of(delegatorPrivateKey).bytes + HexUInt.of(gasPayerPrivateKey).bytes ); // 7 - Encode transaction @@ -85,4 +85,4 @@ const decodedTx = Transaction.decode(encodedRaw, true); // END_SNIPPET: FeeDelegationSnippet expect(decodedTx.isDelegated).toBeTruthy(); -expect(decodedTx.gasPayer.toString()).toBe(delegatorAddress); +expect(decodedTx.gasPayer.toString()).toBe(gasPayerAddress); diff --git a/docs/examples/transactions/full-flow-delegator-private-key.ts b/docs/examples/transactions/full-flow-delegator-private-key.ts index 99b7c978a..0187ec338 100644 --- a/docs/examples/transactions/full-flow-delegator-private-key.ts +++ b/docs/examples/transactions/full-flow-delegator-private-key.ts @@ -29,8 +29,8 @@ const senderAccount: { privateKey: string; address: string } = { address: '0x7a28e7361fd10f4f058f9fefc77544349ecff5d6' }; -// Delegator account with private key -const delegatorAccount: { privateKey: string; address: string } = { +// Gas-payer account with private key +const gasPayerAccount: { privateKey: string; address: string } = { privateKey: '521b7793c6eb27d137b617627c6b85d57c0aa303380e9ca4e30a30302fbc6676', address: '0x062F167A905C1484DE7e75B88EDC7439f82117DE' @@ -50,8 +50,9 @@ const providerWithDelegationEnabled = new VeChainProvider( } ], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorPrivateKey: delegatorAccount.privateKey + delegatorPrivateKey: gasPayerAccount.privateKey } } ), @@ -119,7 +120,7 @@ const txReceipt = await thorSoloClient.transactions.waitForTransaction( // Check the signed transaction expect(delegatedSigned.isSigned).toEqual(true); expect(delegatedSigned.isDelegated).toEqual(true); -expect(delegatedSigned.gasPayer.toString()).toEqual(delegatorAccount.address); +expect(delegatedSigned.gasPayer.toString()).toEqual(gasPayerAccount.address); // Check the transaction receipt expect(txReceipt).toBeDefined(); diff --git a/docs/examples/transactions/full-flow-delegator-url.ts b/docs/examples/transactions/full-flow-delegator-url.ts index 71822faff..453414efe 100644 --- a/docs/examples/transactions/full-flow-delegator-url.ts +++ b/docs/examples/transactions/full-flow-delegator-url.ts @@ -35,8 +35,8 @@ const senderAccount: { address: '0x571E3E1fBE342891778151f037967E107fb89bd0' }; -// Delegator account with private key -const delegatorAccount = { +// Gas-payer account with private key +const gasPayerAccount = { URL: 'https://sponsor-testnet.vechain.energy/by/269' }; @@ -54,8 +54,9 @@ const providerWithDelegationEnabled = new VeChainProvider( } ], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorUrl: delegatorAccount.URL + delegatorUrl: gasPayerAccount.URL } } ), @@ -123,7 +124,7 @@ const txReceipt = await thorClient.transactions.waitForTransaction( // Check the signed transaction expect(delegatedSigned.isSigned).toEqual(true); expect(delegatedSigned.isDelegated).toEqual(true); -// expect(signedTx.delegator).toEqual(delegatorAccount.address); --- +// expect(signedTx.delegator).toEqual(gasPayerAccount.address); --- // Check the transaction receipt expect(txReceipt).toBeDefined(); diff --git a/docs/templates/contracts.md b/docs/templates/contracts.md index 76b5a70f5..ca7a16c46 100644 --- a/docs/templates/contracts.md +++ b/docs/templates/contracts.md @@ -70,6 +70,6 @@ You can specify revisions (`best` or `finalized`) for read functions, similar to ## Delegating a Contract Call -VeChain supports delegated contract calls where fees are paid by the delegator. +VeChain supports delegated contract calls where fees are paid by the gas-payer. [ERC20FunctionCallDelegatedSnippet](examples/contracts/contract-delegation-ERC20.ts) diff --git a/docs/transactions.md b/docs/transactions.md index ace8a8231..524571b13 100644 --- a/docs/transactions.md +++ b/docs/transactions.md @@ -163,17 +163,17 @@ const body: TransactionBody = { // 4 - Create private keys of sender and delegate const nodeDelegate = HDKey.fromMnemonic(Mnemonic.of()); -const delegatorPrivateKey = nodeDelegate.privateKey; +const gasPayerPrivateKey = nodeDelegate.privateKey; // 5 - Get address of delegate -const delegatorAddress = Address.ofPublicKey(nodeDelegate.publicKey).toString(); +const gasPayerAddress = Address.ofPublicKey(nodeDelegate.publicKey).toString(); // 6 - Sign transaction as sender and delegate const signedTransaction = Transaction.of(body).signAsSenderAndGasPayer( HexUInt.of(senderAccount.privateKey).bytes, - HexUInt.of(delegatorPrivateKey).bytes + HexUInt.of(gasPayerPrivateKey).bytes ); // 7 - Encode transaction @@ -463,8 +463,8 @@ const senderAccount: { privateKey: string; address: string } = { address: '0x7a28e7361fd10f4f058f9fefc77544349ecff5d6' }; -// Delegator account with private key -const delegatorAccount: { privateKey: string; address: string } = { +// Gas-payer account with private key +const gasPayerAccount: { privateKey: string; address: string } = { privateKey: '521b7793c6eb27d137b617627c6b85d57c0aa303380e9ca4e30a30302fbc6676', address: '0x062F167A905C1484DE7e75B88EDC7439f82117DE' @@ -484,8 +484,9 @@ const providerWithDelegationEnabled = new VeChainProvider( } ], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorPrivateKey: delegatorAccount.privateKey + delegatorPrivateKey: gasPayerAccount.privateKey } } ), @@ -570,8 +571,8 @@ const senderAccount: { address: '0x571E3E1fBE342891778151f037967E107fb89bd0' }; -// Delegator account with private key -const delegatorAccount = { +// Gas-payer account with private key +const gasPayerAccount = { URL: 'https://sponsor-testnet.vechain.energy/by/269' }; @@ -589,8 +590,9 @@ const providerWithDelegationEnabled = new VeChainProvider( } ], { + // The term `delegator` will be deprecated soon and renamed `gasPayer`. delegator: { - delegatorUrl: delegatorAccount.URL + delegatorUrl: gasPayerAccount.URL } } ), diff --git a/packages/core/tests/transaction/Transaction.unit.test.ts b/packages/core/tests/transaction/Transaction.unit.test.ts index ea092f77b..b70fbd904 100644 --- a/packages/core/tests/transaction/Transaction.unit.test.ts +++ b/packages/core/tests/transaction/Transaction.unit.test.ts @@ -17,13 +17,13 @@ import { VTHO } from '../../src'; -const DelegatorPrivateKeyFix = HexUInt.of( +const GasPayerPrivateKeyFix = HexUInt.of( '40de805e918403683fb9a6081c3fba072cdc5c88232c62a9509165122488dab7' ).bytes; -const DelegatorFix = { - privateKey: DelegatorPrivateKeyFix, - address: Address.ofPrivateKey(DelegatorPrivateKeyFix) +const GasPayerFix = { + privateKey: GasPayerPrivateKeyFix, + address: Address.ofPrivateKey(GasPayerPrivateKeyFix) }; const SignerPrivateKeyFix = HexUInt.of( @@ -233,7 +233,7 @@ describe('Transaction class tests', () => { const signer = Address.ofPrivateKey(SignerFix.privateKey); const actual = signed.signAsGasPayer( signer, - DelegatorFix.privateKey + GasPayerFix.privateKey ); expect(actual.isDelegated).toBe(true); expect(actual.isSigned).toBe(true); @@ -276,7 +276,7 @@ describe('Transaction class tests', () => { expected.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ); expect(actual).toBeInstanceOf(Transaction); expect(actual.signature).toBeDefined(); @@ -291,9 +291,7 @@ describe('Transaction class tests', () => { .isEqual(expected.transactionHash) ).toBe(true); expect(actual.origin.isEqual(SignerFix.address)).toBe(true); - expect(actual.gasPayer.isEqual(DelegatorFix.address)).toBe( - true - ); + expect(actual.gasPayer.isEqual(GasPayerFix.address)).toBe(true); expect(actual.id.isEqual(expected.signedTransactionId)).toBe( true ); @@ -306,7 +304,7 @@ describe('Transaction class tests', () => { expected.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ); expect(actual).toBeInstanceOf(Transaction); expect(actual.signature).toBeDefined(); @@ -321,9 +319,7 @@ describe('Transaction class tests', () => { .isEqual(expected.transactionHash) ).toBe(true); expect(actual.origin.isEqual(SignerFix.address)).toBe(true); - expect(actual.gasPayer.isEqual(DelegatorFix.address)).toBe( - true - ); + expect(actual.gasPayer.isEqual(GasPayerFix.address)).toBe(true); expect(actual.id.isEqual(expected.signedTransactionId)).toBe( true ); @@ -425,9 +421,7 @@ describe('Transaction class tests', () => { expect(actual.origin).toBeDefined(); expect(actual.origin.isEqual(SignerFix.address)).toBe(true); expect(actual.gasPayer).toBeDefined(); - expect(actual.gasPayer.isEqual(DelegatorFix.address)).toBe( - true - ); + expect(actual.gasPayer.isEqual(GasPayerFix.address)).toBe(true); expect(actual.isDelegated).toBe(true); expect(actual.id).toBeDefined(); expect(actual.isSigned).toBe(true); @@ -477,9 +471,7 @@ describe('Transaction class tests', () => { expect(actual.origin).toBeDefined(); expect(actual.origin.isEqual(SignerFix.address)).toBe(true); expect(actual.gasPayer).toBeDefined(); - expect(actual.gasPayer.isEqual(DelegatorFix.address)).toBe( - true - ); + expect(actual.gasPayer.isEqual(GasPayerFix.address)).toBe(true); expect(actual.isDelegated).toBe(true); expect(actual.id).toBeDefined(); expect(actual.isSigned).toBe(true); @@ -654,7 +646,7 @@ describe('Transaction class tests', () => { }); describe('sign method tests', () => { - test('signature <- undelegated', () => { + test('signature <- undelegated tx', () => { const actual = Transaction.of( TransactionFixture.undelegated.body ).sign(SignerFix.privateKey); @@ -662,7 +654,7 @@ describe('Transaction class tests', () => { expect(actual.signature?.length).toBe(Secp256k1.SIGNATURE_LENGTH); }); - test('Throw <- delegated', () => { + test('Throw <- delegated tx', () => { expect(() => Transaction.of(TransactionFixture.delegated.body).sign( SignerFix.privateKey @@ -679,13 +671,13 @@ describe('Transaction class tests', () => { }); }); - describe('signAsDelegator method tests', () => { - test('signature (complete) <- delegator', () => { + describe('signAsGasPayer method tests', () => { + test('signature (complete) <- delegated tx', () => { const expected = Transaction.of( TransactionFixture.delegated.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ); const signed = Transaction.of( TransactionFixture.delegated.body @@ -693,12 +685,12 @@ describe('Transaction class tests', () => { const signer = Address.ofPrivateKey(SignerFix.privateKey); const actual = signed.signAsGasPayer( signer, - DelegatorFix.privateKey + GasPayerFix.privateKey ); expect(actual.signature).toEqual(expected.signature); }); - test('Throw <- undelegated', () => { + test('Throw <- undelegated tx', () => { expect(() => { Transaction.of(TransactionFixture.undelegated.body) .sign(SignerFix.privateKey) @@ -709,7 +701,7 @@ describe('Transaction class tests', () => { }).toThrowError(NotDelegatedTransaction); }); - test('Throw <- unsigned', () => { + test('Throw <- unsigned tx', () => { expect(() => { Transaction.of( TransactionFixture.delegated.body @@ -720,7 +712,7 @@ describe('Transaction class tests', () => { }).toThrowError(InvalidTransactionField); }); - test('Throw <- invalid private keys - delegator', () => { + test('Throw <- invalid private keys - delegated tx', () => { expect(() => Transaction.of( TransactionFixture.undelegated.body @@ -731,26 +723,26 @@ describe('Transaction class tests', () => { }); }); - describe('signForDelegator method tests', () => { + describe('signAsSender method tests', () => { test('signature (incomplete) <- signed', () => { const expected = Transaction.of( TransactionFixture.delegated.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ); const actual = Transaction.of( TransactionFixture.delegated.body ).signAsSender(SignerFix.privateKey); expect(actual.signature).toBeDefined(); // The signer's signature exists, but... - // ... the delegator signature is missing, hence... + // ... the gasPayer signature is missing, hence... expect(actual.isSigned).toBe(false); // ... the signature is incomplete. expect(actual.signature).toEqual( expected.signature?.slice(0, actual.signature?.length) ); }); - test('Throw <- undelegated', () => { + test('Throw <- undelegated tx', () => { expect(() => Transaction.of( TransactionFixture.undelegated.body @@ -769,13 +761,13 @@ describe('Transaction class tests', () => { }); }); - describe('signWithDelegator method tests', () => { - test('signature <- delegated', () => { + describe('signAsSenderAndGasPayer method tests', () => { + test('signature <- delegated tx', () => { const actual = Transaction.of( TransactionFixture.delegated.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ); expect(actual.isDelegated).toBe(true); expect(actual.id).toBeDefined(); @@ -784,16 +776,16 @@ describe('Transaction class tests', () => { Secp256k1.SIGNATURE_LENGTH * 2 ); expect(actual.origin.isEqual(SignerFix.address)).toBe(true); - expect(actual.gasPayer.isEqual(DelegatorFix.address)).toBe(true); + expect(actual.gasPayer.isEqual(GasPayerFix.address)).toBe(true); }); - test('Throw <- undelegated', () => { + test('Throw <- undelegated tx', () => { expect(() => Transaction.of( TransactionFixture.undelegated.body ).signAsSenderAndGasPayer( SignerFix.privateKey, - DelegatorFix.privateKey + GasPayerFix.privateKey ) ).toThrowError(NotDelegatedTransaction); }); @@ -804,12 +796,12 @@ describe('Transaction class tests', () => { TransactionFixture.undelegated.body ).signAsSenderAndGasPayer( HexUInt.of('0xF00DBABE').bytes, // https://en.wikipedia.org/wiki/Hexspeak - DelegatorFix.privateKey + GasPayerFix.privateKey ) ).toThrowError(InvalidSecp256k1PrivateKey); }); - test('Throw <- invalid private keys - delegator', () => { + test('Throw <- invalid private keys - delegated tx', () => { expect(() => { Transaction.of( TransactionFixture.undelegated.body diff --git a/packages/errors/src/available-errors/transaction/transaction.ts b/packages/errors/src/available-errors/transaction/transaction.ts index 69dc3434a..3466ee312 100644 --- a/packages/errors/src/available-errors/transaction/transaction.ts +++ b/packages/errors/src/available-errors/transaction/transaction.ts @@ -30,7 +30,7 @@ class InvalidTransactionField extends VechainSDKError< * * Error will be thrown when the transaction is not delegated. */ class NotDelegatedTransaction extends VechainSDKError< - undefined | { delegatorUrl: string } + undefined | { gasPayerUrl: string } > {} /** diff --git a/packages/errors/tests/available-errors/transaction.unit.test.ts b/packages/errors/tests/available-errors/transaction.unit.test.ts index d3b12b164..7ae1c4830 100644 --- a/packages/errors/tests/available-errors/transaction.unit.test.ts +++ b/packages/errors/tests/available-errors/transaction.unit.test.ts @@ -66,7 +66,7 @@ describe('Error package Available errors test - Transaction', () => { */ const testNotDelegatedTransaction = ( innerError?: Error, - data?: { delegatorUrl: string } + data?: { gasPayerUrl: string } ): void => { expect(() => { throw new NotDelegatedTransaction( @@ -84,8 +84,8 @@ describe('Error package Available errors test - Transaction', () => { test('NotDelegatedTransaction', () => { [undefined, new Error('error')].forEach((innerError) => { // Use a valid object or explicitly check for undefined - const dataOptions: Array<{ delegatorUrl: string } | undefined> = [ - { delegatorUrl: 'url' }, + const dataOptions: Array<{ gasPayerUrl: string } | undefined> = [ + { gasPayerUrl: 'url' }, undefined ]; diff --git a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts index a51fd591e..7e7bd982f 100644 --- a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts +++ b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts @@ -51,7 +51,7 @@ abstract class VeChainAbstractSigner implements VeChainSigner { * @param provider - The provider to connect to */ protected constructor(provider?: AvailableVeChainProviders) { - // Store provider and delegator + // Store provider and gasPayer this.provider = provider; } diff --git a/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts b/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts index c4483d2af..d50385a0b 100644 --- a/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts +++ b/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts @@ -94,13 +94,13 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { ); } - let delegator = DelegationHandler( + let gasPayer = DelegationHandler( await this.provider.wallet?.getDelegator() ).delegatorOrNull(); - // Override the delegator if the transaction has a delegation URL + // Override the gasPayer if the transaction has a delegation URL if (transactionToSign.delegationUrl !== undefined) { - delegator = { + gasPayer = { delegatorUrl: transactionToSign.delegationUrl }; } @@ -108,7 +108,7 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { // Sign the transaction return await this._signFlow( transactionToSign, - delegator, + gasPayer, this.provider.thorClient ); } @@ -164,14 +164,14 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { * Signs a transaction internal method * * @param transaction - The transaction to sign - * @param delegator - The delegator to use + * @param gasPayer - The gasPayer to use * @param thorClient - The ThorClient instance * @returns The fully signed transaction * @throws {InvalidSecp256k1PrivateKey, InvalidDataType} */ async _signFlow( transaction: TransactionRequestInput, - delegator: SignTransactionOptions | null, + gasPayer: SignTransactionOptions | null, thorClient: ThorClient ): Promise { // Populate the call, to get proper from and to address (compatible with multi-clause transactions) @@ -179,12 +179,12 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { await this.populateTransaction(transaction); // Sign the transaction - return delegator !== null - ? await this._signWithDelegator( + return gasPayer !== null + ? await this._signWithGasPayer( populatedTransaction, this.privateKey, thorClient, - delegator + gasPayer ) : Hex.of( Transaction.of(populatedTransaction).sign(this.privateKey) @@ -198,34 +198,34 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { * @param unsignedTransactionBody - The unsigned transaction body to sign. * @param originPrivateKey - The private key of the origin account. * @param thorClient - The ThorClient instance. - * @param delegatorOptions - Optional parameters for the request. Includes the `delegatorUrl` and `delegatorPrivateKey` fields. + * @param gasPayerOptions - Optional parameters for the request. Includes the `delegatorUrl` and `delegatorPrivateKey` fields. * Only one of the following options can be specified: `delegatorUrl`, `delegatorPrivateKey`. * @returns A promise that resolves to the signed transaction. * @throws {NotDelegatedTransaction} */ - private async _signWithDelegator( + private async _signWithGasPayer( unsignedTransactionBody: TransactionBody, originPrivateKey: Uint8Array, thorClient: ThorClient, - delegatorOptions?: SignTransactionOptions + gasPayerOptions?: SignTransactionOptions ): Promise { // Address of the origin account const originAddress = Address.ofPrivateKey(originPrivateKey).toString(); const unsignedTx = Transaction.of(unsignedTransactionBody); - // Sign transaction with origin private key and delegator private key - if (delegatorOptions?.delegatorPrivateKey !== undefined) + // Sign transaction with origin private key and gasPayer private key + if (gasPayerOptions?.delegatorPrivateKey !== undefined) return Hex.of( Transaction.of(unsignedTransactionBody).signAsSenderAndGasPayer( originPrivateKey, - HexUInt.of(delegatorOptions?.delegatorPrivateKey).bytes + HexUInt.of(gasPayerOptions?.delegatorPrivateKey).bytes ).encoded ).toString(); - // Otherwise, get the signature of the delegator from the delegator endpoint - const delegatorSignature = await DelegationHandler( - delegatorOptions + // Otherwise, get the signature of the gasPayer from the gasPayer endpoint + const gasPayerSignature = await DelegationHandler( + gasPayerOptions ).getDelegationSignatureUsingUrl( unsignedTx, originAddress, @@ -240,10 +240,10 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { // Sign the transaction with both signatures. Concat both signatures to get the final signature const signature = new Uint8Array( - originSignature.length + delegatorSignature.length + originSignature.length + gasPayerSignature.length ); signature.set(originSignature); - signature.set(delegatorSignature, originSignature.length); + signature.set(gasPayerSignature, originSignature.length); // Return new signed transaction return Hex.of( diff --git a/packages/network/src/thor-client/transactions/helpers/delegation-handler.ts b/packages/network/src/thor-client/transactions/helpers/delegation-handler.ts index cdce9e3b4..2f43d16c3 100644 --- a/packages/network/src/thor-client/transactions/helpers/delegation-handler.ts +++ b/packages/network/src/thor-client/transactions/helpers/delegation-handler.ts @@ -48,7 +48,7 @@ const _getDelegationSignature = async ( '_getDelegationSignature()', 'Delegation failed: Cannot get signature from delegator.', { - delegatorUrl + gasPayerUrl: delegatorUrl }, error );