Comparing version 0.0.38 to 0.0.39
@@ -10,2 +10,3 @@ const sui = require('@mysten/sui.js'); | ||
const SuiEvent = require('./SuiEvent.js'); | ||
const SuiCoins = require('./SuiCoins.js'); | ||
@@ -107,4 +108,13 @@ class SuiMaster extends SuiCommonMethods { | ||
this._packages = {}; | ||
this._suiCoins = new SuiCoins({ | ||
suiMaster: this, | ||
debug: this._debug, | ||
}); | ||
} | ||
get suiCoins() { | ||
return this._suiCoins; | ||
} | ||
get MIST_PER_SUI() { | ||
@@ -242,22 +252,19 @@ return BigInt(sui.MIST_PER_SUI); | ||
async getBalance(coinType = '0x2::sui::SUI') { | ||
/** | ||
* Query the balance of specific coinType for an owner. If owner == null, returns balance of connected address owner | ||
* | ||
* @param {String} coinType | ||
* @param {String|null} owner | ||
* @returns BigInt | ||
*/ | ||
async getBalance(coinType = '0x2::sui::SUI', owner = null) { | ||
await this.initialize(); | ||
this.log('requesting balance of coin', coinType, '...'); | ||
let res = null; | ||
try { | ||
res = await this._provider.getBalance({owner: this._address, coinType}); | ||
} catch (e) { | ||
this.log('error', e); | ||
res = null; | ||
let queryingBalanceOf = owner; | ||
if (!queryingBalanceOf && this.address) { | ||
queryingBalanceOf = this.address; | ||
} | ||
let ret = BigInt(0); | ||
if (res && res.totalBalance) { | ||
ret = BigInt(res.totalBalance); | ||
} | ||
this.log('balance of', coinType, 'is', ret); | ||
return ret; | ||
const suiCoin = this._suiCoins.get(coinType); | ||
return await (suiCoin.getBalance(queryingBalanceOf)); | ||
} | ||
@@ -264,0 +271,0 @@ |
@@ -127,11 +127,17 @@ const sui = require('@mysten/sui.js'); | ||
for (let param of params) { | ||
if (param && param.type && param.amount && param.type === 'SUI') { | ||
let amount = BigInt(param.amount); | ||
const coin = tx.add(sui.Transactions.SplitCoins(tx.gas, [tx.pure(amount)])); | ||
callArgs.push(coin); | ||
} else if (param && Array.isArray(param) && param.length == 1 && param[0].type && param[0].amount && param[0].type === 'SUI') { | ||
if (param && param.type && param.amount) { | ||
const ownerAddress = this._suiMaster.address; | ||
const suiCoin = await this._suiMaster.suiCoins.get(param.type); | ||
const txCoinToSend = await suiCoin.coinOfAmountToTxCoin(tx, ownerAddress, param.amount); | ||
callArgs.push(txCoinToSend); | ||
} else if (param && Array.isArray(param) && param.length == 1 && param[0].type && param[0].amount) { | ||
// vector<Coin<SUI>> | ||
let amount = BigInt(param[0].amount); | ||
const coin = tx.add(sui.Transactions.SplitCoins(tx.gas, [tx.pure(amount)])); | ||
callArgs.push(tx.makeMoveVec({ objects: [coin]})); | ||
const ownerAddress = this._suiMaster.address; | ||
const suiCoin = await this._suiMaster.suiCoins.get(param.type); | ||
const txCoinToSend = await suiCoin.coinOfAmountToTxCoin(tx, ownerAddress, param.amount); | ||
callArgs.push(tx.makeMoveVec({ objects: [txCoinToSend]})); | ||
} else { | ||
@@ -138,0 +144,0 @@ callArgs.push(tx.pure(param)); |
{ | ||
"name": "suidouble", | ||
"version": "0.0.38", | ||
"version": "0.0.39", | ||
"description": "Set of provider, package and object classes for javascript representation of Sui Move smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser web3 dapps", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,6 +11,8 @@ # suidouble | ||
- [SuiObject](#suiobject) | ||
- [Fetching objects](#fetching-objects) | ||
- [Fetching Events](#fetching-events) | ||
- [Subscribe to Events](#subscribing-to-events) | ||
- [Executing smart contract method](#executing-smart-contract-method) | ||
- [Fetching objects](#fetching-objects) | ||
- [Sending sui / coins with smart contract methods](#sending-sui--coins-with-smart-contract-methods) | ||
- [Composing transaction block yourself](#composing-transaction-block-yourself) | ||
- [Publishing the package](#publishing-the-package) | ||
@@ -209,12 +211,62 @@ - [Upgrading the package](#upgrading-the-package) | ||
If you need to transfer some SUI as part of executing contract method, you can use a magic parameter in form of {type: 'SUI', amount: 400000000000n} where 400000000000 is the amount of MIST you want to send. SuiPackageModule will convert this amount to Coin object using Transactions.SplitCoins method. | ||
##### sending sui / coins with smart contract methods | ||
`amount: 400000000000n`, `amount: '400000000000'`, `amount: 400000000000` will work too | ||
If you need to transfer some SUI/coins as part of executing contract method, you can use a magic parameter in form of: | ||
```javascript | ||
const moveCallResult = await contract.moveCall('suidouble_chat', 'post_pay', [chatShopObjectId, {type: 'SUI', amount: 400000000000n}, messageText, 'metadata']); | ||
{type: 'SUI', amount: 400000000000n} | ||
// 400000000000 MISTs, if amount is BigInt, it's used in decimal items | ||
{type: 'SUI', amount: '0.2'} | ||
// 0.2 SUI , if amount is String, it's translated to decimals, using coin metadata in a lazy way | ||
{type: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', amount: '1'} | ||
// 1 USDC | ||
{type: '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', amount: '99.99'} | ||
// 99.99 USDT | ||
``` | ||
@todo: sending other Coins | ||
So executing | ||
```javascript | ||
const params = [ | ||
chatShopObjectId, | ||
{type: '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', amount: '9.99'}, | ||
messageText, | ||
]; | ||
const moveCallResult = await contract.moveCall('suidouble_chat', 'post_pay', params); | ||
``` | ||
will send 9.99 USDT as the second parameter of the package method. Suidouble will convert needed coins using Sui's SplitCoins and MergeCoins internally to match amount you expect to send. | ||
Some smart contracts requires clients to send coins in form of vectors. This is covered too, just pass magic parameter if the form of an array with one element: | ||
```javascript | ||
const params = [ | ||
chatShopObjectId, | ||
[{type: '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', amount: '9.99'}], | ||
messageText, | ||
]; | ||
``` | ||
Don't forget to test transactions sending real money on devnet/testnet first! | ||
##### composing transaction block yourself | ||
If you need more flexebility, there's always an option to construct the transaction block yourself: | ||
```javascript | ||
const { TransactionBlock, Transactions } = require('suidobule'); // this exposes classes from the "@mysten/sui.js", so you don't have to import them separately | ||
const txb = new TransactionBlock(); | ||
txb.moveCall({ | ||
target: `package_id::module_id::method_name`, | ||
arguments: [ | ||
txb.pure(something), | ||
txb.object(someid), | ||
], | ||
}); | ||
const moveCallResult = await contract.moveCall('suidouble_chat', 'post_pay', {tx: txb}); | ||
``` | ||
##### fetching objects | ||
@@ -374,6 +426,4 @@ | ||
- subscribe to events | ||
- sending other coins as contract methods execution | ||
- suiobject invalidation/fetching optimization | ||
- better documentation | ||
- unit tests coverage to 90%+ |
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
208317
33
3220
426