Skip to content

Commit

Permalink
Merge pull request #542 from multiversx/TOOL-390-update-transaction-t…
Browse files Browse the repository at this point in the history
…o-follow-specs

Remove ITransaction interface and update Transaction to follow specs
  • Loading branch information
danielailie authored Dec 12, 2024
2 parents 89f37b2 + 6345f37 commit 372cd24
Show file tree
Hide file tree
Showing 47 changed files with 802 additions and 751 deletions.
2 changes: 1 addition & 1 deletion src/abi/interaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe("test smart contract interactor", function () {
contract: contract,
deployer: alice,
codePath: "src/testdata/basic-features.wasm",
gasLimit: 600000000,
gasLimit: 600000000n,
initArguments: [],
chainID: network.ChainID,
});
Expand Down
38 changes: 19 additions & 19 deletions src/abi/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("test smart contract interactor", function () {
const hexBar = "4241522d356263303866";
const hexLKMEX = "4c4b4d45582d616162393130";
const hexNFT = "4d4f532d623962346232";
const hexContractAddress = new Address(contract.getAddress().bech32()).hex();
const hexContractAddress = new Address(contract.getAddress().toBech32()).hex();
const hexDummyFunction = "64756d6d79";

// ESDT, single
Expand All @@ -76,8 +76,8 @@ describe("test smart contract interactor", function () {
.withSingleESDTNFTTransfer(LKMEX(123456, 123.456))
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`ESDTNFTTransfer@${hexLKMEX}@01e240@06b14bd1e6eea00000@${hexContractAddress}@${hexDummyFunction}`,
Expand All @@ -89,8 +89,8 @@ describe("test smart contract interactor", function () {
.withSender(alice)
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`ESDTNFTTransfer@${hexLKMEX}@01e240@06b14bd1e6eea00000@${hexContractAddress}@${hexDummyFunction}`,
Expand All @@ -102,8 +102,8 @@ describe("test smart contract interactor", function () {
.withSingleESDTNFTTransfer(nonFungibleToken(1))
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`ESDTNFTTransfer@${hexNFT}@01@01@${hexContractAddress}@${hexDummyFunction}`,
Expand All @@ -115,8 +115,8 @@ describe("test smart contract interactor", function () {
.withSender(alice)
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`ESDTNFTTransfer@${hexNFT}@01@01@${hexContractAddress}@${hexDummyFunction}`,
Expand All @@ -128,8 +128,8 @@ describe("test smart contract interactor", function () {
.withMultiESDTNFTTransfer([TokenFoo(3), TokenBar(3.14)])
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`MultiESDTNFTTransfer@${hexContractAddress}@02@${hexFoo}@@03@${hexBar}@@0c44@${hexDummyFunction}`,
Expand All @@ -141,8 +141,8 @@ describe("test smart contract interactor", function () {
.withSender(alice)
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`MultiESDTNFTTransfer@${hexContractAddress}@02@${hexFoo}@@03@${hexBar}@@0c44@${hexDummyFunction}`,
Expand All @@ -154,8 +154,8 @@ describe("test smart contract interactor", function () {
.withMultiESDTNFTTransfer([nonFungibleToken(1), nonFungibleToken(42)])
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
assert.equal(
transaction.getData().toString(),
`MultiESDTNFTTransfer@${hexContractAddress}@02@${hexNFT}@01@01@${hexNFT}@2a@01@${hexDummyFunction}`,
Expand All @@ -167,8 +167,8 @@ describe("test smart contract interactor", function () {
.withSender(alice)
.buildTransaction();

assert.equal(transaction.getSender().bech32(), alice.bech32());
assert.equal(transaction.getReceiver().bech32(), alice.bech32());
assert.equal(transaction.getSender().toBech32(), alice.toBech32());
assert.equal(transaction.getReceiver().toBech32(), alice.toBech32());
});

it("should create transaction, with ABI, with transfer & execute", async function () {
Expand All @@ -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
32 changes: 19 additions & 13 deletions src/abi/smartContract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ describe("test contract", () => {
let owner = new Address("93ee6143cdc10ce79f15b2a6c2ad38e9b6021c72a1779051f47154fd54cfbd5e");

let firstContractAddress = SmartContract.computeAddress(owner, 0);
assert.equal(firstContractAddress.bech32(), "erd1qqqqqqqqqqqqqpgqhdjjyq8dr7v5yq9tv6v5vt9tfvd00vg7h40q6779zn");
assert.equal(firstContractAddress.toBech32(), "erd1qqqqqqqqqqqqqpgqhdjjyq8dr7v5yq9tv6v5vt9tfvd00vg7h40q6779zn");

let secondContractAddress = SmartContract.computeAddress(owner, 1);
assert.equal(secondContractAddress.bech32(), "erd1qqqqqqqqqqqqqpgqde8eqjywyu6zlxjxuxqfg5kgtmn3setxh40qen8egy");
assert.equal(
secondContractAddress.toBech32(),
"erd1qqqqqqqqqqqqqpgqde8eqjywyu6zlxjxuxqfg5kgtmn3setxh40qen8egy",
);
});

it("should deploy", async () => {
Expand All @@ -42,7 +45,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 All @@ -60,7 +63,10 @@ describe("test contract", () => {

// Compute & set the contract address
contract.setAddress(SmartContract.computeAddress(alice.address, 42));
assert.equal(contract.getAddress().bech32(), "erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q");
assert.equal(
contract.getAddress().toBech32(),
"erd1qqqqqqqqqqqqqpgq3ytm9m8dpeud35v3us20vsafp77smqghd8ss4jtm0q",
);

// Sign the transaction
deployTransaction.applySignature(await alice.signer.sign(deployTransaction.serializeForSigning()));
Expand Down Expand Up @@ -97,15 +103,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 +167,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 +239,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 +249,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 +258,7 @@ describe("test contract", () => {
func: "foo",
args: [[1, 2, 3]],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand Down Expand Up @@ -302,7 +308,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 +318,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 +327,7 @@ describe("test contract", () => {
func: "foo",
args: [1, 2, 3],
chainID: "D",
gasLimit: 1000000,
gasLimit: 1000000n,
caller: callerAddress,
});

Expand Down
8 changes: 4 additions & 4 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 All @@ -276,7 +276,7 @@ export class SmartContract implements ISmartContract {
}

private ensureHasAddress() {
if (!this.getAddress().bech32()) {
if (!this.getAddress().toBech32()) {
throw new ErrContractHasNoAddress();
}
}
Expand All @@ -289,7 +289,7 @@ export class SmartContract implements ISmartContract {
* @param nonce The owner nonce used for the deployment transaction
*/
static computeAddress(owner: Address, nonce: INonce): Address {
const deployer = Address.fromBech32(owner.bech32());
const deployer = Address.fromBech32(owner.toBech32());
const addressComputer = new AddressComputer();
return addressComputer.computeContractAddress(deployer, BigInt(nonce.valueOf()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/abi/smartContractResults.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ describe("fetch transactions from local testnet", function () {
contract: contract,
deployer: alice,
codePath: "src/testdata/counter.wasm",
gasLimit: 3000000,
gasLimit: 3000000n,
initArguments: [],
chainID: network.ChainID,
});

// ++
let transactionIncrement = contract.call({
func: new ContractFunction("increment"),
gasLimit: 3000000,
gasLimit: 3000000n,
chainID: network.ChainID,
caller: alice.address,
});
Expand Down
5 changes: 2 additions & 3 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,9 +20,9 @@ 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());
this.value = Address.newFromBech32(value.toBech32());
}

getClassName(): string {
Expand Down
Loading

0 comments on commit 372cd24

Please sign in to comment.