Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Issue 423: Array items getting lost #219

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "node"
- "lts/carbon"
- "lts/*"
- "lts/carbon"


addons:
Expand Down
91 changes: 82 additions & 9 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions test/ArrayOfStructs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity ^0.4.2;

contract ArrayOfStructs {
struct PayRecord {
address sender;
uint256 sum;
uint256 blockNumber;
uint256 status;
}

event PaymentPlaced(address senderAddress, uint256 blockNumber, uint256 payIndex, string guid);

PayRecord[] public payments;

function payForSomething(string guid) public payable {
uint256 newLength = payments.push(PayRecord(msg.sender, msg.value, block.number, 0));
PaymentPlaced(msg.sender, block.number, newLength-1, guid);
}

function changeSomething(uint256 paymentIndex) public view {
if (payments[paymentIndex].status == 0) {
payments[paymentIndex].status == 1;
}
}
}
74 changes: 74 additions & 0 deletions test/solidity_array_structs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const Web3 = require("web3");
const assert = require("assert");
const Ganache = require("../index");
const path = require("path");
const compileAndDeploy = require("./helpers/contracts").compileAndDeploy;

const mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";

function setUp(options = { mnemonic }, contractName = "ArrayOfStructs") {
const context = {
options: options,
provider: null,
web3: null,
accounts: [],
contractArtifact: {},
instance: null
};

before("setup web3", async function() {
context.provider = Ganache.provider(context.options);
context.options.blockTime = 2000;
context.web3 = new Web3(context.provider);
});

before("compile source", async function() {
this.timeout(10000);
context.contractArtifact = await compileAndDeploy(
path.join(__dirname, ".", `${contractName}.sol`),
contractName,
context.web3
);
context.instance = context.contractArtifact.instance;
});
return context;
}

describe("Array of Structures", function() {
const context = setUp();
it("can add Structs to an Array", async function() {
this.timeout(10000);
const myGuid = "Payment1";
const paymentIndex = 0;
const account = "0x627306090abab3a6e1400e9345bc60c78a8bef57";
const amount = 10;
const gas = 5000000;
const iterations = 100;

let response = await context.instance.methods.payForSomething(myGuid).send({
from: account,
value: amount,
gas: gas
});

assert.strictEqual(response.events.PaymentPlaced.returnValues.guid, myGuid);
assert.strictEqual(account, response.events.PaymentPlaced.returnValues.senderAddress.toLowerCase());
assert.strictEqual(parseInt(response.events.PaymentPlaced.returnValues.blockNumber), 2);
assert.strictEqual(parseInt(response.events.PaymentPlaced.returnValues.payIndex), 0);

await context.instance.methods.changeSomething(paymentIndex).call();

for (let i = 0; i < iterations; i++) {
const response = await context.instance.methods.payForSomething(myGuid).send({
from: account,
value: amount,
gas: gas
});

assert.strictEqual(response.events.PaymentPlaced.returnValues.guid, myGuid);
assert.strictEqual(account, response.events.PaymentPlaced.returnValues.senderAddress.toLowerCase());
assert.strictEqual(parseInt(response.events.PaymentPlaced.returnValues.blockNumber), i + 3);
assert.strictEqual(parseInt(response.events.PaymentPlaced.returnValues.payIndex), i + 1);
}
});
});