Browse Source

fix anchor-lang import in safety-check, add custom-coder to ci, and fix spl-token coder (#1604)

Paul 3 years ago
parent
commit
94b0fec714

+ 2 - 0
.github/workflows/tests.yaml

@@ -319,6 +319,8 @@ jobs:
             path: tests/floats
             path: tests/floats
           - cmd: cd tests/safety-checks && ./test.sh
           - cmd: cd tests/safety-checks && ./test.sh
             path: tests/safety-checks
             path: tests/safety-checks
+          - cmd: cd tests/custom-coder && anchor test --skip-lint
+            path: tests/custom-coder
           - cmd: cd tests/validator-clone && yarn && anchor test --skip-lint
           - cmd: cd tests/validator-clone && yarn && anchor test --skip-lint
             path: tests/validator-clone
             path: tests/validator-clone
     steps:
     steps:

+ 1 - 1
CHANGELOG.md

@@ -30,7 +30,7 @@ incremented for features.
 * client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).
 * client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).
 * lang: implement `Key` for `Pubkey` again, so `associated_token::*` constraints can use pubkey targets again ([#1601](https://github.com/project-serum/anchor/pull/1601)).
 * lang: implement `Key` for `Pubkey` again, so `associated_token::*` constraints can use pubkey targets again ([#1601](https://github.com/project-serum/anchor/pull/1601)).
 * lang: adjust error code so `#[error_code]` works with just importing `anchor_lang::error_code` ([#1610](https://github.com/project-serum/anchor/pull/1610)).
 * lang: adjust error code so `#[error_code]` works with just importing `anchor_lang::error_code` ([#1610](https://github.com/project-serum/anchor/pull/1610)).
-* cli/ts: generated `IDL` variable in `types/<project-name.ts>` should equal the `<project-name>.json` idl file ([#1609](https://github.com/project-serum/anchor/pull/1609)) 
+* ts: fix `spl-token` coder account parsing ([#1604](https://github.com/project-serum/anchor/pull/1604)).
 
 
 ### Breaking
 ### Breaking
 
 

+ 6 - 5
cli/src/template.rs

@@ -29,19 +29,20 @@ token = "{}"
 }
 }
 
 
 pub fn idl_ts(idl: &Idl) -> Result<String> {
 pub fn idl_ts(idl: &Idl) -> Result<String> {
-    let mut idl_types = idl.clone();
-    for acc in idl_types.accounts.iter_mut() {
+    let mut idl = idl.clone();
+    for acc in idl.accounts.iter_mut() {
         acc.name = acc.name.to_mixed_case();
         acc.name = acc.name.to_mixed_case();
     }
     }
+    let idl_json = serde_json::to_string_pretty(&idl)?;
     Ok(format!(
     Ok(format!(
         r#"export type {} = {};
         r#"export type {} = {};
 
 
 export const IDL: {} = {};
 export const IDL: {} = {};
 "#,
 "#,
-        idl_types.name.to_camel_case(),
-        serde_json::to_string_pretty(&idl_types)?,
         idl.name.to_camel_case(),
         idl.name.to_camel_case(),
-        serde_json::to_string_pretty(&idl)?
+        idl_json,
+        idl.name.to_camel_case(),
+        idl_json
     ))
     ))
 }
 }
 
 

+ 1 - 1
tests/custom-coder/tests/custom-coder.ts

@@ -63,7 +63,7 @@ describe("custom-coder", () => {
     assert.ok(token.mint.equals(mintKeypair.publicKey));
     assert.ok(token.mint.equals(mintKeypair.publicKey));
     assert.ok(token.amount.toNumber() === 0);
     assert.ok(token.amount.toNumber() === 0);
     assert.ok(token.delegate === null);
     assert.ok(token.delegate === null);
-    assert.ok(token.state === 0);
+    assert.ok(token.state === 1);
     assert.ok(token.isNative === null);
     assert.ok(token.isNative === null);
     assert.ok(token.delegatedAmount.toNumber() === 0);
     assert.ok(token.delegatedAmount.toNumber() === 0);
     assert.ok(token.closeAuthority === null);
     assert.ok(token.closeAuthority === null);

+ 1 - 1
tests/safety-checks/programs/account-info/Cargo.toml

@@ -16,4 +16,4 @@ cpi = ["no-entrypoint"]
 default = []
 default = []
 
 
 [dependencies]
 [dependencies]
-anchor-lang = "0.22.1"
+anchor-lang = { path = "../../../../lang" }

+ 6 - 6
ts/src/coder/spl-token/accounts.ts

@@ -10,12 +10,12 @@ export class SplTokenAccountsCoder<A extends string = string>
 
 
   public async encode<T = any>(accountName: A, account: T): Promise<Buffer> {
   public async encode<T = any>(accountName: A, account: T): Promise<Buffer> {
     switch (accountName) {
     switch (accountName) {
-      case "Token": {
+      case "token": {
         const buffer = Buffer.alloc(165);
         const buffer = Buffer.alloc(165);
         const len = TOKEN_ACCOUNT_LAYOUT.encode(account, buffer);
         const len = TOKEN_ACCOUNT_LAYOUT.encode(account, buffer);
         return buffer.slice(0, len);
         return buffer.slice(0, len);
       }
       }
-      case "Mint": {
+      case "mint": {
         const buffer = Buffer.alloc(82);
         const buffer = Buffer.alloc(82);
         const len = MINT_ACCOUNT_LAYOUT.encode(account, buffer);
         const len = MINT_ACCOUNT_LAYOUT.encode(account, buffer);
         return buffer.slice(0, len);
         return buffer.slice(0, len);
@@ -32,10 +32,10 @@ export class SplTokenAccountsCoder<A extends string = string>
 
 
   public decodeUnchecked<T = any>(accountName: A, ix: Buffer): T {
   public decodeUnchecked<T = any>(accountName: A, ix: Buffer): T {
     switch (accountName) {
     switch (accountName) {
-      case "Token": {
+      case "token": {
         return decodeTokenAccount(ix);
         return decodeTokenAccount(ix);
       }
       }
-      case "Mint": {
+      case "mint": {
         return decodeMintAccount(ix);
         return decodeMintAccount(ix);
       }
       }
       default: {
       default: {
@@ -47,12 +47,12 @@ export class SplTokenAccountsCoder<A extends string = string>
   // TODO: this won't use the appendData.
   // TODO: this won't use the appendData.
   public memcmp(accountName: A, _appendData?: Buffer): any {
   public memcmp(accountName: A, _appendData?: Buffer): any {
     switch (accountName) {
     switch (accountName) {
-      case "Token": {
+      case "token": {
         return {
         return {
           dataSize: 165,
           dataSize: 165,
         };
         };
       }
       }
-      case "Mint": {
+      case "mint": {
         return {
         return {
           dataSize: 82,
           dataSize: 82,
         };
         };

+ 1 - 1
ts/src/program/accounts-resolver.ts

@@ -237,7 +237,7 @@ export class AccountStore<IDL extends Idl> {
         if (accountInfo === null) {
         if (accountInfo === null) {
           throw new Error(`invalid account info for ${address}`);
           throw new Error(`invalid account info for ${address}`);
         }
         }
-        const data = coder().accounts.decode("Token", accountInfo.data);
+        const data = coder().accounts.decode("token", accountInfo.data);
         this._cache.set(address, data);
         this._cache.set(address, data);
       } else {
       } else {
         const account = this._accounts[camelCase(name)].fetch(publicKey);
         const account = this._accounts[camelCase(name)].fetch(publicKey);