-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Txn relayer and v0 transaction sign support added (#48)
* v0 txn support add on signAndSendTransaction * txn relayer added * 0.2.29
- Loading branch information
Showing
8 changed files
with
140 additions
and
15 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,62 @@ | ||
import { ShyftConfig } from '@/utils'; | ||
import { restApiCall } from '@/utils'; | ||
import { Network, SendTransactionResp, TxnCommitment } from '@/types'; | ||
|
||
export class TxnRelayerClient { | ||
constructor(private readonly config: ShyftConfig) {} | ||
|
||
async getOrCreate(): Promise<string> { | ||
const data = await restApiCall(this.config.apiKey, { | ||
method: 'post', | ||
url: 'txn_relayer/create', | ||
}); | ||
const wallet = data.result.wallet as string; | ||
return wallet; | ||
} | ||
|
||
async sign(input: { | ||
network?: Network; | ||
encodedTransaction: string; | ||
}): Promise<string> { | ||
const reqBody = { | ||
network: input.network ?? this.config.network, | ||
encoded_transaction: input.encodedTransaction, | ||
}; | ||
|
||
const data = await restApiCall(this.config.apiKey, { | ||
method: 'post', | ||
url: 'txn_relayer/sign', | ||
data: reqBody, | ||
}); | ||
const result = data.result?.tx as string; | ||
return result; | ||
} | ||
|
||
async signMany(input: { | ||
network?: Network; | ||
encodedTransactions: string[]; | ||
commitment?: TxnCommitment; | ||
}): Promise<SendTransactionResp[]> { | ||
if ( | ||
input.encodedTransactions.length > 50 || | ||
input.encodedTransactions.length < 1 | ||
) { | ||
throw new Error('allowed between 1 to 50: encodedTransactions'); | ||
} | ||
const reqBody = { | ||
network: input.network ?? this.config.network, | ||
encoded_transactions: input.encodedTransactions, | ||
}; | ||
if (input?.commitment) { | ||
reqBody['commitment'] = input.commitment; | ||
} | ||
|
||
const data = await restApiCall(this.config.apiKey, { | ||
method: 'post', | ||
url: 'txn_relayer/sign_many', | ||
data: reqBody, | ||
}); | ||
const result = data.result as SendTransactionResp[]; | ||
return result; | ||
} | ||
} |
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,36 @@ | ||
import 'dotenv/config'; | ||
import { ShyftSdk } from '@/index'; | ||
import { Network } from '@/types'; | ||
|
||
const shyft = new ShyftSdk({ | ||
apiKey: process.env.API_KEY as string, | ||
network: Network.Devnet, | ||
}); | ||
|
||
describe('Transaction Relayer client test', () => { | ||
it('get or create relay wallet', async () => { | ||
const wallet = await shyft.txnRelayer.getOrCreate(); | ||
expect(typeof wallet).toBe('string'); | ||
}); | ||
|
||
it('sign transaction', async () => { | ||
const signature = await shyft.txnRelayer.sign({ | ||
network: Network.Devnet, | ||
encodedTransaction: | ||
'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQACBc1gsUE/MCpM0cXSNRdR/uvKkw28CfT/UmFjeQO1P3RTZzmxZaKzGUFs516b0/MuXMRFJmwuLzL221Zha9e6/yURWkj1VBal8ukhGe3QD/WcUgoNM/i/hJJbB20A/LLeuAbd9uHXZaGT2cvhRs7reawctIXtX1s3kTqM9YV+/wCpxkeHlDF/XZwFVODF42SUvNKnI6z+t1y+mcP3Eqbp1vLLapjYzOUB2ZYSvnp0kYl1UD4ZcXxiFaQEf+mE1ygN6wEDBAEEAgAKDEBCDwAAAAAACQA=', | ||
}); | ||
expect(typeof signature).toBe('string'); | ||
}); | ||
|
||
it('sign multiple transactions', async () => { | ||
const response = await shyft.txnRelayer.signMany({ | ||
network: Network.Devnet, | ||
encodedTransactions: [ | ||
'AflRiSHDT+mkiqNqJ6MsY5cqITOcZ37+txZQqqYzazphSa/VBhFcFibFzi2rQ8IsaQkgBDHznqabolOzA/mNlAUBAAEDGMqfUcVHHu2+lyU5gx9wU2rGxk2NoSXtodMSdev+DxVALAeVfwkorAqlrAeN8dOn8Jeu6H4u3ofHzGz4FntQPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAR9uaAIxMGKAuXPKB9d9mAA3oMZ/GRc3kbW+YxugY/jcBAgIAAQwCAAAAMBsPAAAAAAA=', | ||
'AW+cdqIs1kmQzpWS9YjXdx8eHqqv8IQsurH88P54ZUvbdysyCnsEpxEt2Y2xZ8+yCP/jj8hFyih8NZiWUqnb3QUBAAEDGMqfUcVHHu2+lyU5gx9wU2rGxk2NoSXtodMSdev+DxVALAeVfwkorAqlrAeN8dOn8Jeu6H4u3ofHzGz4FntQPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAR9uaAIxMGKAuXPKB9d9mAA3oMZ/GRc3kbW+YxugY/jcBAgIAAQwCAAAAQEIPAAAAAAA=', | ||
], | ||
commitment: 'confirmed', | ||
}); | ||
expect(typeof response).toBe('object'); | ||
}); | ||
}); |