Skip to content

Commit

Permalink
Remove ITransaction interface and update Transaction to follow specs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Dec 10, 2024
1 parent 3192410 commit 9916699
Show file tree
Hide file tree
Showing 39 changed files with 788 additions and 695 deletions.
4 changes: 2 additions & 2 deletions src/abi/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ describe("test smart contract interactor", function () {
transaction,
new Transaction({
chainID: "T",
sender: alice.toBech32(),
receiver: dummyAddress.toBech32(),
sender: alice,
receiver: dummyAddress,
data: Buffer.from("ESDTTransfer@464f4f2d616263646566@64@676574556c74696d617465416e73776572"),
gasLimit: 543210n,
value: 0n,
Expand Down
26 changes: 13 additions & 13 deletions src/abi/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from "../address";
import { IChainID, IGasLimit, IGasPrice, ITransactionValue } from "../interface";
import { ITransactionValue } from "../interface";
import { Transaction } from "../transaction";
import { ReturnCode } from "./returnCode";
import { TypedValue } from "./typesystem";
Expand Down Expand Up @@ -33,32 +33,32 @@ export interface DeployArguments {
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
gasPrice?: IGasPrice;
chainID: IChainID;
value?: bigint;
gasLimit: bigint;
gasPrice?: bigint;
chainID: string;
deployer: Address;
}

export interface UpgradeArguments {
code: ICode;
codeMetadata?: ICodeMetadata;
initArguments?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
gasPrice?: IGasPrice;
chainID: IChainID;
value?: bigint;
gasLimit: bigint;
gasPrice?: bigint;
chainID: string;
caller: Address;
}

export interface CallArguments {
func: IContractFunction;
args?: any[];
value?: ITransactionValue;
gasLimit: IGasLimit;
value?: bigint;
gasLimit: bigint;
receiver?: Address;
gasPrice?: IGasPrice;
chainID: IChainID;
gasPrice?: bigint;
chainID: string;
caller: Address;
}

Expand Down
6 changes: 3 additions & 3 deletions src/abi/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {
export namespace NativeTypes {
export type NativeBuffer = Buffer | string;
export type NativeBytes = Buffer | { valueOf(): Buffer } | string;
export type NativeAddress = string | Buffer | IAddress | { getAddress(): IAddress };
export type NativeAddress = string | Buffer | Address | { getAddress(): IAddress };
export type NativeBigNumber = BigNumber.Value | bigint;
}

Expand Down Expand Up @@ -403,9 +403,9 @@ export namespace NativeSerializer {
export function convertNativeToAddress(
native: NativeTypes.NativeAddress,
errorContext: ArgumentErrorContext,
): IAddress {
): Address {
if ((<any>native).bech32) {
return <IAddress>native;
return <Address>native;
}
if ((<any>native).getAddress) {
return (<any>native).getAddress();
Expand Down
20 changes: 10 additions & 10 deletions src/abi/smartContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("test contract", () => {
let contract = new SmartContract();
let deployTransaction = contract.deploy({
code: Code.fromBuffer(Buffer.from([1, 2, 3, 4])),
gasLimit: 1000000,
gasLimit: 1000000n,
chainID: chainID,
deployer: alice.address,
});
Expand Down Expand Up @@ -97,15 +97,15 @@ describe("test contract", () => {
let callTransactionOne = contract.call({
func: new ContractFunction("helloEarth"),
args: [new U32Value(5), BytesValue.fromHex("0123")],
gasLimit: 150000,
gasLimit: 150000n,
chainID: chainID,
caller: alice.address,
});

let callTransactionTwo = contract.call({
func: new ContractFunction("helloMars"),
args: [new U32Value(5), BytesValue.fromHex("0123")],
gasLimit: 1500000,
gasLimit: 1500000n,
chainID: chainID,
caller: alice.address,
});
Expand Down Expand Up @@ -161,7 +161,7 @@ describe("test contract", () => {

let deployTransaction = contract.upgrade({
code: Code.fromBuffer(Buffer.from([1, 2, 3, 4])),
gasLimit: 1000000,
gasLimit: 1000000n,
chainID: chainID,
caller: alice.address,
});
Expand Down Expand Up @@ -233,7 +233,7 @@ describe("test contract", () => {
func: "foo",
args: [new U8Value(1), new U8Value(2), new U8Value(3)],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});
}, "Wrong number of arguments for endpoint foo: expected between 0 and 1 arguments, have 3");
Expand All @@ -243,7 +243,7 @@ describe("test contract", () => {
func: "foo",
args: [[new U8Value(1), new U8Value(2), new U8Value(3)]],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand All @@ -252,7 +252,7 @@ describe("test contract", () => {
func: "foo",
args: [[1, 2, 3]],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand Down Expand Up @@ -302,7 +302,7 @@ describe("test contract", () => {
func: "foo",
args: [new U8Value(1), new U8Value(2), new U8Value(3)],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});
}, "Invalid argument: Wrong argument type for endpoint foo: typed value provided; expected variadic type, have U8Value");
Expand All @@ -312,7 +312,7 @@ describe("test contract", () => {
func: "foo",
args: [VariadicValue.fromItems(new U8Value(1), new U8Value(2), new U8Value(3))],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand All @@ -321,7 +321,7 @@ describe("test contract", () => {
func: "foo",
args: [1, 2, 3],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand Down
4 changes: 2 additions & 2 deletions src/abi/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ export class SmartContract implements ISmartContract {
});

args = args || [];
value = value || 0;
value = value || 0n;

const transaction = factory.createTransactionForExecute(caller, {
contract: receiver ? receiver : this.getAddress(),
function: func.toString(),
gasLimit: BigInt(gasLimit.valueOf()),
gasLimit: gasLimit,
arguments: args,
});

Expand Down
3 changes: 1 addition & 2 deletions src/abi/typesystem/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address } from "../../address";
import { IAddress } from "../../interface";
import { PrimitiveType, PrimitiveValue } from "./types";

export class AddressType extends PrimitiveType {
Expand All @@ -21,7 +20,7 @@ export class AddressValue extends PrimitiveValue {
static ClassName = "AddressValue";
private readonly value: Address;

constructor(value: IAddress) {
constructor(value: Address) {
super(new AddressType());
this.value = Address.newFromBech32(value.bech32());
}
Expand Down
44 changes: 34 additions & 10 deletions src/accountManagement/accountTransactionsFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,38 @@ describe("test account transactions factory", function () {
keyValuePairs: keyValuePairs,
});

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.deepEqual(
transaction.sender,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.deepEqual(
transaction.receiver,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.equal(Buffer.from(transaction.data).toString(), "SaveKeyValue@6b657930@76616c756530");
assert.equal(transaction.value, 0n);
assert.equal(transaction.chainID, config.chainID);
assert.equal(transaction.gasLimit, 271000n);
});

it("should create 'Transaction' for setting guardian", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const guardian = Address.fromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx");
const sender = Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const guardian = Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx");
const serviceID = "MultiversXTCSService";

const transaction = factory.createTransactionForSettingGuardian(sender, {
guardianAddress: guardian,
serviceID: serviceID,
});

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.deepEqual(
transaction.sender,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.deepEqual(
transaction.receiver,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.equal(
Buffer.from(transaction.data).toString(),
"SetGuardian@8049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f8@4d756c7469766572735854435353657276696365",
Expand All @@ -49,8 +61,14 @@ describe("test account transactions factory", function () {

const transaction = factory.createTransactionForGuardingAccount(sender);

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.deepEqual(
transaction.sender,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.deepEqual(
transaction.receiver,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.equal(Buffer.from(transaction.data).toString(), "GuardAccount");
assert.equal(transaction.value, 0n);
assert.equal(transaction.chainID, config.chainID);
Expand All @@ -62,8 +80,14 @@ describe("test account transactions factory", function () {

const transaction = factory.createTransactionForUnguardingAccount(sender);

assert.equal(transaction.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(transaction.receiver, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.deepEqual(
transaction.sender,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.deepEqual(
transaction.receiver,
Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
);
assert.equal(Buffer.from(transaction.data).toString(), "UnGuardAccount");
assert.equal(transaction.value, 0n);
assert.equal(transaction.chainID, config.chainID);
Expand Down
9 changes: 4 additions & 5 deletions src/accountManagement/accountTransactionsFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address } from "../address";
import { IAddress } from "../interface";
import { Transaction } from "../transaction";
import { TransactionBuilder } from "../transactionBuilder";
import { SaveKeyValueInput, SetGuardianInput } from "./resources";
Expand All @@ -23,7 +22,7 @@ export class AccountTransactionsFactory {
this.config = options.config;
}

createTransactionForSavingKeyValue(sender: IAddress, options: SaveKeyValueInput): Transaction {
createTransactionForSavingKeyValue(sender: Address, options: SaveKeyValueInput): Transaction {
const functionName = "SaveKeyValue";
const keyValueParts = this.computeDataPartsForSavingKeyValue(options.keyValuePairs);
const dataParts = [functionName, ...keyValueParts];
Expand Down Expand Up @@ -61,7 +60,7 @@ export class AccountTransactionsFactory {
return dataParts;
}

createTransactionForSettingGuardian(sender: IAddress, options: SetGuardianInput): Transaction {
createTransactionForSettingGuardian(sender: Address, options: SetGuardianInput): Transaction {
const dataParts = [
"SetGuardian",
Address.fromBech32(options.guardianAddress.bech32()).toHex(),
Expand All @@ -78,7 +77,7 @@ export class AccountTransactionsFactory {
}).build();
}

createTransactionForGuardingAccount(sender: IAddress): Transaction {
createTransactionForGuardingAccount(sender: Address): Transaction {
const dataParts = ["GuardAccount"];

return new TransactionBuilder({
Expand All @@ -91,7 +90,7 @@ export class AccountTransactionsFactory {
}).build();
}

createTransactionForUnguardingAccount(sender: IAddress): Transaction {
createTransactionForUnguardingAccount(sender: Address): Transaction {
const dataParts = ["UnGuardAccount"];

return new TransactionBuilder({
Expand Down
17 changes: 9 additions & 8 deletions src/converters/transactionsConverter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IPlainTransactionObject, ITransaction } from "../interface";
import { Address } from "../address";
import { IPlainTransactionObject } from "../interface";
import { Transaction } from "../transaction";

export class TransactionsConverter {
public transactionToPlainObject(transaction: ITransaction): IPlainTransactionObject {
public transactionToPlainObject(transaction: Transaction): IPlainTransactionObject {
const plainObject = {
nonce: Number(transaction.nonce),
value: transaction.value.toString(),
receiver: transaction.receiver,
sender: transaction.sender,
receiver: transaction.receiver.bech32(),
sender: transaction.sender.bech32(),
senderUsername: this.toBase64OrUndefined(transaction.senderUsername),
receiverUsername: this.toBase64OrUndefined(transaction.receiverUsername),
gasPrice: Number(transaction.gasPrice),
Expand All @@ -16,7 +17,7 @@ export class TransactionsConverter {
chainID: transaction.chainID.valueOf(),
version: transaction.version,
options: transaction.options == 0 ? undefined : transaction.options,
guardian: transaction.guardian ? transaction.guardian : undefined,
guardian: transaction.guardian.isEmpty() ? undefined : transaction.guardian.bech32(),
signature: this.toHexOrUndefined(transaction.signature),
guardianSignature: this.toHexOrUndefined(transaction.guardianSignature),
};
Expand All @@ -36,11 +37,11 @@ export class TransactionsConverter {
const transaction = new Transaction({
nonce: BigInt(object.nonce),
value: BigInt(object.value || ""),
receiver: object.receiver,
receiver: Address.newFromBech32(object.receiver),
receiverUsername: this.bufferFromBase64(object.receiverUsername).toString(),
sender: object.sender,
sender: Address.newFromBech32(object.sender),
senderUsername: this.bufferFromBase64(object.senderUsername).toString(),
guardian: object.guardian,
guardian: object.guardian ? Address.newFromBech32(object.guardian) : Address.empty(),
gasPrice: BigInt(object.gasPrice),
gasLimit: BigInt(object.gasLimit),
data: this.bufferFromBase64(object.data),
Expand Down
11 changes: 6 additions & 5 deletions src/converters/transactionsConverters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from "chai";
import { Address } from "../address";
import { Transaction } from "../transaction";
import { TransactionsConverter } from "./transactionsConverter";

Expand All @@ -7,14 +8,14 @@ describe("test transactions converter", async () => {
const converter = new TransactionsConverter();

const transaction = new Transaction({
nonce: 90,
nonce: 90n,
value: BigInt("123456789000000000000000000000"),
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx",
sender: Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"),
receiver: Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"),
senderUsername: "alice",
receiverUsername: "bob",
gasPrice: 1000000000,
gasLimit: 80000,
gasPrice: 1000000000n,
gasLimit: 80000n,
data: Buffer.from("hello"),
chainID: "localnet",
version: 2,
Expand Down
Loading

0 comments on commit 9916699

Please sign in to comment.