@coral-xyz/barter-sdk
Version:
Node.js client for the Barter protocol
100 lines • 5.24 kB
JavaScript
/*
* Copyright (C) 2023 Blue Coral, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Barter_program, _Barter_provider;
import { AnchorProvider, parseIdlErrors, Program, translateError, Wallet } from "@coral-xyz/anchor";
import { IDL } from "./barter";
import { createCancelTransaction, createCreateTransaction, createFinalizeTransaction, createSettleTransaction, PROGRAM_ID, } from "./instructions";
const idlErrors = parseIdlErrors(IDL);
export class Barter {
constructor(provider) {
_Barter_program.set(this, void 0);
_Barter_provider.set(this, void 0);
if (!provider.publicKey) {
throw new Error("no public key found on the argued provider");
}
else if (!provider.sendAndConfirm) {
throw new Error("no sendAndConfirm function found on the argued provider");
}
__classPrivateFieldSet(this, _Barter_program, new Program(IDL, PROGRAM_ID, provider), "f");
__classPrivateFieldSet(this, _Barter_provider, provider, "f");
}
static fromKeypair(keypair, connection) {
return new Barter(new AnchorProvider(connection, new Wallet(keypair), {}));
}
get program() {
return __classPrivateFieldGet(this, _Barter_program, "f");
}
get provider() {
return __classPrivateFieldGet(this, _Barter_provider, "f");
}
cancel(opts) {
return __awaiter(this, void 0, void 0, function* () {
const tx = yield createCancelTransaction(__classPrivateFieldGet(this, _Barter_program, "f"), opts);
return this._withParsedTransactionError(tx);
});
}
create(opts) {
return __awaiter(this, void 0, void 0, function* () {
const tx = yield createCreateTransaction(__classPrivateFieldGet(this, _Barter_program, "f"), opts);
return this._withParsedTransactionError(tx);
});
}
finalize(opts) {
return __awaiter(this, void 0, void 0, function* () {
const tx = yield createFinalizeTransaction(__classPrivateFieldGet(this, _Barter_program, "f"), opts);
return this._withParsedTransactionError(tx);
});
}
settle(opts) {
return __awaiter(this, void 0, void 0, function* () {
const tx = yield createSettleTransaction(__classPrivateFieldGet(this, _Barter_program, "f"), opts);
return this._withParsedTransactionError(tx);
});
}
_withParsedTransactionError(tx) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield __classPrivateFieldGet(this, _Barter_provider, "f").sendAndConfirm(tx);
}
catch (err) {
throw translateError(err, idlErrors);
}
});
}
}
_Barter_program = new WeakMap(), _Barter_provider = new WeakMap();
//# sourceMappingURL=client.js.map