Skip to content

Commit

Permalink
🎨 Add account optional support (#1)
Browse files Browse the repository at this point in the history
* 🎨 Add account optional support

* 💚 Fix CI build

* 💚 Fix CI

* 💚 Fix CI

* 💚 Fix CI
  • Loading branch information
GabrielePicco authored May 22, 2023
1 parent d44eba0 commit f9c007b
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"commands": [
"dotnet-format"
]
},
"solana.unity.anchor.tool": {
"version": "0.2.8",
"commands": [
"dotnet-anchorgen"
]
}
}
}
14 changes: 7 additions & 7 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Get the sources
uses: actions/checkout@v1
- name: Run the build script
uses: cake-build/cake-action@v1
with:
script-path: build.cake
target: Pack
cake-bootstrap: true
uses: actions/checkout@v2
- name: Run cake
shell: bash
run: |
dotnet tool install Cake.Tool --version 1.1.0
dotnet tool restore
dotnet cake --target=Test --verbosity=verbose
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://raw.githubusercontent.com/garbles-labs/Solana.Unity.Anchor/master/assets/icon.png" margin="auto" height="175"/>
<img src="https://raw.githubusercontent.com/magicblock-labs/Solana.Unity.Anchor/master/assets/icon.png" margin="auto" height="175"/>
</p>

<p align="center">
Expand All @@ -12,7 +12,7 @@
<a href="">
<img src="https://img.shields.io/github/license/magicblock-labs/Solana.Unity-Core?style=flat-square"
alt="Code License"></a>
<a href="https://discord.gg/cReXaBReZt">
<a href="https://discord.com/invite/MBkdC3gxcv">
<img alt="Discord" src="https://img.shields.io/discord/849407317761064961?style=flat-square"
alt="Join the discussion!"></a>
</p>
Expand All @@ -30,18 +30,16 @@ Solana.Unity.Anchor rely on [Solana.Unity-Core](https://github.com/garbles-labs/
## Features

This repo contains 3 main projects:
- Solnet.Anchor: IDL parsing and code generation
- Solnet.Anchor.Tool: dotnet tool executable that interfaces with the project above.
- Solnet.Anchor.SourceGenerator: Roslyn source generator that depends on the tool above to automatically generate code from IDL in your IDE.
- Solana.Unity.Anchor: IDL parsing and code generation
- Solana.Unity.Anchor.Tool: dotnet tool executable that interfaces with the project above.
- Solana.Unity.Anchor.SourceGenerator: Roslyn source generator that depends on the tool above to automatically generate code from IDL in your IDE.

Currently covers all of IDL features with the exception of events and seeds.

## Requirements

Solnet.Anchor and Solnet.Anchor.Tool are compiled and run in net6. Could be easily backported to net5.
Solnet.Anchor.SourceGenerator is compiled in netstandard2.1 to be able to be used as a Roslyn Source Generator. However, machine needs net6 as it just calls Solnet.Anchor.Tool that requires net6.

Generated code can be run using net5 or net6, and the respective Solnet version >=5.0.3 or >=6.0.3 libraries.
Solana.Unity.Anchor and Solana.Unity.Anchor.Tool are compiled and run in net6. Could be easily backported to net5.
Solana.Unity.Anchor.SourceGenerator is compiled in netstandard2.1 to be able to be used as a Roslyn Source Generator. However, machine needs net6 as it just calls Solnet.Anchor.Tool that requires net6.

## Instructions

Expand Down Expand Up @@ -74,7 +72,11 @@ Consider supporting us:

We encourage everyone to contribute, submit issues, PRs, discuss. Every kind of help is welcome.

## Maintainers
## Solana.Unity Maintainers

* **Gabriele Picco** - [PiccoGabriele](https://github.com/PiccoGabriele)

## Solnet Maintainers

* **Hugo** - [murlokito](https://github.com/murlokito)
* **Tiago** - [tiago](https://github.com/tiago18c)
Expand Down
2 changes: 1 addition & 1 deletion SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Solana.Unity.Anchor</Product>
<Version>0.2.8</Version>
<Version>0.2.9</Version>
<Copyright>Copyright 2022 &#169; Garbles Labs</Copyright>
<Authors>Garbles Labs</Authors>
<PublisherName>Garbles Labs</PublisherName>
Expand Down
3 changes: 2 additions & 1 deletion Solana.Unity.Anchor.Test/Resources/seq.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
{
"name": "sequenceAccount",
"isMut": true,
"isSigner": false
"isSigner": false,
"isOptional": true
},
{
"name": "authority",
Expand Down
13 changes: 3 additions & 10 deletions Solana.Unity.Anchor.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Solana.Unity.Anchor;
using System.Collections.Generic;
using System;
using System.Buffers.Binary;
using System.Numerics;

namespace Solana.Unity.Anchor.Test
{
Expand All @@ -18,14 +12,13 @@ public void TestMethod1()
var res = IdlParser.ParseFile("Resources/seq.json");
Assert.IsNotNull(res);




ClientGenerator c = new();

c.GenerateSyntaxTree(res);
Assert.IsNotNull(c);


var code = c.GenerateCode(res);
Assert.IsNotNull(code);
}
}
}
Expand Down
30 changes: 24 additions & 6 deletions Solana.Unity.Anchor/ClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private List<ExpressionSyntax> GenerateKeysInitExpressions(IIdlAccountItem[] acc
}
else if (acc is IdlAccount singleAcc)
{



initExpressions.Add(InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
Expand All @@ -167,10 +166,29 @@ private List<ExpressionSyntax> GenerateKeysInitExpressions(IIdlAccountItem[] acc
IdentifierName(singleAcc.IsMut ? "Writable" : "ReadOnly")),
ArgumentList(SeparatedList(new ArgumentSyntax[]
{
Argument(MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
identifierNameSyntax,
IdentifierName(singleAcc.Name.ToPascalCase()))),
Argument(
singleAcc.IsOptional ?
ConditionalExpression(
BinaryExpression(
SyntaxKind.EqualsExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
identifierNameSyntax,
IdentifierName(singleAcc.Name.ToPascalCase())),
LiteralExpression(SyntaxKind.NullLiteralExpression)
),

IdentifierName("programId"),
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
identifierNameSyntax,
IdentifierName(singleAcc.Name.ToPascalCase()))
) :
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
identifierNameSyntax,
IdentifierName(singleAcc.Name.ToPascalCase())
)
),
Argument(LiteralExpression(singleAcc.IsSigner ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))
}))));

Expand Down
2 changes: 2 additions & 0 deletions Solana.Unity.Anchor/Models/Accounts/IdlAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class IdlAccount : IIdlAccountItem
public bool IsMut { get; set; }

public bool IsSigner { get; set; }

public bool IsOptional { get; set; }

public IdlPda Pda { get; set; }

Expand Down
42 changes: 39 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#addin nuget:?package=Cake.Coverlet&version=2.5.4
#tool dotnet:?package=dotnet-reportgenerator-globaltool&version=4.8.7

var testProjectsRelativePaths = new string[]
{
"./Solana.Unity.Anchor.Test/Solana.Unity.Anchor.Test.csproj",
};


var target = Argument("target", "Pack");
var configuration = Argument("configuration", "Release");
Expand All @@ -10,17 +15,19 @@ var artifactsDir = MakeAbsolute(Directory("artifacts"));

var reportTypes = "HtmlInline";
var coverageFolder = "./code_coverage";
var coverageFileName = "results.info";
var coverageFolderIntegration = "./code_coverage_integration";

var coberturaFileName = "results";
var coverageFilePath = Directory(coverageFolder) + File(coberturaFileName + ".info");
var jsonFilePath = Directory(coverageFolder) + File(coberturaFileName + ".json");

var coverageFilePath = Directory(coverageFolder) + File(coverageFileName);
var packagesDir = artifactsDir.Combine(Directory("packages"));

var deliverables = new[] {"Solana.Unity.Anchor.Tool", "Solana.Unity.Anchor.SourceGenerator"};

Task("Clean")
.Does(() => {
CleanDirectory(artifactsDir);
CleanDirectory(coverageFolder);
});

Task("Restore")
Expand All @@ -40,7 +47,36 @@ Task("Build")
});


Task("Test")
.IsDependentOn("Build")
.Does(() => {
var coverletSettings = new CoverletSettings {
CollectCoverage = true,
CoverletOutputDirectory = coverageFolder,
CoverletOutputName = coberturaFileName
};
var testSettings = new DotNetCoreTestSettings
{
NoRestore = true,
Configuration = configuration,
NoBuild = true,
ArgumentCustomization = args => args.Append($"--logger trx")
};
DotNetCoreTest(testProjectsRelativePaths[0], testSettings, coverletSettings);
coverletSettings.MergeWithFile = jsonFilePath;
for (int i = 1; i < testProjectsRelativePaths.Length; i++)
{
if (i == testProjectsRelativePaths.Length - 1)
{
coverletSettings.CoverletOutputFormat = CoverletOutputFormat.lcov;
}
DotNetCoreTest(testProjectsRelativePaths[i], testSettings, coverletSettings);
}
});

Task("Publish")
.IsDependentOn("Build")
Expand Down

0 comments on commit f9c007b

Please sign in to comment.