Skip to content

Commit

Permalink
fix: borsh serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
vecheslav committed Sep 16, 2021
1 parent bf63cc6 commit c06c989
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
15 changes: 7 additions & 8 deletions src/transactions/SetStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ export interface SetStoreArgs {
public: boolean;
}

const setStoreStruct = borsh.struct<SetStoreArgs>(
[
['instruction', 'u8'],
['public', 'u8'],
],
[],
);
const setStoreStruct = borsh.struct<SetStoreArgs>([
['instruction', 'u8'],
['public', 'u8'],
]);

type SetStoreParams = {
store: PublicKey;
Expand All @@ -35,7 +32,9 @@ export class SetStore extends Transaction {
const { feePayer } = options;
const { admin, store, isPublic } = params;

const data = setStoreStruct.serialize({ instruction: 8, public: isPublic });
const data = setStoreStruct.serialize(
new setStoreStruct.type({ instruction: 8, public: isPublic }),
);

this.add(
new TransactionInstruction({
Expand Down
19 changes: 13 additions & 6 deletions src/utils/borsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,25 @@ extendBorsh();
export class Struct<T> {
readonly fields;
readonly dependencies: Struct<any>[] = [];
readonly type: (args: T) => T;
readonly type: any; //(args: T) => T;

This comment has been minimized.

Copy link
@vecheslav

vecheslav Sep 20, 2021

Author Contributor

shame on me

readonly schema: Map<any, any>;

constructor(fields: any[][], dependencies: Struct<any>[] = [], parse?: (args: T) => T) {
this.fields = fields;
this.dependencies = dependencies;
this.type = function (args: T = {} as T) {
// Ensure all args exist as undefined.
for (const [name] of fields) {
if (!(name in args)) (args as any)[name] = undefined;

this.type = class Type {
constructor(args: T = {} as T) {
for (const [name] of fields) {
if (!(name in args)) {
(args as any)[name] = undefined;
}
}
parse && parse(args);
for (const key of Object.keys(args)) {
this[key] = args[key];
}
}
return parse ? parse(args) : args;
};

const entries = [
Expand Down
5 changes: 4 additions & 1 deletion test/metaplex.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jest } from '@jest/globals';
import { Keypair, sendAndConfirmTransaction } from '@solana/web3.js';
import {
Auction,
Expand All @@ -21,6 +22,8 @@ describe('Metaplex', () => {
let connection: Connection;
let owner: Keypair;

jest.setTimeout(80000);

beforeAll(() => {
connection = new Connection('devnet');
owner = Keypair.generate();
Expand Down Expand Up @@ -64,7 +67,7 @@ describe('Metaplex', () => {
commitment: 'confirmed',
});

console.log(txid);
// console.log(txid);
});
});

Expand Down

0 comments on commit c06c989

Please sign in to comment.