-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
280 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Keypair } from "@solana/web3.js"; | ||
import { ITransactionExt } from "./external-types"; | ||
|
||
// TODO use export model instead and map across | ||
|
||
export const EXAMPLES: Record< | ||
string, | ||
(walletPublicKey: string) => ITransactionExt | ||
> = { | ||
systemProgramCreateAccount: (walletPublicKey: string) => ({ | ||
name: "System Program: Create Account", | ||
instructions: [ | ||
{ | ||
name: "Create Account", | ||
programId: "11111111111111111111111111111111", | ||
accounts: [ | ||
{ | ||
name: "From", | ||
pubkey: walletPublicKey, | ||
isWritable: true, | ||
isSigner: true, | ||
}, | ||
{ | ||
name: "To", | ||
pubkey: Keypair.generate().publicKey.toBase58(), | ||
isWritable: true, | ||
isSigner: false, | ||
}, | ||
], | ||
data: { | ||
format: "bufferLayout", | ||
value: [ | ||
{ | ||
name: "Instruction", | ||
type: "u32", | ||
value: 0, | ||
}, | ||
{ | ||
name: "Lamport", | ||
type: "u64", | ||
value: 1400000, | ||
}, | ||
{ | ||
name: "Space", | ||
type: "u64", | ||
value: 100, | ||
}, | ||
{ | ||
name: "Program ID", | ||
type: "publicKey", | ||
value: 100, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}), | ||
|
||
systemProgramTransfer: (walletPublicKey: string) => ({ | ||
name: "System Program: Transfer", | ||
instructions: [ | ||
{ | ||
name: "Transfer", | ||
programId: "11111111111111111111111111111111", | ||
accounts: [ | ||
{ | ||
name: "From", | ||
pubkey: walletPublicKey, | ||
isWritable: true, | ||
isSigner: true, | ||
}, | ||
{ | ||
name: "To", | ||
pubkey: "GoctE4EU5jZqbWg1Ffo5sjCqjrnzW1m76JmWwd84pwtV", | ||
isWritable: true, | ||
isSigner: false, | ||
}, | ||
], | ||
data: { | ||
format: "bufferLayout", | ||
value: [ | ||
{ | ||
name: "Instruction", | ||
type: "u32", | ||
value: 2, | ||
}, | ||
{ | ||
name: "Lamport", | ||
type: "u64", | ||
value: 1, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { v4 as uuid } from "uuid"; | ||
import { IInstrctionDataFieldExt, ITransactionExt } from "./external-types"; | ||
import { IInstrctionDataField, ITransaction } from "./internal-types"; | ||
import { | ||
Identifiable, | ||
SortableCollection, | ||
toSortableCollection, | ||
toSortedArray, | ||
} from "./sortable"; | ||
|
||
const mapToIInstrctionDataFieldExt = ({ | ||
name, | ||
type, | ||
value, | ||
}: IInstrctionDataField): IInstrctionDataFieldExt => ({ | ||
name, | ||
type, | ||
value, | ||
}); | ||
|
||
export const mapToTransactionExt = ({ | ||
name, | ||
instructions, | ||
}: ITransaction): ITransactionExt => ({ | ||
name, | ||
instructions: toSortedArray(instructions).map( | ||
({ name, programId, accounts, data }) => ({ | ||
name, | ||
programId, | ||
accounts: toSortedArray(accounts).map( | ||
({ name, pubkey, isWritable, isSigner }) => ({ | ||
name, | ||
pubkey, | ||
isWritable, | ||
isSigner, | ||
}) | ||
), | ||
data: { | ||
format: data.format, | ||
value: | ||
data.format === "borsh" | ||
? toSortedArray(data.borsh).map(mapToIInstrctionDataFieldExt) | ||
: data.format === "bufferLayout" | ||
? toSortedArray(data.bufferLayout).map(mapToIInstrctionDataFieldExt) | ||
: data.raw, | ||
}, | ||
}) | ||
), | ||
}); | ||
|
||
const mapToSortableCollection = <T>( | ||
items: T[] | ||
): SortableCollection<T & Identifiable> => | ||
toSortableCollection(items.map((item) => ({ ...item, id: uuid() }))); | ||
|
||
export const mapFromTransactionExt = ({ | ||
name, | ||
instructions, | ||
}: ITransactionExt): ITransaction => ({ | ||
name, | ||
instructions: mapToSortableCollection( | ||
instructions.map(({ name, programId, accounts, data }) => ({ | ||
name, | ||
programId, | ||
accounts: mapToSortableCollection( | ||
accounts.map(({ name, pubkey, isWritable, isSigner }) => ({ | ||
name, | ||
pubkey, | ||
isWritable, | ||
isSigner, | ||
})) | ||
), | ||
data: { | ||
format: data.format, | ||
raw: data.format === "raw" ? (data.value as string) : "", | ||
borsh: mapToSortableCollection( | ||
data.format === "borsh" | ||
? (data.value as IInstrctionDataFieldExt[]) | ||
: [] | ||
), | ||
bufferLayout: mapToSortableCollection( | ||
data.format === "bufferLayout" | ||
? (data.value as IInstrctionDataFieldExt[]) | ||
: [] | ||
), | ||
}, | ||
})) | ||
), | ||
}); | ||
|
||
// const mapFromTransactionExport = ( | ||
// transaction: ITransactionExport | ||
// ): ITransaction => { | ||
// }; | ||
|
||
// export const serialiseTransaction = (transaction: ITransaction): string => {}; | ||
|
||
// export const deserialiseTransaction = (serialised: string): ITransaction => {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Models for the transaction to be exported | ||
|
||
import { | ||
DataFormat, | ||
InstructionDataFieldType, | ||
IPubKey, | ||
} from "./internal-types"; | ||
|
||
export interface IInstrctionDataFieldExt { | ||
name?: string; | ||
type: InstructionDataFieldType; | ||
value: any; | ||
} | ||
|
||
export interface IAccountExt { | ||
name?: string; | ||
pubkey: IPubKey; | ||
isWritable: boolean; | ||
isSigner: boolean; | ||
} | ||
|
||
export interface IInstructionExt { | ||
name?: string; | ||
programId: IPubKey; | ||
accounts: IAccountExt[]; | ||
data: { | ||
format: DataFormat; | ||
value: IInstrctionDataFieldExt[] | string; | ||
}; | ||
} | ||
|
||
export interface ITransactionExt { | ||
name?: string; | ||
instructions: IInstructionExt[]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters