@solana/web3.js
Advanced tools
Comparing version 0.3.1 to 0.4.0
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 263, | ||
"end": 302, | ||
"loc": { | ||
@@ -11,3 +11,3 @@ "start": { | ||
"end": { | ||
"line": 8, | ||
"line": 9, | ||
"column": 0 | ||
@@ -19,3 +19,3 @@ } | ||
"start": 0, | ||
"end": 263, | ||
"end": 302, | ||
"loc": { | ||
@@ -27,3 +27,3 @@ "start": { | ||
"end": { | ||
"line": 8, | ||
"line": 9, | ||
"column": 0 | ||
@@ -581,2 +581,90 @@ } | ||
"exportKind": "value" | ||
}, | ||
{ | ||
"type": "ExportNamedDeclaration", | ||
"start": 263, | ||
"end": 301, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 38 | ||
} | ||
}, | ||
"declaration": null, | ||
"specifiers": [ | ||
{ | ||
"type": "ExportSpecifier", | ||
"start": 271, | ||
"end": 276, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 13 | ||
} | ||
}, | ||
"local": { | ||
"type": "Identifier", | ||
"start": 271, | ||
"end": 276, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 13 | ||
}, | ||
"identifierName": "Token" | ||
}, | ||
"name": "Token" | ||
}, | ||
"exported": { | ||
"type": "Identifier", | ||
"start": 271, | ||
"end": 276, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 13 | ||
}, | ||
"identifierName": "Token" | ||
}, | ||
"name": "Token" | ||
} | ||
} | ||
], | ||
"source": { | ||
"type": "StringLiteral", | ||
"start": 283, | ||
"end": 300, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 37 | ||
} | ||
}, | ||
"extra": { | ||
"rawValue": "./token-program", | ||
"raw": "'./token-program'" | ||
}, | ||
"value": "./token-program" | ||
}, | ||
"exportKind": "value" | ||
} | ||
@@ -1721,3 +1809,4 @@ ], | ||
"type": { | ||
"label": "eof", | ||
"label": "export", | ||
"keyword": "export", | ||
"beforeExpr": false, | ||
@@ -1733,4 +1822,5 @@ "startsExpr": false, | ||
}, | ||
"value": "export", | ||
"start": 263, | ||
"end": 263, | ||
"end": 269, | ||
"loc": { | ||
@@ -1743,3 +1833,184 @@ "start": { | ||
"line": 8, | ||
"column": 6 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "{", | ||
"beforeExpr": true, | ||
"startsExpr": true, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null | ||
}, | ||
"start": 270, | ||
"end": 271, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 7 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 8 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "name", | ||
"beforeExpr": false, | ||
"startsExpr": true, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null | ||
}, | ||
"value": "Token", | ||
"start": 271, | ||
"end": 276, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 8 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 13 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "}", | ||
"beforeExpr": false, | ||
"startsExpr": false, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null | ||
}, | ||
"start": 276, | ||
"end": 277, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 13 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 14 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "name", | ||
"beforeExpr": false, | ||
"startsExpr": true, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null | ||
}, | ||
"value": "from", | ||
"start": 278, | ||
"end": 282, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 15 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 19 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "string", | ||
"beforeExpr": false, | ||
"startsExpr": true, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null, | ||
"updateContext": null | ||
}, | ||
"value": "./token-program", | ||
"start": 283, | ||
"end": 300, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 37 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": ";", | ||
"beforeExpr": true, | ||
"startsExpr": false, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null, | ||
"updateContext": null | ||
}, | ||
"start": 300, | ||
"end": 301, | ||
"loc": { | ||
"start": { | ||
"line": 8, | ||
"column": 37 | ||
}, | ||
"end": { | ||
"line": 8, | ||
"column": 38 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": { | ||
"label": "eof", | ||
"beforeExpr": false, | ||
"startsExpr": false, | ||
"rightAssociative": false, | ||
"isLoop": false, | ||
"isAssign": false, | ||
"prefix": false, | ||
"postfix": false, | ||
"binop": null, | ||
"updateContext": null | ||
}, | ||
"start": 302, | ||
"end": 302, | ||
"loc": { | ||
"start": { | ||
"line": 9, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 9, | ||
"column": 0 | ||
} | ||
@@ -1746,0 +2017,0 @@ } |
{ | ||
"coverage": "100%", | ||
"expectCount": 64, | ||
"actualCount": 64, | ||
"expectCount": 87, | ||
"actualCount": 87, | ||
"files": { | ||
@@ -21,2 +21,7 @@ "src/account.js": { | ||
}, | ||
"src/layout.js": { | ||
"expectCount": 2, | ||
"actualCount": 2, | ||
"undocumentLines": [] | ||
}, | ||
"src/publickey.js": { | ||
@@ -32,2 +37,7 @@ "expectCount": 8, | ||
}, | ||
"src/token-program.js": { | ||
"expectCount": 19, | ||
"actualCount": 19, | ||
"undocumentLines": [] | ||
}, | ||
"src/transaction.js": { | ||
@@ -37,4 +47,14 @@ "expectCount": 11, | ||
"undocumentLines": [] | ||
}, | ||
"src/util/send-and-confirm-transaction.js": { | ||
"expectCount": 1, | ||
"actualCount": 1, | ||
"undocumentLines": [] | ||
}, | ||
"src/util/sleep.js": { | ||
"expectCount": 1, | ||
"actualCount": 1, | ||
"undocumentLines": [] | ||
} | ||
} | ||
} |
@@ -33,2 +33,14 @@ window.esdocSearchIndex = [ | ||
[ | ||
"@solana/web3~token", | ||
"class/src/token-program.js~Token.html", | ||
"<span>Token</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"class" | ||
], | ||
[ | ||
"@solana/web3~tokenamount", | ||
"class/src/token-program.js~TokenAmount.html", | ||
"<span>TokenAmount</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"class" | ||
], | ||
[ | ||
"@solana/web3~transaction", | ||
@@ -40,2 +52,26 @@ "class/src/transaction.js~Transaction.html", | ||
[ | ||
"@solana/web3~publickey", | ||
"function/index.html#static-function-publicKey", | ||
"<span>publicKey</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"function" | ||
], | ||
[ | ||
"@solana/web3~sendandconfirmtransaction", | ||
"function/index.html#static-function-sendAndConfirmTransaction", | ||
"<span>sendAndConfirmTransaction</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"function" | ||
], | ||
[ | ||
"@solana/web3~sleep", | ||
"function/index.html#static-function-sleep", | ||
"<span>sleep</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"function" | ||
], | ||
[ | ||
"@solana/web3~uint64", | ||
"function/index.html#static-function-uint64", | ||
"<span>uint64</span> <span class=\"search-result-import-path\">@solana/web3</span>", | ||
"function" | ||
], | ||
[ | ||
"src/.external-ecmascript.js~array", | ||
@@ -509,2 +545,8 @@ "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array", | ||
[ | ||
"src/layout.js", | ||
"file/src/layout.js.html", | ||
"src/layout.js", | ||
"file" | ||
], | ||
[ | ||
"src/publickey.js", | ||
@@ -582,2 +624,80 @@ "file/src/publickey.js.html", | ||
[ | ||
"src/token-program.js", | ||
"file/src/token-program.js.html", | ||
"src/token-program.js", | ||
"file" | ||
], | ||
[ | ||
"src/token-program.js~token#accountinfo", | ||
"class/src/token-program.js~Token.html#instance-method-accountInfo", | ||
"src/token-program.js~Token#accountInfo", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#approve", | ||
"class/src/token-program.js~Token.html#instance-method-approve", | ||
"src/token-program.js~Token#approve", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#constructor", | ||
"class/src/token-program.js~Token.html#instance-constructor-constructor", | ||
"src/token-program.js~Token#constructor", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#newaccount", | ||
"class/src/token-program.js~Token.html#instance-method-newAccount", | ||
"src/token-program.js~Token#newAccount", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#revoke", | ||
"class/src/token-program.js~Token.html#instance-method-revoke", | ||
"src/token-program.js~Token#revoke", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#token", | ||
"class/src/token-program.js~Token.html#instance-member-token", | ||
"src/token-program.js~Token#token", | ||
"member" | ||
], | ||
[ | ||
"src/token-program.js~token#tokeninfo", | ||
"class/src/token-program.js~Token.html#instance-method-tokenInfo", | ||
"src/token-program.js~Token#tokenInfo", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token#transfer", | ||
"class/src/token-program.js~Token.html#instance-method-transfer", | ||
"src/token-program.js~Token#transfer", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token.createnewtoken", | ||
"class/src/token-program.js~Token.html#static-method-createNewToken", | ||
"src/token-program.js~Token.createNewToken", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~token.programid", | ||
"class/src/token-program.js~Token.html#static-get-programId", | ||
"src/token-program.js~Token.programId", | ||
"member" | ||
], | ||
[ | ||
"src/token-program.js~tokenamount#tobuffer", | ||
"class/src/token-program.js~TokenAmount.html#instance-method-toBuffer", | ||
"src/token-program.js~TokenAmount#toBuffer", | ||
"method" | ||
], | ||
[ | ||
"src/token-program.js~tokenamount.frombuffer", | ||
"class/src/token-program.js~TokenAmount.html#static-method-fromBuffer", | ||
"src/token-program.js~TokenAmount.fromBuffer", | ||
"method" | ||
], | ||
[ | ||
"src/transaction.js", | ||
@@ -659,3 +779,15 @@ "file/src/transaction.js.html", | ||
"typedef" | ||
], | ||
[ | ||
"src/util/send-and-confirm-transaction.js", | ||
"file/src/util/send-and-confirm-transaction.js.html", | ||
"src/util/send-and-confirm-transaction.js", | ||
"file" | ||
], | ||
[ | ||
"src/util/sleep.js", | ||
"file/src/util/sleep.js.html", | ||
"src/util/sleep.js", | ||
"file" | ||
] | ||
] |
@@ -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 _classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),BN=_interopDefault(require("bn.js")),bs58=_interopDefault(require("bs58")),nacl=_interopDefault(require("tweetnacl")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),assert=_interopDefault(require("assert")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(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 r=Buffer.alloc(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):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}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var u,o=_getIterator(this.keys);!(n=(u=o.next()).done);n=!0){u.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&o.return&&o.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),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,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var u=arguments.length,o=Array(u>4?u-4:0),i=4;u>i;i++)o[i-4]=arguments[i];switch(o.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(o[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var y,m=_getIterator(o);!(p=(y=m.next()).done);p=!0){var d=serializeCondition(y.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&m.return&&m.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+o.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var u=Buffer.alloc(1024),o=0;u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(3,o+=8),o+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(u,o),o+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(u,o),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:u.slice(0,o+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,u;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return u=r.sent,r.next=7,u.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:r,id:"string",error:"any"}),superstruct.struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(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(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(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(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(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(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);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(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var u=Buffer.alloc(53),o=0;u.writeUInt32LE(0,o),u.writeUInt32LE(n,o+=4),u.writeUInt32LE(a,o+=8),o+=8;var i=s.toBuffer();return i.copy(u,o),assert(u.length>=(o+=i.length)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:u})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();exports.Account=Account,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Transaction=Transaction; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var _classCallCheck=_interopDefault(require("babel-runtime/helpers/classCallCheck")),_createClass=_interopDefault(require("babel-runtime/helpers/createClass")),BN=_interopDefault(require("bn.js")),bs58=_interopDefault(require("bs58")),nacl=_interopDefault(require("tweetnacl")),BufferLayout=require("buffer-layout"),_Object$assign=_interopDefault(require("babel-runtime/core-js/object/assign")),assert=_interopDefault(require("assert")),_getIterator=_interopDefault(require("babel-runtime/core-js/get-iterator")),_toConsumableArray=_interopDefault(require("babel-runtime/helpers/toConsumableArray")),_Promise=_interopDefault(require("babel-runtime/core-js/promise")),_regeneratorRuntime=_interopDefault(require("babel-runtime/regenerator")),_asyncToGenerator=_interopDefault(require("babel-runtime/helpers/asyncToGenerator")),fetch=_interopDefault(require("node-fetch")),jayson=_interopDefault(require("jayson/lib/client/browser")),superstruct=require("superstruct"),_JSON$stringify=_interopDefault(require("babel-runtime/core-js/json/stringify")),_Object$getPrototypeOf=_interopDefault(require("babel-runtime/core-js/object/get-prototype-of")),_possibleConstructorReturn=_interopDefault(require("babel-runtime/helpers/possibleConstructorReturn")),_get=_interopDefault(require("babel-runtime/helpers/get")),_inherits=_interopDefault(require("babel-runtime/helpers/inherits")),PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(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 r=Buffer.alloc(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):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")},Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId,r=this.keys,t=this.programId,n=this.userdata;if(!e)throw Error("Transaction lastId required");var a=BufferLayout.struct([BufferLayout.ns64("keysLength"),BufferLayout.seq(publicKey("key"),r.length,"keys"),publicKey("programId"),publicKey("lastId"),BufferLayout.ns64("fee"),BufferLayout.ns64("userdataLength"),BufferLayout.blob(n.length,"userdata")]),u=Buffer.alloc(2048),o=a.encode({keysLength:r.length,keys:r.map(function(e){return e.toBuffer()}),programId:t.toBuffer(),lastId:Buffer.from(bs58.decode(e)),fee:0,userdataLength:n.length,userdata:n},u);return 0===n.length&&(o-=8),u=u.slice(0,o)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.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,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),u=0;a.writeUInt32LE(0,u),a.writeUInt32LE(n,u+=4),u+=8;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:t});return c.copy(a,u),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,u+=c.length)});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:t});return l.copy(a,u),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,u+=l.length)});case 2:a.writeUInt32LE(2,u),u+=4;var p=!0,y=!1,g=void 0;try{for(var m,h=_getIterator(s);!(p=(m=h.next()).done);p=!0){var d=serializeCondition(m.value);d.copy(a,u),u+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,u),u+=v.length}}catch(e){y=!0,g=e}finally{try{!p&&h.return&&h.return()}finally{if(y)throw g}}return new Transaction({fee:0,keys:[e,r,t],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,r,t,n,a,u){var o=Buffer.alloc(1024),s=0;o.writeUInt32LE(0,s),o.writeUInt32LE(n,s+=4),o.writeUInt32LE(3,s+=8),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:t});return l.copy(o,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:o.slice(0,s+=l.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),u=Buffer.alloc(4+a.length);return u.writeUInt32LE(1,0),a.copy(u,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:u})}},{key:"applySignature",value:function(e,r,t){var n=BufferLayout.struct([BufferLayout.u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var u,o;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return u={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,u);case 4:return o=r.sent,r.next=7,o.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=superstruct.struct({jsonrpc:superstruct.struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=superstruct.struct.literal("2.0");return superstruct.struct.union([superstruct.struct({jsonrpc:r,id:"string",error:"any"}),superstruct.struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(superstruct.struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(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(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(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(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(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(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);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(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,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({fee:0,keys:[r,t],programId:e.programId,userdata:s})}},{key:"move",value:function(r,t,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({fee:0,keys:[r,t],programId:e.programId,userdata:u})}},{key:"assign",value:function(r,t){var n=BufferLayout.struct([BufferLayout.u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:t.toBuffer()},a),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(r){return setTimeout(r,e)})}var sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t,n){var a,u,o,s=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.sendTransaction(t,n);case 2:a=e.sent,u=4;case 4:return e.next=6,r.getSignatureStatus(a);case 6:if("Confirmed"!=(o=e.sent)){e.next=9;break}return e.abrupt("return");case 9:if(!s||"ProgramRuntimeError"!=o){e.next=11;break}return e.abrupt("return");case 11:return e.next=13,sleep(500);case 13:if(0<=--u){e.next=15;break}throw Error("Transaction '"+a+"' was not confirmed ("+o+")");case 15:e.next=4;break;case 17:case"end":return e.stop()}},e,this)}));return function(r,t,n){return e.apply(this,arguments)}}(),RustStringLayout=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"rustString",r=BufferLayout.u8("length"),t=BufferLayout.struct([r,BufferLayout.seq(BufferLayout.u8(),7,"u8 into u64 padding"),BufferLayout.blob(BufferLayout.offset(r,-8),"chars")],e),n=t.decode.bind(t),a=t.encode.bind(t);return t.decode=function(e,r){return n(e,r).chars.toString("utf8")},t.encode=function(e,r,t){var n={chars:Buffer.from(e,"utf8")};return a(n,r,t)},t},TokenAmount=function(e){function r(){return _classCallCheck(this,r),_possibleConstructorReturn(this,(r.__proto__||_Object$getPrototypeOf(r)).apply(this,arguments))}return _inherits(r,BN),_createClass(r,[{key:"toBuffer",value:function(){var e=_get(r.prototype.__proto__||_Object$getPrototypeOf(r.prototype),"toArray",this).call(this).reverse(),t=Buffer.from(e);if(8===t.length)return t;assert(8>t.length,"TokenAmount too large");var n=Buffer.alloc(8);return t.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)}}]),r}(),TokenInfoLayout=BufferLayout.struct([uint64("supply"),BufferLayout.u8("decimals"),new RustStringLayout("name"),new RustStringLayout("symbol")]),TokenAccountInfoLayout=BufferLayout.struct([publicKey("token"),publicKey("owner"),uint64("amount"),BufferLayout.u8("sourceOption"),publicKey("source")]),Token$$1=function(){function e(r,t){_classCallCheck(this,e),_Object$assign(this,{connection:r,token:t})}return _createClass(e,[{key:"_newAccount",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n){var a,u,o,s,i;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return a=new Account,u=void 0,o=BufferLayout.struct([BufferLayout.u32("instruction")]),s=Buffer.alloc(o.span),o.encode({instruction:1},s),u=SystemProgram.createAccount(t.publicKey,a.publicKey,1,1+TokenAccountInfoLayout.span,e.programId),r.next=8,sendAndConfirmTransaction(this.connection,t,u);case 8:return i=[a.publicKey,t.publicKey,this.token],n&&i.push(n),u=new Transaction({fee:0,keys:i,programId:e.programId,userdata:s}),r.next=13,sendAndConfirmTransaction(this.connection,a,u);case 13:return r.abrupt("return",a);case 14:case"end":return r.stop()}},r,this)}));return function(e,t){return r.apply(this,arguments)}}()},{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._newAccount(r,t);case 2:return e.abrupt("return",e.sent.publicKey);case 3:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(){var t,n,a;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=r.sent).programId.equals(e.programId)){r.next=5;break}throw Error("Invalid token programId: "+_JSON$stringify(t.programId));case 5:if(1===(n=Buffer.from(t.userdata)).readUInt8(0)){r.next=8;break}throw Error("Invalid token userdata");case 8:return(a=TokenInfoLayout.decode(n,1)).supply=TokenAmount.fromBuffer(a.supply),r.abrupt("return",a);case 11:case"end":return r.stop()}},r,this)}));return function(){return r.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t){var n,a,u;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.connection.getAccountInfo(t);case 2:if((n=r.sent).programId.equals(e.programId)){r.next=5;break}throw Error("Invalid token account programId");case 5:if(2===(a=Buffer.from(n.userdata)).readUInt8(0)){r.next=8;break}throw Error("Invalid token account userdata");case 8:if((u=TokenAccountInfoLayout.decode(a,1)).token=new PublicKey(u.token),u.owner=new PublicKey(u.owner),u.amount=TokenAmount.fromBuffer(u.amount),u.source=0===u.sourceOption?null:new PublicKey(u.source),u.token.equals(this.token)){r.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(u.token)+" !== "+_JSON$stringify(this.token));case 15:return r.abrupt("return",u);case 16:case"end":return r.stop()}},r,this)}));return function(e){return r.apply(this,arguments)}}()},{key:"transfer",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,u){var o,s,i,c,f;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.accountInfo(n);case 2:if(t.publicKey.equals((o=r.sent).owner)){r.next=5;break}throw Error("Account owner mismatch");case 5:return s=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),i=Buffer.alloc(s.span),s.encode({instruction:2,amount:new TokenAmount(u).toBuffer()},i),c=[t.publicKey,n,a],o.source&&c.push(o.source),f=new Transaction({fee:0,keys:c,programId:e.programId,userdata:i}),r.next=13,sendAndConfirmTransaction(this.connection,t,f);case 13:case"end":return r.stop()}},r,this)}));return function(e,t,n,a){return r.apply(this,arguments)}}()},{key:"approve",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,u){var o,s,i;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return o=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("amount")]),s=Buffer.alloc(o.span),o.encode({instruction:3,amount:new TokenAmount(u).toBuffer()},s),i=new Transaction({fee:0,keys:[t.publicKey,n,a],programId:e.programId,userdata:s}),r.next=6,sendAndConfirmTransaction(this.connection,t,i);case 6:case"end":return r.stop()}},r,this)}));return function(e,t,n,a){return r.apply(this,arguments)}}()},{key:"revoke",value:function(e,r,t){return this.approve(e,r,t,0)}}],[{key:"createNewToken",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,u,o,s){var i,c,f,l,p,y,g;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return i=new Account,c=new e(t,i.publicKey),r.next=4,c._newAccount(n,null);case 4:return f=r.sent.publicKey,l=void 0,p=BufferLayout.struct([BufferLayout.u32("instruction"),uint64("supply"),BufferLayout.u8("decimals"),new RustStringLayout("name"),new RustStringLayout("symbol")]),y=Buffer.alloc(1024),g=p.encode({instruction:0,supply:a.toBuffer(),decimals:s,name:u,symbol:o},y),y=y.slice(0,g),l=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+y.length,e.programId),r.next=13,sendAndConfirmTransaction(t,n,l);case 13:return l=new Transaction({fee:0,keys:[i.publicKey,f],programId:e.programId,userdata:y}),r.next=16,sendAndConfirmTransaction(t,i,l);case 16:return r.abrupt("return",[c,f]);case 17:case"end":return r.stop()}},r,this)}));return function(e,t,n,a,u,o){return r.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x500000000000000000000000000000000000000000000000000000000000000")}}]),e}();exports.Account=Account,exports.BudgetProgram=BudgetProgram,exports.Connection=Connection,exports.PublicKey=PublicKey,exports.SystemProgram=SystemProgram,exports.Transaction=Transaction,exports.Token=Token$$1; |
@@ -1,1 +0,1 @@ | ||
import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import BN from"bn.js";import bs58 from"bs58";import nacl from"tweetnacl";import _getIterator from"babel-runtime/core-js/get-iterator";import _Object$assign from"babel-runtime/core-js/object/assign";import assert from"assert";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Promise from"babel-runtime/core-js/promise";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct}from"superstruct";var PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(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 r=Buffer.alloc(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):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}(),Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId;if(!e)throw Error("Transaction lastId required");var r=Buffer.alloc(2048),t=0;r.writeUInt32LE(this.keys.length,t),t+=8;var n=!0,a=!1,s=void 0;try{for(var o,u=_getIterator(this.keys);!(n=(o=u.next()).done);n=!0){o.value.toBuffer().copy(r,t),t+=32}}catch(e){a=!0,s=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw s}}this.programId&&this.programId.toBuffer().copy(r,t);t+=32;var i=Buffer.from(bs58.decode(e));return assert(32===i.length),i.copy(r,t),r.writeUInt32LE(this.fee,t+=32),t+=8,this.userdata.length>0&&(r.writeUInt32LE(this.userdata.length,t),this.userdata.copy(r,t+=8),t+=this.userdata.length),r.slice(0,t)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=e.from.toBuffer(),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),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,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),s=0;a.writeUInt32LE(0,s),a.writeUInt32LE(n,s+=4),s+=8;for(var o=arguments.length,u=Array(o>4?o-4:0),i=4;o>i;i++)u[i-4]=arguments[i];switch(u.length){case 0:a.writeUInt32LE(0,s),s+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,s),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,s+=c.length)});case 1:a.writeUInt32LE(1,s),s+=4;var l=serializeCondition(u[0]);l.copy(a,s),s+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s+=f.length)});case 2:a.writeUInt32LE(2,s),s+=4;var p=!0,g=!1,h=void 0;try{for(var m,y=_getIterator(u);!(p=(m=y.next()).done);p=!0){var d=serializeCondition(m.value);d.copy(a,s),s+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,s),s+=v.length}}catch(e){g=!0,h=e}finally{try{!p&&y.return&&y.return()}finally{if(g)throw h}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,s)});default:throw Error("A maximum of two conditions are support: "+u.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,s){var o=Buffer.alloc(1024),u=0;o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(3,u+=8),u+=4;for(var i=[a,s],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(o,u),u+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(o,u),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:o.slice(0,u+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),s=Buffer.alloc(4+a.length);return s.writeUInt32LE(1,0),a.copy(s,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:s})}},{key:"applySignature",value:function(e,r,t){var n=Buffer.alloc(4);return n.writeUInt32LE(2,0),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var s,o;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return s={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,s);case 4:return o=r.sent,r.next=7,o.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=struct({jsonrpc:struct.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=struct.literal("2.0");return struct.union([struct({jsonrpc:r,id:"string",error:"any"}),struct({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(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(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(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(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(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(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);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(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,s){var o=Buffer.alloc(53),u=0;o.writeUInt32LE(0,u),o.writeUInt32LE(n,u+=4),o.writeUInt32LE(a,u+=8),u+=8;var i=s.toBuffer();return i.copy(o,u),assert(o.length>=(u+=i.length)),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:o})}},{key:"move",value:function(r,t,n){var a=Buffer.alloc(12),s=0;return a.writeUInt32LE(2,s),a.writeUInt32LE(n,s+=4),assert((s+=8)===a.length),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:a})}},{key:"assign",value:function(r,t){var n=Buffer.alloc(36),a=0;n.writeUInt32LE(1,a),a+=4;var s=t.toBuffer();return s.copy(n,a),assert((a+=s.length)===n.length),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:n})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();export{Account,BudgetProgram,Connection,PublicKey,SystemProgram,Transaction}; | ||
import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import BN from"bn.js";import bs58 from"bs58";import nacl from"tweetnacl";import{struct,u32,ns64,seq,blob,u8,offset}from"buffer-layout";import _Object$assign from"babel-runtime/core-js/object/assign";import assert from"assert";import _getIterator from"babel-runtime/core-js/get-iterator";import _toConsumableArray from"babel-runtime/helpers/toConsumableArray";import _Promise from"babel-runtime/core-js/promise";import _regeneratorRuntime from"babel-runtime/regenerator";import _asyncToGenerator from"babel-runtime/helpers/asyncToGenerator";import fetch from"node-fetch";import jayson from"jayson/lib/client/browser";import{struct as struct$1}from"superstruct";import _JSON$stringify from"babel-runtime/core-js/json/stringify";import _Object$getPrototypeOf from"babel-runtime/core-js/object/get-prototype-of";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _get from"babel-runtime/helpers/get";import _inherits from"babel-runtime/helpers/inherits";var PublicKey=function(){function e(r){for(_classCallCheck(this,e);;){if("string"==typeof r){if(/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(r)){this._bn=new BN(bs58.decode(r));break}if(r.startsWith("0x")){this._bn=new BN(r.substring(2),16);break}}this._bn=new BN(r);break}if(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 r=Buffer.alloc(32);return e.copy(r),r}},{key:"toString",value:function(){return this.toBase58()}}],[{key:"isPublicKey",value:function(r){return r instanceof e}}]),e}(),Account=function(){function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;_classCallCheck(this,e),this._keypair=r?nacl.sign.keyPair.fromSecretKey(r):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")},Transaction=function(){function e(r){_classCallCheck(this,e),this.keys=[],this.fee=0,this.userdata=Buffer.alloc(0),r&&_Object$assign(this,r)}return _createClass(e,[{key:"_getSignData",value:function(){var e=this.lastId,r=this.keys,t=this.programId,n=this.userdata;if(!e)throw Error("Transaction lastId required");var a=struct([ns64("keysLength"),seq(publicKey("key"),r.length,"keys"),publicKey("programId"),publicKey("lastId"),ns64("fee"),ns64("userdataLength"),blob(n.length,"userdata")]),o=Buffer.alloc(2048),u=a.encode({keysLength:r.length,keys:r.map(function(e){return e.toBuffer()}),programId:t.toBuffer(),lastId:Buffer.from(bs58.decode(e)),fee:0,userdataLength:n.length,userdata:n},o);return 0===n.length&&(u-=8),o=o.slice(0,u)}},{key:"sign",value:function(e){var r=this._getSignData();this.signature=nacl.sign.detached(r,e.secretKey),assert(64===this.signature.length)}},{key:"serialize",value:function(){var e=this.signature;if(!e)throw Error("Transaction has not been signed");var r=this._getSignData(),t=Buffer.alloc(e.length+r.length);return Buffer.from(e).copy(t,0),r.copy(t,e.length),t}}]),e}();function serializePayment(e){var r=e.to.toBuffer(),t=Buffer.alloc(8+r.length);return t.writeUInt32LE(e.amount,0),r.copy(t,8),t}function serializeDate(e){var r=Buffer.alloc(28);return r.writeUInt32LE(20,0),r.write(function(e){function r(e){return 10>e?"0"+e:e}return e.getUTCFullYear()+"-"+r(e.getUTCMonth()+1)+"-"+r(e.getUTCDate())+"T"+r(e.getUTCHours())+":"+r(e.getUTCMinutes())+":"+r(e.getUTCSeconds())+"Z"}(e),8),r}function serializeCondition(e){switch(e.type){case"timestamp":var r=serializeDate(e.when),t=e.from.toBuffer(),n=Buffer.alloc(4+r.length+t.length);return n.writeUInt32LE(0,0),r.copy(n,4),t.copy(n,4+r.length),n;case"signature":var a=struct([u32("condition"),publicKey("from")]),o=e.from.toBuffer(),u=Buffer.alloc(4+o.length);return a.encode({instruction:1,from:o},u),u;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,r){return{type:"timestamp",from:e,when:r}}},{key:"signatureCondition",value:function(e){return{type:"signature",from:e}}},{key:"pay",value:function(e,r,t,n){var a=Buffer.alloc(1024),o=0;a.writeUInt32LE(0,o),a.writeUInt32LE(n,o+=4),o+=8;for(var u=arguments.length,s=Array(u>4?u-4:0),i=4;u>i;i++)s[i-4]=arguments[i];switch(s.length){case 0:a.writeUInt32LE(0,o),o+=4;var c=serializePayment({amount:n,to:t});return c.copy(a,o),new Transaction({fee:0,keys:[e,t],programId:this.programId,userdata:a.slice(0,o+=c.length)});case 1:a.writeUInt32LE(1,o),o+=4;var l=serializeCondition(s[0]);l.copy(a,o),o+=l.length;var f=serializePayment({amount:n,to:t});return f.copy(a,o),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,o+=f.length)});case 2:a.writeUInt32LE(2,o),o+=4;var p=!0,m=!1,g=void 0;try{for(var y,h=_getIterator(s);!(p=(y=h.next()).done);p=!0){var d=serializeCondition(y.value);d.copy(a,o),o+=d.length;var v=serializePayment({amount:n,to:t});v.copy(a,o),o+=v.length}}catch(e){m=!0,g=e}finally{try{!p&&h.return&&h.return()}finally{if(m)throw g}}return new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a.slice(0,o)});default:throw Error("A maximum of two conditions are support: "+s.length+" provided")}}},{key:"payOnBoth",value:function(e,r,t,n,a,o){var u=Buffer.alloc(1024),s=0;u.writeUInt32LE(0,s),u.writeUInt32LE(n,s+=4),u.writeUInt32LE(3,s+=8),s+=4;for(var i=[a,o],c=0;i.length>c;c++){var l=serializeCondition(i[c]);l.copy(u,s),s+=l.length}var f=serializePayment({amount:n,to:t});return f.copy(u,s),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:u.slice(0,s+=f.length)})}},{key:"applyTimestamp",value:function(e,r,t,n){var a=serializeDate(n),o=Buffer.alloc(4+a.length);return o.writeUInt32LE(1,0),a.copy(o,4),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:o})}},{key:"applySignature",value:function(e,r,t){var n=struct([u32("instruction")]),a=Buffer.alloc(n.span);return n.encode({instruction:2},a),new Transaction({fee:0,keys:[e,r,t],programId:this.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x100000000000000000000000000000000000000000000000000000000000000")}},{key:"space",get:function(){return 128}}]),e}();function createRpcRequest(e){var r,t=this,n=jayson((r=_asyncToGenerator(_regeneratorRuntime.mark(function r(n,a){var o,u;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return o={method:"POST",body:n,headers:{"Content-Type":"application/json"}},r.prev=1,r.next=4,fetch(e,o);case 4:return u=r.sent,r.next=7,u.text();case 7:a(null,r.sent),r.next=14;break;case 11:r.prev=11,r.t0=r.catch(1),a(r.t0);case 14:case"end":return r.stop()}},r,t,[[1,11]])})),function(e,t){return r.apply(this,arguments)}));return function(e,r){return new _Promise(function(t,a){n.request(e,r,function(e,r){e?a(e):t(r)})})}}var GetBalanceRpcResult=struct$1({jsonrpc:struct$1.literal("2.0"),id:"string",error:"any?",result:"number?"});function jsonRpcResult(e){var r=struct$1.literal("2.0");return struct$1.union([struct$1({jsonrpc:r,id:"string",error:"any"}),struct$1({jsonrpc:r,id:"string",error:"null?",result:e})])}var GetAccountInfoRpcResult=jsonRpcResult({program_id:"array",tokens:"number",userdata:"array"}),ConfirmTransactionRpcResult=jsonRpcResult("boolean"),GetSignatureStatusRpcResult=jsonRpcResult(struct$1.enum(["Confirmed","SignatureNotFound","ProgramRuntimeError","GenericFailure"])),GetTransactionCountRpcResult=jsonRpcResult("number"),GetLastId=jsonRpcResult("string"),GetFinalityRpcResult=jsonRpcResult("number"),RequestAirdropRpcResult=jsonRpcResult("string"),SendTokensRpcResult=jsonRpcResult("string"),Connection=function(){function e(r){if(_classCallCheck(this,e),"string"!=typeof r)throw Error("Connection endpoint not specified");this._rpcRequest=createRpcRequest(r)}return _createClass(e,[{key:"getBalance",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getBalance",[r.toBase58()]);case 2:if(!(t=GetBalanceRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getAccountInfo",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t,n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getAccountInfo",[r.toBase58()]);case 2:if(!(t=GetAccountInfoRpcResult(e.sent)).error){e.next=6;break}throw Error(t.error.message);case 6:return assert(void 0!==(n=t.result)),e.abrupt("return",{tokens:n.tokens,programId:new PublicKey(n.program_id),userdata:Buffer.from(n.userdata)});case 9:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"confirmTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("confirmTransaction",[r]);case 2:if(!(t=ConfirmTransactionRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getSignatureStatus",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getSignatureStatus",[r]);case 2:if(!(t=GetSignatureStatusRpcResult(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(r){return e.apply(this,arguments)}}()},{key:"getTransactionCount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getTransactionCount",[]);case 2:if(!(r=GetTransactionCountRpcResult(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(){return e.apply(this,arguments)}}()},{key:"getLastId",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getLastId",[]);case 2:if(!(r=GetLastId(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(){return e.apply(this,arguments)}}()},{key:"getFinality",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(){var r;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("getFinality",[]);case 2:if(!(r=GetFinalityRpcResult(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(){return e.apply(this,arguments)}}()},{key:"requestAirdrop",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._rpcRequest("requestAirdrop",[r.toBase58(),t]);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(r,t){return e.apply(this,arguments)}}()},{key:"sendTransaction",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t){var n,a;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getLastId();case 2:return t.lastId=e.sent,t.sign(r),n=t.serialize(),e.next=7,this._rpcRequest("sendTransaction",[[].concat(_toConsumableArray(n))]);case 7:if(!(a=SendTokensRpcResult(e.sent)).error){e.next=11;break}throw Error(a.error.message);case 11:return assert(void 0!==a.result),assert(a.result),e.abrupt("return",a.result);case 14:case"end":return e.stop()}},e,this)}));return function(r,t){return e.apply(this,arguments)}}()}]),e}(),SystemProgram=function(){function e(){_classCallCheck(this,e)}return _createClass(e,null,[{key:"createAccount",value:function(r,t,n,a,o){var u=struct([u32("instruction"),ns64("tokens"),ns64("space"),publicKey("programId")]),s=Buffer.alloc(u.span);return u.encode({instruction:0,tokens:n,space:a,programId:o.toBuffer()},s),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:s})}},{key:"move",value:function(r,t,n){var a=struct([u32("instruction"),ns64("amount")]),o=Buffer.alloc(a.span);return a.encode({instruction:2,amount:n},o),new Transaction({fee:0,keys:[r,t],programId:e.programId,userdata:o})}},{key:"assign",value:function(r,t){var n=struct([u32("instruction"),publicKey("programId")]),a=Buffer.alloc(n.span);return n.encode({instruction:1,programId:t.toBuffer()},a),new Transaction({fee:0,keys:[r],programId:e.programId,userdata:a})}},{key:"programId",get:function(){return new PublicKey("0x000000000000000000000000000000000000000000000000000000000000000")}}]),e}();function sleep(e){return new _Promise(function(r){return setTimeout(r,e)})}var sendAndConfirmTransaction=function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r,t,n){var a,o,u,s=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.sendTransaction(t,n);case 2:a=e.sent,o=4;case 4:return e.next=6,r.getSignatureStatus(a);case 6:if("Confirmed"!=(u=e.sent)){e.next=9;break}return e.abrupt("return");case 9:if(!s||"ProgramRuntimeError"!=u){e.next=11;break}return e.abrupt("return");case 11:return e.next=13,sleep(500);case 13:if(0<=--o){e.next=15;break}throw Error("Transaction '"+a+"' was not confirmed ("+u+")");case 15:e.next=4;break;case 17:case"end":return e.stop()}},e,this)}));return function(r,t,n){return e.apply(this,arguments)}}(),RustStringLayout=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"rustString",r=u8("length"),t=struct([r,seq(u8(),7,"u8 into u64 padding"),blob(offset(r,-8),"chars")],e),n=t.decode.bind(t),a=t.encode.bind(t);return t.decode=function(e,r){return n(e,r).chars.toString("utf8")},t.encode=function(e,r,t){var n={chars:Buffer.from(e,"utf8")};return a(n,r,t)},t},TokenAmount=function(e){function r(){return _classCallCheck(this,r),_possibleConstructorReturn(this,(r.__proto__||_Object$getPrototypeOf(r)).apply(this,arguments))}return _inherits(r,BN),_createClass(r,[{key:"toBuffer",value:function(){var e=_get(r.prototype.__proto__||_Object$getPrototypeOf(r.prototype),"toArray",this).call(this).reverse(),t=Buffer.from(e);if(8===t.length)return t;assert(8>t.length,"TokenAmount too large");var n=Buffer.alloc(8);return t.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)}}]),r}(),TokenInfoLayout=struct([uint64("supply"),u8("decimals"),new RustStringLayout("name"),new RustStringLayout("symbol")]),TokenAccountInfoLayout=struct([publicKey("token"),publicKey("owner"),uint64("amount"),u8("sourceOption"),publicKey("source")]),Token$$1=function(){function e(r,t){_classCallCheck(this,e),_Object$assign(this,{connection:r,token:t})}return _createClass(e,[{key:"_newAccount",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n){var a,o,u,s,i;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return a=new Account,o=void 0,u=struct([u32("instruction")]),s=Buffer.alloc(u.span),u.encode({instruction:1},s),o=SystemProgram.createAccount(t.publicKey,a.publicKey,1,1+TokenAccountInfoLayout.span,e.programId),r.next=8,sendAndConfirmTransaction(this.connection,t,o);case 8:return i=[a.publicKey,t.publicKey,this.token],n&&i.push(n),o=new Transaction({fee:0,keys:i,programId:e.programId,userdata:s}),r.next=13,sendAndConfirmTransaction(this.connection,a,o);case 13:return r.abrupt("return",a);case 14:case"end":return r.stop()}},r,this)}));return function(e,t){return r.apply(this,arguments)}}()},{key:"newAccount",value:function(){var e=_asyncToGenerator(_regeneratorRuntime.mark(function e(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return _regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this._newAccount(r,t);case 2:return e.abrupt("return",e.sent.publicKey);case 3:case"end":return e.stop()}},e,this)}));return function(r){return e.apply(this,arguments)}}()},{key:"tokenInfo",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(){var t,n,a;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.connection.getAccountInfo(this.token);case 2:if((t=r.sent).programId.equals(e.programId)){r.next=5;break}throw Error("Invalid token programId: "+_JSON$stringify(t.programId));case 5:if(1===(n=Buffer.from(t.userdata)).readUInt8(0)){r.next=8;break}throw Error("Invalid token userdata");case 8:return(a=TokenInfoLayout.decode(n,1)).supply=TokenAmount.fromBuffer(a.supply),r.abrupt("return",a);case 11:case"end":return r.stop()}},r,this)}));return function(){return r.apply(this,arguments)}}()},{key:"accountInfo",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t){var n,a,o;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.connection.getAccountInfo(t);case 2:if((n=r.sent).programId.equals(e.programId)){r.next=5;break}throw Error("Invalid token account programId");case 5:if(2===(a=Buffer.from(n.userdata)).readUInt8(0)){r.next=8;break}throw Error("Invalid token account userdata");case 8:if((o=TokenAccountInfoLayout.decode(a,1)).token=new PublicKey(o.token),o.owner=new PublicKey(o.owner),o.amount=TokenAmount.fromBuffer(o.amount),o.source=0===o.sourceOption?null:new PublicKey(o.source),o.token.equals(this.token)){r.next=15;break}throw Error("Invalid token account token: "+_JSON$stringify(o.token)+" !== "+_JSON$stringify(this.token));case 15:return r.abrupt("return",o);case 16:case"end":return r.stop()}},r,this)}));return function(e){return r.apply(this,arguments)}}()},{key:"transfer",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,o){var u,s,i,c,l;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,this.accountInfo(n);case 2:if(t.publicKey.equals((u=r.sent).owner)){r.next=5;break}throw Error("Account owner mismatch");case 5:return s=struct([u32("instruction"),uint64("amount")]),i=Buffer.alloc(s.span),s.encode({instruction:2,amount:new TokenAmount(o).toBuffer()},i),c=[t.publicKey,n,a],u.source&&c.push(u.source),l=new Transaction({fee:0,keys:c,programId:e.programId,userdata:i}),r.next=13,sendAndConfirmTransaction(this.connection,t,l);case 13:case"end":return r.stop()}},r,this)}));return function(e,t,n,a){return r.apply(this,arguments)}}()},{key:"approve",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,o){var u,s,i;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return u=struct([u32("instruction"),uint64("amount")]),s=Buffer.alloc(u.span),u.encode({instruction:3,amount:new TokenAmount(o).toBuffer()},s),i=new Transaction({fee:0,keys:[t.publicKey,n,a],programId:e.programId,userdata:s}),r.next=6,sendAndConfirmTransaction(this.connection,t,i);case 6:case"end":return r.stop()}},r,this)}));return function(e,t,n,a){return r.apply(this,arguments)}}()},{key:"revoke",value:function(e,r,t){return this.approve(e,r,t,0)}}],[{key:"createNewToken",value:function(){var r=_asyncToGenerator(_regeneratorRuntime.mark(function r(t,n,a,o,u,s){var i,c,l,f,p,m,g;return _regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:return i=new Account,c=new e(t,i.publicKey),r.next=4,c._newAccount(n,null);case 4:return l=r.sent.publicKey,f=void 0,p=struct([u32("instruction"),uint64("supply"),u8("decimals"),new RustStringLayout("name"),new RustStringLayout("symbol")]),m=Buffer.alloc(1024),g=p.encode({instruction:0,supply:a.toBuffer(),decimals:s,name:o,symbol:u},m),m=m.slice(0,g),f=SystemProgram.createAccount(n.publicKey,i.publicKey,1,1+m.length,e.programId),r.next=13,sendAndConfirmTransaction(t,n,f);case 13:return f=new Transaction({fee:0,keys:[i.publicKey,l],programId:e.programId,userdata:m}),r.next=16,sendAndConfirmTransaction(t,i,f);case 16:return r.abrupt("return",[c,l]);case 17:case"end":return r.stop()}},r,this)}));return function(e,t,n,a,o,u){return r.apply(this,arguments)}}()},{key:"programId",get:function(){return new PublicKey("0x500000000000000000000000000000000000000000000000000000000000000")}}]),e}();export{Account,BudgetProgram,Connection,PublicKey,SystemProgram,Transaction,Token$$1 as Token}; |
@@ -95,2 +95,6 @@ /** | ||
} | ||
// === src/token-program.js === | ||
/* TODO */ | ||
} |
{ | ||
"name": "@solana/web3.js", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Solana Javascript API", | ||
@@ -56,2 +56,3 @@ "keywords": [ | ||
"bs58": "^4.0.1", | ||
"buffer-layout": "^1.2.0", | ||
"jayson": "^2.0.6", | ||
@@ -58,0 +59,0 @@ "node-fetch": "^2.2.0", |
@@ -72,3 +72,3 @@ [![Build status][travis-image]][travis-url] | ||
## Local Network | ||
The `solana-localnet` program is provide to easily start a test Solana network | ||
The `solana-localnet` program is provided to easily start a test Solana network | ||
locally on your machine. Docker must be installed. The JSON RPC endpoint of | ||
@@ -75,0 +75,0 @@ the local network is `http://localhost:8899`. |
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 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 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 too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7090014
63
248844
8
3
7
+ Addedbuffer-layout@^1.2.0
+ Addedbuffer-layout@1.2.2(transitive)