-
Notifications
You must be signed in to change notification settings - Fork 44
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
15 changed files
with
259 additions
and
26 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
68 changes: 68 additions & 0 deletions
68
gauntlet/packages/gauntlet-solana-contracts/src/commands/contracts/token/createAccount.ts
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,68 @@ | ||
import { Result } from '@chainlink/gauntlet-core' | ||
import { logger, prompt } from '@chainlink/gauntlet-core/dist/utils' | ||
import { SolanaCommand, TransactionResponse } from '@chainlink/gauntlet-solana' | ||
import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token' | ||
import { PublicKey } from '@solana/web3.js' | ||
import { CONTRACT_LIST } from '../../../lib/contracts' | ||
import { isValidTokenAccount } from './utils' | ||
|
||
export default class CreateAccount extends SolanaCommand { | ||
static id = 'token:create_account' | ||
static category = CONTRACT_LIST.TOKEN | ||
static examples = ['yarn gauntlet token:create_account --network=devnet --address=<BASE_ADDRESS> <TOKEN>'] | ||
|
||
constructor(flags, args) { | ||
super(flags, args) | ||
|
||
this.requireFlag('address', `Provide an address from which the 'Token Associated Account' will be derived`) | ||
this.require(!!args[0], 'Provide a token address') | ||
} | ||
|
||
execute = async () => { | ||
const tokenAddress = new PublicKey(this.args[0]) | ||
|
||
const newAccountBase = new PublicKey(this.flags.address) | ||
const associatedAcc = await Token.getAssociatedTokenAddress( | ||
ASSOCIATED_TOKEN_PROGRAM_ID, | ||
TOKEN_PROGRAM_ID, | ||
tokenAddress, | ||
newAccountBase, | ||
true, | ||
) | ||
|
||
const token = new Token(this.provider.connection, tokenAddress, TOKEN_PROGRAM_ID, { | ||
publicKey: this.wallet.publicKey, | ||
secretKey: Buffer.from([]), | ||
}) | ||
|
||
const accountExists = await isValidTokenAccount(token, associatedAcc) | ||
this.require( | ||
!accountExists, | ||
`A Token Associated Account to address ${newAccountBase.toString()} already exists: ${associatedAcc}`, | ||
) | ||
|
||
const ix = Token.createAssociatedTokenAccountInstruction( | ||
ASSOCIATED_TOKEN_PROGRAM_ID, | ||
TOKEN_PROGRAM_ID, | ||
tokenAddress, | ||
associatedAcc, | ||
newAccountBase, | ||
this.wallet.publicKey, | ||
) | ||
|
||
await prompt(`Continue to create new Token associated account to ${newAccountBase.toString()}`) | ||
logger.loading('Creating account...') | ||
const tx = await this.signAndSendRawTx([ix]) | ||
|
||
logger.success(`New account created at ${associatedAcc.toString()} on tx ${tx}`) | ||
|
||
return { | ||
responses: [ | ||
{ | ||
tx: this.wrapResponse(tx, this.args[0]), | ||
contract: this.args[0], | ||
}, | ||
], | ||
} as Result<TransactionResponse> | ||
} | ||
} |
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
6 changes: 5 additions & 1 deletion
6
gauntlet/packages/gauntlet-solana-contracts/src/commands/contracts/token/index.ts
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,9 @@ | ||
import DeployToken from './deploy' | ||
import CreateAccount from './createAccount' | ||
import ReadState from './read' | ||
import TransferToken from './transfer' | ||
import * as tokenUtils from './utils' | ||
|
||
export default [DeployToken, ReadState, TransferToken] | ||
export default [DeployToken, ReadState, TransferToken, CreateAccount] | ||
|
||
export { tokenUtils } |
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
11 changes: 11 additions & 0 deletions
11
gauntlet/packages/gauntlet-solana-contracts/src/commands/contracts/token/utils.ts
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,11 @@ | ||
import { Token } from '@solana/spl-token' | ||
import { PublicKey } from '@solana/web3.js' | ||
|
||
export const isValidTokenAccount = async (token: Token, address: PublicKey) => { | ||
try { | ||
const info = await token.getAccountInfo(address) | ||
return !!info.address | ||
} catch (e) { | ||
return false | ||
} | ||
} |
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
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,37 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package solana | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median" | ||
) | ||
|
||
// Ensure your env is using go 1.18 then in pkg/solana: | ||
// go test -tags=go1.18 -fuzz ./... | ||
func FuzzReportCodecMedianFromReport(f *testing.F) { | ||
cdc := ReportCodec{} | ||
report, err := cdc.BuildReport([]median.ParsedAttributedObservation{ | ||
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(100000)}, | ||
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(200000)}, | ||
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(11), JuelsPerFeeCoin: big.NewInt(300000)}}) | ||
require.NoError(f, err) | ||
|
||
// Seed with valid report | ||
f.Add([]byte(report)) | ||
f.Fuzz(func(t *testing.T, report []byte) { | ||
med, err := cdc.MedianFromReport(report) | ||
if err == nil { | ||
// Should always be able to build a report from the medians extracted | ||
// Note however that juelsPerFeeCoin is only 8 bytes, so we can use the median for it | ||
_, err = cdc.BuildReport([]median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: big.NewInt(100000)}}) | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} |
Oops, something went wrong.