Skip to content

Commit

Permalink
Merge branch 'main' into dependabot-npm_and_yarn-vitest-2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiorigam authored Oct 30, 2024
2 parents 11fa9cb + a67af5a commit 86eb559
Show file tree
Hide file tree
Showing 22 changed files with 922 additions and 270 deletions.
32 changes: 28 additions & 4 deletions docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,34 @@ const multipleClausesResult =
contract.clause.decimals()
]);

expect(multipleClausesResult[0]).toEqual([expectedBalance]);
expect(multipleClausesResult[1]).toEqual(['SampleToken']);
expect(multipleClausesResult[2]).toEqual(['ST']);
expect(multipleClausesResult[3]).toEqual([18]);
expect(multipleClausesResult[0]).toEqual({
success: true,
result: {
plain: expectedBalance,
array: [expectedBalance]
}
});
expect(multipleClausesResult[1]).toEqual({
success: true,
result: {
plain: 'SampleToken',
array: ['SampleToken']
}
});
expect(multipleClausesResult[2]).toEqual({
success: true,
result: {
plain: 'ST',
array: ['ST']
}
});
expect(multipleClausesResult[3]).toEqual({
success: true,
result: {
plain: 18,
array: [18]
}
});
```

> ⚠️ **Warning:**
Expand Down
32 changes: 28 additions & 4 deletions docs/examples/contracts/contract-create-ERC20-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,33 @@ const multipleClausesResult =
contract.clause.decimals()
]);

expect(multipleClausesResult[0]).toEqual([expectedBalance]);
expect(multipleClausesResult[1]).toEqual(['SampleToken']);
expect(multipleClausesResult[2]).toEqual(['ST']);
expect(multipleClausesResult[3]).toEqual([18]);
expect(multipleClausesResult[0]).toEqual({
success: true,
result: {
plain: expectedBalance,
array: [expectedBalance]
}
});
expect(multipleClausesResult[1]).toEqual({
success: true,
result: {
plain: 'SampleToken',
array: ['SampleToken']
}
});
expect(multipleClausesResult[2]).toEqual({
success: true,
result: {
plain: 'ST',
array: ['ST']
}
});
expect(multipleClausesResult[3]).toEqual({
success: true,
result: {
plain: 18,
array: [18]
}
});

// END_SNIPPET: ERC20MultiClausesReadSnippet
8 changes: 7 additions & 1 deletion docs/examples/evm-extension/evm-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,4 +791,10 @@ const totalSupply = await thorSoloClient.contracts.executeCall(
// END_SNIPPET: EVMExtensionSnippet

// Check the result
expect(totalSupply).toStrictEqual([10000000000000000000000000000n]);
expect(totalSupply).toStrictEqual({
result: {
array: [10000000000000000000000000000n],
plain: 10000000000000000000000000000n
},
success: true
});
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"bignumber.js": "^9.1.2",
"thor-devkit": "^2.0.9"
"thor-devkit": "^2.0.9",
"tsd": "^0.31.2"
}
}
4 changes: 2 additions & 2 deletions packages/core/src/transaction/Clause.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InvalidDataType } from '@vechain/sdk-errors';
import { ERC721_ABI, VIP180_ABI } from '../utils';
import {
ABI,
Expand All @@ -11,10 +12,9 @@ import {
} from '../vcdm';
import { Hex } from '../vcdm/Hex';
import { HexInt } from '../vcdm/HexInt';
import { InvalidDataType } from '@vechain/sdk-errors';
import type { ClauseOptions } from './ClauseOptions';
import type { DeployParams } from './DeployParams';
import type { TransactionClause } from './TransactionClause';
import type { ClauseOptions } from './ClauseOptions';

/**
* This class represent a transaction clause.
Expand Down
121 changes: 75 additions & 46 deletions packages/core/src/vcdm/abi/ABIContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
getAbiItem,
type AbiEvent,
type AbiFunction,
type ContractEventName,
type ContractFunctionName,
type DecodeEventLogReturnType,
type DecodeFunctionDataReturnType,
type DecodeFunctionResultReturnType,
Expand All @@ -16,20 +18,20 @@ import { ABI } from './ABI';
import { ABIEvent, type ABIEventData } from './ABIEvent';
import { ABIFunction } from './ABIFunction';

class ABIContract extends ABI {
private readonly abi: ViemABI;
class ABIContract<TAbi extends ViemABI> extends ABI {
private readonly viemABI: ViemABI;

constructor(abi: ViemABI) {
constructor(readonly abi: TAbi) {
super();
this.abi = abi;
this.viemABI = abi as ViemABI;
}

/**
* Creates an ABIContract instance from a viem ABI.
* @param {ViemABI} abi representation of the contract.
* @returns New instance of ABIContract.
*/
public static ofAbi(abi: ViemABI): ABIContract {
public static ofAbi<TAbi extends ViemABI>(abi: TAbi): ABIContract<TAbi> {
return new ABIContract(abi);
}

Expand All @@ -39,10 +41,12 @@ class ABIContract extends ABI {
* @returns {ABIFunction} The function with the given name.
* @throws {InvalidAbiItem}
*/
public getFunction(name: string): ABIFunction {
public getFunction<TFunctionName extends ContractFunctionName<TAbi>>(
name: TFunctionName | string
): ABIFunction<TAbi, TFunctionName> {
const functionAbiItem = getAbiItem({
abi: this.abi,
name
abi: this.viemABI,
name: name as string
});
if (functionAbiItem === null || functionAbiItem === undefined) {
throw new InvalidAbiItem(
Expand All @@ -54,7 +58,9 @@ class ABIContract extends ABI {
}
);
}
return new ABIFunction(functionAbiItem as AbiFunction);
return new ABIFunction<TAbi, TFunctionName>(
functionAbiItem as AbiFunction
);
}

/**
Expand All @@ -63,10 +69,12 @@ class ABIContract extends ABI {
* @returns {ABIEvent} The event with the given name.
* @throws {InvalidAbiItem}
*/
public getEvent(name: string): ABIEvent {
public getEvent<TEventName extends ContractEventName<TAbi>>(
name: TEventName | string
): ABIEvent<TAbi, TEventName> {
const eventAbiItem = getAbiItem({
abi: this.abi,
name
abi: this.viemABI,
name: name as string
});
if (eventAbiItem === null || eventAbiItem === undefined) {
throw new InvalidAbiItem(
Expand All @@ -78,7 +86,7 @@ class ABIContract extends ABI {
}
);
}
return new ABIEvent(eventAbiItem as AbiEvent);
return new ABIEvent<TAbi, TEventName>(eventAbiItem as AbiEvent);
}

/**
Expand All @@ -88,16 +96,17 @@ class ABIContract extends ABI {
* @returns {Hex} The encoded data in hexadecimal that can be used to send a transaction.
* @throws {InvalidAbiDataToEncodeOrDecode}
*/
public encodeFunctionInput(
functionName: string,
functionData?: unknown[]
): Hex {
public encodeFunctionInput<
TFunctionName extends ContractFunctionName<TAbi>
>(functionName: TFunctionName | string, functionData?: unknown[]): Hex {
try {
const functionAbiItem = getAbiItem({
abi: this.abi,
name: functionName
abi: this.viemABI,
name: functionName as string
});
const functionAbi = new ABIFunction(functionAbiItem as AbiFunction);
const functionAbi = new ABIFunction<TAbi, TFunctionName>(
functionAbiItem as AbiFunction
);

return functionAbi.encodeData(functionData);
} catch (error) {
Expand All @@ -117,16 +126,20 @@ class ABIContract extends ABI {
* @returns {DecodeFunctionDataReturnType} an array of the decoded function data
* @throws {InvalidAbiDataToEncodeOrDecode}
*/
public decodeFunctionInput(
functionName: string,
public decodeFunctionInput<
TFunctionName extends ContractFunctionName<TAbi>
>(
functionName: TFunctionName | string,
encodedFunctionInput: Hex
): DecodeFunctionDataReturnType {
): DecodeFunctionDataReturnType<TAbi, TFunctionName> {
try {
const functionAbiItem = getAbiItem({
abi: this.abi,
name: functionName
abi: this.viemABI,
name: functionName as string
});
const functionAbi = new ABIFunction(functionAbiItem as AbiFunction);
const functionAbi = new ABIFunction<TAbi, TFunctionName>(
functionAbiItem as AbiFunction
);

return functionAbi.decodeData(encodedFunctionInput);
} catch (error) {
Expand Down Expand Up @@ -154,16 +167,20 @@ class ABIContract extends ABI {
* const decodedOutput = decodeFunctionOutput('getValue', encodedValue);
*
*/
public decodeFunctionOutput(
functionName: string,
public decodeFunctionOutput<
TFunctionName extends ContractFunctionName<TAbi>
>(
functionName: TFunctionName | string,
encodedFunctionOutput: Hex
): DecodeFunctionResultReturnType {
): DecodeFunctionResultReturnType<TAbi, TFunctionName> {
try {
const functionAbiItem = getAbiItem({
abi: this.abi,
name: functionName
abi: this.viemABI,
name: functionName as string
});
const functionAbi = new ABIFunction(functionAbiItem as AbiFunction);
const functionAbi = new ABIFunction<TAbi, TFunctionName>(
functionAbiItem as AbiFunction
);

return functionAbi.decodeResult(encodedFunctionOutput);
} catch (error) {
Expand All @@ -183,16 +200,18 @@ class ABIContract extends ABI {
* @returns {ABIEventData} An object containing the encoded data and topics.
* @throws {InvalidAbiDataToEncodeOrDecode}
*/
public encodeEventLog(
eventName: string,
public encodeEventLog<TEventName extends ContractEventName<TAbi>>(
eventName: TEventName | string,
eventArgs: unknown[]
): ABIEventData {
try {
const eventAbiItem = getAbiItem({
abi: this.abi,
name: eventName
abi: this.viemABI,
name: eventName as string
});
const eventAbi = new ABIEvent(eventAbiItem as AbiEvent);
const eventAbi = new ABIEvent<TAbi, TEventName>(
eventAbiItem as AbiEvent
);
return eventAbi.encodeEventLog(eventArgs);
} catch (error) {
throw new InvalidAbiDataToEncodeOrDecode(
Expand All @@ -211,16 +230,18 @@ class ABIContract extends ABI {
* @returns {DecodeEventLogReturnType} The decoded data of the event log.
* @throws {InvalidAbiDataToEncodeOrDecode}
*/
public decodeEventLog(
eventName: string,
public decodeEventLog<TEventName extends ContractEventName<TAbi>>(
eventName: TEventName | string,
eventToDecode: ABIEventData
): DecodeEventLogReturnType {
): DecodeEventLogReturnType<TAbi, TEventName> {
try {
const eventAbiItem = getAbiItem({
abi: this.abi,
name: eventName
abi: this.viemABI,
name: eventName as string
});
const eventAbi = new ABIEvent(eventAbiItem as AbiEvent);
const eventAbi = new ABIEvent<TAbi, TEventName>(
eventAbiItem as AbiEvent
);
return eventAbi.decodeEventLog(eventToDecode);
} catch (error) {
throw new InvalidAbiDataToEncodeOrDecode(
Expand All @@ -245,9 +266,15 @@ class ABIContract extends ABI {
* @returns {DecodeEventLogReturnType} - A log object representing the decoded log or null if decoding fails.
* @throws {InvalidAbiDataToEncodeOrDecode}
*/
public parseLog(data: Hex, topics: Hex[]): DecodeEventLogReturnType {
public parseLog<TEventName extends ContractEventName<TAbi>>(
data: Hex,
topics: Hex[]
): DecodeEventLogReturnType<TAbi, TEventName> {
try {
return ABIEvent.parseLog(this.abi, { data, topics });
return ABIEvent.parseLog(this.abi, {
data,
topics
});
} catch (e) {
throw new InvalidAbiDataToEncodeOrDecode(
'ABIContract.parseLog()',
Expand All @@ -272,7 +299,9 @@ class ABIContract extends ABI {
return [];
}

return this.parseObjectValues(eventLogDecoded.args);
return this.parseObjectValues(
eventLogDecoded.args as unknown as object
);
}
}

Expand Down
Loading

0 comments on commit 86eb559

Please sign in to comment.