Skip to content

Commit 8f7700a

Browse files
authored
Move multisend tool and add solidity test (iotexproject#1023)
1 parent 73733a3 commit 8f7700a

File tree

6 files changed

+294
-0
lines changed

6 files changed

+294
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity >=0.4.21 <0.6.0;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
constructor() public {
8+
owner = msg.sender;
9+
}
10+
11+
modifier restricted() {
12+
if (msg.sender == owner) _;
13+
}
14+
15+
function setCompleted(uint completed) public restricted {
16+
last_completed_migration = completed;
17+
}
18+
19+
function upgrade(address new_address) public restricted {
20+
Migrations upgraded = Migrations(new_address);
21+
upgraded.setCompleted(last_completed_migration);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Migrations = artifacts.require("Migrations");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};
File renamed without changes.
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
const Multisend = artifacts.require('Multisend.sol');
2+
3+
contract('Multisend', function (accounts) {
4+
beforeEach(async function(){
5+
this.multisend = await Multisend.new();
6+
})
7+
describe('multisend',function(){
8+
let recipients1 = ["0x709b248f7B839078724B4E40904fBD7B694ba1Dc", "0xB9b727B4B37157103541FAeF8E8A48451d7aC18D"];
9+
let recipients2 = ["0xB9b727B4B37157103541FAeF8E8A48451d7aC18D", "0xB9b727B4B37157103541FAeF8E8A48451d7aC18D"];
10+
let recipients3 = [];
11+
for (let i = 1; i <= 301; i++) {
12+
recipients3.push("0xB9b727B4B37157103541FAeF8E8A48451d7aC18D");
13+
}
14+
let amounts1 = [100, 200];
15+
let amounts2 = [];
16+
for (let i = 1; i <= 301; i++) {
17+
amounts2.push(i+1);
18+
}
19+
let amounts3 = [100, 0]
20+
let payload1 = "";
21+
let payload2 = "testPayload";
22+
let wrongRecipients1 = ["0x709b248f7B839078724B4E40904fBD7B694ba1Dc",
23+
"0xB9b727B4B37157103541FAeF8E8A48451d7aC18D", "0xB9b727B4B37157103541FAeF8E8A48451d7aC18D"];
24+
let wrongRecipients2 = ["0xB9b727B4B37157103541FAeF8E8A48451d7aC18C", "0xB9b727B4B37157103541FAeF8E8A48451d7aC18D"];
25+
let wrongAmounts1 = [100, 200, 400];
26+
let wrongAmounts2 = [100, -200];
27+
let emptyArray = [];
28+
let tx;
29+
it('should emit two Transfer events and one payload event', async function(){
30+
tx = await this.multisend.multiSend(recipients1, amounts1, payload1, { from: accounts[0], value: 300 });
31+
const { logs } = tx;
32+
assert.equal(logs.length, 3);
33+
for (let i = 0; i < 2; i++){
34+
assert.equal(logs[i].event,'Transfer');
35+
assert.equal(logs[i].args.recipient, recipients1[i]);
36+
assert.equal(logs[i].args.amount, amounts1[i]);
37+
}
38+
assert.equal(logs[2].event,'Payload');
39+
assert.equal(logs[2].args.payload, payload1);
40+
})
41+
it('should emit two Transfer events and one payload event', async function(){
42+
tx = await this.multisend.multiSend(recipients2, amounts1, payload2, { from: accounts[0], value: 300 });
43+
const { logs } = tx;
44+
assert.equal(logs.length, 3);
45+
for (let i = 0; i < 2; i++){
46+
assert.equal(logs[i].event,'Transfer');
47+
assert.equal(logs[i].args.recipient, recipients2[i]);
48+
assert.equal(logs[i].args.amount, amounts1[i]);
49+
}
50+
assert.equal(logs[2].event,'Payload');
51+
assert.equal(logs[2].args.payload, payload2);
52+
})
53+
it('should emit two Transfer events and one payload event', async function(){
54+
tx = await this.multisend.multiSend(recipients2, amounts3, payload2, { from: accounts[0], value: 100 });
55+
const { logs } = tx;
56+
assert.equal(logs.length, 3);
57+
for (let i = 0; i < 2; i++){
58+
assert.equal(logs[i].event,'Transfer');
59+
assert.equal(logs[i].args.recipient, recipients2[i]);
60+
assert.equal(logs[i].args.amount, amounts3[i]);
61+
}
62+
assert.equal(logs[2].event,'Payload');
63+
assert.equal(logs[2].args.payload, payload2);
64+
})
65+
it('should emit two Transfer events and one Rufund event and one payload event', async function(){
66+
tx = await this.multisend.multiSend(recipients1, amounts1, payload2, { from: accounts[0], value: 315 });
67+
const { logs } = tx;
68+
assert.equal(logs.length, 4);
69+
for (let i = 0; i < 2; i++){
70+
assert.equal(logs[i].event,'Transfer');
71+
assert.equal(logs[i].args.recipient, recipients1[i]);
72+
assert.equal(logs[i].args.amount, amounts1[i]);
73+
}
74+
assert.equal(logs[2].event,'Refund');
75+
assert.equal(logs[2].args.refund, 15);
76+
assert.equal(logs[3].event,'Payload');
77+
assert.equal(logs[3].args.payload, payload2);
78+
})
79+
it('failed due to not enougn token', async function(){
80+
let Error;
81+
try {
82+
await this.multisend.multiSend(recipients1, amounts1, payload2, { from: accounts[0], value: 215 });
83+
assert.fail();
84+
} catch (error) {
85+
Error = error;
86+
}
87+
assert.notEqual(Error, undefined, 'Exception thrown');
88+
assert.isAbove(Error.message.search('not enough token'), -1);
89+
})
90+
it('failed due to more recipients', async function(){
91+
let Error;
92+
try {
93+
await this.multisend.multiSend(wrongRecipients1, amounts1, payload2, { from: accounts[0], value: 300 });
94+
assert.fail();
95+
} catch (error) {
96+
Error = error;
97+
}
98+
assert.notEqual(Error, undefined, 'Exception thrown');
99+
assert.isAbove(Error.message.search('parameters not match'), -1);
100+
})
101+
it('failed due to more amounts', async function(){
102+
let Error;
103+
try {
104+
await this.multisend.multiSend(recipients1, wrongAmounts1, payload2, { from: accounts[0], value: 300 });
105+
assert.fail();
106+
} catch (error) {
107+
Error = error;
108+
}
109+
assert.notEqual(Error, undefined, 'Exception thrown');
110+
assert.isAbove(Error.message.search('parameters not match'), -1);
111+
112+
})
113+
it('failed due to invalid recipient', async function(){
114+
let Error;
115+
try {
116+
await this.multisend.multiSend(wrongRecipients2, amounts1, payload2, { from: accounts[0], value: 300 });
117+
assert.fail();
118+
} catch (error) {
119+
Error = error;
120+
}
121+
assert.notEqual(Error, undefined, 'Exception thrown');
122+
})
123+
it('failed due to invalid amounts', async function(){
124+
let Error;
125+
try {
126+
await this.multisend.multiSend(recipients1, wrongAmounts2, payload2, { from: accounts[0], value: 300 });
127+
assert.fail();
128+
} catch (error) {
129+
Error = error;
130+
}
131+
assert.notEqual(Error, undefined, 'Exception thrown');
132+
})
133+
it('failed due to empty recipients', async function(){
134+
let Error;
135+
try {
136+
await this.multisend.multiSend(emptyArray, amounts1, payload2, { from: accounts[0], value: 300 });
137+
assert.fail();
138+
} catch (error) {
139+
Error = error;
140+
}
141+
assert.notEqual(Error, undefined, 'Exception thrown');
142+
assert.isAbove(Error.message.search('parameters not match'), -1);
143+
})
144+
it('failed due to empty amounts', async function(){
145+
let Error;
146+
try {
147+
await this.multisend.multiSend(recipients1, emptyArray, payload2, { from: accounts[0], value: 300 });
148+
assert.fail();
149+
} catch (error) {
150+
Error = error;
151+
}
152+
assert.notEqual(Error, undefined, 'Exception thrown');
153+
assert.isAbove(Error.message.search('parameters not match'), -1);
154+
})
155+
it('failed due to too many recipients', async function(){
156+
let Error;
157+
try {
158+
await this.multisend.multiSend(recipients3, amounts2, payload2, { from: accounts[0], value: 300000 });
159+
assert.fail();
160+
} catch (error) {
161+
Error = error;
162+
}
163+
assert.notEqual(Error, undefined, 'Exception thrown');
164+
assert.isAbove(Error.message.search('number of recipients is larger than 300'), -1);
165+
})
166+
})
167+
})

tools/multisend/truffle-config.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Use this file to configure your truffle project. It's seeded with some
3+
* common settings for different networks and features like migrations,
4+
* compilation and testing. Uncomment the ones you need or modify
5+
* them to suit your project as necessary.
6+
*
7+
* More information about configuration can be found at:
8+
*
9+
* truffleframework.com/docs/advanced/configuration
10+
*
11+
* To deploy via Infura you'll need a wallet provider (like truffle-hdwallet-provider)
12+
* to sign your transactions before they're sent to a remote public node. Infura API
13+
* keys are available for free at: infura.io/register
14+
*
15+
* You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
16+
* public/private key pairs. If you're publishing your code to GitHub make sure you load this
17+
* phrase from a file you've .gitignored so it doesn't accidentally become public.
18+
*
19+
*/
20+
21+
// const HDWalletProvider = require('truffle-hdwallet-provider');
22+
// const infuraKey = "fj4jll3k.....";
23+
//
24+
// const fs = require('fs');
25+
// const mnemonic = fs.readFileSync(".secret").toString().trim();
26+
27+
module.exports = {
28+
/**
29+
* Networks define how you connect to your ethereum client and let you set the
30+
* defaults web3 uses to send transactions. If you don't specify one truffle
31+
* will spin up a development blockchain for you on port 9545 when you
32+
* run `develop` or `test`. You can ask a truffle command to use a specific
33+
* network from the command line, e.g
34+
*
35+
* $ truffle test --network <network-name>
36+
*/
37+
38+
networks: {
39+
// Useful for testing. The `development` name is special - truffle uses it by default
40+
// if it's defined here and no other network is specified at the command line.
41+
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
42+
// tab if you use this network and you must also set the `host`, `port` and `network_id`
43+
// options below to some value.
44+
//
45+
// development: {
46+
// host: "127.0.0.1", // Localhost (default: none)
47+
// port: 8545, // Standard Ethereum port (default: none)
48+
// network_id: "*", // Any network (default: none)
49+
// },
50+
51+
// Another network with more advanced options...
52+
// advanced: {
53+
// port: 8777, // Custom port
54+
// network_id: 1342, // Custom network
55+
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
56+
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
57+
// from: <address>, // Account to send txs from (default: accounts[0])
58+
// websockets: true // Enable EventEmitter interface for web3 (default: false)
59+
// },
60+
61+
// Useful for deploying to a public network.
62+
// NB: It's important to wrap the provider as a function.
63+
// ropsten: {
64+
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/${infuraKey}`),
65+
// network_id: 3, // Ropsten's id
66+
// gas: 5500000, // Ropsten has a lower block limit than mainnet
67+
// confirmations: 2, // # of confs to wait between deployments. (default: 0)
68+
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
69+
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
70+
// },
71+
72+
// Useful for private networks
73+
// private: {
74+
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
75+
// network_id: 2111, // This network is yours, in the cloud.
76+
// production: true // Treats this network as if it was a public net. (default: false)
77+
// }
78+
},
79+
80+
// Set default mocha options here, use special reporters etc.
81+
mocha: {
82+
// timeout: 100000
83+
},
84+
85+
// Configure your compilers
86+
compilers: {
87+
solc: {
88+
version: "0.4.24", // Fetch exact version from solc-bin (default: truffle's version)
89+
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
90+
// settings: { // See the solidity docs for advice about optimization and evmVersion
91+
// optimizer: {
92+
// enabled: false,
93+
// runs: 200
94+
// },
95+
// evmVersion: "byzantium"
96+
// }
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)