Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make methods async via swig #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ function sendToAddress(address, amt) {
}

/* Run this method and monitor memory usage to check there are no memory leaks */
function checkMemoryLeak() {
async function checkMemoryLeak() {
for (let i = 0; i < 50; i++) {
let [wallet, online] = initWallet();
let [wallet, online] = await initWallet();
rgblib.dropOnline(online);
wallet.drop();
}
}

function initWallet(vanillaKeychain) {
async function initWallet(vanillaKeychain) {
let bitcoinNetwork = rgblib.BitcoinNetwork.Regtest;
let keys = rgblib.generateKeys(bitcoinNetwork);
console.log("Keys: " + JSON.stringify(keys));
let keys = await rgblib.generateKeys(bitcoinNetwork);

let restoredKeys = rgblib.restoreKeys(bitcoinNetwork, keys.mnemonic);
let restoredKeys = await rgblib.restoreKeys(bitcoinNetwork, keys.mnemonic);
console.log("Restored keys: " + JSON.stringify(restoredKeys));

let walletData = {
Expand All @@ -50,40 +49,49 @@ function initWallet(vanillaKeychain) {
vanillaKeychain: vanillaKeychain,
};
console.log("Creating wallet...");
let wallet = new rgblib.Wallet(new rgblib.WalletData(walletData));
let wallet = await rgblib.Wallet.createInstance(
new rgblib.WalletData(walletData),
);
console.log("Wallet created");

let btcBalance = wallet.getBtcBalance(null, true);
let btcBalance = await wallet.getBtcBalance(null, true);
console.log("BTC balance: " + JSON.stringify(btcBalance));

let address = wallet.getAddress();
let address = await wallet.getAddress();
console.log("Address: " + address);

sendToAddress(address, 1);

console.log("Wallet is going online...");
let online = wallet.goOnline(false, "tcp://localhost:50001");
let online = await wallet.goOnline(false, "tcp://localhost:50001");
console.log("Wallet went online");

btcBalance = wallet.getBtcBalance(online, false);
btcBalance = await wallet.getBtcBalance(online, false);
console.log("BTC balance: " + JSON.stringify(btcBalance));

let created = wallet.createUtxos(online, false, "25", null, "1.0", false);
let created = await wallet.createUtxos(
online,
false,
"25",
null,
"1.0",
false,
);
console.log("Created " + created + " UTXOs");

return [wallet, online];
}

function main() {
let [wallet, online] = initWallet(null);
async function main() {
let [wallet, online] = await initWallet(null);

let asset1 = wallet.issueAssetNIA(online, "USDT", "Tether", "2", [
let asset1 = await wallet.issueAssetNIA(online, "USDT", "Tether", "2", [
"777",
"66",
]);
console.log("Issued a NIA asset " + JSON.stringify(asset1));

let asset2 = wallet.issueAssetCFA(
let asset2 = await wallet.issueAssetCFA(
online,
"Cfa",
"desc",
Expand All @@ -93,7 +101,7 @@ function main() {
);
console.log("Issued a CFA asset: " + JSON.stringify(asset2));

let asset3 = wallet.issueAssetUDA(
let asset3 = await wallet.issueAssetUDA(
online,
"TKN",
"Token",
Expand All @@ -104,18 +112,18 @@ function main() {
);
console.log("Issued a UDA asset: " + JSON.stringify(asset3));

let assets1 = wallet.listAssets([
let assets1 = await wallet.listAssets([
rgblib.AssetSchema.Nia,
rgblib.AssetSchema.Cfa,
]);
console.log("Assets: " + JSON.stringify(assets1));

let assets2 = wallet.listAssets([]);
let assets2 = await wallet.listAssets([]);
console.log("Assets: " + JSON.stringify(assets2));

let [rcvWallet, rcvOnline] = initWallet("3");
let [rcvWallet, rcvOnline] = await initWallet("3");

let receiveData1 = rcvWallet.blindReceive(
let receiveData1 = await rcvWallet.blindReceive(
null,
"100",
null,
Expand All @@ -124,7 +132,7 @@ function main() {
);
console.log("Receive data: " + JSON.stringify(receiveData1));

let receiveData2 = rcvWallet.witnessReceive(
let receiveData2 = await rcvWallet.witnessReceive(
null,
"50",
"60",
Expand Down Expand Up @@ -155,7 +163,7 @@ function main() {
],
};

let sendResult = wallet.send(
let sendResult = await wallet.send(
online,
recipientMap,
false,
Expand All @@ -165,41 +173,41 @@ function main() {
);
console.log("Sent: " + JSON.stringify(sendResult));

rcvWallet.refresh(rcvOnline, null, [], false);
wallet.refresh(online, null, [], false);
await rcvWallet.refresh(rcvOnline, null, [], false);
await wallet.refresh(online, null, [], false);

mine(1);

rcvWallet.refresh(rcvOnline, null, [], false);
wallet.refresh(online, null, [], false);
await rcvWallet.refresh(rcvOnline, null, [], false);
await wallet.refresh(online, null, [], false);

let rcvAssets = rcvWallet.listAssets([]);
let rcvAssets = await rcvWallet.listAssets([]);
console.log("Assets: " + JSON.stringify(rcvAssets));

let rcvAssetBalance = rcvWallet.getAssetBalance(asset1.assetId);
let rcvAssetBalance = await rcvWallet.getAssetBalance(asset1.assetId);
console.log("Asset balance: " + JSON.stringify(rcvAssetBalance));

wallet.sync(online);
await wallet.sync(online);

let transfers = wallet.listTransfers(asset1.assetId);
let transfers = await wallet.listTransfers(asset1.assetId);
console.log("Transfers: " + JSON.stringify(transfers));

let transactions = wallet.listTransactions(online, true);
let transactions = await wallet.listTransactions(online, true);
console.log("Transactions: " + JSON.stringify(transactions));

let unspents = rcvWallet.listUnspents(rcvOnline, false, false);
let unspents = await rcvWallet.listUnspents(rcvOnline, false, false);
console.log("Unspents: " + JSON.stringify(unspents));

try {
let feeEstimation = wallet.getFeeEstimation(online, "7");
let feeEstimation = await wallet.getFeeEstimation(online, "7");
console.log("Fee estimation: " + JSON.stringify(feeEstimation));
} catch (e) {
console.log("Error getting fee estimation: " + e);
}

let txid = wallet.sendBtc(
let txid = await wallet.sendBtc(
online,
rcvWallet.getAddress(),
await rcvWallet.getAddress(),
"700",
"1.6",
false,
Expand Down
18 changes: 11 additions & 7 deletions swig.i
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
#include "./rgb-lib/bindings/c-ffi/rgblib.hpp"
%}

%typemap(out) CResult %{
%typemap(out) CResult (v8::Local<v8::Promise::Resolver> resolver) %{
resolver = v8::Promise::Resolver::New(v8::Isolate::GetCurrent()->GetCurrentContext()).ToLocalChecked();
switch ($1.result) {
case CResultValue::Ok:
$result = SWIG_NewPointerObj((new COpaqueStruct(static_cast< const COpaqueStruct& >($1.inner))), SWIGTYPE_p_COpaqueStruct, SWIG_POINTER_OWN | 0 );
resolver->Resolve(v8::Isolate::GetCurrent()->GetCurrentContext(), SWIG_NewPointerObj((new COpaqueStruct(static_cast< const COpaqueStruct& >($1.inner))), SWIGTYPE_p_COpaqueStruct, SWIG_POINTER_OWN | 0 )).Check();
break;
case CResultValue::Err:
SWIG_V8_Raise((const char*) $1.inner.ptr);
resolver->Reject(v8::Isolate::GetCurrent()->GetCurrentContext(), v8::String::NewFromUtf8(args.GetIsolate(), (const char*) $1.inner.ptr).ToLocalChecked()).Check();
break;
}
$result = resolver->GetPromise();
%}

%typemap(out) CResultString %{
%typemap(out) CResultString (v8::Local<v8::Promise::Resolver> resolver) %{
resolver = v8::Promise::Resolver::New(v8::Isolate::GetCurrent()->GetCurrentContext()).ToLocalChecked();
switch ($1.result) {
case CResultValue::Ok:
if ($1.inner == nullptr) {
$result = v8::Null(v8::Isolate::GetCurrent());
resolver->Resolve(v8::Isolate::GetCurrent()->GetCurrentContext(), v8::Null(v8::Isolate::GetCurrent())).Check();
} else {
$result = v8::String::NewFromUtf8(args.GetIsolate(), (const char*) $1.inner).ToLocalChecked();
resolver->Resolve(v8::Isolate::GetCurrent()->GetCurrentContext(), v8::String::NewFromUtf8(args.GetIsolate(), (const char*) $1.inner).ToLocalChecked()).Check();
delete ($1.inner);
}
break;
case CResultValue::Err:
SWIG_V8_Raise((const char*) $1.inner);
resolver->Reject(v8::Isolate::GetCurrent()->GetCurrentContext(), v8::String::NewFromUtf8(args.GetIsolate(), (const char*) $1.inner).ToLocalChecked()).Check();
break;
}
$result = resolver->GetPromise();
%}


Expand Down
Loading