Socket
Socket
Sign inDemoInstall

@solana/web3.js

Package Overview
Dependencies
Maintainers
1
Versions
1815
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/web3.js - npm Package Compare versions

Comparing version 0.13.1 to 0.13.2

doc/ast/source/util/send-and-confirm-raw-transaction.js.json

9

doc/coverage.json
{
"coverage": "100%",
"expectCount": 133,
"actualCount": 133,
"expectCount": 134,
"actualCount": 134,
"files": {

@@ -61,2 +61,7 @@ "src/account.js": {

},
"src/util/send-and-confirm-raw-transaction.js": {
"expectCount": 1,
"actualCount": 1,
"undocumentLines": []
},
"src/util/send-and-confirm-transaction.js": {

@@ -63,0 +68,0 @@ "expectCount": 1,

@@ -99,2 +99,8 @@ window.esdocSearchIndex = [

[
"@solana/web3~sendandconfirmrawtransaction",
"function/index.html#static-function-sendAndConfirmRawTransaction",
"<span>sendAndConfirmRawTransaction</span> <span class=\"search-result-import-path\">@solana/web3</span>",
"function"
],
[
"@solana/web3~sendandconfirmtransaction",

@@ -982,2 +988,8 @@ "function/index.html#static-function-sendAndConfirmTransaction",

[
"src/util/send-and-confirm-raw-transaction.js",
"file/src/util/send-and-confirm-raw-transaction.js.html",
"src/util/send-and-confirm-raw-transaction.js",
"file"
],
[
"src/util/send-and-confirm-transaction.js",

@@ -984,0 +996,0 @@ "file/src/util/send-and-confirm-transaction.js.html",

@@ -1,1 +0,1 @@

"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var nacl=_interopDefault(require("tweetnacl")),bs58=_interopDefault(require("bs58")),_Object$keys=_interopDefault(require("babel-runtime/core-js/object/keys")),url=require("url"),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),rpcWebsockets=require("rpc-websockets"),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_JSON$stringify=_interopDefault(require("babel-runtime/core-js/json/stringify")),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Object$getPrototypeOf=_interopDefault(require("babel-runtime/core-js/object/get-prototype-of")),_classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),_possibleConstructorReturn=_interopDefault(require("babel-runtime/helpers/possibleConstructorReturn")),_get=_interopDefault(require("babel-runtime/helpers/get")),_inherits=_interopDefault(require("babel-runtime/helpers/inherits")),assert=_interopDefault(require("assert")),BN=_interopDefault(require("bn.js")),BufferLayout=require("buffer-layout"),PublicKey=function(){function e(t){if(_classCallCheck(this,e),this._bn="string"==typeof t?t.startsWith("0x")?new BN(t.substring(2),16):new BN(bs58.decode(t)):new BN(t),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var t=Buffer.alloc(32);return e.copy(t,32-e.length),t}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(t){return t instanceof e}}]),e}(),Account=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=t?nacl.sign.keyPair.fromSecretKey(t):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),publicKey=function(){return BufferLayout.blob(32,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"publicKey")},uint64=function(){return BufferLayout.blob(8,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"uint64")},rustString=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"string",t=BufferLayout.struct([BufferLayout.u32("length"),BufferLayout.u32("lengthPadding"),BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(),-8),"chars")],e),r=t.decode.bind(t),n=t.encode.bind(t);return t.decode=function(e,t){return r(e,t).chars.toString("utf8")},t.encode=function(e,t,r){var a={chars:Buffer.from(e,"utf8")};return n(a,t,r)},t},PACKET_DATA_SIZE=512,TransactionInstruction=function e(t){_classCallCheck(this,e),this.keys=[],this.userdata=Buffer.alloc(0),t&&_Object$assign(this,t)},Transaction=function(){function e(t){_classCallCheck(this,e),this.signatures=[],this.instructions=[],this.fee=1,t&&_Object$assign(this,t)}return _createClass(e,[{key:"signature",get:function(){return this.signatures.length>0?this.signatures[0].signature:null}}]),_createClass(e,[{key:"add",value:function(){for(var t=this,r=arguments.length,n=Array(r),a=0;r>a;a++)n[a]=arguments[a];if(0===n.length)throw Error("No instructions");return n.forEach(function(r){r instanceof e?t.instructions=t.instructions.concat(r.instructions):t.instructions.push(new TransactionInstruction(r))}),this}},{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");if(1>this.instructions.length)throw Error("No instructions provided");var t=this.signatures.map(function(e){return""+e.publicKey}),r=[];this.instructions.forEach(function(e){var n=""+e.programId;r.includes(n)||r.push(n),e.keys.map(function(e){return""+e}).forEach(function(e){t.includes(e)||t.push(e)})});var n=this.instructions.map(function(e){var n=e.userdata;return{programIdIndex:r.indexOf(""+e.programId),keyIndices:e.keys.map(function(e){return t.indexOf(""+e)}),userdata:n}});n.forEach(function(e){assert(e.programIdIndex>=0),e.keyIndices.forEach(function(e){return assert(e>=0)})});var a=BufferLayout.struct([BufferLayout.u8("programIdIndex"),BufferLayout.u32("keyIndicesLength"),BufferLayout.u32("keyIndicesLengthPadding"),BufferLayout.seq(BufferLayout.u8("keyIndex"),BufferLayout.offset(BufferLayout.u32(),-8),"keyIndices"),BufferLayout.u32("userdataLength"),BufferLayout.u32("userdataLengthPadding"),BufferLayout.seq(BufferLayout.u8("userdatum"),BufferLayout.offset(BufferLayout.u32(),-8),"userdata")]),u=BufferLayout.struct([BufferLayout.u32("keysLength"),BufferLayout.u32("keysLengthPadding"),BufferLayout.seq(publicKey("key"),BufferLayout.offset(BufferLayout.u32(),-8),"keys"),publicKey("lastId"),BufferLayout.ns64("fee"),BufferLayout.u32("programIdsLength"),BufferLayout.u32("programIdsLengthPadding"),BufferLayout.seq(publicKey("programId"),BufferLayout.offset(BufferLayout.u32(),-8),"programIds"),BufferLayout.u32("instructionsLength"),BufferLayout.u32("instructionsLengthPadding"),BufferLayout.seq(a,BufferLayout.offset(BufferLayout.u32(),-8),"instructions")]),o={keys:t.map(function(e){return new PublicKey(e).toBuffer()}),lastId:Buffer.from(bs58.decode(e)),fee:this.fee,programIds:r.map(function(e){return new PublicKey(e).toBuffer()}),instructions:n},s=Buffer.alloc(2048),i=u.encode(o,s);return s=s.slice(0,i)}},{key:"sign",value:function(){this.signPartial.apply(this,arguments)}},{key:"signPartial",value:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];if(0===t.length)throw Error("No signers");var n=t.map(function(e){return{signature:null,publicKey:e instanceof Account?e.publicKey:e}});this.signatures=n;var a=this._getSignData();t.forEach(function(e,t){if(!(e instanceof PublicKey)){var r=nacl.sign.detached(a,e.secretKey);assert(64===r.length),n[t].signature=r}})}},{key:"addSigner",value:function(e){var t=this.signatures.findIndex(function(t){return e.publicKey.equals(t.publicKey)});if(0>t)throw Error("Unknown signer: "+e.publicKey);var r=this._getSignData(),n=nacl.sign.detached(r,e.secretKey);assert(64===n.length),this.signatures[t].signature=n}},{key:"serialize",value:function(){var e=this.signatures;if(!e)throw Error("Transaction has not been signed");var t=this._getSignData(),r=Buffer.alloc(8+64*e.length+t.length);return assert(256>e.length),r.writeUInt8(e.length,0),e.forEach(function(e,t){var n=e.signature;assert(null!==n,"null signature"),assert(64===n.length,"signature has invalid length"),Buffer.from(n).copy(r,8+64*t)}),t.copy(r,8+64*e.length),assert(PACKET_DATA_SIZE>=r.length,"Transaction too large: "+r.length+" > "+PACKET_DATA_SIZE),r}},{key:"keys",get:function(){return assert(1===this.instructions.length),this.instructions[0].keys}},{key:"programId",get:function(){return assert(1===this.instructions.length),this.instructions[0].programId}},{key:"userdata",get:function(){return assert(1===this.instructions.length),this.instructions[0].userdata}}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(t,r,n,a,u){var o=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.ns64("tokens"),BufferLayout.ns64("space"),publicKey("programId")]),s=Buffer.alloc(o.span);return o.encode({instruction:0,tokens:n,space:a,programId:u.toBuffer()},s),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:s})}},{key:"move",value:function(t,r,n){var a=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.ns64("amount")]),u=Buffer.alloc(a.span);return a.encode({instruction:2,amount:n},u),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:u})}},{key:"assign",value:function(t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:r.toBuffer()},a),(new Transaction).add({keys:[t],programId:e.programId,userdata:a})}},{key:"spawn",value:function(t){var r=BufferLayout.struct([BufferLayout.u32("instruction")]),n=Buffer.alloc(r.span);return r.encode({instruction:3},n),(new Transaction).add({keys:[t],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(t){return setTimeout(t,e)})}function createRpcRequest(e){var t,r=this,n=jayson((t=_asyncToGenerator(_regeneratorRuntime.mark(function t(n,a){var u,o;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u={method:"POST",body:n,headers:{"Content-Type":"application/json"}},t.prev=1,t.next=4,fetch(e,u);case 4:return o=t.sent,t.next=7,o.text();case 7:a(null,t.sent),t.next=14;break;case 11:t.prev=11,t.t0=t.catch(1),a(t.t0);case 14:case"end":return t.stop()}},t,r,[[1,11]])})),function(e,r){return t.apply(this,arguments)}));return function(e,t){return new _Promise(function(r,a){n.request(e,t,function(e,t){e?a(e):r(t)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var t=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:t,id:"string",error:"any"}),superstruct.struct({jsonrpc:t,id:"string",error:"null?",result:e})])}var AccountInfoResult=superstruct.struct({executable:"boolean",loader:"array",owner:"array",tokens:"number",userdata:"array"}),GetAccountInfoRpcResult=jsonRpcResult(AccountInfoResult),AccountNotificationResult=superstruct.struct({subscription:"number",result:AccountInfoResult}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["AccountInUse","Confirmed","GenericFailure","ProgramRuntimeError","SignatureNotFound"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(t){_classCallCheck(this,e),this._rpcWebSocketConnected=!1,this._disableLastIdCaching=!1,this._accountChangeSubscriptions={},this._accountChangeSubscriptionCounter=0;var r=url.parse(t);this._rpcRequest=createRpcRequest(r.href),this._lastIdInfo={lastId:null,seconds:-1,transactionSignatures:[]},r.protocol="https:"===r.protocol?"wss:":"ws:",r.host="",r.port=+r.port+1+"","1"===r.port&&(r.port="wss:"===r.protocol?"8901":"8900"),this._rpcWebSocket=new rpcWebsockets.Client(url.format(r),{autoconnect:!1,max_reconnects:1/0}),this._rpcWebSocket.on("open",this._wsOnOpen.bind(this)),this._rpcWebSocket.on("error",this._wsOnError.bind(this)),this._rpcWebSocket.on("close",this._wsOnClose.bind(this)),this._rpcWebSocket.on("accountNotification",this._wsOnAccountNotification.bind(this))}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[t.toBase58()]);case 2:if(!(r=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[t.toBase58()]);case 2:if(!(r=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==(n=r.result)),e.abrupt("return",{executable:n.executable,loader:new PublicKey(n.loader),owner:new PublicKey(n.owner),tokens:n.tokens,userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[t]);case 2:if(!(r=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[t]);case 2:if(!(r=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(t=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(t=GetLastId(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(t=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[t.toBase58(),r]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;r>a;a++)n[a-1]=arguments[a];var u,o,s,i,c,f;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(u=(new Date).getSeconds(),null==this._lastIdInfo.lastId||this._lastIdInfo.seconds!==u){e.next=11;break}if(t.lastId=this._lastIdInfo.lastId,t.sign.apply(t,n),t.signature){e.next=6;break}throw Error("!signature");case 6:if(this._lastIdInfo.transactionSignatures.includes(o=""+t.signature)){e.next=11;break}return this._lastIdInfo.transactionSignatures.push(o),this._disableLastIdCaching&&(this._lastIdInfo.seconds=-1),e.abrupt("break",28);case 11:s=0,i=Date.now();case 13:return e.next=15,this.getLastId();case 15:if(this._lastIdInfo.lastId==(c=e.sent)){e.next=19;break}return this._lastIdInfo={lastId:c,seconds:(new Date).getSeconds(),transactionSignatures:[]},e.abrupt("break",26);case 19:if(8!==s){e.next=21;break}throw Error("Unable to obtain a new last id after "+(Date.now()-i)+"ms");case 21:return e.next=23,sleep(250);case 23:++s;case 24:e.next=13;break;case 26:e.next=0;break;case 28:return f=t.serialize(),e.next=31,this.sendRawTransaction(f);case 31:return e.abrupt("return",e.sent);case 32:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"sendRawTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(t))]);case 2:if(!(r=SendTokensRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),assert(r.result),e.abrupt("return",r.result);case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"_wsOnOpen",value:function(){this._rpcWebSocketConnected=!0,this._updateSubscriptions()}},{key:"_wsOnError",value:function(e){console.log("ws error:",e.message)}},{key:"_wsOnClose",value:function(e,t){1e3!==e&&console.log("ws close:",e,t),this._rpcWebSocketConnected=!1}},{key:"_wsOnAccountNotification",value:function(e){var t=AccountNotificationResult(e);if(t.error)throw Error(t.error.message);var r=_Object$keys(this._accountChangeSubscriptions).map(Number),n=!0,a=!1,u=void 0;try{for(var o,s=_getIterator(r);!(n=(o=s.next()).done);n=!0){var i=this._accountChangeSubscriptions[o.value];if(i.subscriptionId===t.subscription){var c=t.result;return assert(void 0!==c),i.callback({executable:c.executable,loader:new PublicKey(c.loader),owner:new PublicKey(c.owner),tokens:c.tokens,userdata:Buffer.from(c.userdata)}),!0}}}catch(e){a=!0,u=e}finally{try{!n&&s.return&&s.return()}finally{if(a)throw u}}}},{key:"_updateSubscriptions",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n,a,u,o,s,i,c,f,l,p,h,d;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(0!==(t=_Object$keys(this._accountChangeSubscriptions).map(Number)).length){e.next=4;break}return this._rpcWebSocket.close(),e.abrupt("return");case 4:if(this._rpcWebSocketConnected){e.next=26;break}for(r=!0,n=!1,a=void 0,e.prev=8,u=_getIterator(t);!(r=(o=u.next()).done);r=!0)this._accountChangeSubscriptions[o.value].subscriptionId=null;e.next=16;break;case 12:e.prev=12,e.t0=e.catch(8),n=!0,a=e.t0;case 16:e.prev=16,e.prev=17,!r&&u.return&&u.return();case 19:if(e.prev=19,!n){e.next=22;break}throw a;case 22:return e.finish(19);case 23:return e.finish(16);case 24:return this._rpcWebSocket.connect(),e.abrupt("return");case 26:s=!0,i=!1,c=void 0,e.prev=29,f=_getIterator(t);case 31:if(s=(l=f.next()).done){e.next=47;break}if(d=(h=this._accountChangeSubscriptions[p=l.value]).publicKey,null!==h.subscriptionId){e.next=44;break}return e.prev=35,e.next=38,this._rpcWebSocket.call("accountSubscribe",[d]);case 38:this._accountChangeSubscriptions[p].subscriptionId=e.sent,e.next=44;break;case 41:e.prev=41,e.t1=e.catch(35),console.log("accountSubscribe error for "+d+": "+e.t1.message);case 44:s=!0,e.next=31;break;case 47:e.next=53;break;case 49:e.prev=49,e.t2=e.catch(29),i=!0,c=e.t2;case 53:e.prev=53,e.prev=54,!s&&f.return&&f.return();case 56:if(e.prev=56,!i){e.next=59;break}throw c;case 59:return e.finish(56);case 60:return e.finish(53);case 61:case"end":return e.stop()}},e,this,[[8,12,16,24],[17,,19,23],[29,49,53,61],[35,41],[54,,56,60]])}));return function(){return e.apply(this,arguments)}}()},{key:"onAccountChange",value:function(e,t){var r=++this._accountChangeSubscriptionCounter;return this._accountChangeSubscriptions[r]={publicKey:e.toBase58(),callback:t,subscriptionId:null},this._updateSubscriptions(),r}},{key:"removeAccountChangeListener",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._accountChangeSubscriptions[t]){e.next=15;break}if(r=this._accountChangeSubscriptions[t].subscriptionId,delete this._accountChangeSubscriptions[t],null===r){e.next=12;break}return e.prev=4,e.next=7,this._rpcWebSocket.call("accountUnsubscribe",[r]);case 7:e.next=12;break;case 9:e.prev=9,e.t0=e.catch(4),console.log("accountUnsubscribe error:",e.t0.message);case 12:this._updateSubscriptions(),e.next=16;break;case 15:throw Error("Unknown account change id: "+t);case 16:case"end":return e.stop()}},e,this,[[4,9]])}));return function(t){return e.apply(this,arguments)}}()}]),e}(),sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){for(var n=arguments.length,a=Array(n>2?n-2:0),u=2;n>u;u++)a[u-2]=arguments[u];var o,s,i,c,f,l;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:o=10,s=void 0;case 2:return i=Date.now(),e.next=5,t.sendTransaction.apply(t,[r].concat(a));case 5:s=e.sent,c="SignatureNotFound",f=4;case 8:return e.next=10,t.getSignatureStatus(s);case 10:if("SignatureNotFound"===(c=e.sent)){e.next=13;break}return e.abrupt("break",20);case 13:return e.next=15,sleep(500);case 15:if(0<--f){e.next=18;break}throw l=(Date.now()-i)/1e3,Error("Transaction '"+s+"' was not confirmed in "+l.toFixed(2)+" seconds ("+c+")");case 18:e.next=8;break;case 20:if("Confirmed"!==c){e.next=22;break}return e.abrupt("break",28);case 22:if("AccountInUse"===c&&--o>0){e.next=24;break}throw Error("Transaction "+s+" failed ("+c+")");case 24:return e.next=26,sleep(100*Math.random());case 26:e.next=2;break;case 28:return e.abrupt("return",s);case 29:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}(),Loader=function(){function e(t,r){_classCallCheck(this,e),_Object$assign(this,{connection:t,programId:r})}return _createClass(e,null,[{key:"chunkSize",get:function(){return 256}}]),_createClass(e,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n){var a,u,o,s,i,c,f,l;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:a=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.u32("offset"),BufferLayout.u32("bytesLength"),BufferLayout.u32("bytesLengthPadding"),BufferLayout.seq(BufferLayout.u8("byte"),BufferLayout.offset(BufferLayout.u32(),-8),"bytes")]),u=e.chunkSize,o=0,s=n,i=[];case 5:if(s.length<=0){t.next=19;break}if(c=s.slice(0,u),f=Buffer.alloc(u+16),a.encode({instruction:0,offset:o,bytes:c},f),l=(new Transaction).add({keys:[r.publicKey],programId:this.programId,userdata:f}),i.push(sendAndConfirmTransaction(this.connection,l,r)),8!==i.length){t.next=15;break}return t.next=14,_Promise.all(i);case 14:i=[];case 15:o+=u,s=s.slice(u),t.next=5;break;case 19:return t.next=21,_Promise.all(i);case 21:case"end":return t.stop()}},t,this)}));return function(e,r){return t.apply(this,arguments)}}()},{key:"finalize",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=BufferLayout.struct([BufferLayout.u32("instruction")]),n=Buffer.alloc(r.span),r.encode({instruction:1},n),(a=new Transaction).add({keys:[t.publicKey],programId:this.programId,userdata:n}),a.add(SystemProgram.spawn(t.publicKey)),e.next=8,sendAndConfirmTransaction(this.connection,a,t);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()}]),e}(),BpfLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var u,o,s;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u=new Account,o=SystemProgram.createAccount(n.publicKey,u.publicKey,1+Math.ceil(a.length/Loader.chunkSize)+1,a.length,e.programId),t.next=4,sendAndConfirmTransaction(r,o,n);case 4:return s=new Loader(r,e.programId),t.next=7,s.load(u,a);case 7:return t.next=9,s.finalize(u);case 9:return t.abrupt("return",u.publicKey);case 10:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x8000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function serializePayment(e){var t=e.to.toBuffer(),r=Buffer.alloc(8+t.length);return r.writeUInt32LE(e.amount,0),t.copy(r,8),r}function serializeDate(e){var t=Buffer.alloc(28);return t.writeUInt32LE(20,0),t.write(function(e){function t(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+t(e.getUTCMonth()+1)+"-"+t(e.getUTCDate())+"T"+t(e.getUTCHours())+":"+t(e.getUTCMinutes())+":"+t(e.getUTCSeconds())+"Z"}(e),8),t}function serializeCondition(e){switch(e.type){case"timestamp":var t=serializeDate(e.when),r=e.from.toBuffer(),n=Buffer.alloc(4+t.length+r.length);return n.writeUInt32LE(0,0),t.copy(n,4),r.copy(n,4+t.length),n;case"signature":var a=BufferLayout.struct([BufferLayout.u32("condition"),publicKey("from")]),u=e.from.toBuffer(),o=Buffer.alloc(4+u.length);return a.encode({instruction:1,from:u},o),o;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,t){return{type:"timestamp",from:e,when:t}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,t,r,n){var a=Buffer.alloc(1024),u=0;a.writeUInt32LE(0,u),u+=4;for(var o=arguments.length,s=Array(o>4?o-4:0),i=4;o>i;i++)s[i-4]=arguments[i];switch(s.length){case 0:a.writeUInt32LE(0,u),u+=4;var c=serializePayment({amount:n,to:r});return c.copy(a,u),u+=c.length,(new Transaction).add({keys:[e,r],programId:this.programId,userdata:a.slice(0,u)});case 1:a.writeUInt32LE(1,u),u+=4;var f=serializeCondition(s[0]);f.copy(a,u),u+=f.length;var l=serializePayment({amount:n,to:r});return l.copy(a,u),u+=l.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,u)});case 2:a.writeUInt32LE(2,u),u+=4;var p=!0,h=!1,d=void 0;try{for(var y,g=_getIterator(s);!(p=(y=g.next()).done);p=!0){var m=serializeCondition(y.value);m.copy(a,u),u+=m.length;var b=serializePayment({amount:n,to:r});b.copy(a,u),u+=b.length}}catch(e){h=!0,d=e}finally{try{!p&&g.return&&g.return()}finally{if(h)throw d}}return(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,u)});default:throw Error("A maximum of two conditions are support: "+s.length+" provided")}}},{key:"payOnBoth",value:function(e,t,r,n,a,u){var o=Buffer.alloc(1024),s=0;o.writeUInt32LE(0,s),o.writeUInt32LE(3,s+=4),s+=4;for(var i=[a,u],c=0;i.length>c;c++){var f=serializeCondition(i[c]);f.copy(o,s),s+=f.length}var l=serializePayment({amount:n,to:r});return l.copy(o,s),s+=l.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:o.slice(0,s)})}},{key:"applyTimestamp",value:function(e,t,r,n){var a=serializeDate(n),u=Buffer.alloc(4+a.length);return u.writeUInt32LE(1,0),a.copy(u,4),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:u})}},{key:"applySignature",value:function(e,t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x8100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}(),NativeLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var u,o,s,i;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u=[].concat(_toConsumableArray(Buffer.from(a))),o=new Account,s=SystemProgram.createAccount(n.publicKey,o.publicKey,3,u.length+1,e.programId),t.next=5,sendAndConfirmTransaction(r,s,n);case 5:return i=new Loader(r,e.programId),t.next=8,i.load(o,u);case 8:return t.next=10,i.finalize(o);case 10:return t.abrupt("return",o.publicKey);case 11:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}}]),e}(),TokenAmount=function(e){function t(){return _classCallCheck(this,t),_possibleConstructorReturn(this,(t.__proto__||_Object$getPrototypeOf(t)).apply(this,arguments))}return _inherits(t,BN),_createClass(t,[{key:"toBuffer",value:function(){var e=_get(t.prototype.__proto__||_Object$getPrototypeOf(t.prototype),"toArray",this).call(this).reverse(),r=Buffer.from(e);if(8===r.length)return r;assert(8>r.length,"TokenAmount too large");var n=Buffer.alloc(8);return r.copy(n),n}}],[{key:"fromBuffer",value:function(e){return assert(8===e.length,"Invalid buffer length: "+e.length),new BN([].concat(_toConsumableArray(e)).reverse().map(function(e){return("00"+e.toString(16)).slice(-2)}).join(""),16)}}]),t}(),TokenInfoLayout=BufferLayout.struct([uint64("supply"),BufferLayout.u8("decimals"),rustString("name"),rustString("symbol")]),TokenAccountInfoLayout=BufferLayout.struct([publicKey("token"),publicKey("owner"),uint64("amount"),BufferLayout.u8("sourceOption"),publicKey("source"),uint64("originalAmount")]),SYSTEM_TOKEN_PROGRAM_ID=new PublicKey("0x8300000000000000000000000000000000000000000000000000000000000000"),Token=function(){function e(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:SYSTEM_TOKEN_PROGRAM_ID;_classCallCheck(this,e),_Object$assign(this,{connection:t,token:r,programId:n})}return _createClass(e,[{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a,u,o,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=new Account,n=void 0,a=BufferLayout.struct([BufferLayout.u32("instruction")]),u=Buffer.alloc(a.span),a.encode({instruction:1},u),n=SystemProgram.createAccount(t.publicKey,r.publicKey,1,1+TokenAccountInfoLayout.span,this.programId),e.next=8,sendAndConfirmTransaction(this.connection,n,t);case 8:return o=[r.publicKey,t.publicKey,this.token],s&&o.push(s),(n=(new Transaction).add({keys:o,programId:this.programId,userdata:u})).fee=0,e.next=14,sendAndConfirmTransaction(this.connection,n,r);case 14:return e.abrupt("return",r.publicKey);case 15:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token owner: "+_JSON$stringify(t.owner));case 5:if(1===(r=Buffer.from(t.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token userdata");case 8:return(n=TokenInfoLayout.decode(r,1)).supply=TokenAmount.fromBuffer(n.supply),e.abrupt("return",n);case 11:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(t);case 2:if((r=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token account owner");case 5:if(2===(n=Buffer.from(r.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token account userdata");case 8:if((a=TokenAccountInfoLayout.decode(n,1)).token=new PublicKey(a.token),a.owner=new PublicKey(a.owner),a.amount=TokenAmount.fromBuffer(a.amount),0===a.sourceOption?(a.source=null,a.originalAmount=new TokenAmount):(a.source=new PublicKey(a.source),a.originalAmount=TokenAmount.fromBuffer(a.originalAmount)),a.token.equals(this.token)){e.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(a.token)+" !== "+_JSON$stringify(this.token));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"transfer",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=sendAndConfirmTransaction,e.t1=this.connection,e.t2=new Transaction,e.next=5,this.transferInstruction(t.publicKey,r,n,a);case 5:return e.t3=e.sent,e.t4=e.t2.add.call(e.t2,e.t3),e.t5=t,e.next=10,(0,e.t0)(e.t1,e.t4,e.t5);case 10:return e.abrupt("return",e.sent);case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approve",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.approveInstruction(t.publicKey,r,n,a)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"revoke",value:function(e,t,r){return this.approve(e,t,r,0)}},{key:"setOwner",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.setOwnerInstruction(t.publicKey,r,n)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n){return e.apply(this,arguments)}}()},{key:"transferInstruction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){var u,o,s,i;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.accountInfo(r);case 2:if(t.equals((u=e.sent).owner)){e.next=5;break}throw Error("Account owner mismatch");case 5:return o=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),s=Buffer.alloc(o.span),o.encode({instruction:2,amount:new TokenAmount(a).toBuffer()},s),i=[t,r,n],u.source&&i.push(u.source),e.abrupt("return",new TransactionInstruction({keys:i,programId:this.programId,userdata:s}));case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approveInstruction",value:function(e,t,r,n){var a=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),u=Buffer.alloc(a.span);return a.encode({instruction:3,amount:new TokenAmount(n).toBuffer()},u),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:u})}},{key:"revokeInstruction",value:function(e,t,r){return this.approveInstruction(e,t,r,0)}},{key:"setOwnerInstruction",value:function(e,t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:4},a),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:a})}}],[{key:"createNewToken",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a,u,o,s){var i,c,f,l,p,h,d,y=arguments.length>6&&void 0!==arguments[6]?arguments[6]:SYSTEM_TOKEN_PROGRAM_ID;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return i=new Account,c=new e(r,i.publicKey,y),t.next=4,c.newAccount(n,null);case 4:return f=t.sent,l=void 0,p=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("supply"),BufferLayout.u8("decimals"),rustString("name"),rustString("symbol")]),h=Buffer.alloc(1024),d=p.encode({instruction:0,supply:a.toBuffer(),decimals:s,name:u,symbol:o},h),h=h.slice(0,d),l=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+h.length,y),t.next=13,sendAndConfirmTransaction(r,l,n);case 13:return(l=(new Transaction).add({keys:[i.publicKey,f],programId:y,userdata:h})).fee=0,t.next=17,sendAndConfirmTransaction(r,l,i);case 17:return t.abrupt("return",[c,f]);case 18:case"end":return t.stop()}},t,this)}));return function(e,r,n,a,u,o){return t.apply(this,arguments)}}()}]),e}();exports.Account=Account,exports.BpfLoader=BpfLoader,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.Loader=Loader,exports.NativeLoader=NativeLoader,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Token=Token,exports.TokenAmount=TokenAmount,exports.Transaction=Transaction,exports.TransactionInstruction=TransactionInstruction,exports.sendAndConfirmTransaction=sendAndConfirmTransaction;
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var nacl=_interopDefault(require("tweetnacl")),bs58=_interopDefault(require("bs58")),_Object$keys=_interopDefault(require("babel-runtime/core-js/object/keys")),url=require("url"),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),rpcWebsockets=require("rpc-websockets"),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_JSON$stringify=_interopDefault(require("babel-runtime/core-js/json/stringify")),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Object$getPrototypeOf=_interopDefault(require("babel-runtime/core-js/object/get-prototype-of")),_classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),_possibleConstructorReturn=_interopDefault(require("babel-runtime/helpers/possibleConstructorReturn")),_get=_interopDefault(require("babel-runtime/helpers/get")),_inherits=_interopDefault(require("babel-runtime/helpers/inherits")),assert=_interopDefault(require("assert")),BN=_interopDefault(require("bn.js")),BufferLayout=require("buffer-layout"),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),PublicKey=function(){function e(t){if(_classCallCheck(this,e),this._bn="string"==typeof t?t.startsWith("0x")?new BN(t.substring(2),16):new BN(bs58.decode(t)):new BN(t),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var t=Buffer.alloc(32);return e.copy(t,32-e.length),t}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(t){return t instanceof e}}]),e}(),Account=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=t?nacl.sign.keyPair.fromSecretKey(t):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),publicKey=function(){return BufferLayout.blob(32,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"publicKey")},uint64=function(){return BufferLayout.blob(8,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"uint64")},rustString=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"string",t=BufferLayout.struct([BufferLayout.u32("length"),BufferLayout.u32("lengthPadding"),BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(),-8),"chars")],e),r=t.decode.bind(t),n=t.encode.bind(t);return t.decode=function(e,t){return r(e,t).chars.toString("utf8")},t.encode=function(e,t,r){var a={chars:Buffer.from(e,"utf8")};return n(a,t,r)},t},PACKET_DATA_SIZE=512,TransactionInstruction=function e(t){_classCallCheck(this,e),this.keys=[],this.userdata=Buffer.alloc(0),t&&_Object$assign(this,t)},Transaction=function(){function e(t){_classCallCheck(this,e),this.signatures=[],this.instructions=[],this.fee=1,t&&_Object$assign(this,t)}return _createClass(e,[{key:"signature",get:function(){return this.signatures.length>0?this.signatures[0].signature:null}}]),_createClass(e,[{key:"add",value:function(){for(var t=this,r=arguments.length,n=Array(r),a=0;r>a;a++)n[a]=arguments[a];if(0===n.length)throw Error("No instructions");return n.forEach(function(r){r instanceof e?t.instructions=t.instructions.concat(r.instructions):t.instructions.push(new TransactionInstruction(r))}),this}},{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");if(1>this.instructions.length)throw Error("No instructions provided");var t=this.signatures.map(function(e){return""+e.publicKey}),r=[];this.instructions.forEach(function(e){var n=""+e.programId;r.includes(n)||r.push(n),e.keys.map(function(e){return""+e}).forEach(function(e){t.includes(e)||t.push(e)})});var n=this.instructions.map(function(e){var n=e.userdata;return{programIdIndex:r.indexOf(""+e.programId),keyIndices:e.keys.map(function(e){return t.indexOf(""+e)}),userdata:n}});n.forEach(function(e){assert(e.programIdIndex>=0),e.keyIndices.forEach(function(e){return assert(e>=0)})});var a=BufferLayout.struct([BufferLayout.u8("programIdIndex"),BufferLayout.u32("keyIndicesLength"),BufferLayout.u32("keyIndicesLengthPadding"),BufferLayout.seq(BufferLayout.u8("keyIndex"),BufferLayout.offset(BufferLayout.u32(),-8),"keyIndices"),BufferLayout.u32("userdataLength"),BufferLayout.u32("userdataLengthPadding"),BufferLayout.seq(BufferLayout.u8("userdatum"),BufferLayout.offset(BufferLayout.u32(),-8),"userdata")]),u=BufferLayout.struct([BufferLayout.u32("keysLength"),BufferLayout.u32("keysLengthPadding"),BufferLayout.seq(publicKey("key"),BufferLayout.offset(BufferLayout.u32(),-8),"keys"),publicKey("lastId"),BufferLayout.ns64("fee"),BufferLayout.u32("programIdsLength"),BufferLayout.u32("programIdsLengthPadding"),BufferLayout.seq(publicKey("programId"),BufferLayout.offset(BufferLayout.u32(),-8),"programIds"),BufferLayout.u32("instructionsLength"),BufferLayout.u32("instructionsLengthPadding"),BufferLayout.seq(a,BufferLayout.offset(BufferLayout.u32(),-8),"instructions")]),o={keys:t.map(function(e){return new PublicKey(e).toBuffer()}),lastId:Buffer.from(bs58.decode(e)),fee:this.fee,programIds:r.map(function(e){return new PublicKey(e).toBuffer()}),instructions:n},s=Buffer.alloc(2048),i=u.encode(o,s);return s=s.slice(0,i)}},{key:"sign",value:function(){this.signPartial.apply(this,arguments)}},{key:"signPartial",value:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];if(0===t.length)throw Error("No signers");var n=t.map(function(e){return{signature:null,publicKey:e instanceof Account?e.publicKey:e}});this.signatures=n;var a=this._getSignData();t.forEach(function(e,t){if(!(e instanceof PublicKey)){var r=nacl.sign.detached(a,e.secretKey);assert(64===r.length),n[t].signature=r}})}},{key:"addSigner",value:function(e){var t=this.signatures.findIndex(function(t){return e.publicKey.equals(t.publicKey)});if(0>t)throw Error("Unknown signer: "+e.publicKey);var r=this._getSignData(),n=nacl.sign.detached(r,e.secretKey);assert(64===n.length),this.signatures[t].signature=n}},{key:"serialize",value:function(){var e=this.signatures;if(!e)throw Error("Transaction has not been signed");var t=this._getSignData(),r=Buffer.alloc(8+64*e.length+t.length);return assert(256>e.length),r.writeUInt8(e.length,0),e.forEach(function(e,t){var n=e.signature;assert(null!==n,"null signature"),assert(64===n.length,"signature has invalid length"),Buffer.from(n).copy(r,8+64*t)}),t.copy(r,8+64*e.length),assert(PACKET_DATA_SIZE>=r.length,"Transaction too large: "+r.length+" > "+PACKET_DATA_SIZE),r}},{key:"keys",get:function(){return assert(1===this.instructions.length),this.instructions[0].keys}},{key:"programId",get:function(){return assert(1===this.instructions.length),this.instructions[0].programId}},{key:"userdata",get:function(){return assert(1===this.instructions.length),this.instructions[0].userdata}}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(t,r,n,a,u){var o=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.ns64("tokens"),BufferLayout.ns64("space"),publicKey("programId")]),s=Buffer.alloc(o.span);return o.encode({instruction:0,tokens:n,space:a,programId:u.toBuffer()},s),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:s})}},{key:"move",value:function(t,r,n){var a=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.ns64("amount")]),u=Buffer.alloc(a.span);return a.encode({instruction:2,amount:n},u),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:u})}},{key:"assign",value:function(t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:r.toBuffer()},a),(new Transaction).add({keys:[t],programId:e.programId,userdata:a})}},{key:"spawn",value:function(t){var r=BufferLayout.struct([BufferLayout.u32("instruction")]),n=Buffer.alloc(r.span);return r.encode({instruction:3},n),(new Transaction).add({keys:[t],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(t){return setTimeout(t,e)})}function createRpcRequest(e){var t,r=this,n=jayson((t=_asyncToGenerator(_regeneratorRuntime.mark(function t(n,a){var u,o;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u={method:"POST",body:n,headers:{"Content-Type":"application/json"}},t.prev=1,t.next=4,fetch(e,u);case 4:return o=t.sent,t.next=7,o.text();case 7:a(null,t.sent),t.next=14;break;case 11:t.prev=11,t.t0=t.catch(1),a(t.t0);case 14:case"end":return t.stop()}},t,r,[[1,11]])})),function(e,r){return t.apply(this,arguments)}));return function(e,t){return new _Promise(function(r,a){n.request(e,t,function(e,t){e?a(e):r(t)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var t=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:t,id:"string",error:"any"}),superstruct.struct({jsonrpc:t,id:"string",error:"null?",result:e})])}var AccountInfoResult=superstruct.struct({executable:"boolean",loader:"array",owner:"array",tokens:"number",userdata:"array"}),GetAccountInfoRpcResult=jsonRpcResult(AccountInfoResult),AccountNotificationResult=superstruct.struct({subscription:"number",result:AccountInfoResult}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["AccountInUse","Confirmed","GenericFailure","ProgramRuntimeError","SignatureNotFound"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTransactionRpcResult=jsonRpcResult("string"),Connection=function(){function e(t){_classCallCheck(this,e),this._rpcWebSocketConnected=!1,this._disableLastIdCaching=!1,this._accountChangeSubscriptions={},this._accountChangeSubscriptionCounter=0;var r=url.parse(t);this._rpcRequest=createRpcRequest(r.href),this._lastIdInfo={lastId:null,seconds:-1,transactionSignatures:[]},r.protocol="https:"===r.protocol?"wss:":"ws:",r.host="",r.port=+r.port+1+"","1"===r.port&&(r.port="wss:"===r.protocol?"8901":"8900"),this._rpcWebSocket=new rpcWebsockets.Client(url.format(r),{autoconnect:!1,max_reconnects:1/0}),this._rpcWebSocket.on("open",this._wsOnOpen.bind(this)),this._rpcWebSocket.on("error",this._wsOnError.bind(this)),this._rpcWebSocket.on("close",this._wsOnClose.bind(this)),this._rpcWebSocket.on("accountNotification",this._wsOnAccountNotification.bind(this))}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[t.toBase58()]);case 2:if(!(r=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[t.toBase58()]);case 2:if(!(r=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==(n=r.result)),e.abrupt("return",{executable:n.executable,loader:new PublicKey(n.loader),owner:new PublicKey(n.owner),tokens:n.tokens,userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[t]);case 2:if(!(r=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[t]);case 2:if(!(r=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(t=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(t=GetLastId(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(t=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[t.toBase58(),r]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;r>a;a++)n[a-1]=arguments[a];var u,o,s,i,c,f;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(u=(new Date).getSeconds(),null==this._lastIdInfo.lastId||this._lastIdInfo.seconds!==u){e.next=11;break}if(t.lastId=this._lastIdInfo.lastId,t.sign.apply(t,n),t.signature){e.next=6;break}throw Error("!signature");case 6:if(this._lastIdInfo.transactionSignatures.includes(o=""+t.signature)){e.next=11;break}return this._lastIdInfo.transactionSignatures.push(o),this._disableLastIdCaching&&(this._lastIdInfo.seconds=-1),e.abrupt("break",28);case 11:s=0,i=Date.now();case 13:return e.next=15,this.getLastId();case 15:if(this._lastIdInfo.lastId==(c=e.sent)){e.next=19;break}return this._lastIdInfo={lastId:c,seconds:(new Date).getSeconds(),transactionSignatures:[]},e.abrupt("break",26);case 19:if(8!==s){e.next=21;break}throw Error("Unable to obtain a new last id after "+(Date.now()-i)+"ms");case 21:return e.next=23,sleep(250);case 23:++s;case 24:e.next=13;break;case 26:e.next=0;break;case 28:return f=t.serialize(),e.next=31,this.sendRawTransaction(f);case 31:return e.abrupt("return",e.sent);case 32:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"sendRawTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(t))]);case 2:if(!(r=SendTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),assert(r.result),e.abrupt("return",r.result);case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"_wsOnOpen",value:function(){this._rpcWebSocketConnected=!0,this._updateSubscriptions()}},{key:"_wsOnError",value:function(e){console.log("ws error:",e.message)}},{key:"_wsOnClose",value:function(e,t){1e3!==e&&console.log("ws close:",e,t),this._rpcWebSocketConnected=!1}},{key:"_wsOnAccountNotification",value:function(e){var t=AccountNotificationResult(e);if(t.error)throw Error(t.error.message);var r=_Object$keys(this._accountChangeSubscriptions).map(Number),n=!0,a=!1,u=void 0;try{for(var o,s=_getIterator(r);!(n=(o=s.next()).done);n=!0){var i=this._accountChangeSubscriptions[o.value];if(i.subscriptionId===t.subscription){var c=t.result;return assert(void 0!==c),i.callback({executable:c.executable,loader:new PublicKey(c.loader),owner:new PublicKey(c.owner),tokens:c.tokens,userdata:Buffer.from(c.userdata)}),!0}}}catch(e){a=!0,u=e}finally{try{!n&&s.return&&s.return()}finally{if(a)throw u}}}},{key:"_updateSubscriptions",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n,a,u,o,s,i,c,f,l,p,d,h;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(0!==(t=_Object$keys(this._accountChangeSubscriptions).map(Number)).length){e.next=4;break}return this._rpcWebSocket.close(),e.abrupt("return");case 4:if(this._rpcWebSocketConnected){e.next=26;break}for(r=!0,n=!1,a=void 0,e.prev=8,u=_getIterator(t);!(r=(o=u.next()).done);r=!0)this._accountChangeSubscriptions[o.value].subscriptionId=null;e.next=16;break;case 12:e.prev=12,e.t0=e.catch(8),n=!0,a=e.t0;case 16:e.prev=16,e.prev=17,!r&&u.return&&u.return();case 19:if(e.prev=19,!n){e.next=22;break}throw a;case 22:return e.finish(19);case 23:return e.finish(16);case 24:return this._rpcWebSocket.connect(),e.abrupt("return");case 26:s=!0,i=!1,c=void 0,e.prev=29,f=_getIterator(t);case 31:if(s=(l=f.next()).done){e.next=47;break}if(h=(d=this._accountChangeSubscriptions[p=l.value]).publicKey,null!==d.subscriptionId){e.next=44;break}return e.prev=35,e.next=38,this._rpcWebSocket.call("accountSubscribe",[h]);case 38:this._accountChangeSubscriptions[p].subscriptionId=e.sent,e.next=44;break;case 41:e.prev=41,e.t1=e.catch(35),console.log("accountSubscribe error for "+h+": "+e.t1.message);case 44:s=!0,e.next=31;break;case 47:e.next=53;break;case 49:e.prev=49,e.t2=e.catch(29),i=!0,c=e.t2;case 53:e.prev=53,e.prev=54,!s&&f.return&&f.return();case 56:if(e.prev=56,!i){e.next=59;break}throw c;case 59:return e.finish(56);case 60:return e.finish(53);case 61:case"end":return e.stop()}},e,this,[[8,12,16,24],[17,,19,23],[29,49,53,61],[35,41],[54,,56,60]])}));return function(){return e.apply(this,arguments)}}()},{key:"onAccountChange",value:function(e,t){var r=++this._accountChangeSubscriptionCounter;return this._accountChangeSubscriptions[r]={publicKey:e.toBase58(),callback:t,subscriptionId:null},this._updateSubscriptions(),r}},{key:"removeAccountChangeListener",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._accountChangeSubscriptions[t]){e.next=15;break}if(r=this._accountChangeSubscriptions[t].subscriptionId,delete this._accountChangeSubscriptions[t],null===r){e.next=12;break}return e.prev=4,e.next=7,this._rpcWebSocket.call("accountUnsubscribe",[r]);case 7:e.next=12;break;case 9:e.prev=9,e.t0=e.catch(4),console.log("accountUnsubscribe error:",e.t0.message);case 12:this._updateSubscriptions(),e.next=16;break;case 15:throw Error("Unknown account change id: "+t);case 16:case"end":return e.stop()}},e,this,[[4,9]])}));return function(t){return e.apply(this,arguments)}}()}]),e}(),sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){for(var n=arguments.length,a=Array(n>2?n-2:0),u=2;n>u;u++)a[u-2]=arguments[u];var o,s,i,c,f,l;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:o=10,s=void 0;case 2:return i=Date.now(),e.next=5,t.sendTransaction.apply(t,[r].concat(a));case 5:s=e.sent,c="SignatureNotFound",f=4;case 8:return e.next=10,t.getSignatureStatus(s);case 10:if("SignatureNotFound"===(c=e.sent)){e.next=13;break}return e.abrupt("break",20);case 13:return e.next=15,sleep(500);case 15:if(0<--f){e.next=18;break}throw l=(Date.now()-i)/1e3,Error("Transaction '"+s+"' was not confirmed in "+l.toFixed(2)+" seconds ("+c+")");case 18:e.next=8;break;case 20:if("Confirmed"!==c){e.next=22;break}return e.abrupt("break",28);case 22:if("AccountInUse"===c&&--o>0){e.next=24;break}throw Error("Transaction "+s+" failed ("+c+")");case 24:return e.next=26,sleep(100*Math.random());case 26:e.next=2;break;case 28:return assert(void 0!==s),e.abrupt("return",s);case 30:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}(),Loader=function(){function e(t,r){_classCallCheck(this,e),_Object$assign(this,{connection:t,programId:r})}return _createClass(e,null,[{key:"chunkSize",get:function(){return 256}}]),_createClass(e,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n){var a,u,o,s,i,c,f,l;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:a=BufferLayout.struct([BufferLayout.u32("instruction"),BufferLayout.u32("offset"),BufferLayout.u32("bytesLength"),BufferLayout.u32("bytesLengthPadding"),BufferLayout.seq(BufferLayout.u8("byte"),BufferLayout.offset(BufferLayout.u32(),-8),"bytes")]),u=e.chunkSize,o=0,s=n,i=[];case 5:if(s.length<=0){t.next=19;break}if(c=s.slice(0,u),f=Buffer.alloc(u+16),a.encode({instruction:0,offset:o,bytes:c},f),l=(new Transaction).add({keys:[r.publicKey],programId:this.programId,userdata:f}),i.push(sendAndConfirmTransaction(this.connection,l,r)),8!==i.length){t.next=15;break}return t.next=14,_Promise.all(i);case 14:i=[];case 15:o+=u,s=s.slice(u),t.next=5;break;case 19:return t.next=21,_Promise.all(i);case 21:case"end":return t.stop()}},t,this)}));return function(e,r){return t.apply(this,arguments)}}()},{key:"finalize",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=BufferLayout.struct([BufferLayout.u32("instruction")]),n=Buffer.alloc(r.span),r.encode({instruction:1},n),(a=new Transaction).add({keys:[t.publicKey],programId:this.programId,userdata:n}),a.add(SystemProgram.spawn(t.publicKey)),e.next=8,sendAndConfirmTransaction(this.connection,a,t);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()}]),e}(),BpfLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var u,o,s;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u=new Account,o=SystemProgram.createAccount(n.publicKey,u.publicKey,1+Math.ceil(a.length/Loader.chunkSize)+1,a.length,e.programId),t.next=4,sendAndConfirmTransaction(r,o,n);case 4:return s=new Loader(r,e.programId),t.next=7,s.load(u,a);case 7:return t.next=9,s.finalize(u);case 9:return t.abrupt("return",u.publicKey);case 10:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x8000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function serializePayment(e){var t=e.to.toBuffer(),r=Buffer.alloc(8+t.length);return r.writeUInt32LE(e.amount,0),t.copy(r,8),r}function serializeDate(e){var t=Buffer.alloc(28);return t.writeUInt32LE(20,0),t.write(function(e){function t(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+t(e.getUTCMonth()+1)+"-"+t(e.getUTCDate())+"T"+t(e.getUTCHours())+":"+t(e.getUTCMinutes())+":"+t(e.getUTCSeconds())+"Z"}(e),8),t}function serializeCondition(e){switch(e.type){case"timestamp":var t=serializeDate(e.when),r=e.from.toBuffer(),n=Buffer.alloc(4+t.length+r.length);return n.writeUInt32LE(0,0),t.copy(n,4),r.copy(n,4+t.length),n;case"signature":var a=BufferLayout.struct([BufferLayout.u32("condition"),publicKey("from")]),u=e.from.toBuffer(),o=Buffer.alloc(4+u.length);return a.encode({instruction:1,from:u},o),o;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,t){return{type:"timestamp",from:e,when:t}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,t,r,n){var a=Buffer.alloc(1024),u=0;a.writeUInt32LE(0,u),u+=4;for(var o=arguments.length,s=Array(o>4?o-4:0),i=4;o>i;i++)s[i-4]=arguments[i];switch(s.length){case 0:a.writeUInt32LE(0,u),u+=4;var c=serializePayment({amount:n,to:r});return c.copy(a,u),u+=c.length,(new Transaction).add({keys:[e,r],programId:this.programId,userdata:a.slice(0,u)});case 1:a.writeUInt32LE(1,u),u+=4;var f=serializeCondition(s[0]);f.copy(a,u),u+=f.length;var l=serializePayment({amount:n,to:r});return l.copy(a,u),u+=l.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,u)});case 2:a.writeUInt32LE(2,u),u+=4;var p=!0,d=!1,h=void 0;try{for(var y,g=_getIterator(s);!(p=(y=g.next()).done);p=!0){var m=serializeCondition(y.value);m.copy(a,u),u+=m.length;var b=serializePayment({amount:n,to:r});b.copy(a,u),u+=b.length}}catch(e){d=!0,h=e}finally{try{!p&&g.return&&g.return()}finally{if(d)throw h}}return(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,u)});default:throw Error("A maximum of two conditions are support: "+s.length+" provided")}}},{key:"payOnBoth",value:function(e,t,r,n,a,u){var o=Buffer.alloc(1024),s=0;o.writeUInt32LE(0,s),o.writeUInt32LE(3,s+=4),s+=4;for(var i=[a,u],c=0;i.length>c;c++){var f=serializeCondition(i[c]);f.copy(o,s),s+=f.length}var l=serializePayment({amount:n,to:r});return l.copy(o,s),s+=l.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:o.slice(0,s)})}},{key:"applyTimestamp",value:function(e,t,r,n){var a=serializeDate(n),u=Buffer.alloc(4+a.length);return u.writeUInt32LE(1,0),a.copy(u,4),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:u})}},{key:"applySignature",value:function(e,t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x8100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}(),NativeLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var u,o,s,i;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u=[].concat(_toConsumableArray(Buffer.from(a))),o=new Account,s=SystemProgram.createAccount(n.publicKey,o.publicKey,3,u.length+1,e.programId),t.next=5,sendAndConfirmTransaction(r,s,n);case 5:return i=new Loader(r,e.programId),t.next=8,i.load(o,u);case 8:return t.next=10,i.finalize(o);case 10:return t.abrupt("return",o.publicKey);case 11:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}}]),e}(),TokenAmount=function(e){function t(){return _classCallCheck(this,t),_possibleConstructorReturn(this,(t.__proto__||_Object$getPrototypeOf(t)).apply(this,arguments))}return _inherits(t,BN),_createClass(t,[{key:"toBuffer",value:function(){var e=_get(t.prototype.__proto__||_Object$getPrototypeOf(t.prototype),"toArray",this).call(this).reverse(),r=Buffer.from(e);if(8===r.length)return r;assert(8>r.length,"TokenAmount too large");var n=Buffer.alloc(8);return r.copy(n),n}}],[{key:"fromBuffer",value:function(e){return assert(8===e.length,"Invalid buffer length: "+e.length),new BN([].concat(_toConsumableArray(e)).reverse().map(function(e){return("00"+e.toString(16)).slice(-2)}).join(""),16)}}]),t}(),TokenInfoLayout=BufferLayout.struct([uint64("supply"),BufferLayout.u8("decimals"),rustString("name"),rustString("symbol")]),TokenAccountInfoLayout=BufferLayout.struct([publicKey("token"),publicKey("owner"),uint64("amount"),BufferLayout.u8("sourceOption"),publicKey("source"),uint64("originalAmount")]),SYSTEM_TOKEN_PROGRAM_ID=new PublicKey("0x8300000000000000000000000000000000000000000000000000000000000000"),Token=function(){function e(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:SYSTEM_TOKEN_PROGRAM_ID;_classCallCheck(this,e),_Object$assign(this,{connection:t,token:r,programId:n})}return _createClass(e,[{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a,u,o,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=new Account,n=void 0,a=BufferLayout.struct([BufferLayout.u32("instruction")]),u=Buffer.alloc(a.span),a.encode({instruction:1},u),n=SystemProgram.createAccount(t.publicKey,r.publicKey,1,1+TokenAccountInfoLayout.span,this.programId),e.next=8,sendAndConfirmTransaction(this.connection,n,t);case 8:return o=[r.publicKey,t.publicKey,this.token],s&&o.push(s),(n=(new Transaction).add({keys:o,programId:this.programId,userdata:u})).fee=0,e.next=14,sendAndConfirmTransaction(this.connection,n,r);case 14:return e.abrupt("return",r.publicKey);case 15:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token owner: "+_JSON$stringify(t.owner));case 5:if(1===(r=Buffer.from(t.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token userdata");case 8:return(n=TokenInfoLayout.decode(r,1)).supply=TokenAmount.fromBuffer(n.supply),e.abrupt("return",n);case 11:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(t);case 2:if((r=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token account owner");case 5:if(2===(n=Buffer.from(r.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token account userdata");case 8:if((a=TokenAccountInfoLayout.decode(n,1)).token=new PublicKey(a.token),a.owner=new PublicKey(a.owner),a.amount=TokenAmount.fromBuffer(a.amount),0===a.sourceOption?(a.source=null,a.originalAmount=new TokenAmount):(a.source=new PublicKey(a.source),a.originalAmount=TokenAmount.fromBuffer(a.originalAmount)),a.token.equals(this.token)){e.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(a.token)+" !== "+_JSON$stringify(this.token));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"transfer",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=sendAndConfirmTransaction,e.t1=this.connection,e.t2=new Transaction,e.next=5,this.transferInstruction(t.publicKey,r,n,a);case 5:return e.t3=e.sent,e.t4=e.t2.add.call(e.t2,e.t3),e.t5=t,e.next=10,(0,e.t0)(e.t1,e.t4,e.t5);case 10:return e.abrupt("return",e.sent);case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approve",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.approveInstruction(t.publicKey,r,n,a)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"revoke",value:function(e,t,r){return this.approve(e,t,r,0)}},{key:"setOwner",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.setOwnerInstruction(t.publicKey,r,n)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n){return e.apply(this,arguments)}}()},{key:"transferInstruction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){var u,o,s,i;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.accountInfo(r);case 2:if(t.equals((u=e.sent).owner)){e.next=5;break}throw Error("Account owner mismatch");case 5:return o=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),s=Buffer.alloc(o.span),o.encode({instruction:2,amount:new TokenAmount(a).toBuffer()},s),i=[t,r,n],u.source&&i.push(u.source),e.abrupt("return",new TransactionInstruction({keys:i,programId:this.programId,userdata:s}));case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approveInstruction",value:function(e,t,r,n){var a=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),u=Buffer.alloc(a.span);return a.encode({instruction:3,amount:new TokenAmount(n).toBuffer()},u),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:u})}},{key:"revokeInstruction",value:function(e,t,r){return this.approveInstruction(e,t,r,0)}},{key:"setOwnerInstruction",value:function(e,t,r){var n=BufferLayout.struct([BufferLayout.u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:4},a),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:a})}}],[{key:"createNewToken",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a,u,o,s){var i,c,f,l,p,d,h,y=arguments.length>6&&void 0!==arguments[6]?arguments[6]:SYSTEM_TOKEN_PROGRAM_ID;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return i=new Account,c=new e(r,i.publicKey,y),t.next=4,c.newAccount(n,null);case 4:return f=t.sent,l=void 0,p=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("supply"),BufferLayout.u8("decimals"),rustString("name"),rustString("symbol")]),d=Buffer.alloc(1024),h=p.encode({instruction:0,supply:a.toBuffer(),decimals:s,name:u,symbol:o},d),d=d.slice(0,h),l=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+d.length,y),t.next=13,sendAndConfirmTransaction(r,l,n);case 13:return(l=(new Transaction).add({keys:[i.publicKey,f],programId:y,userdata:d})).fee=0,t.next=17,sendAndConfirmTransaction(r,l,i);case 17:return t.abrupt("return",[c,f]);case 18:case"end":return t.stop()}},t,this)}));return function(e,r,n,a,u,o){return t.apply(this,arguments)}}()}]),e}(),sendAndConfirmRawTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n,a,u,o,s;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return n=Date.now(),e.next=3,t.sendRawTransaction(r);case 3:a=e.sent,u="",o=4;case 6:return e.next=8,t.getSignatureStatus(a);case 8:if("SignatureNotFound"===(u=e.sent)){e.next=11;break}return e.abrupt("break",18);case 11:return e.next=13,sleep(500);case 13:if(0<--o){e.next=16;break}throw s=(Date.now()-n)/1e3,Error("Raw Transaction '"+a+"' was not confirmed in "+s.toFixed(2)+" seconds ("+u+")");case 16:e.next=6;break;case 18:if("Confirmed"!==u){e.next=20;break}return e.abrupt("return",a);case 20:throw Error("Raw transaction "+a+" failed ("+u+")");case 21:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}();exports.Account=Account,exports.BpfLoader=BpfLoader,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.Loader=Loader,exports.NativeLoader=NativeLoader,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Token=Token,exports.TokenAmount=TokenAmount,exports.Transaction=Transaction,exports.TransactionInstruction=TransactionInstruction,exports.sendAndConfirmTransaction=sendAndConfirmTransaction,exports.sendAndConfirmRawTransaction=sendAndConfirmRawTransaction;

@@ -1,1 +0,1 @@

import nacl from"tweetnacl";import bs58 from"bs58";import _Object$keys from"babel-runtime/core-js/object/keys";import{parse,format}from"url";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct as struct$1}from"superstruct";import{Client}from"rpc-websockets";import _Promise from"babel-runtime/core-js/promise";import _getIterator from"babel-runtime/core-js/get-iterator";import _JSON$stringify from"babel-runtime/core-js/json/stringify";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";import _Object$assign from"babel-runtime/core-js/object/assign";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Object$getPrototypeOf from"babel-runtime/core-js/object/get-prototype-of";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _get from"babel-runtime/helpers/get";import _inherits from"babel-runtime/helpers/inherits";import assert from"assert";import BN from"bn.js";import{struct,u32,seq,u8,offset,ns64,blob}from"buffer-layout";var PublicKey=function(){function e(t){if(_classCallCheck(this,e),this._bn="string"==typeof t?t.startsWith("0x")?new BN(t.substring(2),16):new BN(bs58.decode(t)):new BN(t),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var t=Buffer.alloc(32);return e.copy(t,32-e.length),t}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(t){return t instanceof e}}]),e}(),Account=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=t?nacl.sign.keyPair.fromSecretKey(t):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),publicKey=function(){return blob(32,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"publicKey")},uint64=function(){return blob(8,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"uint64")},rustString=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"string",t=struct([u32("length"),u32("lengthPadding"),blob(offset(u32(),-8),"chars")],e),r=t.decode.bind(t),n=t.encode.bind(t);return t.decode=function(e,t){return r(e,t).chars.toString("utf8")},t.encode=function(e,t,r){var a={chars:Buffer.from(e,"utf8")};return n(a,t,r)},t},PACKET_DATA_SIZE=512,TransactionInstruction=function e(t){_classCallCheck(this,e),this.keys=[],this.userdata=Buffer.alloc(0),t&&_Object$assign(this,t)},Transaction=function(){function e(t){_classCallCheck(this,e),this.signatures=[],this.instructions=[],this.fee=1,t&&_Object$assign(this,t)}return _createClass(e,[{key:"signature",get:function(){return this.signatures.length>0?this.signatures[0].signature:null}}]),_createClass(e,[{key:"add",value:function(){for(var t=this,r=arguments.length,n=Array(r),a=0;r>a;a++)n[a]=arguments[a];if(0===n.length)throw Error("No instructions");return n.forEach(function(r){r instanceof e?t.instructions=t.instructions.concat(r.instructions):t.instructions.push(new TransactionInstruction(r))}),this}},{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");if(1>this.instructions.length)throw Error("No instructions provided");var t=this.signatures.map(function(e){return""+e.publicKey}),r=[];this.instructions.forEach(function(e){var n=""+e.programId;r.includes(n)||r.push(n),e.keys.map(function(e){return""+e}).forEach(function(e){t.includes(e)||t.push(e)})});var n=this.instructions.map(function(e){var n=e.userdata;return{programIdIndex:r.indexOf(""+e.programId),keyIndices:e.keys.map(function(e){return t.indexOf(""+e)}),userdata:n}});n.forEach(function(e){assert(e.programIdIndex>=0),e.keyIndices.forEach(function(e){return assert(e>=0)})});var a=struct([u8("programIdIndex"),u32("keyIndicesLength"),u32("keyIndicesLengthPadding"),seq(u8("keyIndex"),offset(u32(),-8),"keyIndices"),u32("userdataLength"),u32("userdataLengthPadding"),seq(u8("userdatum"),offset(u32(),-8),"userdata")]),o=struct([u32("keysLength"),u32("keysLengthPadding"),seq(publicKey("key"),offset(u32(),-8),"keys"),publicKey("lastId"),ns64("fee"),u32("programIdsLength"),u32("programIdsLengthPadding"),seq(publicKey("programId"),offset(u32(),-8),"programIds"),u32("instructionsLength"),u32("instructionsLengthPadding"),seq(a,offset(u32(),-8),"instructions")]),s={keys:t.map(function(e){return new PublicKey(e).toBuffer()}),lastId:Buffer.from(bs58.decode(e)),fee:this.fee,programIds:r.map(function(e){return new PublicKey(e).toBuffer()}),instructions:n},u=Buffer.alloc(2048),i=o.encode(s,u);return u=u.slice(0,i)}},{key:"sign",value:function(){this.signPartial.apply(this,arguments)}},{key:"signPartial",value:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];if(0===t.length)throw Error("No signers");var n=t.map(function(e){return{signature:null,publicKey:e instanceof Account?e.publicKey:e}});this.signatures=n;var a=this._getSignData();t.forEach(function(e,t){if(!(e instanceof PublicKey)){var r=nacl.sign.detached(a,e.secretKey);assert(64===r.length),n[t].signature=r}})}},{key:"addSigner",value:function(e){var t=this.signatures.findIndex(function(t){return e.publicKey.equals(t.publicKey)});if(0>t)throw Error("Unknown signer: "+e.publicKey);var r=this._getSignData(),n=nacl.sign.detached(r,e.secretKey);assert(64===n.length),this.signatures[t].signature=n}},{key:"serialize",value:function(){var e=this.signatures;if(!e)throw Error("Transaction has not been signed");var t=this._getSignData(),r=Buffer.alloc(8+64*e.length+t.length);return assert(256>e.length),r.writeUInt8(e.length,0),e.forEach(function(e,t){var n=e.signature;assert(null!==n,"null signature"),assert(64===n.length,"signature has invalid length"),Buffer.from(n).copy(r,8+64*t)}),t.copy(r,8+64*e.length),assert(PACKET_DATA_SIZE>=r.length,"Transaction too large: "+r.length+" > "+PACKET_DATA_SIZE),r}},{key:"keys",get:function(){return assert(1===this.instructions.length),this.instructions[0].keys}},{key:"programId",get:function(){return assert(1===this.instructions.length),this.instructions[0].programId}},{key:"userdata",get:function(){return assert(1===this.instructions.length),this.instructions[0].userdata}}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(t,r,n,a,o){var s=struct([u32("instruction"),ns64("tokens"),ns64("space"),publicKey("programId")]),u=Buffer.alloc(s.span);return s.encode({instruction:0,tokens:n,space:a,programId:o.toBuffer()},u),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:u})}},{key:"move",value:function(t,r,n){var a=struct([u32("instruction"),ns64("amount")]),o=Buffer.alloc(a.span);return a.encode({instruction:2,amount:n},o),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:o})}},{key:"assign",value:function(t,r){var n=struct([u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:r.toBuffer()},a),(new Transaction).add({keys:[t],programId:e.programId,userdata:a})}},{key:"spawn",value:function(t){var r=struct([u32("instruction")]),n=Buffer.alloc(r.span);return r.encode({instruction:3},n),(new Transaction).add({keys:[t],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(t){return setTimeout(t,e)})}function createRpcRequest(e){var t,r=this,n=jayson((t=_asyncToGenerator(_regeneratorRuntime.mark(function t(n,a){var o,s;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o={method:"POST",body:n,headers:{"Content-Type":"application/json"}},t.prev=1,t.next=4,fetch(e,o);case 4:return s=t.sent,t.next=7,s.text();case 7:a(null,t.sent),t.next=14;break;case 11:t.prev=11,t.t0=t.catch(1),a(t.t0);case 14:case"end":return t.stop()}},t,r,[[1,11]])})),function(e,r){return t.apply(this,arguments)}));return function(e,t){return new _Promise(function(r,a){n.request(e,t,function(e,t){e?a(e):r(t)})})}}var GetBalanceRpcResult=struct$1({jsonrpc:struct$1.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var t=struct$1.literal("2.0");return struct$1.union([struct$1({jsonrpc:t,id:"string",error:"any"}),struct$1({jsonrpc:t,id:"string",error:"null?",result:e})])}var AccountInfoResult=struct$1({executable:"boolean",loader:"array",owner:"array",tokens:"number",userdata:"array"}),GetAccountInfoRpcResult=jsonRpcResult(AccountInfoResult),AccountNotificationResult=struct$1({subscription:"number",result:AccountInfoResult}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct$1.enum(["AccountInUse","Confirmed","GenericFailure","ProgramRuntimeError","SignatureNotFound"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(t){_classCallCheck(this,e),this._rpcWebSocketConnected=!1,this._disableLastIdCaching=!1,this._accountChangeSubscriptions={},this._accountChangeSubscriptionCounter=0;var r=parse(t);this._rpcRequest=createRpcRequest(r.href),this._lastIdInfo={lastId:null,seconds:-1,transactionSignatures:[]},r.protocol="https:"===r.protocol?"wss:":"ws:",r.host="",r.port=+r.port+1+"","1"===r.port&&(r.port="wss:"===r.protocol?"8901":"8900"),this._rpcWebSocket=new Client(format(r),{autoconnect:!1,max_reconnects:1/0}),this._rpcWebSocket.on("open",this._wsOnOpen.bind(this)),this._rpcWebSocket.on("error",this._wsOnError.bind(this)),this._rpcWebSocket.on("close",this._wsOnClose.bind(this)),this._rpcWebSocket.on("accountNotification",this._wsOnAccountNotification.bind(this))}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[t.toBase58()]);case 2:if(!(r=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[t.toBase58()]);case 2:if(!(r=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==(n=r.result)),e.abrupt("return",{executable:n.executable,loader:new PublicKey(n.loader),owner:new PublicKey(n.owner),tokens:n.tokens,userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[t]);case 2:if(!(r=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[t]);case 2:if(!(r=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(t=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(t=GetLastId(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(t=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[t.toBase58(),r]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;r>a;a++)n[a-1]=arguments[a];var o,s,u,i,c,l;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(o=(new Date).getSeconds(),null==this._lastIdInfo.lastId||this._lastIdInfo.seconds!==o){e.next=11;break}if(t.lastId=this._lastIdInfo.lastId,t.sign.apply(t,n),t.signature){e.next=6;break}throw Error("!signature");case 6:if(this._lastIdInfo.transactionSignatures.includes(s=""+t.signature)){e.next=11;break}return this._lastIdInfo.transactionSignatures.push(s),this._disableLastIdCaching&&(this._lastIdInfo.seconds=-1),e.abrupt("break",28);case 11:u=0,i=Date.now();case 13:return e.next=15,this.getLastId();case 15:if(this._lastIdInfo.lastId==(c=e.sent)){e.next=19;break}return this._lastIdInfo={lastId:c,seconds:(new Date).getSeconds(),transactionSignatures:[]},e.abrupt("break",26);case 19:if(8!==u){e.next=21;break}throw Error("Unable to obtain a new last id after "+(Date.now()-i)+"ms");case 21:return e.next=23,sleep(250);case 23:++u;case 24:e.next=13;break;case 26:e.next=0;break;case 28:return l=t.serialize(),e.next=31,this.sendRawTransaction(l);case 31:return e.abrupt("return",e.sent);case 32:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"sendRawTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(t))]);case 2:if(!(r=SendTokensRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),assert(r.result),e.abrupt("return",r.result);case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"_wsOnOpen",value:function(){this._rpcWebSocketConnected=!0,this._updateSubscriptions()}},{key:"_wsOnError",value:function(e){console.log("ws error:",e.message)}},{key:"_wsOnClose",value:function(e,t){1e3!==e&&console.log("ws close:",e,t),this._rpcWebSocketConnected=!1}},{key:"_wsOnAccountNotification",value:function(e){var t=AccountNotificationResult(e);if(t.error)throw Error(t.error.message);var r=_Object$keys(this._accountChangeSubscriptions).map(Number),n=!0,a=!1,o=void 0;try{for(var s,u=_getIterator(r);!(n=(s=u.next()).done);n=!0){var i=this._accountChangeSubscriptions[s.value];if(i.subscriptionId===t.subscription){var c=t.result;return assert(void 0!==c),i.callback({executable:c.executable,loader:new PublicKey(c.loader),owner:new PublicKey(c.owner),tokens:c.tokens,userdata:Buffer.from(c.userdata)}),!0}}}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}}},{key:"_updateSubscriptions",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n,a,o,s,u,i,c,l,f,p,h,d;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(0!==(t=_Object$keys(this._accountChangeSubscriptions).map(Number)).length){e.next=4;break}return this._rpcWebSocket.close(),e.abrupt("return");case 4:if(this._rpcWebSocketConnected){e.next=26;break}for(r=!0,n=!1,a=void 0,e.prev=8,o=_getIterator(t);!(r=(s=o.next()).done);r=!0)this._accountChangeSubscriptions[s.value].subscriptionId=null;e.next=16;break;case 12:e.prev=12,e.t0=e.catch(8),n=!0,a=e.t0;case 16:e.prev=16,e.prev=17,!r&&o.return&&o.return();case 19:if(e.prev=19,!n){e.next=22;break}throw a;case 22:return e.finish(19);case 23:return e.finish(16);case 24:return this._rpcWebSocket.connect(),e.abrupt("return");case 26:u=!0,i=!1,c=void 0,e.prev=29,l=_getIterator(t);case 31:if(u=(f=l.next()).done){e.next=47;break}if(d=(h=this._accountChangeSubscriptions[p=f.value]).publicKey,null!==h.subscriptionId){e.next=44;break}return e.prev=35,e.next=38,this._rpcWebSocket.call("accountSubscribe",[d]);case 38:this._accountChangeSubscriptions[p].subscriptionId=e.sent,e.next=44;break;case 41:e.prev=41,e.t1=e.catch(35),console.log("accountSubscribe error for "+d+": "+e.t1.message);case 44:u=!0,e.next=31;break;case 47:e.next=53;break;case 49:e.prev=49,e.t2=e.catch(29),i=!0,c=e.t2;case 53:e.prev=53,e.prev=54,!u&&l.return&&l.return();case 56:if(e.prev=56,!i){e.next=59;break}throw c;case 59:return e.finish(56);case 60:return e.finish(53);case 61:case"end":return e.stop()}},e,this,[[8,12,16,24],[17,,19,23],[29,49,53,61],[35,41],[54,,56,60]])}));return function(){return e.apply(this,arguments)}}()},{key:"onAccountChange",value:function(e,t){var r=++this._accountChangeSubscriptionCounter;return this._accountChangeSubscriptions[r]={publicKey:e.toBase58(),callback:t,subscriptionId:null},this._updateSubscriptions(),r}},{key:"removeAccountChangeListener",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._accountChangeSubscriptions[t]){e.next=15;break}if(r=this._accountChangeSubscriptions[t].subscriptionId,delete this._accountChangeSubscriptions[t],null===r){e.next=12;break}return e.prev=4,e.next=7,this._rpcWebSocket.call("accountUnsubscribe",[r]);case 7:e.next=12;break;case 9:e.prev=9,e.t0=e.catch(4),console.log("accountUnsubscribe error:",e.t0.message);case 12:this._updateSubscriptions(),e.next=16;break;case 15:throw Error("Unknown account change id: "+t);case 16:case"end":return e.stop()}},e,this,[[4,9]])}));return function(t){return e.apply(this,arguments)}}()}]),e}(),sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){for(var n=arguments.length,a=Array(n>2?n-2:0),o=2;n>o;o++)a[o-2]=arguments[o];var s,u,i,c,l,f;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:s=10,u=void 0;case 2:return i=Date.now(),e.next=5,t.sendTransaction.apply(t,[r].concat(a));case 5:u=e.sent,c="SignatureNotFound",l=4;case 8:return e.next=10,t.getSignatureStatus(u);case 10:if("SignatureNotFound"===(c=e.sent)){e.next=13;break}return e.abrupt("break",20);case 13:return e.next=15,sleep(500);case 15:if(0<--l){e.next=18;break}throw f=(Date.now()-i)/1e3,Error("Transaction '"+u+"' was not confirmed in "+f.toFixed(2)+" seconds ("+c+")");case 18:e.next=8;break;case 20:if("Confirmed"!==c){e.next=22;break}return e.abrupt("break",28);case 22:if("AccountInUse"===c&&--s>0){e.next=24;break}throw Error("Transaction "+u+" failed ("+c+")");case 24:return e.next=26,sleep(100*Math.random());case 26:e.next=2;break;case 28:return e.abrupt("return",u);case 29:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}(),Loader=function(){function e(t,r){_classCallCheck(this,e),_Object$assign(this,{connection:t,programId:r})}return _createClass(e,null,[{key:"chunkSize",get:function(){return 256}}]),_createClass(e,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n){var a,o,s,u,i,c,l,f;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:a=struct([u32("instruction"),u32("offset"),u32("bytesLength"),u32("bytesLengthPadding"),seq(u8("byte"),offset(u32(),-8),"bytes")]),o=e.chunkSize,s=0,u=n,i=[];case 5:if(u.length<=0){t.next=19;break}if(c=u.slice(0,o),l=Buffer.alloc(o+16),a.encode({instruction:0,offset:s,bytes:c},l),f=(new Transaction).add({keys:[r.publicKey],programId:this.programId,userdata:l}),i.push(sendAndConfirmTransaction(this.connection,f,r)),8!==i.length){t.next=15;break}return t.next=14,_Promise.all(i);case 14:i=[];case 15:s+=o,u=u.slice(o),t.next=5;break;case 19:return t.next=21,_Promise.all(i);case 21:case"end":return t.stop()}},t,this)}));return function(e,r){return t.apply(this,arguments)}}()},{key:"finalize",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=struct([u32("instruction")]),n=Buffer.alloc(r.span),r.encode({instruction:1},n),(a=new Transaction).add({keys:[t.publicKey],programId:this.programId,userdata:n}),a.add(SystemProgram.spawn(t.publicKey)),e.next=8,sendAndConfirmTransaction(this.connection,a,t);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()}]),e}(),BpfLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var o,s,u;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o=new Account,s=SystemProgram.createAccount(n.publicKey,o.publicKey,1+Math.ceil(a.length/Loader.chunkSize)+1,a.length,e.programId),t.next=4,sendAndConfirmTransaction(r,s,n);case 4:return u=new Loader(r,e.programId),t.next=7,u.load(o,a);case 7:return t.next=9,u.finalize(o);case 9:return t.abrupt("return",o.publicKey);case 10:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x8000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function serializePayment(e){var t=e.to.toBuffer(),r=Buffer.alloc(8+t.length);return r.writeUInt32LE(e.amount,0),t.copy(r,8),r}function serializeDate(e){var t=Buffer.alloc(28);return t.writeUInt32LE(20,0),t.write(function(e){function t(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+t(e.getUTCMonth()+1)+"-"+t(e.getUTCDate())+"T"+t(e.getUTCHours())+":"+t(e.getUTCMinutes())+":"+t(e.getUTCSeconds())+"Z"}(e),8),t}function serializeCondition(e){switch(e.type){case"timestamp":var t=serializeDate(e.when),r=e.from.toBuffer(),n=Buffer.alloc(4+t.length+r.length);return n.writeUInt32LE(0,0),t.copy(n,4),r.copy(n,4+t.length),n;case"signature":var a=struct([u32("condition"),publicKey("from")]),o=e.from.toBuffer(),s=Buffer.alloc(4+o.length);return a.encode({instruction:1,from:o},s),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,t){return{type:"timestamp",from:e,when:t}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,t,r,n){var a=Buffer.alloc(1024),o=0;a.writeUInt32LE(0,o),o+=4;for(var s=arguments.length,u=Array(s>4?s-4:0),i=4;s>i;i++)u[i-4]=arguments[i];switch(u.length){case 0:a.writeUInt32LE(0,o),o+=4;var c=serializePayment({amount:n,to:r});return c.copy(a,o),o+=c.length,(new Transaction).add({keys:[e,r],programId:this.programId,userdata:a.slice(0,o)});case 1:a.writeUInt32LE(1,o),o+=4;var l=serializeCondition(u[0]);l.copy(a,o),o+=l.length;var f=serializePayment({amount:n,to:r});return f.copy(a,o),o+=f.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,o)});case 2:a.writeUInt32LE(2,o),o+=4;var p=!0,h=!1,d=void 0;try{for(var g,m=_getIterator(u);!(p=(g=m.next()).done);p=!0){var y=serializeCondition(g.value);y.copy(a,o),o+=y.length;var b=serializePayment({amount:n,to:r});b.copy(a,o),o+=b.length}}catch(e){h=!0,d=e}finally{try{!p&&m.return&&m.return()}finally{if(h)throw d}}return(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,o)});default:throw Error("A maximum of two conditions are support: "+u.length+" provided")}}},{key:"payOnBoth",value:function(e,t,r,n,a,o){var s=Buffer.alloc(1024),u=0;s.writeUInt32LE(0,u),s.writeUInt32LE(3,u+=4),u+=4;for(var i=[a,o],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(s,u),u+=l.length}var f=serializePayment({amount:n,to:r});return f.copy(s,u),u+=f.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:s.slice(0,u)})}},{key:"applyTimestamp",value:function(e,t,r,n){var a=serializeDate(n),o=Buffer.alloc(4+a.length);return o.writeUInt32LE(1,0),a.copy(o,4),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:o})}},{key:"applySignature",value:function(e,t,r){var n=struct([u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x8100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}(),NativeLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var o,s,u,i;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o=[].concat(_toConsumableArray(Buffer.from(a))),s=new Account,u=SystemProgram.createAccount(n.publicKey,s.publicKey,3,o.length+1,e.programId),t.next=5,sendAndConfirmTransaction(r,u,n);case 5:return i=new Loader(r,e.programId),t.next=8,i.load(s,o);case 8:return t.next=10,i.finalize(s);case 10:return t.abrupt("return",s.publicKey);case 11:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}}]),e}(),TokenAmount=function(e){function t(){return _classCallCheck(this,t),_possibleConstructorReturn(this,(t.__proto__||_Object$getPrototypeOf(t)).apply(this,arguments))}return _inherits(t,BN),_createClass(t,[{key:"toBuffer",value:function(){var e=_get(t.prototype.__proto__||_Object$getPrototypeOf(t.prototype),"toArray",this).call(this).reverse(),r=Buffer.from(e);if(8===r.length)return r;assert(8>r.length,"TokenAmount too large");var n=Buffer.alloc(8);return r.copy(n),n}}],[{key:"fromBuffer",value:function(e){return assert(8===e.length,"Invalid buffer length: "+e.length),new BN([].concat(_toConsumableArray(e)).reverse().map(function(e){return("00"+e.toString(16)).slice(-2)}).join(""),16)}}]),t}(),TokenInfoLayout=struct([uint64("supply"),u8("decimals"),rustString("name"),rustString("symbol")]),TokenAccountInfoLayout=struct([publicKey("token"),publicKey("owner"),uint64("amount"),u8("sourceOption"),publicKey("source"),uint64("originalAmount")]),SYSTEM_TOKEN_PROGRAM_ID=new PublicKey("0x8300000000000000000000000000000000000000000000000000000000000000"),Token=function(){function e(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:SYSTEM_TOKEN_PROGRAM_ID;_classCallCheck(this,e),_Object$assign(this,{connection:t,token:r,programId:n})}return _createClass(e,[{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a,o,s,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=new Account,n=void 0,a=struct([u32("instruction")]),o=Buffer.alloc(a.span),a.encode({instruction:1},o),n=SystemProgram.createAccount(t.publicKey,r.publicKey,1,1+TokenAccountInfoLayout.span,this.programId),e.next=8,sendAndConfirmTransaction(this.connection,n,t);case 8:return s=[r.publicKey,t.publicKey,this.token],u&&s.push(u),(n=(new Transaction).add({keys:s,programId:this.programId,userdata:o})).fee=0,e.next=14,sendAndConfirmTransaction(this.connection,n,r);case 14:return e.abrupt("return",r.publicKey);case 15:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token owner: "+_JSON$stringify(t.owner));case 5:if(1===(r=Buffer.from(t.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token userdata");case 8:return(n=TokenInfoLayout.decode(r,1)).supply=TokenAmount.fromBuffer(n.supply),e.abrupt("return",n);case 11:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(t);case 2:if((r=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token account owner");case 5:if(2===(n=Buffer.from(r.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token account userdata");case 8:if((a=TokenAccountInfoLayout.decode(n,1)).token=new PublicKey(a.token),a.owner=new PublicKey(a.owner),a.amount=TokenAmount.fromBuffer(a.amount),0===a.sourceOption?(a.source=null,a.originalAmount=new TokenAmount):(a.source=new PublicKey(a.source),a.originalAmount=TokenAmount.fromBuffer(a.originalAmount)),a.token.equals(this.token)){e.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(a.token)+" !== "+_JSON$stringify(this.token));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"transfer",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=sendAndConfirmTransaction,e.t1=this.connection,e.t2=new Transaction,e.next=5,this.transferInstruction(t.publicKey,r,n,a);case 5:return e.t3=e.sent,e.t4=e.t2.add.call(e.t2,e.t3),e.t5=t,e.next=10,(0,e.t0)(e.t1,e.t4,e.t5);case 10:return e.abrupt("return",e.sent);case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approve",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.approveInstruction(t.publicKey,r,n,a)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"revoke",value:function(e,t,r){return this.approve(e,t,r,0)}},{key:"setOwner",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.setOwnerInstruction(t.publicKey,r,n)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n){return e.apply(this,arguments)}}()},{key:"transferInstruction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){var o,s,u,i;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.accountInfo(r);case 2:if(t.equals((o=e.sent).owner)){e.next=5;break}throw Error("Account owner mismatch");case 5:return s=struct([u32("instruction"),uint64("amount")]),u=Buffer.alloc(s.span),s.encode({instruction:2,amount:new TokenAmount(a).toBuffer()},u),i=[t,r,n],o.source&&i.push(o.source),e.abrupt("return",new TransactionInstruction({keys:i,programId:this.programId,userdata:u}));case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approveInstruction",value:function(e,t,r,n){var a=struct([u32("instruction"),uint64("amount")]),o=Buffer.alloc(a.span);return a.encode({instruction:3,amount:new TokenAmount(n).toBuffer()},o),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:o})}},{key:"revokeInstruction",value:function(e,t,r){return this.approveInstruction(e,t,r,0)}},{key:"setOwnerInstruction",value:function(e,t,r){var n=struct([u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:4},a),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:a})}}],[{key:"createNewToken",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a,o,s,u){var i,c,l,f,p,h,d,g=arguments.length>6&&void 0!==arguments[6]?arguments[6]:SYSTEM_TOKEN_PROGRAM_ID;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return i=new Account,c=new e(r,i.publicKey,g),t.next=4,c.newAccount(n,null);case 4:return l=t.sent,f=void 0,p=struct([u32("instruction"),uint64("supply"),u8("decimals"),rustString("name"),rustString("symbol")]),h=Buffer.alloc(1024),d=p.encode({instruction:0,supply:a.toBuffer(),decimals:u,name:o,symbol:s},h),h=h.slice(0,d),f=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+h.length,g),t.next=13,sendAndConfirmTransaction(r,f,n);case 13:return(f=(new Transaction).add({keys:[i.publicKey,l],programId:g,userdata:h})).fee=0,t.next=17,sendAndConfirmTransaction(r,f,i);case 17:return t.abrupt("return",[c,l]);case 18:case"end":return t.stop()}},t,this)}));return function(e,r,n,a,o,s){return t.apply(this,arguments)}}()}]),e}();export{Account,BpfLoader,BudgetProgram,Connection,Loader,NativeLoader,PublicKey,SystemProgram,Token,TokenAmount,Transaction,TransactionInstruction,sendAndConfirmTransaction};
import nacl from"tweetnacl";import bs58 from"bs58";import _Object$keys from"babel-runtime/core-js/object/keys";import{parse,format}from"url";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct as struct$1}from"superstruct";import{Client}from"rpc-websockets";import _Promise from"babel-runtime/core-js/promise";import _getIterator from"babel-runtime/core-js/get-iterator";import _JSON$stringify from"babel-runtime/core-js/json/stringify";import _Object$assign from"babel-runtime/core-js/object/assign";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Object$getPrototypeOf from"babel-runtime/core-js/object/get-prototype-of";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _get from"babel-runtime/helpers/get";import _inherits from"babel-runtime/helpers/inherits";import assert from"assert";import BN from"bn.js";import{struct,u32,seq,u8,offset,ns64,blob}from"buffer-layout";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";var PublicKey=function(){function e(t){if(_classCallCheck(this,e),this._bn="string"==typeof t?t.startsWith("0x")?new BN(t.substring(2),16):new BN(bs58.decode(t)):new BN(t),this._bn.byteLength()>32)throw Error("Invalid public key input")}return _createClass(e,[{key:"equals",value:function(e){return this._bn.eq(e._bn)}},{key:"toBase58",value:function(){return bs58.encode(this.toBuffer())}},{key:"toBuffer",value:function(){var e=this._bn.toArrayLike(Buffer);if(32===e.length)return e;var t=Buffer.alloc(32);return e.copy(t,32-e.length),t}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(t){return t instanceof e}}]),e}(),Account=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=t?nacl.sign.keyPair.fromSecretKey(t):nacl.sign.keyPair()}return _createClass(e,[{key:"publicKey",get:function(){return new PublicKey(this._keypair.publicKey)}},{key:"secretKey",get:function(){return this._keypair.secretKey}}]),e}(),publicKey=function(){return blob(32,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"publicKey")},uint64=function(){return blob(8,arguments.length>0&&void 0!==arguments[0]?arguments[0]:"uint64")},rustString=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"string",t=struct([u32("length"),u32("lengthPadding"),blob(offset(u32(),-8),"chars")],e),r=t.decode.bind(t),n=t.encode.bind(t);return t.decode=function(e,t){return r(e,t).chars.toString("utf8")},t.encode=function(e,t,r){var a={chars:Buffer.from(e,"utf8")};return n(a,t,r)},t},PACKET_DATA_SIZE=512,TransactionInstruction=function e(t){_classCallCheck(this,e),this.keys=[],this.userdata=Buffer.alloc(0),t&&_Object$assign(this,t)},Transaction=function(){function e(t){_classCallCheck(this,e),this.signatures=[],this.instructions=[],this.fee=1,t&&_Object$assign(this,t)}return _createClass(e,[{key:"signature",get:function(){return this.signatures.length>0?this.signatures[0].signature:null}}]),_createClass(e,[{key:"add",value:function(){for(var t=this,r=arguments.length,n=Array(r),a=0;r>a;a++)n[a]=arguments[a];if(0===n.length)throw Error("No instructions");return n.forEach(function(r){r instanceof e?t.instructions=t.instructions.concat(r.instructions):t.instructions.push(new TransactionInstruction(r))}),this}},{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");if(1>this.instructions.length)throw Error("No instructions provided");var t=this.signatures.map(function(e){return""+e.publicKey}),r=[];this.instructions.forEach(function(e){var n=""+e.programId;r.includes(n)||r.push(n),e.keys.map(function(e){return""+e}).forEach(function(e){t.includes(e)||t.push(e)})});var n=this.instructions.map(function(e){var n=e.userdata;return{programIdIndex:r.indexOf(""+e.programId),keyIndices:e.keys.map(function(e){return t.indexOf(""+e)}),userdata:n}});n.forEach(function(e){assert(e.programIdIndex>=0),e.keyIndices.forEach(function(e){return assert(e>=0)})});var a=struct([u8("programIdIndex"),u32("keyIndicesLength"),u32("keyIndicesLengthPadding"),seq(u8("keyIndex"),offset(u32(),-8),"keyIndices"),u32("userdataLength"),u32("userdataLengthPadding"),seq(u8("userdatum"),offset(u32(),-8),"userdata")]),o=struct([u32("keysLength"),u32("keysLengthPadding"),seq(publicKey("key"),offset(u32(),-8),"keys"),publicKey("lastId"),ns64("fee"),u32("programIdsLength"),u32("programIdsLengthPadding"),seq(publicKey("programId"),offset(u32(),-8),"programIds"),u32("instructionsLength"),u32("instructionsLengthPadding"),seq(a,offset(u32(),-8),"instructions")]),s={keys:t.map(function(e){return new PublicKey(e).toBuffer()}),lastId:Buffer.from(bs58.decode(e)),fee:this.fee,programIds:r.map(function(e){return new PublicKey(e).toBuffer()}),instructions:n},u=Buffer.alloc(2048),i=o.encode(s,u);return u=u.slice(0,i)}},{key:"sign",value:function(){this.signPartial.apply(this,arguments)}},{key:"signPartial",value:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];if(0===t.length)throw Error("No signers");var n=t.map(function(e){return{signature:null,publicKey:e instanceof Account?e.publicKey:e}});this.signatures=n;var a=this._getSignData();t.forEach(function(e,t){if(!(e instanceof PublicKey)){var r=nacl.sign.detached(a,e.secretKey);assert(64===r.length),n[t].signature=r}})}},{key:"addSigner",value:function(e){var t=this.signatures.findIndex(function(t){return e.publicKey.equals(t.publicKey)});if(0>t)throw Error("Unknown signer: "+e.publicKey);var r=this._getSignData(),n=nacl.sign.detached(r,e.secretKey);assert(64===n.length),this.signatures[t].signature=n}},{key:"serialize",value:function(){var e=this.signatures;if(!e)throw Error("Transaction has not been signed");var t=this._getSignData(),r=Buffer.alloc(8+64*e.length+t.length);return assert(256>e.length),r.writeUInt8(e.length,0),e.forEach(function(e,t){var n=e.signature;assert(null!==n,"null signature"),assert(64===n.length,"signature has invalid length"),Buffer.from(n).copy(r,8+64*t)}),t.copy(r,8+64*e.length),assert(PACKET_DATA_SIZE>=r.length,"Transaction too large: "+r.length+" > "+PACKET_DATA_SIZE),r}},{key:"keys",get:function(){return assert(1===this.instructions.length),this.instructions[0].keys}},{key:"programId",get:function(){return assert(1===this.instructions.length),this.instructions[0].programId}},{key:"userdata",get:function(){return assert(1===this.instructions.length),this.instructions[0].userdata}}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(t,r,n,a,o){var s=struct([u32("instruction"),ns64("tokens"),ns64("space"),publicKey("programId")]),u=Buffer.alloc(s.span);return s.encode({instruction:0,tokens:n,space:a,programId:o.toBuffer()},u),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:u})}},{key:"move",value:function(t,r,n){var a=struct([u32("instruction"),ns64("amount")]),o=Buffer.alloc(a.span);return a.encode({instruction:2,amount:n},o),(new Transaction).add({keys:[t,r],programId:e.programId,userdata:o})}},{key:"assign",value:function(t,r){var n=struct([u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:r.toBuffer()},a),(new Transaction).add({keys:[t],programId:e.programId,userdata:a})}},{key:"spawn",value:function(t){var r=struct([u32("instruction")]),n=Buffer.alloc(r.span);return r.encode({instruction:3},n),(new Transaction).add({keys:[t],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(t){return setTimeout(t,e)})}function createRpcRequest(e){var t,r=this,n=jayson((t=_asyncToGenerator(_regeneratorRuntime.mark(function t(n,a){var o,s;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o={method:"POST",body:n,headers:{"Content-Type":"application/json"}},t.prev=1,t.next=4,fetch(e,o);case 4:return s=t.sent,t.next=7,s.text();case 7:a(null,t.sent),t.next=14;break;case 11:t.prev=11,t.t0=t.catch(1),a(t.t0);case 14:case"end":return t.stop()}},t,r,[[1,11]])})),function(e,r){return t.apply(this,arguments)}));return function(e,t){return new _Promise(function(r,a){n.request(e,t,function(e,t){e?a(e):r(t)})})}}var GetBalanceRpcResult=struct$1({jsonrpc:struct$1.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var t=struct$1.literal("2.0");return struct$1.union([struct$1({jsonrpc:t,id:"string",error:"any"}),struct$1({jsonrpc:t,id:"string",error:"null?",result:e})])}var AccountInfoResult=struct$1({executable:"boolean",loader:"array",owner:"array",tokens:"number",userdata:"array"}),GetAccountInfoRpcResult=jsonRpcResult(AccountInfoResult),AccountNotificationResult=struct$1({subscription:"number",result:AccountInfoResult}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct$1.enum(["AccountInUse","Confirmed","GenericFailure","ProgramRuntimeError","SignatureNotFound"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTransactionRpcResult=jsonRpcResult("string"),Connection=function(){function e(t){_classCallCheck(this,e),this._rpcWebSocketConnected=!1,this._disableLastIdCaching=!1,this._accountChangeSubscriptions={},this._accountChangeSubscriptionCounter=0;var r=parse(t);this._rpcRequest=createRpcRequest(r.href),this._lastIdInfo={lastId:null,seconds:-1,transactionSignatures:[]},r.protocol="https:"===r.protocol?"wss:":"ws:",r.host="",r.port=+r.port+1+"","1"===r.port&&(r.port="wss:"===r.protocol?"8901":"8900"),this._rpcWebSocket=new Client(format(r),{autoconnect:!1,max_reconnects:1/0}),this._rpcWebSocket.on("open",this._wsOnOpen.bind(this)),this._rpcWebSocket.on("error",this._wsOnError.bind(this)),this._rpcWebSocket.on("close",this._wsOnClose.bind(this)),this._rpcWebSocket.on("accountNotification",this._wsOnAccountNotification.bind(this))}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[t.toBase58()]);case 2:if(!(r=GetBalanceRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[t.toBase58()]);case 2:if(!(r=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==(n=r.result)),e.abrupt("return",{executable:n.executable,loader:new PublicKey(n.loader),owner:new PublicKey(n.owner),tokens:n.tokens,userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[t]);case 2:if(!(r=ConfirmTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[t]);case 2:if(!(r=GetSignatureStatusRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),e.abrupt("return",r.result);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(t=GetTransactionCountRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(t=GetLastId(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(t=GetFinalityRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==t.result),e.abrupt("return",+t.result);case 8:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[t.toBase58(),r]);case 2:if(!(n=RequestAirdropRpcResult(e.sent)).error){e.next=6;break}throw Error(n.error.message);case 6:return assert(void 0!==n.result),e.abrupt("return",n.result);case 8:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){for(var r=arguments.length,n=Array(r>1?r-1:0),a=1;r>a;a++)n[a-1]=arguments[a];var o,s,u,i,c,l;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(o=(new Date).getSeconds(),null==this._lastIdInfo.lastId||this._lastIdInfo.seconds!==o){e.next=11;break}if(t.lastId=this._lastIdInfo.lastId,t.sign.apply(t,n),t.signature){e.next=6;break}throw Error("!signature");case 6:if(this._lastIdInfo.transactionSignatures.includes(s=""+t.signature)){e.next=11;break}return this._lastIdInfo.transactionSignatures.push(s),this._disableLastIdCaching&&(this._lastIdInfo.seconds=-1),e.abrupt("break",28);case 11:u=0,i=Date.now();case 13:return e.next=15,this.getLastId();case 15:if(this._lastIdInfo.lastId==(c=e.sent)){e.next=19;break}return this._lastIdInfo={lastId:c,seconds:(new Date).getSeconds(),transactionSignatures:[]},e.abrupt("break",26);case 19:if(8!==u){e.next=21;break}throw Error("Unable to obtain a new last id after "+(Date.now()-i)+"ms");case 21:return e.next=23,sleep(250);case 23:++u;case 24:e.next=13;break;case 26:e.next=0;break;case 28:return l=t.serialize(),e.next=31,this.sendRawTransaction(l);case 31:return e.abrupt("return",e.sent);case 32:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"sendRawTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(t))]);case 2:if(!(r=SendTransactionRpcResult(e.sent)).error){e.next=6;break}throw Error(r.error.message);case 6:return assert(void 0!==r.result),assert(r.result),e.abrupt("return",r.result);case 9:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"_wsOnOpen",value:function(){this._rpcWebSocketConnected=!0,this._updateSubscriptions()}},{key:"_wsOnError",value:function(e){console.log("ws error:",e.message)}},{key:"_wsOnClose",value:function(e,t){1e3!==e&&console.log("ws close:",e,t),this._rpcWebSocketConnected=!1}},{key:"_wsOnAccountNotification",value:function(e){var t=AccountNotificationResult(e);if(t.error)throw Error(t.error.message);var r=_Object$keys(this._accountChangeSubscriptions).map(Number),n=!0,a=!1,o=void 0;try{for(var s,u=_getIterator(r);!(n=(s=u.next()).done);n=!0){var i=this._accountChangeSubscriptions[s.value];if(i.subscriptionId===t.subscription){var c=t.result;return assert(void 0!==c),i.callback({executable:c.executable,loader:new PublicKey(c.loader),owner:new PublicKey(c.owner),tokens:c.tokens,userdata:Buffer.from(c.userdata)}),!0}}}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}}},{key:"_updateSubscriptions",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n,a,o,s,u,i,c,l,f,p,h,d;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(0!==(t=_Object$keys(this._accountChangeSubscriptions).map(Number)).length){e.next=4;break}return this._rpcWebSocket.close(),e.abrupt("return");case 4:if(this._rpcWebSocketConnected){e.next=26;break}for(r=!0,n=!1,a=void 0,e.prev=8,o=_getIterator(t);!(r=(s=o.next()).done);r=!0)this._accountChangeSubscriptions[s.value].subscriptionId=null;e.next=16;break;case 12:e.prev=12,e.t0=e.catch(8),n=!0,a=e.t0;case 16:e.prev=16,e.prev=17,!r&&o.return&&o.return();case 19:if(e.prev=19,!n){e.next=22;break}throw a;case 22:return e.finish(19);case 23:return e.finish(16);case 24:return this._rpcWebSocket.connect(),e.abrupt("return");case 26:u=!0,i=!1,c=void 0,e.prev=29,l=_getIterator(t);case 31:if(u=(f=l.next()).done){e.next=47;break}if(d=(h=this._accountChangeSubscriptions[p=f.value]).publicKey,null!==h.subscriptionId){e.next=44;break}return e.prev=35,e.next=38,this._rpcWebSocket.call("accountSubscribe",[d]);case 38:this._accountChangeSubscriptions[p].subscriptionId=e.sent,e.next=44;break;case 41:e.prev=41,e.t1=e.catch(35),console.log("accountSubscribe error for "+d+": "+e.t1.message);case 44:u=!0,e.next=31;break;case 47:e.next=53;break;case 49:e.prev=49,e.t2=e.catch(29),i=!0,c=e.t2;case 53:e.prev=53,e.prev=54,!u&&l.return&&l.return();case 56:if(e.prev=56,!i){e.next=59;break}throw c;case 59:return e.finish(56);case 60:return e.finish(53);case 61:case"end":return e.stop()}},e,this,[[8,12,16,24],[17,,19,23],[29,49,53,61],[35,41],[54,,56,60]])}));return function(){return e.apply(this,arguments)}}()},{key:"onAccountChange",value:function(e,t){var r=++this._accountChangeSubscriptionCounter;return this._accountChangeSubscriptions[r]={publicKey:e.toBase58(),callback:t,subscriptionId:null},this._updateSubscriptions(),r}},{key:"removeAccountChangeListener",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._accountChangeSubscriptions[t]){e.next=15;break}if(r=this._accountChangeSubscriptions[t].subscriptionId,delete this._accountChangeSubscriptions[t],null===r){e.next=12;break}return e.prev=4,e.next=7,this._rpcWebSocket.call("accountUnsubscribe",[r]);case 7:e.next=12;break;case 9:e.prev=9,e.t0=e.catch(4),console.log("accountUnsubscribe error:",e.t0.message);case 12:this._updateSubscriptions(),e.next=16;break;case 15:throw Error("Unknown account change id: "+t);case 16:case"end":return e.stop()}},e,this,[[4,9]])}));return function(t){return e.apply(this,arguments)}}()}]),e}(),sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){for(var n=arguments.length,a=Array(n>2?n-2:0),o=2;n>o;o++)a[o-2]=arguments[o];var s,u,i,c,l,f;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:s=10,u=void 0;case 2:return i=Date.now(),e.next=5,t.sendTransaction.apply(t,[r].concat(a));case 5:u=e.sent,c="SignatureNotFound",l=4;case 8:return e.next=10,t.getSignatureStatus(u);case 10:if("SignatureNotFound"===(c=e.sent)){e.next=13;break}return e.abrupt("break",20);case 13:return e.next=15,sleep(500);case 15:if(0<--l){e.next=18;break}throw f=(Date.now()-i)/1e3,Error("Transaction '"+u+"' was not confirmed in "+f.toFixed(2)+" seconds ("+c+")");case 18:e.next=8;break;case 20:if("Confirmed"!==c){e.next=22;break}return e.abrupt("break",28);case 22:if("AccountInUse"===c&&--s>0){e.next=24;break}throw Error("Transaction "+u+" failed ("+c+")");case 24:return e.next=26,sleep(100*Math.random());case 26:e.next=2;break;case 28:return assert(void 0!==u),e.abrupt("return",u);case 30:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}(),Loader=function(){function e(t,r){_classCallCheck(this,e),_Object$assign(this,{connection:t,programId:r})}return _createClass(e,null,[{key:"chunkSize",get:function(){return 256}}]),_createClass(e,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n){var a,o,s,u,i,c,l,f;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:a=struct([u32("instruction"),u32("offset"),u32("bytesLength"),u32("bytesLengthPadding"),seq(u8("byte"),offset(u32(),-8),"bytes")]),o=e.chunkSize,s=0,u=n,i=[];case 5:if(u.length<=0){t.next=19;break}if(c=u.slice(0,o),l=Buffer.alloc(o+16),a.encode({instruction:0,offset:s,bytes:c},l),f=(new Transaction).add({keys:[r.publicKey],programId:this.programId,userdata:l}),i.push(sendAndConfirmTransaction(this.connection,f,r)),8!==i.length){t.next=15;break}return t.next=14,_Promise.all(i);case 14:i=[];case 15:s+=o,u=u.slice(o),t.next=5;break;case 19:return t.next=21,_Promise.all(i);case 21:case"end":return t.stop()}},t,this)}));return function(e,r){return t.apply(this,arguments)}}()},{key:"finalize",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=struct([u32("instruction")]),n=Buffer.alloc(r.span),r.encode({instruction:1},n),(a=new Transaction).add({keys:[t.publicKey],programId:this.programId,userdata:n}),a.add(SystemProgram.spawn(t.publicKey)),e.next=8,sendAndConfirmTransaction(this.connection,a,t);case 8:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()}]),e}(),BpfLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var o,s,u;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o=new Account,s=SystemProgram.createAccount(n.publicKey,o.publicKey,1+Math.ceil(a.length/Loader.chunkSize)+1,a.length,e.programId),t.next=4,sendAndConfirmTransaction(r,s,n);case 4:return u=new Loader(r,e.programId),t.next=7,u.load(o,a);case 7:return t.next=9,u.finalize(o);case 9:return t.abrupt("return",o.publicKey);case 10:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x8000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function serializePayment(e){var t=e.to.toBuffer(),r=Buffer.alloc(8+t.length);return r.writeUInt32LE(e.amount,0),t.copy(r,8),r}function serializeDate(e){var t=Buffer.alloc(28);return t.writeUInt32LE(20,0),t.write(function(e){function t(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+t(e.getUTCMonth()+1)+"-"+t(e.getUTCDate())+"T"+t(e.getUTCHours())+":"+t(e.getUTCMinutes())+":"+t(e.getUTCSeconds())+"Z"}(e),8),t}function serializeCondition(e){switch(e.type){case"timestamp":var t=serializeDate(e.when),r=e.from.toBuffer(),n=Buffer.alloc(4+t.length+r.length);return n.writeUInt32LE(0,0),t.copy(n,4),r.copy(n,4+t.length),n;case"signature":var a=struct([u32("condition"),publicKey("from")]),o=e.from.toBuffer(),s=Buffer.alloc(4+o.length);return a.encode({instruction:1,from:o},s),s;default:throw Error("Unknown condition type: "+e.type)}}var BudgetProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"timestampCondition",value:function(e,t){return{type:"timestamp",from:e,when:t}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,t,r,n){var a=Buffer.alloc(1024),o=0;a.writeUInt32LE(0,o),o+=4;for(var s=arguments.length,u=Array(s>4?s-4:0),i=4;s>i;i++)u[i-4]=arguments[i];switch(u.length){case 0:a.writeUInt32LE(0,o),o+=4;var c=serializePayment({amount:n,to:r});return c.copy(a,o),o+=c.length,(new Transaction).add({keys:[e,r],programId:this.programId,userdata:a.slice(0,o)});case 1:a.writeUInt32LE(1,o),o+=4;var l=serializeCondition(u[0]);l.copy(a,o),o+=l.length;var f=serializePayment({amount:n,to:r});return f.copy(a,o),o+=f.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,o)});case 2:a.writeUInt32LE(2,o),o+=4;var p=!0,h=!1,d=void 0;try{for(var g,m=_getIterator(u);!(p=(g=m.next()).done);p=!0){var y=serializeCondition(g.value);y.copy(a,o),o+=y.length;var b=serializePayment({amount:n,to:r});b.copy(a,o),o+=b.length}}catch(e){h=!0,d=e}finally{try{!p&&m.return&&m.return()}finally{if(h)throw d}}return(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a.slice(0,o)});default:throw Error("A maximum of two conditions are support: "+u.length+" provided")}}},{key:"payOnBoth",value:function(e,t,r,n,a,o){var s=Buffer.alloc(1024),u=0;s.writeUInt32LE(0,u),s.writeUInt32LE(3,u+=4),u+=4;for(var i=[a,o],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(s,u),u+=l.length}var f=serializePayment({amount:n,to:r});return f.copy(s,u),u+=f.length,(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:s.slice(0,u)})}},{key:"applyTimestamp",value:function(e,t,r,n){var a=serializeDate(n),o=Buffer.alloc(4+a.length);return o.writeUInt32LE(1,0),a.copy(o,4),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:o})}},{key:"applySignature",value:function(e,t,r){var n=struct([u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),(new Transaction).add({keys:[e,t,r],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x8100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}(),NativeLoader=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"load",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a){var o,s,u,i;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return o=[].concat(_toConsumableArray(Buffer.from(a))),s=new Account,u=SystemProgram.createAccount(n.publicKey,s.publicKey,3,o.length+1,e.programId),t.next=5,sendAndConfirmTransaction(r,u,n);case 5:return i=new Loader(r,e.programId),t.next=8,i.load(s,o);case 8:return t.next=10,i.finalize(s);case 10:return t.abrupt("return",s.publicKey);case 11:case"end":return t.stop()}},t,this)}));return function(e,r,n){return t.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}}]),e}(),TokenAmount=function(e){function t(){return _classCallCheck(this,t),_possibleConstructorReturn(this,(t.__proto__||_Object$getPrototypeOf(t)).apply(this,arguments))}return _inherits(t,BN),_createClass(t,[{key:"toBuffer",value:function(){var e=_get(t.prototype.__proto__||_Object$getPrototypeOf(t.prototype),"toArray",this).call(this).reverse(),r=Buffer.from(e);if(8===r.length)return r;assert(8>r.length,"TokenAmount too large");var n=Buffer.alloc(8);return r.copy(n),n}}],[{key:"fromBuffer",value:function(e){return assert(8===e.length,"Invalid buffer length: "+e.length),new BN([].concat(_toConsumableArray(e)).reverse().map(function(e){return("00"+e.toString(16)).slice(-2)}).join(""),16)}}]),t}(),TokenInfoLayout=struct([uint64("supply"),u8("decimals"),rustString("name"),rustString("symbol")]),TokenAccountInfoLayout=struct([publicKey("token"),publicKey("owner"),uint64("amount"),u8("sourceOption"),publicKey("source"),uint64("originalAmount")]),SYSTEM_TOKEN_PROGRAM_ID=new PublicKey("0x8300000000000000000000000000000000000000000000000000000000000000"),Token=function(){function e(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:SYSTEM_TOKEN_PROGRAM_ID;_classCallCheck(this,e),_Object$assign(this,{connection:t,token:r,programId:n})}return _createClass(e,[{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a,o,s,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=new Account,n=void 0,a=struct([u32("instruction")]),o=Buffer.alloc(a.span),a.encode({instruction:1},o),n=SystemProgram.createAccount(t.publicKey,r.publicKey,1,1+TokenAccountInfoLayout.span,this.programId),e.next=8,sendAndConfirmTransaction(this.connection,n,t);case 8:return s=[r.publicKey,t.publicKey,this.token],u&&s.push(u),(n=(new Transaction).add({keys:s,programId:this.programId,userdata:o})).fee=0,e.next=14,sendAndConfirmTransaction(this.connection,n,r);case 14:return e.abrupt("return",r.publicKey);case 15:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var t,r,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token owner: "+_JSON$stringify(t.owner));case 5:if(1===(r=Buffer.from(t.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token userdata");case 8:return(n=TokenInfoLayout.decode(r,1)).supply=TokenAmount.fromBuffer(n.supply),e.abrupt("return",n);case 11:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t){var r,n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.connection.getAccountInfo(t);case 2:if((r=e.sent).owner.equals(this.programId)){e.next=5;break}throw Error("Invalid token account owner");case 5:if(2===(n=Buffer.from(r.userdata)).readUInt8(0)){e.next=8;break}throw Error("Invalid token account userdata");case 8:if((a=TokenAccountInfoLayout.decode(n,1)).token=new PublicKey(a.token),a.owner=new PublicKey(a.owner),a.amount=TokenAmount.fromBuffer(a.amount),0===a.sourceOption?(a.source=null,a.originalAmount=new TokenAmount):(a.source=new PublicKey(a.source),a.originalAmount=TokenAmount.fromBuffer(a.originalAmount)),a.token.equals(this.token)){e.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(a.token)+" !== "+_JSON$stringify(this.token));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}()},{key:"transfer",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=sendAndConfirmTransaction,e.t1=this.connection,e.t2=new Transaction,e.next=5,this.transferInstruction(t.publicKey,r,n,a);case 5:return e.t3=e.sent,e.t4=e.t2.add.call(e.t2,e.t3),e.t5=t,e.next=10,(0,e.t0)(e.t1,e.t4,e.t5);case 10:return e.abrupt("return",e.sent);case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approve",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.approveInstruction(t.publicKey,r,n,a)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"revoke",value:function(e,t,r){return this.approve(e,t,r,0)}},{key:"setOwner",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n){return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,sendAndConfirmTransaction(this.connection,(new Transaction).add(this.setOwnerInstruction(t.publicKey,r,n)),t);case 2:case"end":return e.stop()}},e,this)}));return function(t,r,n){return e.apply(this,arguments)}}()},{key:"transferInstruction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r,n,a){var o,s,u,i;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.accountInfo(r);case 2:if(t.equals((o=e.sent).owner)){e.next=5;break}throw Error("Account owner mismatch");case 5:return s=struct([u32("instruction"),uint64("amount")]),u=Buffer.alloc(s.span),s.encode({instruction:2,amount:new TokenAmount(a).toBuffer()},u),i=[t,r,n],o.source&&i.push(o.source),e.abrupt("return",new TransactionInstruction({keys:i,programId:this.programId,userdata:u}));case 11:case"end":return e.stop()}},e,this)}));return function(t,r,n,a){return e.apply(this,arguments)}}()},{key:"approveInstruction",value:function(e,t,r,n){var a=struct([u32("instruction"),uint64("amount")]),o=Buffer.alloc(a.span);return a.encode({instruction:3,amount:new TokenAmount(n).toBuffer()},o),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:o})}},{key:"revokeInstruction",value:function(e,t,r){return this.approveInstruction(e,t,r,0)}},{key:"setOwnerInstruction",value:function(e,t,r){var n=struct([u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:4},a),new TransactionInstruction({keys:[e,t,r],programId:this.programId,userdata:a})}}],[{key:"createNewToken",value:function(){var t=_asyncToGenerator(_regeneratorRuntime.mark(function t(r,n,a,o,s,u){var i,c,l,f,p,h,d,g=arguments.length>6&&void 0!==arguments[6]?arguments[6]:SYSTEM_TOKEN_PROGRAM_ID;return _regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return i=new Account,c=new e(r,i.publicKey,g),t.next=4,c.newAccount(n,null);case 4:return l=t.sent,f=void 0,p=struct([u32("instruction"),uint64("supply"),u8("decimals"),rustString("name"),rustString("symbol")]),h=Buffer.alloc(1024),d=p.encode({instruction:0,supply:a.toBuffer(),decimals:u,name:o,symbol:s},h),h=h.slice(0,d),f=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+h.length,g),t.next=13,sendAndConfirmTransaction(r,f,n);case 13:return(f=(new Transaction).add({keys:[i.publicKey,l],programId:g,userdata:h})).fee=0,t.next=17,sendAndConfirmTransaction(r,f,i);case 17:return t.abrupt("return",[c,l]);case 18:case"end":return t.stop()}},t,this)}));return function(e,r,n,a,o,s){return t.apply(this,arguments)}}()}]),e}(),sendAndConfirmRawTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(t,r){var n,a,o,s,u;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return n=Date.now(),e.next=3,t.sendRawTransaction(r);case 3:a=e.sent,o="",s=4;case 6:return e.next=8,t.getSignatureStatus(a);case 8:if("SignatureNotFound"===(o=e.sent)){e.next=11;break}return e.abrupt("break",18);case 11:return e.next=13,sleep(500);case 13:if(0<--s){e.next=16;break}throw u=(Date.now()-n)/1e3,Error("Raw Transaction '"+a+"' was not confirmed in "+u.toFixed(2)+" seconds ("+o+")");case 16:e.next=6;break;case 18:if("Confirmed"!==o){e.next=20;break}return e.abrupt("return",a);case 20:throw Error("Raw transaction "+a+" failed ("+o+")");case 21:case"end":return e.stop()}},e,this)}));return function(t,r){return e.apply(this,arguments)}}();export{Account,BpfLoader,BudgetProgram,Connection,Loader,NativeLoader,PublicKey,SystemProgram,Token,TokenAmount,Transaction,TransactionInstruction,sendAndConfirmTransaction,sendAndConfirmRawTransaction};

@@ -101,3 +101,3 @@ /**

declare type TransactionInstructionCtorFields = {|
keys?: Array<PublicKey>,
keys: ?Array<PublicKey>,
programId?: PublicKey,

@@ -108,3 +108,2 @@ userdata?: Buffer,

declare export class TransactionInstruction {
fee: number;
keys: Array<PublicKey>;

@@ -127,9 +126,11 @@ programId: PublicKey;

declare export class Transaction {
signatures: Array<SignaturePubkeyPair>;
signature: ?Buffer;
instructions: Array<TransactionInstruction>;
lastId: ?TransactionId;
fee: number;
instructions: Array<TransactionInstruction>;
constructor(opts?: TransactionCtorFields): Transaction;
add(
...items: Array<TransactionInstruction | TransactionInstructionCtorFields>
...items: Array<Transaction | TransactionInstructionCtorFields>
): Transaction;

@@ -261,2 +262,8 @@ sign(...signers: Array<Account>): void;

): Promise<TransactionSignature>;
// === src/util/send-and-confirm-raw-transaction.js ===
declare export function sendAndConfirmRawTransaction(
connection: Connection,
wireTransaction: Buffer,
): Promise<TransactionSignature>;
}
{
"name": "@solana/web3.js",
"version": "0.13.1",
"version": "0.13.2",
"description": "Solana Javascript API",

@@ -101,3 +101,3 @@ "keywords": [

"eslint-plugin-prettier": "^3.0.0",
"flow-bin": "0.86.0",
"flow-bin": "0.87.0",
"flow-typed": "2.5.1",

@@ -104,0 +104,0 @@ "fs-file-tree": "1.0.6",

@@ -145,3 +145,3 @@ // @flow

*/
const SendTokensRpcResult = jsonRpcResult('string');
const SendTransactionRpcResult = jsonRpcResult('string');

@@ -433,8 +433,8 @@ /**

async sendRawTransaction(
wireTransaction: Buffer,
rawTransaction: Buffer,
): Promise<TransactionSignature> {
const unsafeRes = await this._rpcRequest('sendTransaction', [
[...wireTransaction],
[...rawTransaction],
]);
const res = SendTokensRpcResult(unsafeRes);
const res = SendTransactionRpcResult(unsafeRes);
if (res.error) {

@@ -441,0 +441,0 @@ throw new Error(res.error.message);

@@ -13,1 +13,4 @@ // @flow

export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
export {
sendAndConfirmRawTransaction,
} from './util/send-and-confirm-raw-transaction';
// @flow
import invariant from 'assert';
import {Connection} from '../connection';

@@ -16,3 +18,3 @@ import {Transaction} from '../transaction';

...signers: Array<Account>
): Promise<?TransactionSignature> {
): Promise<TransactionSignature> {
let sendRetries = 10;

@@ -56,3 +58,4 @@ let signature;

invariant(signature !== undefined);
return signature;
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc