@zilliqa-js/contract
Advanced tools
Comparing version 0.2.10 to 0.3.0
@@ -1,8 +0,5 @@ | ||
/// <reference types="bn.js" /> | ||
/// <reference types="long" /> | ||
import { Wallet, Transaction } from '@zilliqa-js/account'; | ||
import { Provider } from '@zilliqa-js/core'; | ||
import { BN, Long } from '@zilliqa-js/util'; | ||
import { Contracts } from './factory'; | ||
import { ABI, ContractStatus, Init, State, Value } from './types'; | ||
import { ABI, ContractStatus, Init, State, Value, CallParams, DeployParams } from './types'; | ||
export declare class Contract { | ||
@@ -44,5 +41,11 @@ factory: Contracts; | ||
isRejected(): boolean; | ||
prepareTx(tx: Transaction): Promise<Transaction>; | ||
deploy(gasPrice: BN, gasLimit: Long): Promise<Contract>; | ||
prepareTx(tx: Transaction, attempts?: number, interval?: number): Promise<Transaction>; | ||
/** | ||
* deploy | ||
* | ||
* @param {DeployParams} params | ||
* @returns {Promise<Contract>} | ||
*/ | ||
deploy(params: DeployParams, attempts?: number, interval?: number): Promise<[Transaction, Contract]>; | ||
/** | ||
* call | ||
@@ -54,5 +57,5 @@ * | ||
*/ | ||
call(transition: string, params: Value[], amount?: BN, gasLimit?: Long, gasPrice?: BN): Promise<Transaction>; | ||
call(transition: string, args: Value[], params: CallParams, attempts?: number, interval?: number): Promise<Transaction>; | ||
getState(): Promise<State>; | ||
} | ||
//# sourceMappingURL=contract.d.ts.map |
@@ -61,3 +61,5 @@ "use strict"; | ||
}; | ||
Contract.prototype.prepareTx = function (tx) { | ||
Contract.prototype.prepareTx = function (tx, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -70,5 +72,5 @@ var response; | ||
response = _a.sent(); | ||
return [2 /*return*/, util_1.types.isError(response) | ||
return [2 /*return*/, response.error | ||
? tx.setStatus(3 /* Rejected */) | ||
: tx.confirm(response.result.TranID)]; | ||
: tx.confirm(response.result.TranID, attempts, interval)]; | ||
} | ||
@@ -78,3 +80,11 @@ }); | ||
}; | ||
Contract.prototype.deploy = function (gasPrice, gasLimit) { | ||
/** | ||
* deploy | ||
* | ||
* @param {DeployParams} params | ||
* @returns {Promise<Contract>} | ||
*/ | ||
Contract.prototype.deploy = function (params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -91,12 +101,3 @@ var tx, err_1; | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new account_1.Transaction({ | ||
version: 0, | ||
toAddr: NIL_ADDRESS, | ||
// amount should be 0. we don't accept implicitly anymore. | ||
amount: new util_1.BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
code: this.code, | ||
data: JSON.stringify(this.init).replace(/\\"/g, '"'), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new account_1.Transaction(tslib_1.__assign({}, params, { version: 0, toAddr: NIL_ADDRESS, amount: new util_1.BN(0), code: this.code, data: JSON.stringify(this.init).replace(/\\"/g, '"') }), this.provider), attempts, interval)]; | ||
case 2: | ||
@@ -106,7 +107,7 @@ tx = _a.sent(); | ||
this.status = 1 /* Rejected */; | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
} | ||
this.status = 0 /* Deployed */; | ||
this.address = factory_1.Contracts.getAddressForContract(tx); | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
case 3: | ||
@@ -127,15 +128,13 @@ err_1 = _a.sent(); | ||
*/ | ||
Contract.prototype.call = function (transition, params, amount, gasLimit, gasPrice) { | ||
if (amount === void 0) { amount = new util_1.BN(0); } | ||
if (gasLimit === void 0) { gasLimit = util_1.Long.fromNumber(1000); } | ||
if (gasPrice === void 0) { gasPrice = new util_1.BN(100); } | ||
Contract.prototype.call = function (transition, args, params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var msg, err_2; | ||
var data, err_2; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
msg = { | ||
data = { | ||
_tag: transition, | ||
// TODO: this should be string, but is not yet supported by lookup. | ||
params: params, | ||
params: args, | ||
}; | ||
@@ -148,10 +147,3 @@ if (!this.address) { | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new account_1.Transaction({ | ||
version: 0, | ||
toAddr: this.address, | ||
amount: new util_1.BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
data: JSON.stringify(msg), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new account_1.Transaction(tslib_1.__assign({}, params, { version: 0, toAddr: this.address, data: JSON.stringify(data) }), this.provider), attempts, interval)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
@@ -186,3 +178,3 @@ case 3: | ||
tslib_1.__metadata("design:type", Function), | ||
tslib_1.__metadata("design:paramtypes", [account_1.Transaction]), | ||
tslib_1.__metadata("design:paramtypes", [account_1.Transaction, Number, Number]), | ||
tslib_1.__metadata("design:returntype", Promise) | ||
@@ -189,0 +181,0 @@ ], Contract.prototype, "prepareTx", null); |
import { Transaction, util } from '@zilliqa-js/account'; | ||
import { sign } from '@zilliqa-js/core'; | ||
import { BN, Long, types, bytes } from '@zilliqa-js/util'; | ||
import { BN, bytes } from '@zilliqa-js/util'; | ||
@@ -1259,2 +1259,13 @@ function createCommonjsModule(fn, module) { | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __decorate(decorators, target, key, desc) { | ||
@@ -1361,3 +1372,5 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
}; | ||
Contract.prototype.prepareTx = function (tx) { | ||
Contract.prototype.prepareTx = function (tx, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -1370,5 +1383,5 @@ var response; | ||
response = _a.sent(); | ||
return [2 /*return*/, types.isError(response) | ||
return [2 /*return*/, response.error | ||
? tx.setStatus(3 /* Rejected */) | ||
: tx.confirm(response.result.TranID)]; | ||
: tx.confirm(response.result.TranID, attempts, interval)]; | ||
} | ||
@@ -1378,3 +1391,11 @@ }); | ||
}; | ||
Contract.prototype.deploy = function (gasPrice, gasLimit) { | ||
/** | ||
* deploy | ||
* | ||
* @param {DeployParams} params | ||
* @returns {Promise<Contract>} | ||
*/ | ||
Contract.prototype.deploy = function (params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -1391,12 +1412,3 @@ var tx, err_1; | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new Transaction({ | ||
version: 0, | ||
toAddr: NIL_ADDRESS, | ||
// amount should be 0. we don't accept implicitly anymore. | ||
amount: new BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
code: this.code, | ||
data: JSON.stringify(this.init).replace(/\\"/g, '"'), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new Transaction(__assign({}, params, { version: 0, toAddr: NIL_ADDRESS, amount: new BN(0), code: this.code, data: JSON.stringify(this.init).replace(/\\"/g, '"') }), this.provider), attempts, interval)]; | ||
case 2: | ||
@@ -1406,7 +1418,7 @@ tx = _a.sent(); | ||
this.status = 1 /* Rejected */; | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
} | ||
this.status = 0 /* Deployed */; | ||
this.address = Contracts.getAddressForContract(tx); | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
case 3: | ||
@@ -1427,15 +1439,13 @@ err_1 = _a.sent(); | ||
*/ | ||
Contract.prototype.call = function (transition, params, amount, gasLimit, gasPrice) { | ||
if (amount === void 0) { amount = new BN(0); } | ||
if (gasLimit === void 0) { gasLimit = Long.fromNumber(1000); } | ||
if (gasPrice === void 0) { gasPrice = new BN(100); } | ||
Contract.prototype.call = function (transition, args, params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var msg, err_2; | ||
var data, err_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
msg = { | ||
data = { | ||
_tag: transition, | ||
// TODO: this should be string, but is not yet supported by lookup. | ||
params: params, | ||
params: args, | ||
}; | ||
@@ -1448,10 +1458,3 @@ if (!this.address) { | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new Transaction({ | ||
version: 0, | ||
toAddr: this.address, | ||
amount: new BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
data: JSON.stringify(msg), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new Transaction(__assign({}, params, { version: 0, toAddr: this.address, data: JSON.stringify(data) }), this.provider), attempts, interval)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
@@ -1486,3 +1489,3 @@ case 3: | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Transaction]), | ||
__metadata("design:paramtypes", [Transaction, Number, Number]), | ||
__metadata("design:returntype", Promise) | ||
@@ -1534,3 +1537,10 @@ ], Contract.prototype, "prepareTx", null); | ||
export { Contracts, Contract }; | ||
var ContractStatus; | ||
(function (ContractStatus) { | ||
ContractStatus[ContractStatus["Deployed"] = 0] = "Deployed"; | ||
ContractStatus[ContractStatus["Rejected"] = 1] = "Rejected"; | ||
ContractStatus[ContractStatus["Initialised"] = 2] = "Initialised"; | ||
})(ContractStatus || (ContractStatus = {})); | ||
export { Contracts, Contract, ContractStatus }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1261,2 +1261,13 @@ (function (global, factory) { | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __decorate(decorators, target, key, desc) { | ||
@@ -1363,3 +1374,5 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
}; | ||
Contract.prototype.prepareTx = function (tx) { | ||
Contract.prototype.prepareTx = function (tx, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -1372,5 +1385,5 @@ var response; | ||
response = _a.sent(); | ||
return [2 /*return*/, util.types.isError(response) | ||
return [2 /*return*/, response.error | ||
? tx.setStatus(3 /* Rejected */) | ||
: tx.confirm(response.result.TranID)]; | ||
: tx.confirm(response.result.TranID, attempts, interval)]; | ||
} | ||
@@ -1380,3 +1393,11 @@ }); | ||
}; | ||
Contract.prototype.deploy = function (gasPrice, gasLimit) { | ||
/** | ||
* deploy | ||
* | ||
* @param {DeployParams} params | ||
* @returns {Promise<Contract>} | ||
*/ | ||
Contract.prototype.deploy = function (params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -1393,12 +1414,3 @@ var tx, err_1; | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new account.Transaction({ | ||
version: 0, | ||
toAddr: NIL_ADDRESS, | ||
// amount should be 0. we don't accept implicitly anymore. | ||
amount: new util.BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
code: this.code, | ||
data: JSON.stringify(this.init).replace(/\\"/g, '"'), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new account.Transaction(__assign({}, params, { version: 0, toAddr: NIL_ADDRESS, amount: new util.BN(0), code: this.code, data: JSON.stringify(this.init).replace(/\\"/g, '"') }), this.provider), attempts, interval)]; | ||
case 2: | ||
@@ -1408,7 +1420,7 @@ tx = _a.sent(); | ||
this.status = 1 /* Rejected */; | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
} | ||
this.status = 0 /* Deployed */; | ||
this.address = Contracts.getAddressForContract(tx); | ||
return [2 /*return*/, this]; | ||
return [2 /*return*/, [tx, this]]; | ||
case 3: | ||
@@ -1429,15 +1441,13 @@ err_1 = _a.sent(); | ||
*/ | ||
Contract.prototype.call = function (transition, params, amount, gasLimit, gasPrice) { | ||
if (amount === void 0) { amount = new util.BN(0); } | ||
if (gasLimit === void 0) { gasLimit = util.Long.fromNumber(1000); } | ||
if (gasPrice === void 0) { gasPrice = new util.BN(100); } | ||
Contract.prototype.call = function (transition, args, params, attempts, interval) { | ||
if (attempts === void 0) { attempts = 20; } | ||
if (interval === void 0) { interval = 1000; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var msg, err_2; | ||
var data, err_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
msg = { | ||
data = { | ||
_tag: transition, | ||
// TODO: this should be string, but is not yet supported by lookup. | ||
params: params, | ||
params: args, | ||
}; | ||
@@ -1450,10 +1460,3 @@ if (!this.address) { | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.prepareTx(new account.Transaction({ | ||
version: 0, | ||
toAddr: this.address, | ||
amount: new util.BN(0), | ||
gasPrice: gasPrice, | ||
gasLimit: gasLimit, | ||
data: JSON.stringify(msg), | ||
}, this.provider))]; | ||
return [4 /*yield*/, this.prepareTx(new account.Transaction(__assign({}, params, { version: 0, toAddr: this.address, data: JSON.stringify(data) }), this.provider), attempts, interval)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
@@ -1488,3 +1491,3 @@ case 3: | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [account.Transaction]), | ||
__metadata("design:paramtypes", [account.Transaction, Number, Number]), | ||
__metadata("design:returntype", Promise) | ||
@@ -1536,2 +1539,8 @@ ], Contract.prototype, "prepareTx", null); | ||
(function (ContractStatus) { | ||
ContractStatus[ContractStatus["Deployed"] = 0] = "Deployed"; | ||
ContractStatus[ContractStatus["Rejected"] = 1] = "Rejected"; | ||
ContractStatus[ContractStatus["Initialised"] = 2] = "Initialised"; | ||
})(exports.ContractStatus || (exports.ContractStatus = {})); | ||
exports.Contracts = Contracts; | ||
@@ -1538,0 +1547,0 @@ exports.Contract = Contract; |
@@ -0,1 +1,3 @@ | ||
import { Omit } from 'utility-types'; | ||
import { TxParams } from '@zilliqa-js/account'; | ||
export declare const enum ContractStatus { | ||
@@ -6,2 +8,4 @@ Deployed = 0, | ||
} | ||
export declare type DeployParams = Omit<TxParams, 'version' | 'toAddr' | 'amount' | 'code' | 'data' | 'receipt' | 'signature'>; | ||
export declare type CallParams = Omit<TxParams, 'version' | 'toAddr' | 'data' | 'code' | 'receipt' | 'signature'>; | ||
export interface ContractObj { | ||
@@ -8,0 +12,0 @@ address: string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ContractStatus; | ||
(function (ContractStatus) { | ||
ContractStatus[ContractStatus["Deployed"] = 0] = "Deployed"; | ||
ContractStatus[ContractStatus["Rejected"] = 1] = "Rejected"; | ||
ContractStatus[ContractStatus["Initialised"] = 2] = "Initialised"; | ||
})(ContractStatus = exports.ContractStatus || (exports.ContractStatus = {})); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@zilliqa-js/contract", | ||
"version": "0.2.10", | ||
"version": "0.3.0", | ||
"description": "Contract-related functionality.", | ||
@@ -22,9 +22,10 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@zilliqa-js/account": "0.2.10", | ||
"@zilliqa-js/blockchain": "0.2.10", | ||
"@zilliqa-js/core": "0.2.10", | ||
"@zilliqa-js/util": "0.2.10", | ||
"hash.js": "^1.1.5" | ||
"@zilliqa-js/account": "0.3.0", | ||
"@zilliqa-js/blockchain": "0.3.0", | ||
"@zilliqa-js/core": "0.3.0", | ||
"@zilliqa-js/util": "0.3.0", | ||
"hash.js": "^1.1.5", | ||
"utility-types": "^2.1.0" | ||
}, | ||
"gitHead": "5c213717525c658e5ac1a9c1efeb46bdd856e204" | ||
"gitHead": "7099301a8c3cde45cf083f9f947ba6461001c927" | ||
} |
@@ -7,2 +7,17 @@ # @zilliqa-js/contract | ||
```typescript | ||
interface DeployParams { | ||
gasPrice: BN; | ||
gasLimit: Long; | ||
nonce?: number; | ||
pubKey?: string; | ||
} | ||
interface CallParams { | ||
amount: BN; | ||
gasPrice: BN; | ||
gasLimit: Long; | ||
nonce?: number; | ||
pubKey?: string; | ||
} | ||
interface Field { | ||
@@ -171,3 +186,3 @@ name: string; | ||
### `deploy(gasPrice: BN, gasLimit: Long): Promise<Contract>` | ||
### `deploy(params: DeployParams, attempts: number = 20, interval: number = 1000): Promise<Contract>` | ||
@@ -183,4 +198,10 @@ Deploys a contract to the blockchain. This method will automatically generate | ||
- `gasPrice`: `BN` - an instance of `BN.js`. | ||
- `gasLimit`: `Long` - an instance of `Long.js`. | ||
- `params`: `DeployParams` - a subset of TxParams. Passed to the underlying | ||
`Transaction`. This can be used to manually provide `nonce` and `pubKey`, | ||
if it is desirable to sign the underlying transaction with a non-default | ||
account in the `Wallet`. | ||
- `attempts` (Optional - default 20): `number` - the number of times to poll the lookup node for | ||
transaction receipt. | ||
- `interval` (Optional - default 1000): `number` - the amount of time to wait | ||
between attempts. increases linearly (`numAttempts * interval`). | ||
@@ -194,3 +215,3 @@ **Returns** | ||
### `call(transition: string, params: Value[], amount: BN = new BN(0), gasLimit: Long = Long.fromNumber(1000), gasPrice: BN = new BN(10)): Promise<Transaction>` | ||
### `call(transition: string, args: Value[], params: CallParams): Promise<Transaction>` | ||
@@ -206,6 +227,9 @@ Calls a transition of the current contract. At the moment, this is a low-level | ||
invoked. _case matters_ | ||
- `params`: `Value[]` - JSON-encoded array of transition parameters. | ||
- `amount`: `BN` (Optional) - an instance of `BN.js`. Default: 0. | ||
- `gasLimit`: `Long` (Optional) - an instance of `Long.js`. Default: 1000. | ||
- `gasPrice`: `BN` (Optional) - an instance of `BN.js`. Default: 10. | ||
- `args`: `Value[]` - JSON-encoded array of transition arguments. | ||
- `params`: `CallParams` - a subset of `TxParams`. Passed to the underlying | ||
`Transaction`. | ||
- `attempts` (Optional - default 20): `number` - the number of times to poll the lookup node for | ||
transaction receipt. | ||
- `interval` (Optional - default 1000): `number` - the amount of time to wait | ||
between attempts. increases linearly (`numAttempts * interval`). | ||
@@ -212,0 +236,0 @@ **Returns** |
import { Wallet, Transaction, TxStatus } from '@zilliqa-js/account'; | ||
import { RPCMethod, Provider, sign } from '@zilliqa-js/core'; | ||
import { BN, Long, types } from '@zilliqa-js/util'; | ||
import { BN } from '@zilliqa-js/util'; | ||
@@ -14,2 +14,4 @@ import { Contracts } from './factory'; | ||
DeploySuccess, | ||
CallParams, | ||
DeployParams, | ||
} from './types'; | ||
@@ -93,3 +95,7 @@ | ||
@sign | ||
async prepareTx(tx: Transaction): Promise<Transaction> { | ||
async prepareTx( | ||
tx: Transaction, | ||
attempts: number = 20, | ||
interval: number = 1000, | ||
): Promise<Transaction> { | ||
const response = await this.provider.send<DeploySuccess, DeployError>( | ||
@@ -100,8 +106,18 @@ RPCMethod.CreateTransaction, | ||
return types.isError(response) | ||
return response.error | ||
? tx.setStatus(TxStatus.Rejected) | ||
: tx.confirm(response.result.TranID); | ||
: tx.confirm(response.result.TranID, attempts, interval); | ||
} | ||
async deploy(gasPrice: BN, gasLimit: Long): Promise<Contract> { | ||
/** | ||
* deploy | ||
* | ||
* @param {DeployParams} params | ||
* @returns {Promise<Contract>} | ||
*/ | ||
async deploy( | ||
params: DeployParams, | ||
attempts: number = 20, | ||
interval: number = 1000, | ||
): Promise<[Transaction, Contract]> { | ||
if (!this.code || !this.init) { | ||
@@ -117,8 +133,6 @@ throw new Error( | ||
{ | ||
...params, | ||
version: 0, | ||
toAddr: NIL_ADDRESS, | ||
// amount should be 0. we don't accept implicitly anymore. | ||
amount: new BN(0), | ||
gasPrice, | ||
gasLimit, | ||
code: this.code, | ||
@@ -129,2 +143,4 @@ data: JSON.stringify(this.init).replace(/\\"/g, '"'), | ||
), | ||
attempts, | ||
interval, | ||
); | ||
@@ -134,3 +150,3 @@ | ||
this.status = ContractStatus.Rejected; | ||
return this; | ||
return [tx, this]; | ||
} | ||
@@ -141,3 +157,3 @@ | ||
return this; | ||
return [tx, this]; | ||
} catch (err) { | ||
@@ -157,11 +173,10 @@ throw err; | ||
transition: string, | ||
params: Value[], | ||
amount: BN = new BN(0), | ||
gasLimit: Long = Long.fromNumber(1000), | ||
gasPrice: BN = new BN(100), | ||
args: Value[], | ||
params: CallParams, | ||
attempts: number = 20, | ||
interval: number = 1000, | ||
): Promise<Transaction> { | ||
const msg = { | ||
const data = { | ||
_tag: transition, | ||
// TODO: this should be string, but is not yet supported by lookup. | ||
params, | ||
params: args, | ||
}; | ||
@@ -177,11 +192,11 @@ | ||
{ | ||
...params, | ||
version: 0, | ||
toAddr: this.address, | ||
amount: new BN(0), | ||
gasPrice, | ||
gasLimit, | ||
data: JSON.stringify(msg), | ||
data: JSON.stringify(data), | ||
}, | ||
this.provider, | ||
), | ||
attempts, | ||
interval, | ||
); | ||
@@ -188,0 +203,0 @@ } catch (err) { |
@@ -0,1 +1,4 @@ | ||
import { Omit } from 'utility-types'; | ||
import { TxParams } from '@zilliqa-js/account'; | ||
export const enum ContractStatus { | ||
@@ -7,2 +10,12 @@ Deployed, | ||
export type DeployParams = Omit< | ||
TxParams, | ||
'version' | 'toAddr' | 'amount' | 'code' | 'data' | 'receipt' | 'signature' | ||
>; | ||
export type CallParams = Omit< | ||
TxParams, | ||
'version' | 'toAddr' | 'data' | 'code' | 'receipt' | 'signature' | ||
>; | ||
export interface ContractObj { | ||
@@ -9,0 +22,0 @@ address: string; |
@@ -5,3 +5,3 @@ import { Account, Wallet } from '@zilliqa-js/account'; | ||
import { Contracts, Contract, ContractStatus } from '../src/index'; | ||
import { Contracts, ContractStatus } from '../src/index'; | ||
import { abi } from './test.abi'; | ||
@@ -17,6 +17,6 @@ import { testContract } from './fixtures'; | ||
describe('Contract - hello world', () => { | ||
let contract: Contract; | ||
let address: string; | ||
it('should be able to deploy the contract', async () => { | ||
contract = await contractFactory | ||
let [tx, contract] = await contractFactory | ||
.new( | ||
@@ -30,7 +30,18 @@ testContract, | ||
}, | ||
{ | ||
vname: '_scilla_version', | ||
type: 'Uint32', | ||
value: '0', | ||
}, | ||
], | ||
abi, | ||
) | ||
.deploy(new BN(1000), Long.fromNumber(5000)); | ||
.deploy({ | ||
gasPrice: new BN(100), | ||
gasLimit: Long.fromNumber(2500), | ||
}); | ||
address = <string>contract.address; | ||
expect(tx.isConfirmed()).toBeTruthy(); | ||
expect(contract.status).toEqual(ContractStatus.Deployed); | ||
@@ -40,10 +51,19 @@ }); | ||
it('should be able to call setHello', async () => { | ||
const contract = contractFactory.at(address); | ||
// now let's transfer some tokens | ||
const call = await contract.call('setHello', [ | ||
const call = await contract.call( | ||
'setHello', | ||
[ | ||
{ | ||
vname: 'msg', | ||
type: 'String', | ||
value: 'Hello World', | ||
}, | ||
], | ||
{ | ||
vname: 'msg', | ||
type: 'String', | ||
value: 'Hello World', | ||
amount: new BN(0), | ||
gasPrice: new BN(100), | ||
gasLimit: Long.fromNumber(2500), | ||
}, | ||
]); | ||
); | ||
@@ -63,15 +83,21 @@ expect(call.txParams.receipt && call.txParams.receipt.success).toBeTruthy(); | ||
// setup a non-existent address | ||
const original = contract.address; | ||
contract.address = '0123456789'.repeat(4); | ||
const call = await contract.call('setHello', [ | ||
const contract = contractFactory.at('0123456789'.repeat(4)); | ||
const call = await contract.call( | ||
'setHello', | ||
[ | ||
{ | ||
vname: 'msg', | ||
type: 'String', | ||
value: 'Hello World', | ||
}, | ||
], | ||
{ | ||
vname: 'msg', | ||
type: 'String', | ||
value: 'Hello World', | ||
amount: new BN(0), | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}, | ||
]); | ||
); | ||
contract.address = original; | ||
expect(call.isRejected()).toBeTruthy(); | ||
}); | ||
}); |
@@ -80,4 +80,8 @@ import { Wallet } from '@zilliqa-js/account'; | ||
const deployed = await contract.deploy(new BN(1000), Long.fromNumber(1000)); | ||
const [tx, deployed] = await contract.deploy({ | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}); | ||
expect(tx.isConfirmed()).toBeTruthy(); | ||
expect(deployed.isDeployed()).toBeTruthy(); | ||
@@ -127,3 +131,6 @@ expect(deployed.status).toEqual(ContractStatus.Deployed); | ||
await expect( | ||
contract.deploy(new BN(1000), Long.fromNumber(1000)), | ||
contract.deploy({ | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}), | ||
).rejects.toThrow(); | ||
@@ -145,4 +152,5 @@ }); | ||
jsonrpc: '2.0', | ||
result: { | ||
Error: 'Mega fail', | ||
error: { | ||
code: 444, | ||
message: 'Mega fail', | ||
}, | ||
@@ -154,3 +162,3 @@ }, | ||
const contract = await contractFactory | ||
const [tx, contract] = await contractFactory | ||
.new( | ||
@@ -169,4 +177,8 @@ testContract, | ||
) | ||
.deploy(new BN(1000), Long.fromNumber(1000)); | ||
.deploy({ | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}); | ||
expect(tx.isRejected()).toBeTruthy(); | ||
expect(contract.isRejected()).toBeTruthy(); | ||
@@ -208,3 +220,3 @@ expect(contract.status).toEqual(ContractStatus.Rejected); | ||
const contract = await contractFactory | ||
const [tx, contract] = await contractFactory | ||
.new( | ||
@@ -223,5 +235,9 @@ testContract, | ||
) | ||
.deploy(new BN(1000), Long.fromNumber(1000)); | ||
.deploy({ | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}); | ||
expect(contract.isRejected).toBeTruthy(); | ||
expect(tx.isRejected()).toBeTruthy(); | ||
expect(contract.isRejected()).toBeTruthy(); | ||
}); | ||
@@ -287,3 +303,3 @@ | ||
const contract = await contractFactory | ||
const [, contract] = await contractFactory | ||
.new( | ||
@@ -302,8 +318,19 @@ testContract, | ||
) | ||
.deploy(new BN(1000), Long.fromNumber(1000)); | ||
.deploy({ | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}); | ||
const callTx = await contract.call('myTransition', [ | ||
{ vname: 'param_1', type: 'String', value: 'hello' }, | ||
{ vname: 'param_2', type: 'String', value: 'world' }, | ||
]); | ||
const callTx = await contract.call( | ||
'myTransition', | ||
[ | ||
{ vname: 'param_1', type: 'String', value: 'hello' }, | ||
{ vname: 'param_2', type: 'String', value: 'world' }, | ||
], | ||
{ | ||
amount: new BN(0), | ||
gasPrice: new BN(1000), | ||
gasLimit: Long.fromNumber(1000), | ||
}, | ||
); | ||
@@ -310,0 +337,0 @@ const { receipt } = callTx.txParams; |
@@ -1,3 +0,5 @@ | ||
export const testContract = `(* Test contract *) | ||
export const testContract = `scilla_version 0 | ||
(* HelloWorld contract *) | ||
import ListUtils | ||
@@ -15,2 +17,3 @@ | ||
let not_owner_code = Int32 1 | ||
let set_hello_code = Int32 2 | ||
@@ -28,13 +31,21 @@ | ||
transition setHello (msg : String) | ||
welcome_msg := msg; | ||
msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; code : set_hello_code}; | ||
msgs = one_msg msg; | ||
send msgs | ||
is_owner = builtin eq owner _sender; | ||
match is_owner with | ||
| False => | ||
msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; code : not_owner_code}; | ||
msgs = one_msg msg; | ||
send msgs | ||
| True => | ||
welcome_msg := msg; | ||
msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; code : set_hello_code}; | ||
msgs = one_msg msg; | ||
send msgs | ||
end | ||
end | ||
transition getHello () | ||
r <- welcome_msg; | ||
msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; msg : r}; | ||
msgs = one_msg msg; | ||
send msgs | ||
e = {_eventname: "getHello()"; msg: r}; | ||
event e | ||
end`; |
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
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
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
347989
3970
252
6
+ Addedutility-types@^2.1.0
+ Added@zilliqa-js/account@0.3.0(transitive)
+ Added@zilliqa-js/blockchain@0.3.0(transitive)
+ Added@zilliqa-js/core@0.3.0(transitive)
+ Added@zilliqa-js/crypto@0.3.0(transitive)
+ Added@zilliqa-js/proto@0.3.0(transitive)
+ Added@zilliqa-js/util@0.3.0(transitive)
+ Addedcipher-base@1.0.6(transitive)
+ Addedutility-types@2.1.0(transitive)
- Removed@zilliqa-js/account@0.2.10(transitive)
- Removed@zilliqa-js/blockchain@0.2.10(transitive)
- Removed@zilliqa-js/core@0.2.10(transitive)
- Removed@zilliqa-js/crypto@0.2.10(transitive)
- Removed@zilliqa-js/proto@0.2.10(transitive)
- Removed@zilliqa-js/util@0.2.10(transitive)
- Removedcipher-base@1.0.5(transitive)
Updated@zilliqa-js/account@0.3.0
Updated@zilliqa-js/blockchain@0.3.0
Updated@zilliqa-js/core@0.3.0
Updated@zilliqa-js/util@0.3.0