ContractTest.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. <?php
  2. namespace Test\Unit;
  3. use Test\TestCase;
  4. use Web3\Providers\HttpProvider;
  5. use Web3\RequestManagers\RequestManager;
  6. use Web3\RequestManagers\HttpRequestManager;
  7. use Web3\Contract;
  8. use Web3\Utils;
  9. use Web3\Contracts\Ethabi;
  10. use Web3\Formatters\IntegerFormatter;
  11. use phpseclib\Math\BigInteger as BigNumber;
  12. class ContractTest extends TestCase
  13. {
  14. /**
  15. * contract
  16. *
  17. * @var \Web3\Contract
  18. */
  19. protected $contract;
  20. /**
  21. * testAbi
  22. * GameToken abi from https://github.com/sc0Vu/GameToken
  23. *
  24. * @var string
  25. */
  26. protected $testAbi = '[
  27. {
  28. "constant": true,
  29. "inputs": [],
  30. "name": "name",
  31. "outputs": [
  32. {
  33. "name": "",
  34. "type": "string"
  35. }
  36. ],
  37. "payable": false,
  38. "stateMutability": "view",
  39. "type": "function"
  40. },
  41. {
  42. "constant": false,
  43. "inputs": [
  44. {
  45. "name": "_spender",
  46. "type": "address"
  47. },
  48. {
  49. "name": "_value",
  50. "type": "uint256"
  51. }
  52. ],
  53. "name": "approve",
  54. "outputs": [
  55. {
  56. "name": "success",
  57. "type": "bool"
  58. }
  59. ],
  60. "payable": false,
  61. "stateMutability": "nonpayable",
  62. "type": "function"
  63. },
  64. {
  65. "constant": true,
  66. "inputs": [],
  67. "name": "totalSupply",
  68. "outputs": [
  69. {
  70. "name": "",
  71. "type": "uint256"
  72. }
  73. ],
  74. "payable": false,
  75. "stateMutability": "view",
  76. "type": "function"
  77. },
  78. {
  79. "constant": false,
  80. "inputs": [
  81. {
  82. "name": "_from",
  83. "type": "address"
  84. },
  85. {
  86. "name": "_to",
  87. "type": "address"
  88. },
  89. {
  90. "name": "_value",
  91. "type": "uint256"
  92. }
  93. ],
  94. "name": "transferFrom",
  95. "outputs": [
  96. {
  97. "name": "success",
  98. "type": "bool"
  99. }
  100. ],
  101. "payable": false,
  102. "stateMutability": "nonpayable",
  103. "type": "function"
  104. },
  105. {
  106. "constant": true,
  107. "inputs": [],
  108. "name": "decimals",
  109. "outputs": [
  110. {
  111. "name": "",
  112. "type": "uint8"
  113. }
  114. ],
  115. "payable": false,
  116. "stateMutability": "view",
  117. "type": "function"
  118. },
  119. {
  120. "constant": true,
  121. "inputs": [],
  122. "name": "standard",
  123. "outputs": [
  124. {
  125. "name": "",
  126. "type": "string"
  127. }
  128. ],
  129. "payable": false,
  130. "stateMutability": "view",
  131. "type": "function"
  132. },
  133. {
  134. "constant": true,
  135. "inputs": [
  136. {
  137. "name": "",
  138. "type": "address"
  139. }
  140. ],
  141. "name": "balanceOf",
  142. "outputs": [
  143. {
  144. "name": "",
  145. "type": "uint256"
  146. }
  147. ],
  148. "payable": false,
  149. "stateMutability": "view",
  150. "type": "function"
  151. },
  152. {
  153. "constant": true,
  154. "inputs": [],
  155. "name": "symbol",
  156. "outputs": [
  157. {
  158. "name": "",
  159. "type": "string"
  160. }
  161. ],
  162. "payable": false,
  163. "stateMutability": "view",
  164. "type": "function"
  165. },
  166. {
  167. "constant": false,
  168. "inputs": [
  169. {
  170. "name": "_to",
  171. "type": "address"
  172. },
  173. {
  174. "name": "_value",
  175. "type": "uint256"
  176. }
  177. ],
  178. "name": "transfer",
  179. "outputs": [],
  180. "payable": false,
  181. "stateMutability": "nonpayable",
  182. "type": "function"
  183. },
  184. {
  185. "constant": true,
  186. "inputs": [
  187. {
  188. "name": "",
  189. "type": "address"
  190. },
  191. {
  192. "name": "",
  193. "type": "address"
  194. }
  195. ],
  196. "name": "allowance",
  197. "outputs": [
  198. {
  199. "name": "",
  200. "type": "uint256"
  201. }
  202. ],
  203. "payable": false,
  204. "stateMutability": "view",
  205. "type": "function"
  206. },
  207. {
  208. "inputs": [
  209. {
  210. "name": "initialSupply",
  211. "type": "uint256"
  212. },
  213. {
  214. "name": "tokenName",
  215. "type": "string"
  216. },
  217. {
  218. "name": "decimalUnits",
  219. "type": "uint8"
  220. },
  221. {
  222. "name": "tokenSymbol",
  223. "type": "string"
  224. }
  225. ],
  226. "payable": false,
  227. "stateMutability": "nonpayable",
  228. "type": "constructor"
  229. },
  230. {
  231. "anonymous": false,
  232. "inputs": [
  233. {
  234. "indexed": true,
  235. "name": "from",
  236. "type": "address"
  237. },
  238. {
  239. "indexed": true,
  240. "name": "to",
  241. "type": "address"
  242. },
  243. {
  244. "indexed": false,
  245. "name": "value",
  246. "type": "uint256"
  247. }
  248. ],
  249. "name": "Transfer",
  250. "type": "event"
  251. },
  252. {
  253. "anonymous": false,
  254. "inputs": [
  255. {
  256. "indexed": true,
  257. "name": "_owner",
  258. "type": "address"
  259. },
  260. {
  261. "indexed": true,
  262. "name": "_spender",
  263. "type": "address"
  264. },
  265. {
  266. "indexed": false,
  267. "name": "_value",
  268. "type": "uint256"
  269. }
  270. ],
  271. "name": "Approval",
  272. "type": "event"
  273. }
  274. ]';
  275. /**
  276. * testBytecode
  277. * GameToken abi from https://github.com/sc0Vu/GameToken
  278. *
  279. * @var string
  280. */
  281. protected $testBytecode = '0x60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a20029';
  282. /**
  283. * testUserAbi
  284. * User abi from https://github.com/BlockHR/contracts
  285. *
  286. * @var string
  287. */
  288. protected $testUserAbi = '[
  289. {
  290. "constant": false,
  291. "inputs": [
  292. {
  293. "name": "ethAddress",
  294. "type": "address"
  295. }
  296. ],
  297. "name": "getUser",
  298. "outputs": [
  299. {
  300. "name": "firstName",
  301. "type": "string"
  302. },
  303. {
  304. "name": "lastName",
  305. "type": "string"
  306. },
  307. {
  308. "name": "age",
  309. "type": "uint256"
  310. }
  311. ],
  312. "payable": false,
  313. "stateMutability": "nonpayable",
  314. "type": "function"
  315. },
  316. {
  317. "constant": false,
  318. "inputs": [
  319. {
  320. "name": "ethAddress",
  321. "type": "address"
  322. },
  323. {
  324. "name": "firstName",
  325. "type": "string"
  326. },
  327. {
  328. "name": "lastName",
  329. "type": "string"
  330. },
  331. {
  332. "name": "age",
  333. "type": "uint256"
  334. }
  335. ],
  336. "name": "addUser",
  337. "outputs": [],
  338. "payable": false,
  339. "stateMutability": "nonpayable",
  340. "type": "function"
  341. },
  342. {
  343. "inputs": [],
  344. "payable": false,
  345. "stateMutability": "nonpayable",
  346. "type": "constructor"
  347. },
  348. {
  349. "payable": false,
  350. "stateMutability": "nonpayable",
  351. "type": "fallback"
  352. },
  353. {
  354. "anonymous": false,
  355. "inputs": [
  356. {
  357. "indexed": true,
  358. "name": "ethAddress",
  359. "type": "address"
  360. },
  361. {
  362. "indexed": false,
  363. "name": "firstName",
  364. "type": "string"
  365. },
  366. {
  367. "indexed": false,
  368. "name": "lastName",
  369. "type": "string"
  370. },
  371. {
  372. "indexed": false,
  373. "name": "age",
  374. "type": "uint256"
  375. }
  376. ],
  377. "name": "AddUser",
  378. "type": "event"
  379. }
  380. ]';
  381. /**
  382. * testUserBytecode
  383. * User bytecode from https://github.com/BlockHR/contracts
  384. *
  385. * @var string
  386. */
  387. protected $testUserBytecode = '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506108ee8061005e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636f77926b1461005c578063b3c2583514610181575b341561005757600080fd5b600080fd5b341561006757600080fd5b610093600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610249565b604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b838110156100dd5780820151818401526020810190506100c2565b50505050905090810190601f16801561010a5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610143578082015181840152602081019050610128565b50505050905090810190601f1680156101705780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b341561018c57600080fd5b610247600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091908035906020019091905050610545565b005b6102516107c7565b6102596107c7565b60003373ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16141580156102e457506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614155b156102ee5761053e565b6000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020154141561033e5761053e565b600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104145780601f106103e957610100808354040283529160200191610414565b820191906000526020600020905b8154815290600101906020018083116103f757829003601f168201915b50505050509250600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104f15780601f106104c6576101008083540402835291602001916104f1565b820191906000526020600020905b8154815290600101906020018083116104d457829003601f168201915b50505050509150600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206002015490505b9193909250565b61054d6107db565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156107c0576000600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201541415156105f3576107bf565b8381600001819052508281602001819052508181604001818152505080600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082015181600001908051906020019061066b929190610809565b506020820151816001019080519060200190610688929190610809565b50604082015181600201559050508473ffffffffffffffffffffffffffffffffffffffff167f2771be550edc032daf255a8987e0164bcfad0a5b97238d8961f9c5e38fa3e4f3858585604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561071b578082015181840152602081019050610700565b50505050905090810190601f1680156107485780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610781578082015181840152602081019050610766565b50505050905090810190601f1680156107ae5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390a25b5b5050505050565b602060405190810160405280600081525090565b6060604051908101604052806107ef610889565b81526020016107fc610889565b8152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061084a57805160ff1916838001178555610878565b82800160010185558215610878579182015b8281111561087757825182559160200191906001019061085c565b5b509050610885919061089d565b5090565b602060405190810160405280600081525090565b6108bf91905b808211156108bb5760008160009055506001016108a3565b5090565b905600a165627a7a72305820f4dd7dc4c22792dec463ec68973ad2ed12d5994e96a9e7b34db512d6e38143090029';
  388. /**
  389. * accounts
  390. *
  391. * @var array
  392. */
  393. protected $accounts;
  394. /**
  395. * contractAddress
  396. *
  397. * @var string
  398. */
  399. protected $contractAddress;
  400. /**
  401. * setUp
  402. *
  403. * @return void
  404. */
  405. public function setUp()
  406. {
  407. parent::setUp();
  408. $this->contract = new Contract($this->web3->provider, $this->testAbi);
  409. $this->contract->eth->accounts(function ($err, $accounts) {
  410. if ($err === null) {
  411. if (isset($accounts)) {
  412. $this->accounts = $accounts;
  413. return;
  414. }
  415. }
  416. });
  417. }
  418. /**
  419. * testInstance
  420. *
  421. * @return void
  422. */
  423. public function testInstance()
  424. {
  425. $contract = new Contract($this->testHost, $this->testAbi);
  426. $this->assertTrue($contract->provider instanceof HttpProvider);
  427. $this->assertTrue($contract->provider->requestManager instanceof RequestManager);
  428. }
  429. /**
  430. * testSetProvider
  431. *
  432. * @return void
  433. */
  434. public function testSetProvider()
  435. {
  436. $contract = $this->contract;
  437. $requestManager = new HttpRequestManager('http://localhost:8545');
  438. $contract->provider = new HttpProvider($requestManager);
  439. $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545');
  440. $contract->provider = null;
  441. $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545');
  442. }
  443. /**
  444. * testDeploy
  445. *
  446. * @return void
  447. */
  448. public function testDeploy()
  449. {
  450. $contract = $this->contract;
  451. if (!isset($this->accounts[0])) {
  452. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  453. } else {
  454. $account = $this->accounts[0];
  455. }
  456. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  457. 'from' => $account,
  458. 'gas' => '0x200b20'
  459. ], function ($err, $result) use ($contract) {
  460. if ($err !== null) {
  461. return $this->fail($err->getMessage());
  462. }
  463. if ($result) {
  464. echo "\nTransaction has made:) id: " . $result . "\n";
  465. }
  466. $transactionId = $result;
  467. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  468. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  469. if ($err !== null) {
  470. return $this->fail($err);
  471. }
  472. if ($transaction) {
  473. $this->contractAddress = $transaction->contractAddress;
  474. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  475. }
  476. });
  477. });
  478. }
  479. /**
  480. * testSend
  481. *
  482. * @return void
  483. */
  484. public function testSend()
  485. {
  486. $contract = $this->contract;
  487. if (!isset($this->accounts[0])) {
  488. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  489. } else {
  490. $fromAccount = $this->accounts[0];
  491. }
  492. if (!isset($this->accounts[1])) {
  493. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  494. } else {
  495. $toAccount = $this->accounts[1];
  496. }
  497. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  498. 'from' => $fromAccount,
  499. 'gas' => '0x200b20'
  500. ], function ($err, $result) use ($contract) {
  501. if ($err !== null) {
  502. return $this->fail($err->getMessage());
  503. }
  504. if ($result) {
  505. echo "\nTransaction has made:) id: " . $result . "\n";
  506. }
  507. $transactionId = $result;
  508. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  509. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  510. if ($err !== null) {
  511. return $this->fail($err);
  512. }
  513. if ($transaction) {
  514. $this->contractAddress = $transaction->contractAddress;
  515. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  516. }
  517. });
  518. });
  519. if (!isset($this->contractAddress)) {
  520. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  521. }
  522. $contract->at($this->contractAddress)->send('transfer', $toAccount, 16, [
  523. 'from' => $fromAccount,
  524. 'gas' => '0x200b20'
  525. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  526. if ($err !== null) {
  527. return $this->fail($err->getMessage());
  528. }
  529. if ($result) {
  530. echo "\nTransaction has made:) id: " . $result . "\n";
  531. }
  532. $transactionId = $result;
  533. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  534. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  535. if ($err !== null) {
  536. return $this->fail($err);
  537. }
  538. if ($transaction) {
  539. $topics = $transaction->logs[0]->topics;
  540. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  541. // validate topics
  542. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['Transfer']), $topics[0]);
  543. $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]);
  544. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]);
  545. }
  546. });
  547. });
  548. }
  549. /**
  550. * testCall
  551. *
  552. * @return void
  553. */
  554. public function testCall()
  555. {
  556. $contract = $this->contract;
  557. if (!isset($this->accounts[0])) {
  558. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  559. } else {
  560. $fromAccount = $this->accounts[0];
  561. }
  562. if (!isset($this->accounts[1])) {
  563. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  564. } else {
  565. $toAccount = $this->accounts[1];
  566. }
  567. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  568. 'from' => $fromAccount,
  569. 'gas' => '0x200b20'
  570. ], function ($err, $result) use ($contract) {
  571. if ($err !== null) {
  572. return $this->fail($err->getMessage());
  573. }
  574. if ($result) {
  575. echo "\nTransaction has made:) id: " . $result . "\n";
  576. }
  577. $transactionId = $result;
  578. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  579. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  580. if ($err !== null) {
  581. return $this->fail($err);
  582. }
  583. if ($transaction) {
  584. $this->contractAddress = $transaction->contractAddress;
  585. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  586. }
  587. });
  588. });
  589. if (!isset($this->contractAddress)) {
  590. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  591. }
  592. $contract->at($this->contractAddress)->call('balanceOf', $fromAccount, [
  593. 'from' => $fromAccount
  594. ], function ($err, $result) use ($contract) {
  595. if ($err !== null) {
  596. return $this->fail($err->getMessage());
  597. }
  598. if (isset($result)) {
  599. // $bn = Utils::toBn($result);
  600. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  601. $this->assertTrue($result !== null);
  602. }
  603. });
  604. // test issue 143
  605. $contract->at($this->contractAddress)->call('balanceOf', $fromAccount, function ($err, $result) use ($contract) {
  606. if ($err !== null) {
  607. return $this->fail($err->getMessage());
  608. }
  609. if (isset($result)) {
  610. // $bn = Utils::toBn($result);
  611. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  612. $this->assertTrue($result !== null);
  613. }
  614. });
  615. }
  616. /**
  617. * testEstimateGas
  618. *
  619. * @return void
  620. */
  621. public function testEstimateGas()
  622. {
  623. $contract = $this->contract;
  624. if (!isset($this->accounts[0])) {
  625. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  626. } else {
  627. $fromAccount = $this->accounts[0];
  628. }
  629. if (!isset($this->accounts[1])) {
  630. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  631. } else {
  632. $toAccount = $this->accounts[1];
  633. }
  634. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  635. 'from' => $fromAccount,
  636. 'gas' => '0x200b20'
  637. ], function ($err, $result) use ($contract) {
  638. if ($err !== null) {
  639. return $this->fail($err->getMessage());
  640. }
  641. if ($result) {
  642. echo "\nTransaction has made:) id: " . $result . "\n";
  643. }
  644. $transactionId = $result;
  645. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  646. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  647. if ($err !== null) {
  648. return $this->fail($err);
  649. }
  650. if ($transaction) {
  651. $this->contractAddress = $transaction->contractAddress;
  652. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  653. }
  654. });
  655. });
  656. $contract->bytecode($this->testBytecode)->estimateGas(10000, 'Game Token', 1, 'GT', [
  657. 'from' => $fromAccount,
  658. 'gas' => '0x200b20'
  659. ], function ($err, $result) use ($contract) {
  660. if ($err !== null) {
  661. return $this->fail($err->getMessage());
  662. }
  663. if (isset($result)) {
  664. echo "\nEstimate gas: " . $result->toString() . "\n";
  665. $this->assertTrue($result !== null);
  666. }
  667. });
  668. if (!isset($this->contractAddress)) {
  669. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  670. }
  671. $contract->at($this->contractAddress)->estimateGas('balanceOf', $fromAccount, [
  672. 'from' => $fromAccount
  673. ], function ($err, $result) use ($contract) {
  674. if ($err !== null) {
  675. // infura api gg
  676. return $this->assertTrue($err !== null);
  677. }
  678. if (isset($result)) {
  679. echo "\nEstimate gas: " . $result->toString() . "\n";
  680. $this->assertTrue($result !== null);
  681. }
  682. });
  683. }
  684. /**
  685. * testGetData
  686. *
  687. * @return void
  688. */
  689. public function testGetData()
  690. {
  691. $contract = $this->contract;
  692. if (!isset($this->accounts[0])) {
  693. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  694. } else {
  695. $fromAccount = $this->accounts[0];
  696. }
  697. if (!isset($this->accounts[1])) {
  698. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  699. } else {
  700. $toAccount = $this->accounts[1];
  701. }
  702. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  703. 'from' => $fromAccount,
  704. 'gas' => '0x200b20'
  705. ], function ($err, $result) use ($contract) {
  706. if ($err !== null) {
  707. return $this->fail($err->getMessage());
  708. }
  709. if ($result) {
  710. echo "\nTransaction has made:) id: " . $result . "\n";
  711. }
  712. $transactionId = $result;
  713. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  714. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  715. if ($err !== null) {
  716. return $this->fail($err);
  717. }
  718. if ($transaction) {
  719. $this->contractAddress = $transaction->contractAddress;
  720. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  721. }
  722. });
  723. });
  724. $constructorData = $contract->bytecode($this->testBytecode)->getData(10000, 'Game Token', 1, 'GT');
  725. $this->assertEquals('60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a2002900000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000a47616d6520546f6b656e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024754000000000000000000000000000000000000000000000000000000000000', $constructorData);
  726. if (!isset($this->contractAddress)) {
  727. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  728. }
  729. $balanceOfData = $contract->at($this->contractAddress)->getData('balanceOf', $fromAccount);
  730. $this->assertEquals('70a08231000000000000000000000000' . Utils::stripZero($fromAccount), $balanceOfData);
  731. }
  732. /**
  733. * testDecodeMethodReturn
  734. *
  735. * @return void
  736. */
  737. public function testDecodeMethodReturn()
  738. {
  739. $contract = $this->contract;
  740. $contract->abi($this->testUserAbi);
  741. if (!isset($this->accounts[0])) {
  742. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  743. } else {
  744. $fromAccount = $this->accounts[0];
  745. }
  746. if (!isset($this->accounts[1])) {
  747. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  748. } else {
  749. $toAccount = $this->accounts[1];
  750. }
  751. // Deploy user contract.
  752. $contract->bytecode($this->testUserBytecode)->new([
  753. 'from' => $fromAccount,
  754. 'gas' => '0x200b20'
  755. ], function ($err, $result) use ($contract) {
  756. if ($err !== null) {
  757. return $this->fail($err->getMessage());
  758. }
  759. if ($result) {
  760. echo "\nTransaction has made:) id: " . $result . "\n";
  761. }
  762. $transactionId = $result;
  763. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  764. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  765. if ($err !== null) {
  766. return $this->fail($err);
  767. }
  768. if ($transaction) {
  769. $this->contractAddress = $transaction->contractAddress;
  770. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  771. }
  772. });
  773. });
  774. if (!isset($this->contractAddress)) {
  775. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  776. }
  777. // Add user.
  778. $contract->at($this->contractAddress)->send('addUser', $toAccount, 'Peter', 'Lai', 18, [
  779. 'from' => $fromAccount,
  780. 'gas' => '0x200b20'
  781. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  782. if ($err !== null) {
  783. return $this->fail($err->getMessage());
  784. }
  785. if ($result) {
  786. echo "\nTransaction has made:) id: " . $result . "\n";
  787. }
  788. $transactionId = $result;
  789. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  790. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  791. if ($err !== null) {
  792. return $this->fail($err);
  793. }
  794. if ($transaction) {
  795. $topics = $transaction->logs[0]->topics;
  796. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  797. // validate topics
  798. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['AddUser']), $topics[0]);
  799. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[1]);
  800. }
  801. });
  802. });
  803. // Get user.
  804. $contract->call('getUser', $toAccount, [
  805. 'from' => $fromAccount
  806. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  807. if ($err !== null) {
  808. return $this->fail($err->getMessage());
  809. }
  810. if ($result) {
  811. $this->assertEquals($result['firstName'], 'Peter');
  812. $this->assertEquals($result['lastName'], 'Lai');
  813. $this->assertEquals($result['age']->toString(), '18');
  814. }
  815. });
  816. }
  817. /**
  818. * testIssue85
  819. *
  820. * @return void
  821. */
  822. public function testIssue85()
  823. {
  824. $bytecode = '0x608060405234801561001057600080fd5b50610a36806100206000396000f3006080604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630bcd3b33146100b45780631f9030371461014457806325f35c36146101775780632d1be94d146102735780638b84925b146102a2578063a15e05fd146102cd578063a5f807cc1461030f578063d07326681461039f578063ded516d21461043e578063f1d0876a146104ad578063f5c2e88a1461054c575b600080fd5b3480156100c057600080fd5b506100c96105dc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101095780820151818401526020810190506100ee565b50505050905090810190601f1680156101365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015057600080fd5b5061015961061e565b60405180826000191660001916815260200191505060405180910390f35b34801561018357600080fd5b5061018c61064b565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b838110156101d05780820151818401526020810190506101b5565b50505050905090810190601f1680156101fd5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b34801561027f57600080fd5b506102886106cd565b604051808215151515815260200191505060405180910390f35b3480156102ae57600080fd5b506102b76106db565b6040518082815260200191505060405180910390f35b3480156102d957600080fd5b506102e26106e9565b60405180836000191660001916815260200182600019166000191681526020019250505060405180910390f35b34801561031b57600080fd5b50610324610741565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610364578082015181840152602081019050610349565b50505050905090810190601f1680156103915780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103ab57600080fd5b506103b461076a565b60405180806020018360001916600019168152602001828103825284818151815260200191508051906020019080838360005b838110156104025780820151818401526020810190506103e7565b50505050905090810190601f16801561042f5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561044a57600080fd5b506104536107d9565b60405180827effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200191505060405180910390f35b3480156104b957600080fd5b506104c2610806565b60405180836000191660001916815260200180602001828103825283818151815260200191508051906020019080838360005b838110156105105780820151818401526020810190506104f5565b50505050905090810190601f16801561053d5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561055857600080fd5b50610561610875565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156105a1578082015181840152602081019050610586565b50505050905090810190601f1680156105ce5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525090508091505090565b6000807f616263000000000000000000000000000000000000000000000000000000000090508091505090565b6060806060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091506040805190810160405280600381526020017f78797a0000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b600080600190508091505090565b600080606390508091505090565b6000806000807f616263000000000000000000000000000000000000000000000000000000000091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6060806101606040519081016040528061012c81526020016108df61012c913990508091505090565b60606000606060006040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6000807f610000000000000000000000000000000000000000000000000000000000000090508091505090565b60006060600060607f78797a000000000000000000000000000000000000000000000000000000000091506040805190810160405280600381526020017f6162630000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b606080606060405190810160405280603c81526020017f616263616263616263636162636162636162636361626361626361626363616281526020017f6361626361626363616263616263616263636162636162636162636300000000815250905080915050905600616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363a165627a7a7230582020ee9e6b05918d0df987776ee24808f4dd1c522806bf9fa8f4336c93f6b0ec050029';
  825. $abi = '[
  826. {
  827. "constant": true,
  828. "inputs": [],
  829. "name": "getBytes",
  830. "outputs": [
  831. {
  832. "name": "",
  833. "type": "bytes"
  834. }
  835. ],
  836. "payable": false,
  837. "stateMutability": "pure",
  838. "type": "function"
  839. },
  840. {
  841. "constant": true,
  842. "inputs": [],
  843. "name": "getBytes32",
  844. "outputs": [
  845. {
  846. "name": "",
  847. "type": "bytes32"
  848. }
  849. ],
  850. "payable": false,
  851. "stateMutability": "pure",
  852. "type": "function"
  853. },
  854. {
  855. "constant": true,
  856. "inputs": [],
  857. "name": "getDynamicData",
  858. "outputs": [
  859. {
  860. "name": "",
  861. "type": "bytes"
  862. },
  863. {
  864. "name": "",
  865. "type": "bytes"
  866. }
  867. ],
  868. "payable": false,
  869. "stateMutability": "pure",
  870. "type": "function"
  871. },
  872. {
  873. "constant": true,
  874. "inputs": [],
  875. "name": "getBoolean",
  876. "outputs": [
  877. {
  878. "name": "",
  879. "type": "bool"
  880. }
  881. ],
  882. "payable": false,
  883. "stateMutability": "pure",
  884. "type": "function"
  885. },
  886. {
  887. "constant": true,
  888. "inputs": [],
  889. "name": "getInteger",
  890. "outputs": [
  891. {
  892. "name": "",
  893. "type": "int256"
  894. }
  895. ],
  896. "payable": false,
  897. "stateMutability": "pure",
  898. "type": "function"
  899. },
  900. {
  901. "constant": true,
  902. "inputs": [],
  903. "name": "getTwoBytes32",
  904. "outputs": [
  905. {
  906. "name": "",
  907. "type": "bytes32"
  908. },
  909. {
  910. "name": "",
  911. "type": "bytes32"
  912. }
  913. ],
  914. "payable": false,
  915. "stateMutability": "pure",
  916. "type": "function"
  917. },
  918. {
  919. "constant": true,
  920. "inputs": [],
  921. "name": "getBytesVeryLong",
  922. "outputs": [
  923. {
  924. "name": "",
  925. "type": "bytes"
  926. }
  927. ],
  928. "payable": false,
  929. "stateMutability": "pure",
  930. "type": "function"
  931. },
  932. {
  933. "constant": true,
  934. "inputs": [],
  935. "name": "getDynamicDataMixedOne",
  936. "outputs": [
  937. {
  938. "name": "",
  939. "type": "bytes"
  940. },
  941. {
  942. "name": "",
  943. "type": "bytes32"
  944. }
  945. ],
  946. "payable": false,
  947. "stateMutability": "pure",
  948. "type": "function"
  949. },
  950. {
  951. "constant": true,
  952. "inputs": [],
  953. "name": "getBytes1",
  954. "outputs": [
  955. {
  956. "name": "",
  957. "type": "bytes1"
  958. }
  959. ],
  960. "payable": false,
  961. "stateMutability": "pure",
  962. "type": "function"
  963. },
  964. {
  965. "constant": true,
  966. "inputs": [],
  967. "name": "getDynamicDataMixedTwo",
  968. "outputs": [
  969. {
  970. "name": "",
  971. "type": "bytes32"
  972. },
  973. {
  974. "name": "",
  975. "type": "bytes"
  976. }
  977. ],
  978. "payable": false,
  979. "stateMutability": "pure",
  980. "type": "function"
  981. },
  982. {
  983. "constant": true,
  984. "inputs": [],
  985. "name": "getBytesLong",
  986. "outputs": [
  987. {
  988. "name": "",
  989. "type": "bytes"
  990. }
  991. ],
  992. "payable": false,
  993. "stateMutability": "pure",
  994. "type": "function"
  995. },
  996. {
  997. "inputs": [],
  998. "payable": false,
  999. "stateMutability": "nonpayable",
  1000. "type": "constructor"
  1001. }
  1002. ]';
  1003. $functions = [
  1004. [
  1005. 'name' => 'getBoolean',
  1006. 'test' => function ($value) {
  1007. return is_bool($value);
  1008. }
  1009. ], [
  1010. 'name' => 'getInteger',
  1011. 'test' => function ($value) {
  1012. return ($value instanceof BigNumber);
  1013. }
  1014. ], [
  1015. 'name' => 'getBytes1',
  1016. 'test' => function ($value) {
  1017. return Utils::isHex($value) && mb_strlen($value) === 4;
  1018. }
  1019. ], [
  1020. 'name' => 'getBytes32',
  1021. 'test' => function ($value) {
  1022. return Utils::isHex($value) && mb_strlen($value) === 66;
  1023. }
  1024. ], [
  1025. 'name' => 'getBytesLong',
  1026. 'test' => function ($value) {
  1027. return Utils::isHex($value);
  1028. }
  1029. ], [
  1030. 'name' => 'getDynamicData',
  1031. 'test' => function ($value) {
  1032. return Utils::isHex($value);
  1033. }
  1034. ], [
  1035. 'name' => 'getDynamicDataMixedOne',
  1036. 'test' => function ($value) {
  1037. return Utils::isHex($value);
  1038. }
  1039. ], [
  1040. 'name' => 'getDynamicDataMixedTwo',
  1041. 'test' => function ($value) {
  1042. return Utils::isHex($value);
  1043. }
  1044. ], [
  1045. 'name' => 'getTwoBytes32',
  1046. 'test' => function ($value) {
  1047. return Utils::isHex($value) && mb_strlen($value) === 66;
  1048. }
  1049. ]
  1050. ];
  1051. $contractAddress = "";
  1052. if (!isset($this->accounts[0])) {
  1053. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1054. } else {
  1055. $account = $this->accounts[0];
  1056. }
  1057. $contract = new Contract($this->web3->provider, $abi);
  1058. $contract->bytecode($bytecode)->new([
  1059. 'from' => $account,
  1060. 'gas' => '0x200b20'
  1061. ], function ($err, $result) use ($contract, &$contractAddress) {
  1062. if ($err !== null) {
  1063. return $this->fail($err->getMessage());
  1064. }
  1065. if ($result) {
  1066. echo "\nTransaction has made:) id: " . $result . "\n";
  1067. }
  1068. $transactionId = $result;
  1069. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1070. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1071. if ($err !== null) {
  1072. return $this->fail($err);
  1073. }
  1074. if ($transaction) {
  1075. $contractAddress = $transaction->contractAddress;
  1076. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1077. }
  1078. });
  1079. });
  1080. $contract->at($contractAddress);
  1081. foreach ($functions as $function) {
  1082. $contract->call($function['name'], [], 'latest', function ($err, $res) use ($function) {
  1083. if ($err !== null) {
  1084. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1085. return;
  1086. }
  1087. foreach ($res as $r) {
  1088. if (!call_user_func($function['test'], $r)) {
  1089. var_dump($r);
  1090. }
  1091. $this->assertTrue(call_user_func($function['test'], $r));
  1092. }
  1093. });
  1094. }
  1095. }
  1096. /**
  1097. * testIssue125
  1098. *
  1099. * @return void
  1100. */
  1101. public function testIssue125()
  1102. {
  1103. $bytecode = '0x608060405234801561001057600080fd5b506103bb806100206000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806373d4a13a1461004b578063ab62f0e1146100db575b600080fd5b34801561005757600080fd5b506100606101a3565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a0578082015181840152602081019050610085565b50505050905090810190601f1680156100cd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100e757600080fd5b506101a1600480360360208110156100fe57600080fd5b810190808035906020019064010000000081111561011b57600080fd5b82018360208201111561012d57600080fd5b8035906020019184600183028401116401000000008311171561014f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610241565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102395780601f1061020e57610100808354040283529160200191610239565b820191906000526020600020905b81548152906001019060200180831161021c57829003601f168201915b505050505081565b80600090805190602001906102579291906102ea565b50806040518082805190602001908083835b60208310151561028e5780518252602082019150602081019050602083039250610269565b6001836020036101000a03801982511681845116808217855250505050505090500191505060405180910390207fae08ae866f3211e692b7cdd30e8b6ec658a153397150437cb873f95239953a5460405160405180910390a250565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061032b57805160ff1916838001178555610359565b82800160010185558215610359579182015b8281111561035857825182559160200191906001019061033d565b5b509050610366919061036a565b5090565b61038c91905b80821115610388576000816000905550600101610370565b5090565b9056fea165627a7a72305820bb836a29a397513a3f2e2199520262ef5978d3a3d7e3d3532da41064d39177e30029';
  1104. $abi = '[
  1105. {
  1106. "constant": false,
  1107. "inputs": [
  1108. {
  1109. "name": "_data",
  1110. "type": "bytes"
  1111. }
  1112. ],
  1113. "name": "setData",
  1114. "outputs": [],
  1115. "payable": false,
  1116. "stateMutability": "nonpayable",
  1117. "type": "function"
  1118. },
  1119. {
  1120. "anonymous": false,
  1121. "inputs": [
  1122. {
  1123. "indexed": true,
  1124. "name": "_data",
  1125. "type": "bytes"
  1126. }
  1127. ],
  1128. "name": "SetData",
  1129. "type": "event"
  1130. },
  1131. {
  1132. "constant": true,
  1133. "inputs": [],
  1134. "name": "data",
  1135. "outputs": [
  1136. {
  1137. "name": "",
  1138. "type": "bytes"
  1139. }
  1140. ],
  1141. "payable": false,
  1142. "stateMutability": "view",
  1143. "type": "function"
  1144. }
  1145. ]';
  1146. $contractAddress = "";
  1147. if (!isset($this->accounts[0])) {
  1148. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1149. } else {
  1150. $account = $this->accounts[0];
  1151. }
  1152. $contract = new Contract($this->web3->provider, $abi);
  1153. $contract->bytecode($bytecode)->new([
  1154. 'from' => $account,
  1155. 'gas' => '0x200b20'
  1156. ], function ($err, $result) use ($contract, &$contractAddress) {
  1157. if ($err !== null) {
  1158. return $this->fail($err->getMessage());
  1159. }
  1160. if ($result) {
  1161. echo "\nTransaction has made:) id: " . $result . "\n";
  1162. }
  1163. $transactionId = $result;
  1164. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1165. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1166. if ($err !== null) {
  1167. return $this->fail($err);
  1168. }
  1169. if ($transaction) {
  1170. $contractAddress = $transaction->contractAddress;
  1171. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1172. }
  1173. });
  1174. });
  1175. $contract->at($contractAddress);
  1176. $contract->call('data', function ($err, $res) {
  1177. if ($err !== null) {
  1178. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1179. return;
  1180. }
  1181. $this->assertEquals('0', $res[0]);
  1182. });
  1183. $contract->send('setData', '0x44aec9b900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000021000000000000000000000000000000000000000000000000000000000000000b', [
  1184. 'from' => $account,
  1185. 'gas' => '0x200b20'
  1186. ], function ($err, $result) use ($contract) {
  1187. if ($err !== null) {
  1188. var_dump('Error' . $err->getMessage());
  1189. return $this->fail($err->getMessage());
  1190. }
  1191. if ($result) {
  1192. echo "\nTransaction has made:) id: " . $result . "\n";
  1193. }
  1194. $transactionId = $result;
  1195. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1196. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  1197. if ($err !== null) {
  1198. return $this->fail($err);
  1199. }
  1200. $this->assertTrue($transaction !== null);
  1201. });
  1202. });
  1203. $contract->call('data', function ($err, $res) {
  1204. if ($err !== null) {
  1205. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1206. return;
  1207. }
  1208. $this->assertEquals('0x44aec9b900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000021000000000000000000000000000000000000000000000000000000000000000b', $res[0]);
  1209. });
  1210. }
  1211. /**
  1212. * testIssue134
  1213. *
  1214. * @return void
  1215. */
  1216. public function testIssue134()
  1217. {
  1218. $bytecode = '0x608060405234801561001057600080fd5b50610487806100206000396000f3fe60806040526004361061005c576000357c01000000000000000000000000000000000000000000000000000000009004806373d4a13a146100615780638381f58a146100f15780639a73c4091461011c578063c3fad957146101ee575b600080fd5b34801561006d57600080fd5b50610076610229565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b657808201518184015260208101905061009b565b50505050905090810190601f1680156100e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100fd57600080fd5b506101066102c7565b6040518082815260200191505060405180910390f35b34801561012857600080fd5b506101ec6004803603604081101561013f57600080fd5b81019080803590602001909291908035906020019064010000000081111561016657600080fd5b82018360208201111561017857600080fd5b8035906020019184600183028401116401000000008311171561019a57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102cd565b005b3480156101fa57600080fd5b506102276004803603602081101561021157600080fd5b810190808035906020019092919050505061037f565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102bf5780601f10610294576101008083540402835291602001916102bf565b820191906000526020600020905b8154815290600101906020018083116102a257829003601f168201915b505050505081565b60015481565b80600090805190602001906102e39291906103b6565b5081600181905550806040518082805190602001908083835b60208310151561032157805182526020820191506020810190506020830392506102fc565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040518091039020827f95d23dc1ab7ea37e46bd697006b873f80ffecb4cb4ae085c7cc60a977ba7f2f560405160405180910390a35050565b80600181905550807f9d9c58c068c2bc9cb27d4e5c437f624ccbf4910ba8893ffd03ee311830becab160405160405180910390a250565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103f757805160ff1916838001178555610425565b82800160010185558215610425579182015b82811115610424578251825591602001919060010190610409565b5b5090506104329190610436565b5090565b61045891905b8082111561045457600081600090555060010161043c565b5090565b9056fea165627a7a72305820830ee43c52ac89b1fbc68647e1893aea17aa8ca132532723176508a6bc4586c90029';
  1219. $abi = '[
  1220. {
  1221. "constant": true,
  1222. "inputs": [],
  1223. "name": "data",
  1224. "outputs": [
  1225. {
  1226. "name": "",
  1227. "type": "bytes"
  1228. }
  1229. ],
  1230. "payable": false,
  1231. "stateMutability": "view",
  1232. "type": "function"
  1233. },
  1234. {
  1235. "constant": true,
  1236. "inputs": [],
  1237. "name": "number",
  1238. "outputs": [
  1239. {
  1240. "name": "",
  1241. "type": "uint256"
  1242. }
  1243. ],
  1244. "payable": false,
  1245. "stateMutability": "view",
  1246. "type": "function"
  1247. },
  1248. {
  1249. "constant": false,
  1250. "inputs": [
  1251. {
  1252. "name": "_number",
  1253. "type": "uint256"
  1254. },
  1255. {
  1256. "name": "_data",
  1257. "type": "bytes"
  1258. }
  1259. ],
  1260. "name": "say",
  1261. "outputs": [],
  1262. "payable": false,
  1263. "stateMutability": "nonpayable",
  1264. "type": "function"
  1265. },
  1266. {
  1267. "constant": false,
  1268. "inputs": [
  1269. {
  1270. "name": "_number",
  1271. "type": "uint256"
  1272. }
  1273. ],
  1274. "name": "say",
  1275. "outputs": [],
  1276. "payable": false,
  1277. "stateMutability": "nonpayable",
  1278. "type": "function"
  1279. },
  1280. {
  1281. "anonymous": false,
  1282. "inputs": [
  1283. {
  1284. "indexed": true,
  1285. "name": "_number",
  1286. "type": "uint256"
  1287. }
  1288. ],
  1289. "name": "Say",
  1290. "type": "event"
  1291. },
  1292. {
  1293. "anonymous": false,
  1294. "inputs": [
  1295. {
  1296. "indexed": true,
  1297. "name": "_number",
  1298. "type": "uint256"
  1299. },
  1300. {
  1301. "indexed": true,
  1302. "name": "_data",
  1303. "type": "bytes"
  1304. }
  1305. ],
  1306. "name": "Say",
  1307. "type": "event"
  1308. }
  1309. ]';
  1310. $testNumber = 16;
  1311. $testData = "0x01234";
  1312. $contractAddress = "";
  1313. $contract = new Contract($this->web3->provider, $abi);
  1314. if (!isset($this->accounts[0])) {
  1315. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1316. } else {
  1317. $account = $this->accounts[0];
  1318. }
  1319. $contract = new Contract($this->web3->provider, $abi);
  1320. $contract->bytecode($bytecode)->new([
  1321. 'from' => $account,
  1322. 'gas' => '0x200b20'
  1323. ], function ($err, $result) use ($contract, &$contractAddress) {
  1324. if ($err !== null) {
  1325. return $this->fail($err->getMessage());
  1326. }
  1327. if ($result) {
  1328. echo "\nTransaction has made:) id: " . $result . "\n";
  1329. }
  1330. $transactionId = $result;
  1331. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1332. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1333. if ($err !== null) {
  1334. return $this->fail($err);
  1335. }
  1336. if ($transaction) {
  1337. $contractAddress = $transaction->contractAddress;
  1338. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1339. }
  1340. });
  1341. });
  1342. $contract->at($contractAddress);
  1343. // test for send transaction and get data
  1344. $contract->send('say', $testNumber, $testData, [
  1345. 'from' => $account,
  1346. 'gas' => '0x200b20'
  1347. ], function ($err, $result) use ($contract, $testNumber, $testData) {
  1348. if ($err !== null) {
  1349. return $this->fail($err->getMessage());
  1350. }
  1351. if ($result) {
  1352. echo "\nTransaction has made:) id: " . $result . "\n";
  1353. }
  1354. $transactionId = $result;
  1355. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1356. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($testNumber, $testData, $contract) {
  1357. if ($err !== null) {
  1358. return $this->fail($err);
  1359. }
  1360. if ($transaction) {
  1361. $topics = $transaction->logs[0]->topics;
  1362. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1363. // validate topics
  1364. $this->assertEquals($contract->ethabi->encodeEventSignature($contract->events['Say']), $topics[0]);
  1365. $this->assertEquals('0x' . IntegerFormatter::format($testNumber), $topics[1], $topics[2]);
  1366. }
  1367. $contract->call('number', function ($err, $res) use ($testNumber) {
  1368. if ($err !== null) {
  1369. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1370. return;
  1371. }
  1372. $this->assertEquals((string) $testNumber, $res[0]->toString());
  1373. });
  1374. });
  1375. });
  1376. $testNumber++;
  1377. $contract->send('say', $testNumber, [
  1378. 'from' => $account,
  1379. 'gas' => '0x200b20'
  1380. ], function ($err, $result) use ($contract, $testNumber) {
  1381. if ($err !== null) {
  1382. return $this->fail($err->getMessage());
  1383. }
  1384. if ($result) {
  1385. echo "\nTransaction has made:) id: " . $result . "\n";
  1386. }
  1387. $transactionId = $result;
  1388. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1389. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($testNumber, $contract) {
  1390. if ($err !== null) {
  1391. return $this->fail($err);
  1392. }
  1393. if ($transaction) {
  1394. $topics = $transaction->logs[0]->topics;
  1395. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1396. // validate topics
  1397. // $this->assertEquals($contract->ethabi->encodeEventSignature($contract->events['Say']), $topics[0]);
  1398. $this->assertEquals('0x' . IntegerFormatter::format($testNumber), $topics[1]);
  1399. }
  1400. $blockNumber = Utils::toBn($transaction->blockNumber);
  1401. $contract->call('number', Utils::toHex($blockNumber, true), function ($err, $res) use ($testNumber) {
  1402. if ($err !== null) {
  1403. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1404. return;
  1405. }
  1406. $this->assertEquals((string) $testNumber, $res[0]->toString());
  1407. });
  1408. $blockNumber = $blockNumber->subtract(Utils::toBn(1));
  1409. $contract->call('number', Utils::toHex($blockNumber, true), function ($err, $res) use ($testNumber) {
  1410. if ($err !== null) {
  1411. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1412. return;
  1413. }
  1414. $this->assertEquals((string) $testNumber-1, $res[0]->toString());
  1415. });
  1416. });
  1417. });
  1418. // test for estimate gas
  1419. $contract->estimateGas("say", $testNumber, $testData, [
  1420. 'from' => $account,
  1421. 'gas' => '0x200b20'
  1422. ], function ($err, $result) {
  1423. if ($err !== null) {
  1424. return $this->fail($err->getMessage());
  1425. }
  1426. if (isset($result)) {
  1427. echo "\nEstimate gas: " . $result->toString() . "\n";
  1428. $this->assertTrue($result !== null);
  1429. }
  1430. });
  1431. $contract->estimateGas("say", $testNumber, [
  1432. 'from' => $account,
  1433. 'gas' => '0x200b20'
  1434. ], function ($err, $result) {
  1435. if ($err !== null) {
  1436. return $this->fail($err->getMessage());
  1437. }
  1438. if (isset($result)) {
  1439. echo "\nEstimate gas: " . $result->toString() . "\n";
  1440. $this->assertTrue($result !== null);
  1441. }
  1442. });
  1443. }
  1444. }