github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/multisend/test/Multisend.test.js (about) 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 })