-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* token: Add Zig implementation #### Problem There's only a Rust implementation of SPL Token in Rosetta, but we can write a token program in many other languages. #### Summary of changes Add a simple SPL Token clone in Zig. It does not have multisig support just yet, but it has most of the functionality needed. CU usage is much lower than the Rust version. We might be able to improve it more if we can make pubkey comparisons cheaper. Each one seems to use ~30 CUs currently. * Run zig fmt
- Loading branch information
Showing
11 changed files
with
1,377 additions
and
5 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#![cfg(feature = "test-sbf")] | ||
|
||
mod action; | ||
use { | ||
solana_program_test::{processor, tokio, ProgramTest}, | ||
|
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,24 @@ | ||
const std = @import("std"); | ||
const solana = @import("solana-program-sdk"); | ||
|
||
pub fn build(b: *std.Build) !void { | ||
const target = b.resolveTargetQuery(solana.sbf_target); | ||
const optimize = .ReleaseFast; | ||
|
||
//const dep_opts = .{ .target = target, .optimize = optimize }; | ||
//const solana_lib_dep = b.dependency("solana-program-library", dep_opts); | ||
//const solana_lib_mod = solana_lib_dep.module("solana-program-library"); | ||
|
||
const program = b.addSharedLibrary(.{ | ||
.name = "spl_token", | ||
.root_source_file = b.path("src/main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
//program.root_module.addImport("solana-program-library", solana_lib_mod); | ||
|
||
_ = solana.buildProgram(b, program, target, optimize); | ||
|
||
b.installArtifact(program); | ||
} |
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,38 @@ | ||
.{ | ||
.name = "solana-program-rosetta-token-zig", | ||
// This is a [Semantic Version](https://semver.org/). | ||
// In a future version of Zig it will be used for package deduplication. | ||
.version = "0.13.0", | ||
|
||
// This field is optional. | ||
// This is currently advisory only; Zig does not yet do anything | ||
// with this value. | ||
.minimum_zig_version = "0.13.0", | ||
|
||
// This field is optional. | ||
// Each dependency must either provide a `url` and `hash`, or a `path`. | ||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively. | ||
// Once all dependencies are fetched, `zig build` no longer requires | ||
// internet connectivity. | ||
.dependencies = .{ | ||
.@"solana-program-sdk" = .{ | ||
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.15.0.tar.gz", | ||
.hash = "1220c255d7d80a59251d901da4d2982eb660d099680c1207b14f51078987c655c979", | ||
}, | ||
}, | ||
|
||
// Specifies the set of files and directories that are included in this package. | ||
// Only files and directories listed here are included in the `hash` that | ||
// is computed for this package. | ||
// Paths are relative to the build root. Use the empty string (`""`) to refer to | ||
// the build root itself. | ||
// A directory listed here means that all files within, recursively, are included. | ||
.paths = .{ | ||
// For example... | ||
"build.zig", | ||
"build.zig.zon", | ||
"src", | ||
"../../LICENSE", | ||
"../../README.md", | ||
}, | ||
} |
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,140 @@ | ||
const sol = @import("solana-program-sdk"); | ||
|
||
pub const TokenError = error{ | ||
NotRentExempt, | ||
InsufficientFunds, | ||
InvalidMint, | ||
MintMismatch, | ||
OwnerMismatch, | ||
FixedSupply, | ||
AlreadyInUse, | ||
InvalidNumberOfProvidedSigners, | ||
InvalidNumberOfRequiredSigners, | ||
UninitializedState, | ||
NativeNotSupported, | ||
NonNativeHasBalance, | ||
InvalidInstruction, | ||
InvalidState, | ||
Overflow, | ||
AuthorityTypeNotSupported, | ||
MintCannotFreeze, | ||
AccountFrozen, | ||
MintDecimalsMismatch, | ||
NonNativeNotSupported, | ||
// generic program errors | ||
InvalidArgument, | ||
InvalidInstructionData, | ||
InvalidAccountData, | ||
AccountDataTooSmall, | ||
//InsufficientFunds, | ||
IncorrectProgramId, | ||
MissingRequiredSignature, | ||
AccountAlreadyInitialized, | ||
UninitializedAccount, | ||
NotEnoughAccountKeys, | ||
AccountBorrowFailed, | ||
MaxSeedLengthExceeded, | ||
InvalidSeeds, | ||
BorshIoError, | ||
AccountNotRentExempt, | ||
UnsupportedSysvar, | ||
IllegalOwner, | ||
MaxAccountsDataAllocationsExceeded, | ||
InvalidRealloc, | ||
MaxInstructionTraceLengthExceeded, | ||
BuiltinProgramsMustConsumeComputeUnits, | ||
InvalidAccountOwner, | ||
ArithmeticOverflow, | ||
Immutable, | ||
IncorrectAuthority, | ||
}; | ||
|
||
pub fn logError(e: TokenError) void { | ||
switch (e) { | ||
TokenError.NotRentExempt => { | ||
sol.log("Error: Lamport balance below rent-exempt threshold"); | ||
}, | ||
TokenError.InsufficientFunds => { | ||
sol.log("Error: insufficient funds"); | ||
}, | ||
TokenError.InvalidMint => { | ||
sol.log("Error: Invalid Mint"); | ||
}, | ||
TokenError.MintMismatch => { | ||
sol.log("Error: Account not associated with this Mint"); | ||
}, | ||
TokenError.OwnerMismatch => { | ||
sol.log("Error: owner does not match"); | ||
}, | ||
TokenError.FixedSupply => { | ||
sol.log("Error: the total supply of this token is fixed"); | ||
}, | ||
TokenError.AlreadyInUse => { | ||
sol.log("Error: account or token already in use"); | ||
}, | ||
TokenError.InvalidNumberOfProvidedSigners => { | ||
sol.log("Error: Invalid number of provided signers"); | ||
}, | ||
TokenError.InvalidNumberOfRequiredSigners => { | ||
sol.log("Error: Invalid number of required signers"); | ||
}, | ||
TokenError.UninitializedState => { | ||
sol.log("Error: State is uninitialized"); | ||
}, | ||
TokenError.NativeNotSupported => { | ||
sol.log("Error: Instruction does not support native tokens"); | ||
}, | ||
TokenError.NonNativeHasBalance => { | ||
sol.log("Error: Non-native account can only be closed if its balance is zero"); | ||
}, | ||
TokenError.InvalidInstruction => { | ||
sol.log("Error: Invalid instruction"); | ||
}, | ||
TokenError.InvalidState => { | ||
sol.log("Error: Invalid account state for operation"); | ||
}, | ||
TokenError.Overflow => { | ||
sol.log("Error: Operation overflowed"); | ||
}, | ||
TokenError.AuthorityTypeNotSupported => { | ||
sol.log("Error: Account does not support specified authority type"); | ||
}, | ||
TokenError.MintCannotFreeze => { | ||
sol.log("Error: This token mint cannot freeze accounts"); | ||
}, | ||
TokenError.AccountFrozen => { | ||
sol.log("Error: Account is frozen"); | ||
}, | ||
TokenError.MintDecimalsMismatch => { | ||
sol.log("Error: decimals different from the Mint decimals"); | ||
}, | ||
TokenError.NonNativeNotSupported => { | ||
sol.log("Error: Instruction does not support non-native tokens"); | ||
}, | ||
TokenError.InvalidArgument => {}, | ||
TokenError.InvalidInstructionData => {}, | ||
TokenError.InvalidAccountData => {}, | ||
TokenError.AccountDataTooSmall => {}, | ||
TokenError.InsufficientFunds => {}, | ||
TokenError.IncorrectProgramId => {}, | ||
TokenError.MissingRequiredSignature => {}, | ||
TokenError.AccountAlreadyInitialized => {}, | ||
TokenError.UninitializedAccount => {}, | ||
TokenError.NotEnoughAccountKeys => {}, | ||
TokenError.AccountBorrowFailed => {}, | ||
TokenError.MaxSeedLengthExceeded => {}, | ||
TokenError.InvalidSeeds => {}, | ||
TokenError.BorshIoError => {}, | ||
TokenError.AccountNotRentExempt => {}, | ||
TokenError.UnsupportedSysvar => {}, | ||
TokenError.IllegalOwner => {}, | ||
TokenError.MaxAccountsDataAllocationsExceeded => {}, | ||
TokenError.InvalidRealloc => {}, | ||
TokenError.MaxInstructionTraceLengthExceeded => {}, | ||
TokenError.BuiltinProgramsMustConsumeComputeUnits => {}, | ||
TokenError.InvalidAccountOwner => {}, | ||
TokenError.ArithmeticOverflow => {}, | ||
TokenError.Immutable => {}, | ||
TokenError.IncorrectAuthority => {}, | ||
} | ||
} |
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,4 @@ | ||
const PublicKey = @import("solana-program-sdk").PublicKey; | ||
pub const id = PublicKey.comptimeFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); | ||
pub const native_mint_id = PublicKey.comptimeFromBase58("So11111111111111111111111111111111111111112"); | ||
pub const system_program_id = PublicKey.comptimeFromBase58("11111111111111111111111111111111"); |
Oops, something went wrong.