Skip to content

Commit

Permalink
[TransactionRequestInput] - add fields (#1562)
Browse files Browse the repository at this point in the history
* fix: add coment and delegateUrl params

* fix: fix failing test

* fix: increase testing time

---------

Co-authored-by: Miguel Angel Rojo <freemanz1486@gmail.com>
  • Loading branch information
fabiorigam and freemanzMrojo authored Dec 12, 2024
1 parent 0b323ea commit 1e0e536
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/network/src/signer/signers/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ interface TransactionRequestInput {
*/
gasPayer?: string;

/**
* The delegation URL to use to sponsor the transaction.
*/
delegationUrl?: string;

/**
* A comment describing the transaction request.
*/
comment?: string;

// START: NOT SUPPORTED FIELDS in VeChain BUT added to take compatibility with ethers

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner {
);
}

let delegator = DelegationHandler(
await this.provider.wallet?.getDelegator()
).delegatorOrNull();

// Override the delegator if the transaction has a delegation URL
if (transactionToSign.delegationUrl !== undefined) {
delegator = {
delegatorUrl: transactionToSign.delegationUrl
};
}

// Sign the transaction
return await this._signFlow(
transactionToSign,
DelegationHandler(
await this.provider?.wallet?.getDelegator()
).delegatorOrNull(),
delegator,
this.provider.thorClient
);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/network/src/thor-client/contracts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ declare module 'viem/node_modules/abitype' {
type ContractTransactionOptions = {
value?: number;
signTransactionOptions?: SignTransactionOptions;

/**
* The delegation URL to use to sponsor the transaction.
*/
delegationUrl?: string;

/**
* A comment describing the transaction request.
*/
comment?: string;
} & TransactionBodyOptions &
ClauseOptions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ class TransactionsModule {
dependsOn: options?.dependsOn,
expiration: options?.expiration,
chainTag: options?.chainTag,
blockRef: options?.blockRef
blockRef: options?.blockRef,
delegationUrl: options?.delegationUrl,
comment: options?.comment
});

return {
Expand Down Expand Up @@ -813,7 +815,9 @@ class TransactionsModule {
dependsOn: options?.dependsOn,
expiration: options?.expiration,
chainTag: options?.chainTag,
blockRef: options?.blockRef
blockRef: options?.blockRef,
delegationUrl: options?.delegationUrl,
comment: options?.comment
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ describe('VeChain base signer tests - testnet', () => {
}
}
}, 8000);

/**
* Should be able to request delegation URLs per transaction
*/
test('Should be able to request delegation URLs per transaction', async () => {
for (const fixture of signTransactionTestCases.testnet.correct) {
if (fixture.isDelegated) {
const signer = new VeChainPrivateKeySigner(
HexUInt.of(fixture.origin.privateKey).bytes,
new VeChainProvider(
thorClient,
THOR_SOLO_ACCOUNTS_BASE_WALLET,
false
)
);

// Sign the transaction
const signedTransaction = await signer.signTransaction({
from: fixture.origin.address,
delegationUrl: fixture.options.delegatorUrl
});

expect(signedTransaction).toBeDefined();
}
}
});
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ProviderInternalBaseWallet,
TESTNET_URL,
ThorClient,
type TransactionReceipt,
VeChainProvider,
type VeChainSigner
} from '../../../src';
Expand All @@ -11,7 +12,7 @@ import {
ERC20_CONTRACT_ADDRESS_ON_TESTNET,
TESTNET_DELEGATE_URL
} from './fixture';
import { ERC20_ABI, HexUInt } from '@vechain/sdk-core';
import { ABIContract, ERC20_ABI, HexUInt } from '@vechain/sdk-core';

/**
* Tests for the ThorClient class, specifically focusing on ERC20 contract-related functionality.
Expand Down Expand Up @@ -75,4 +76,27 @@ describe('ThorClient - ERC20 Contracts on testnet', () => {

expect(txResult?.reverted).toBe(false);
}, 30000);

/**
* Test transaction with url delegation per transaction.
*/
test('transaction with url delegation per transaction', async () => {
const txResult = await thorTestnetClient.contracts.executeTransaction(
(await providerWithDelegationEnabled.getSigner(
TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.address
)) as VeChainSigner,
ERC20_CONTRACT_ADDRESS_ON_TESTNET,
ABIContract.ofAbi(ERC20_ABI).getFunction('transfer'),
[TEST_ACCOUNTS.TRANSACTION.DELEGATOR.address, 1000n],
{
comment: 'test comment',
delegationUrl: TESTNET_DELEGATE_URL
}
);

const result = (await txResult.wait()) as TransactionReceipt;

expect(result.reverted).toBeFalsy();
expect(result.gasPayer).not.toBe(result.meta.txOrigin);
}, 30000);
});

1 comment on commit 1e0e536

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 99%
99% (4369/4413) 97.55% (1398/1433) 99.01% (906/915)
Title Tests Skipped Failures Errors Time
core 836 0 πŸ’€ 0 ❌ 0 πŸ”₯ 2m 18s ⏱️
network 729 0 πŸ’€ 0 ❌ 0 πŸ”₯ 4m 46s ⏱️
errors 42 0 πŸ’€ 0 ❌ 0 πŸ”₯ 17.307s ⏱️
logging 3 0 πŸ’€ 0 ❌ 0 πŸ”₯ 17.927s ⏱️
hardhat-plugin 19 0 πŸ’€ 0 ❌ 0 πŸ”₯ 59.859s ⏱️
aws-kms-adapter 23 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 26s ⏱️
ethers-adapter 5 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 14s ⏱️
rpc-proxy 37 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 1s ⏱️

Please sign in to comment.