Skip to content

Commit

Permalink
SRC-01 and SR0-01 (#1758)
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonneal authored Jan 29, 2025
1 parent 0a30d35 commit 67073ff
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 57 deletions.
3 changes: 1 addition & 2 deletions docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/contracts/contract-clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
3 changes: 1 addition & 2 deletions docs/examples/transactions/multiple-clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions packages/core/src/transaction/Clause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -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';

Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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}` }
);
Expand Down
55 changes: 22 additions & 33 deletions packages/core/tests/transaction/Clause.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
type ClauseOptions,
type DeployParams,
type TransactionClause,
ABIContract
ABIContract,
VTHO_ADDRESS
} from '../../src';

const ClauseFixture = {
Expand Down Expand Up @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -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);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,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
Expand Down

1 comment on commit 67073ff

@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: 98%
98.91% (4372/4420) 97.2% (1392/1432) 98.8% (906/917)
Title Tests Skipped Failures Errors Time
core 838 0 πŸ’€ 0 ❌ 0 πŸ”₯ 2m 26s ⏱️
network 731 0 πŸ’€ 0 ❌ 0 πŸ”₯ 5m 7s ⏱️
errors 40 0 πŸ’€ 0 ❌ 0 πŸ”₯ 17.756s ⏱️
logging 3 0 πŸ’€ 0 ❌ 0 πŸ”₯ 19.649s ⏱️
hardhat-plugin 19 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 1s ⏱️
aws-kms-adapter 23 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 31s ⏱️
ethers-adapter 5 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 17s ⏱️
rpc-proxy 37 0 πŸ’€ 0 ❌ 0 πŸ”₯ 1m 6s ⏱️

Please sign in to comment.