Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Sep 7, 2023
1 parent 7db0a4a commit 5ba4506
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 122 deletions.
18 changes: 12 additions & 6 deletions src/transactionIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ export class TransactionIntent {
public value?: BigNumber.Value;
public data?: Uint8Array;

public constructor(sender: string, receiver: string, gasLimit: BigNumber.Value, value?: BigNumber.Value, data?: Uint8Array) {
this.sender = sender;
this.receiver = receiver;
this.gasLimit = gasLimit;
this.value = value;
this.data = data;
public constructor(options: {
sender: string,
receiver: string,
gasLimit: BigNumber.Value,
value?: BigNumber.Value,
data?: Uint8Array
}) {
this.sender = options.sender;
this.receiver = options.receiver;
this.gasLimit = options.gasLimit;
this.value = options.value;
this.data = options.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,78 @@ import { assert, expect } from "chai";
import { SmartContractTransactionIntentsFactory } from "./smartContractTransactionIntentsFactory";
import { Address } from "../address";
import { Code } from "../smartcontracts/code";
import { promises } from "fs";
import { AbiRegistry } from "../smartcontracts/typesystem/abiRegistry";
import { U32Value } from "../smartcontracts";
import { CONTRACT_DEPLOY_ADDRESS } from "../constants";
import { loadContractCode, loadAbiRegistry } from "../testutils/utils";
import { Err } from "../errors";

describe("test smart contract intents factory", function () {
let smartContractIntentsFactory: SmartContractTransactionIntentsFactory;
let abiSmartContractIntentsFactory: SmartContractTransactionIntentsFactory;
let factory: SmartContractTransactionIntentsFactory;
let abiAwareFactory: SmartContractTransactionIntentsFactory;
let adderByteCode: Code;
let abiRegistry: AbiRegistry;

before(async function () {
smartContractIntentsFactory = new SmartContractTransactionIntentsFactory({ chainID: "D", minGasLimit: 50000, gasLimitPerByte: 1500 });
let adderBuffer = await promises.readFile("src/testdata/adder.wasm");
adderByteCode = Code.fromBuffer(adderBuffer);

let abiJson = await promises.readFile("src/testdata/adder.abi.json", { encoding: "utf8" });
let abiObj = JSON.parse(abiJson);
abiRegistry = AbiRegistry.create(abiObj);

abiSmartContractIntentsFactory = new SmartContractTransactionIntentsFactory({ chainID: "D", minGasLimit: 50000, gasLimitPerByte: 1500 }, abiRegistry);
factory = new SmartContractTransactionIntentsFactory({
config:
{
chainID: "D",
minGasLimit: 50000,
gasLimitPerByte: 1500
}
});

adderByteCode = await loadContractCode("src/testdata/adder.wasm");
abiRegistry = await loadAbiRegistry("src/testdata/adder.abi.json");

abiAwareFactory = new SmartContractTransactionIntentsFactory({
config:
{
chainID: "D",
minGasLimit: 50000,
gasLimitPerByte: 1500
},
abi: abiRegistry
},
);
});

it("should throw error", async function () {
it("should throw error when args are not of type 'TypedValue'", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const gasLimit = 6000000;
const args = [0];
try {
smartContractIntentsFactory.createTransactionIntentForDeploy(sender, adderByteCode.valueOf(), gasLimit, args);
}
catch (err) {
expect(err.message).to.equal("Can't convert args to TypedValues");
}

assert.throws(() => factory.createTransactionIntentForDeploy({
sender: sender,
bytecode: adderByteCode.valueOf(),
gasLimit: gasLimit,
args: args
}), Err, "Can't convert args to TypedValues");
});

it("should build deploy intent", async function () {
it("should build intent for deploy", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const gasLimit = 6000000;
const args = [new U32Value(0)];

const deployIntent = smartContractIntentsFactory.createTransactionIntentForDeploy(sender, adderByteCode.valueOf(), gasLimit, args);
const abiDeployIntent = abiSmartContractIntentsFactory.createTransactionIntentForDeploy(sender, adderByteCode.valueOf(), gasLimit, args);
const deployIntent = factory.createTransactionIntentForDeploy({
sender: sender,
bytecode: adderByteCode.valueOf(),
gasLimit: gasLimit,
args: args
});
const abiDeployIntent = abiAwareFactory.createTransactionIntentForDeploy({
sender: sender,
bytecode: adderByteCode.valueOf(),
gasLimit: gasLimit,
args: args
});

assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(deployIntent.receiver, CONTRACT_DEPLOY_ADDRESS);
assert.isDefined(deployIntent.data);
expect(deployIntent.data?.length).to.be.greaterThan(0);

if (deployIntent.data) {
const expectedGasLimit = 6000000 + 50000 + 1500 * deployIntent.data.length;
Expand All @@ -58,45 +84,67 @@ describe("test smart contract intents factory", function () {
assert.deepEqual(deployIntent, abiDeployIntent);
});

it("should build execute intent", async function () {
it("should build intent for execute", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4");
const func = "add";
const gasLimit = 6000000;
const args = [new U32Value(7)];

const deployIntent = smartContractIntentsFactory.createTransactionIntentForExecute(sender, contract, func, gasLimit, args);
const abiDeployIntent = abiSmartContractIntentsFactory.createTransactionIntentForExecute(sender, contract, func, gasLimit, args);
const deployIntent = factory.createTransactionIntentForExecute({
sender: sender,
contractAddress: contract,
func: func,
gasLimit: gasLimit,
args: args
});
const abiDeployIntent = abiAwareFactory.createTransactionIntentForExecute({
sender: sender,
contractAddress: contract,
func: func,
gasLimit: gasLimit,
args: args
});

assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4");

assert.isDefined(deployIntent.data);
let decoder = new TextDecoder();
assert.equal(decoder.decode(deployIntent.data), "add@07");
assert.deepEqual(deployIntent.data, Buffer.from("add@07"));

assert.equal(deployIntent.gasLimit.valueOf(), 6059000);
assert.equal(deployIntent.value, 0);

assert.deepEqual(deployIntent, abiDeployIntent);
});

it("should build upgrade intent", async function () {
it("should build intent for upgrade", async function () {
const sender = Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
const contract = Address.fromBech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4");
const gasLimit = 6000000;
const args = [new U32Value(0)];

const deployIntent = smartContractIntentsFactory.createTransactionIntentForUpgrade(sender, contract, adderByteCode.valueOf(), gasLimit, args);
const abiDeployIntent = abiSmartContractIntentsFactory.createTransactionIntentForUpgrade(sender, contract, adderByteCode.valueOf(), gasLimit, args);
const deployIntent = factory.createTransactionIntentForUpgrade({
sender: sender,
contract: contract,
bytecode: adderByteCode.valueOf(),
gasLimit: gasLimit,
args: args
});
const abiDeployIntent = abiAwareFactory.createTransactionIntentForUpgrade({
sender: sender,
contract: contract,
bytecode: adderByteCode.valueOf(),
gasLimit: gasLimit,
args: args
});

assert.equal(deployIntent.sender, "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th");
assert.equal(deployIntent.receiver, "erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4");
assert.isDefined(deployIntent.data);

if (deployIntent.data) {
let decoder = new TextDecoder();
assert(decoder.decode(deployIntent.data).startsWith("upgradeContract@", 0));
assert(checkIfByteArrayStartsWith(deployIntent.data, "upgradeContract@"));

const expectedGasLimit = 6000000 + 50000 + 1500 * deployIntent.data.length;
assert.equal(deployIntent.gasLimit.valueOf(), expectedGasLimit);
Expand All @@ -105,4 +153,15 @@ describe("test smart contract intents factory", function () {

assert.deepEqual(deployIntent, abiDeployIntent);
});

function checkIfByteArrayStartsWith(array: Uint8Array, sequence: string) {
const sequenceBytes = Buffer.from(sequence);

for (let i = 0; i < sequenceBytes.length; i++) {
if (sequenceBytes[i] !== array[i]) {
return false;
}
}
return true;
}
});
Loading

0 comments on commit 5ba4506

Please sign in to comment.