-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1023 from starknet-io/next-version
Next version
- Loading branch information
Showing
23 changed files
with
1,453 additions
and
655 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// TODO Mock: get-starknet UI connect/disconnect wallet | ||
// TODO Create Mock Wallet; | ||
|
||
describe('wallet account test', () => { | ||
test('estimateInvokeFee Cairo 0', async () => { | ||
return true; | ||
}); | ||
}); |
Large diffs are not rendered by default.
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
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,188 @@ | ||
import { Account, AccountInterface } from '../account'; | ||
import { StarknetChainId } from '../constants'; | ||
import { ProviderInterface } from '../provider'; | ||
import { | ||
AllowArray, | ||
CairoVersion, | ||
Call, | ||
CompiledSierra, | ||
DeclareContractPayload, | ||
DeployAccountContractPayload, | ||
MultiDeployContractResponse, | ||
ProviderOptions, | ||
TypedData, | ||
UniversalDeployerContractPayload, | ||
} from '../types'; | ||
import { CallData } from '../utils/calldata'; | ||
import { extractContractHashes } from '../utils/contract'; | ||
import { stringify } from '../utils/json'; | ||
import { buildUDCCall } from '../utils/transaction'; | ||
import { | ||
addDeclareTransaction, | ||
addDeployAccountTransaction, | ||
addInvokeTransaction, | ||
addStarknetChain, | ||
getPermissions, | ||
onAccountChange, | ||
onNetworkChanged, | ||
requestAccounts, | ||
signMessage, | ||
switchStarknetChain, | ||
watchAsset, | ||
} from './connect'; | ||
import { | ||
AccountChangeEventHandler, | ||
AddStarknetChainParameters, | ||
NetworkChangeEventHandler, | ||
WatchAssetParameters, | ||
} from './getst/main'; | ||
import { StarknetWalletProvider } from './types'; | ||
|
||
// Represent 'Selected Active' Account inside Connected Wallet | ||
export class WalletAccount extends Account implements AccountInterface { | ||
public address: string = ''; | ||
|
||
public walletProvider: StarknetWalletProvider; | ||
|
||
constructor( | ||
providerOrOptions: ProviderOptions | ProviderInterface, | ||
walletProvider: StarknetWalletProvider, | ||
cairoVersion?: CairoVersion | ||
) { | ||
super(providerOrOptions, '', '', cairoVersion); // At this point unknown address | ||
this.walletProvider = walletProvider; | ||
|
||
// Update Address on change | ||
this.walletProvider.on('accountsChanged', (res) => { | ||
if (!res) return; | ||
this.address = res[0].toLowerCase(); | ||
}); | ||
|
||
// Update Channel chainId on Network change | ||
this.walletProvider.on('networkChanged', (res) => { | ||
if (!res) return; | ||
// Determine is it better to set chainId or replace channel with new one | ||
// At the moment channel is stateless but it could change | ||
this.channel.setChainId(res as StarknetChainId); | ||
}); | ||
|
||
// Get and Set Address !!! Post constructor initial empty string | ||
walletProvider | ||
.request({ | ||
type: 'wallet_requestAccounts', | ||
params: { | ||
silentMode: false, | ||
}, | ||
}) | ||
.then((res) => { | ||
this.address = res[0].toLowerCase(); | ||
}); | ||
} | ||
|
||
/** | ||
* WALLET EVENTS | ||
*/ | ||
public onAccountChange(callback: AccountChangeEventHandler) { | ||
onAccountChange(this.walletProvider, callback); | ||
} | ||
|
||
public onNetworkChanged(callback: NetworkChangeEventHandler) { | ||
onNetworkChanged(this.walletProvider, callback); | ||
} | ||
|
||
/** | ||
* WALLET SPECIFIC METHODS | ||
*/ | ||
public requestAccounts(silentMode = false) { | ||
return requestAccounts(this.walletProvider, silentMode); | ||
} | ||
|
||
public getPermissions() { | ||
return getPermissions(this.walletProvider); | ||
} | ||
|
||
public switchStarknetChain(chainId: StarknetChainId) { | ||
return switchStarknetChain(this.walletProvider, chainId); | ||
} | ||
|
||
public watchAsset(asset: WatchAssetParameters) { | ||
return watchAsset(this.walletProvider, asset); | ||
} | ||
|
||
public addStarknetChain(chain: AddStarknetChainParameters) { | ||
return addStarknetChain(this.walletProvider, chain); | ||
} | ||
|
||
/** | ||
* ACCOUNT METHODS | ||
*/ | ||
override execute(calls: AllowArray<Call>) { | ||
const txCalls = [].concat(calls as any).map((it) => { | ||
const { contractAddress, entrypoint, calldata } = it; | ||
return { | ||
contract_address: contractAddress, | ||
entrypoint, | ||
calldata, | ||
}; | ||
}); | ||
|
||
const params = { | ||
calls: txCalls, | ||
}; | ||
|
||
return addInvokeTransaction(this.walletProvider, params); | ||
} | ||
|
||
override declare(payload: DeclareContractPayload) { | ||
const declareContractPayload = extractContractHashes(payload); | ||
|
||
// DISCUSS: HOTFIX: Adapt Abi format | ||
const pContract = payload.contract as CompiledSierra; | ||
const cairo1Contract = { | ||
...pContract, | ||
abi: stringify(pContract.abi), | ||
}; | ||
|
||
// Check FIx | ||
if (!declareContractPayload.compiledClassHash) { | ||
throw Error('compiledClassHash is required'); | ||
} | ||
|
||
const params = { | ||
compiled_class_hash: declareContractPayload.compiledClassHash, | ||
contract_class: cairo1Contract, | ||
}; | ||
|
||
return addDeclareTransaction(this.walletProvider, params); | ||
} | ||
|
||
override async deploy( | ||
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[] | ||
): Promise<MultiDeployContractResponse> { | ||
const { calls, addresses } = buildUDCCall(payload, this.address); | ||
const invokeResponse = await this.execute(calls); | ||
|
||
return { | ||
...invokeResponse, | ||
contract_address: addresses, | ||
}; | ||
} | ||
|
||
override deployAccount(payload: DeployAccountContractPayload) { | ||
const params = { | ||
contract_address_salt: payload.addressSalt?.toString() || '0', | ||
constructor_calldata: payload.constructorCalldata | ||
? CallData.compile(payload.constructorCalldata) | ||
: [], | ||
class_hash: payload.classHash, | ||
}; | ||
|
||
return addDeployAccountTransaction(this.walletProvider, params); | ||
} | ||
|
||
override signMessage(typedData: TypedData) { | ||
return signMessage(this.walletProvider, typedData); | ||
} | ||
|
||
// TODO: MISSING ESTIMATES | ||
} |
Oops, something went wrong.