Skip to content

Commit

Permalink
Add basic feature abi and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Sep 4, 2024
1 parent c59657d commit ad8d581
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/abi/typeFormulaParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class TypeFormulaParser {

parseExpression(expression: string): TypeFormula {
expression = expression.trim();

const tokens = this.tokenizeExpression(expression).filter((token) => token !== TypeFormulaParser.COMMA);
const stack: any[] = [];

Expand All @@ -31,8 +30,9 @@ export class TypeFormulaParser {
// It's a type name. We push it as a simple string.
stack.push(token);
}
console.log({ stack });
}

console.log({ stack: JSON.stringify(stack) });
if (stack.length !== 1) {
throw new Error(`Unexpected stack length at end of parsing: ${stack.length}`);
}
Expand Down Expand Up @@ -83,6 +83,7 @@ export class TypeFormulaParser {
private acquireTypeWithParameters(stack: any[]): TypeFormula {
const typeParameters = this.acquireTypeParameters(stack);
const typeName = stack.pop();

if (typeName === "ManagedDecimal" || typeName === "ManagedDecimalSigned") {
const typeFormula = new TypeFormula(typeName, [], typeParameters[0].name);
return typeFormula;
Expand All @@ -96,7 +97,7 @@ export class TypeFormulaParser {

while (true) {
const item = stack.pop();

console.log({ item });
if (item === undefined) {
throw new Error("Badly specified type parameters");
}
Expand Down
1 change: 0 additions & 1 deletion src/smartcontracts/codec/managedDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class ManagedDecimalCodec {
encodeNested(value: ManagedDecimalValue): Buffer {
let buffers: Buffer[] = [];
buffers.push(Buffer.from(this.binaryCodec.encodeTopLevel(new BigUIntValue(value.valueOf()))));
buffers.push(Buffer.from(this.binaryCodec.encodeTopLevel(new U64Value(value.getScale()))));
return Buffer.concat(buffers);
}

Expand Down
81 changes: 81 additions & 0 deletions src/smartcontracts/interaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ResultsParser } from "./resultsParser";
import { TransactionWatcher } from "../transactionWatcher";
import { SmartContractQueriesController } from "../smartContractQueriesController";
import { QueryRunnerAdapter } from "../adapters/queryRunnerAdapter";
import { ManagedDecimalValue } from "./typesystem";

describe("test smart contract interactor", function () {
let provider = createLocalnetProvider();
Expand Down Expand Up @@ -184,6 +185,86 @@ describe("test smart contract interactor", function () {
assert.isTrue(typedBundle.returnCode.equals(ReturnCode.Ok));
});

it.only("should interact with 'basic-features' (local testnet) using the SmartContractTransactionsFactory", async function () {
this.timeout(140000);

let abiRegistry = await loadAbiRegistry("src/testdata/basic-features.abi.json");
let contract = new SmartContract({ abi: abiRegistry });
let controller = new ContractController(provider);

let network = await provider.getNetworkConfig();
await alice.sync(provider);

// Deploy the contract
let deployTransaction = await prepareDeployment({
contract: contract,
deployer: alice,
codePath: "src/testdata/basic-features.wasm",
gasLimit: 600000000,
initArguments: [],
chainID: network.ChainID,
});

let {
bundle: { returnCode },
} = await controller.deploy(deployTransaction);
assert.isTrue(returnCode.isSuccess());

// let startInteraction = <Interaction>(
// contract.methods
// .returns_egld_decimal([])
// .withGasLimit(8000000)
// .withChainID(network.ChainID)
// .withSender(alice.address)
// .withValue(1)
// );

// // start()
// let startTransaction = startInteraction
// .withSender(alice.address)
// .useThenIncrementNonceOf(alice.account)
// .buildTransaction();

let additionInteraction = <Interaction>contract.methods
.managed_decimal_addition([new ManagedDecimalValue(122, 5), new ManagedDecimalValue(3, 6)])
.withGasLimit(10000000)
.withChainID(network.ChainID)
.withSender(alice.address)
.withValue(0);

// addition()
let additionTransaction = additionInteraction
.withSender(alice.address)
.useThenIncrementNonceOf(alice.account)
.buildTransaction();

// let mdLnInteraction = <Interaction>contract.methods
// .managed_decimal_ln([new ManagedDecimalValue(23, 9)])
// .withGasLimit(10000000)
// .withChainID(network.ChainID)
// .withSender(alice.address)
// .withValue(0);

// // mdLn()
// let mdLnTransaction = mdLnInteraction
// .withSender(alice.address)
// .useThenIncrementNonceOf(alice.account)
// .buildTransaction();

// await signTransaction({ transaction: startTransaction, wallet: alice });
// let { bundle: bundleStart } = await controller.execute(startInteraction, startTransaction);
// assert.isTrue(bundleStart.returnCode.equals(ReturnCode.Ok));
// assert.lengthOf(bundleStart.values, 1);
// assert.deepEqual(bundleStart.values[0], new ManagedDecimalValue(1, 18));

await signTransaction({ transaction: additionTransaction, wallet: alice });
let { bundle: bundleAddition } = await controller.execute(additionInteraction, additionTransaction);
assert.isTrue(bundleAddition.returnCode.equals(ReturnCode.Ok));
assert.lengthOf(bundleAddition.values, 1);
assert.deepEqual(bundleAddition.values[0], new ManagedDecimalValue(new BigNumber(5), 2));
// assert.deepEqual(bundleAddition.values[0], new ManagedDecimalValue(new BigNumber(3.135553845), 9));
});

it("should interact with 'counter' (local testnet)", async function () {
this.timeout(120000);

Expand Down
Loading

0 comments on commit ad8d581

Please sign in to comment.